Skip to content

Commit 118a744

Browse files
committed
Add tests that broadcast does allow spawns to run
... even on "fallback" targets like `wasm32-wasi`!
1 parent e3dd0a5 commit 118a744

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

rayon-core/src/broadcast/test.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,35 @@ fn broadcast_sleep_race() {
228228
});
229229
}
230230
}
231+
232+
#[test]
233+
fn broadcast_after_spawn_broadcast() {
234+
let (tx, rx) = crossbeam_channel::unbounded();
235+
236+
// Queue a non-blocking spawn_broadcast.
237+
crate::spawn_broadcast(move |ctx| tx.send(ctx.index()).unwrap());
238+
239+
// This blocking broadcast runs after all prior broadcasts.
240+
crate::broadcast(|_| {});
241+
242+
// The spawn_broadcast **must** have run by now on all threads.
243+
let mut v: Vec<_> = rx.try_iter().collect();
244+
v.sort_unstable();
245+
assert!(v.into_iter().eq(0..crate::current_num_threads()));
246+
}
247+
248+
#[test]
249+
fn broadcast_after_spawn() {
250+
let (tx, rx) = crossbeam_channel::bounded(1);
251+
252+
// Queue a regular spawn on a thread-local deque.
253+
crate::registry::in_worker(move |_, _| {
254+
crate::spawn(move || tx.send(22).unwrap());
255+
});
256+
257+
// Broadcast runs after the local deque is empty.
258+
crate::broadcast(|_| {});
259+
260+
// The spawn **must** have run by now.
261+
assert_eq!(22, rx.try_recv().unwrap());
262+
}

0 commit comments

Comments
 (0)