diff options
Diffstat (limited to 'tests/vcs.rs')
| -rw-r--r-- | tests/vcs.rs | 40 |
1 files changed, 26 insertions, 14 deletions
diff --git a/tests/vcs.rs b/tests/vcs.rs index e41adfd..52f4372 100644 --- a/tests/vcs.rs +++ b/tests/vcs.rs @@ -117,22 +117,34 @@ fn stage_b_is_cached_on_pkgbase_recipe_and_tip() { #[test] fn stage_b_concurrency_cap_is_enforced() { let (env, _fx) = env_with_fixture("git-sourced"); - let slots = std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(0)); - let s1 = slots.clone(); + use std::sync::atomic::{AtomicUsize, Ordering}; + let inside = std::sync::Arc::new(AtomicUsize::new(0)); + let attempted = std::sync::Arc::new(AtomicUsize::new(0)); let held = upstream::StageBSlot::acquire(1); - let entered = std::thread::spawn(move || { - let _g = upstream::StageBSlot::acquire(1); - s1.fetch_add(1, std::sync::atomic::Ordering::SeqCst); - }); - std::thread::sleep(std::time::Duration::from_millis(80)); - assert_eq!( - slots.load(std::sync::atomic::Ordering::SeqCst), - 0, - "a second Stage B entered while the only slot was held" - ); + let racers: Vec<_> = (0..3) + .map(|_| { + let inside = inside.clone(); + let attempted = attempted.clone(); + std::thread::spawn(move || { + attempted.fetch_add(1, Ordering::SeqCst); + let _slot = upstream::StageBSlot::acquire(1); + let n = inside.fetch_add(1, Ordering::SeqCst) + 1; + assert_eq!(n, 1, "two Stage B runs held the only slot at once"); + inside.fetch_sub(1, Ordering::SeqCst); + }) + }) + .collect(); + // Every racer is queued on the slot before it is released, so a cap that + // leaked would let them all in at once rather than by luck of scheduling. + while attempted.load(Ordering::SeqCst) < 3 { + std::thread::yield_now(); + } + assert_eq!(inside.load(Ordering::SeqCst), 0, "a second Stage B entered while the only slot was held"); drop(held); - entered.join().unwrap(); - assert_eq!(slots.load(std::sync::atomic::Ordering::SeqCst), 1); + for r in racers { + r.join().unwrap(); + } + assert_eq!(inside.load(Ordering::SeqCst), 0); let _ = env; } |
