diff --git a/tests/try_spawn.rs b/tests/try_spawn.rs index 859125a..1de3c88 100644 --- a/tests/try_spawn.rs +++ b/tests/try_spawn.rs @@ -12,6 +12,14 @@ use smarm::{spawn, try_spawn, try_spawn_under_with, yield_now, SpawnError, Spawn use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; use std::sync::Arc; +struct SetOnDrop(Arc); + +impl Drop for SetOnDrop { + fn drop(&mut self) { + self.0.store(true, Ordering::Release); + } +} + /// A child that holds its slot until `release` flips, without parking /// machinery: busy-yield keeps the scheduler moving and the slot occupied. fn holder(release: Arc) -> impl FnOnce() + Send + 'static { @@ -130,6 +138,12 @@ fn racing_try_spawns_claim_exactly_the_free_slots() { const SPAWNERS: usize = 4; smarm::runtime::init(Config::exact(4).max_actors(MAX)).run(|| { let release = Arc::new(AtomicBool::new(false)); + // A failing assert below must fail the test, not hang it: holders spawned by a + // still-running spawner are not reached by the root-exit sweep. + let _release_on_unwind = SetOnDrop(release.clone()); + // Racers start only once every spawner holds its slot; otherwise the first ones + // can drain the table while root is still spawning the rest. + let go = Arc::new(AtomicBool::new(false)); let won = Arc::new(AtomicUsize::new(0)); let done = Arc::new(AtomicUsize::new(0)); @@ -144,9 +158,13 @@ fn racing_try_spawns_claim_exactly_the_free_slots() { let mut spawners = Vec::new(); for _ in 0..SPAWNERS { let release = release.clone(); + let go = go.clone(); let won = won.clone(); let done = done.clone(); spawners.push(spawn(move || { + while !go.load(Ordering::Acquire) { + yield_now(); + } loop { match try_spawn(holder(release.clone())) { Ok(h) => { @@ -160,6 +178,7 @@ fn racing_try_spawns_claim_exactly_the_free_slots() { done.fetch_add(1, Ordering::AcqRel); })); } + go.store(true, Ordering::Release); // Wait for every racer to hit AtCapacity. while done.load(Ordering::Acquire) < SPAWNERS { yield_now();