]> git.proxmox.com Git - rustc.git/blob - library/std/src/sync/barrier/tests.rs
New upstream version 1.48.0~beta.8+dfsg1
[rustc.git] / library / std / src / sync / barrier / tests.rs
1 use crate::sync::mpsc::{channel, TryRecvError};
2 use crate::sync::{Arc, Barrier};
3 use crate::thread;
4
5 #[test]
6 #[cfg_attr(target_os = "emscripten", ignore)]
7 fn test_barrier() {
8 const N: usize = 10;
9
10 let barrier = Arc::new(Barrier::new(N));
11 let (tx, rx) = channel();
12
13 for _ in 0..N - 1 {
14 let c = barrier.clone();
15 let tx = tx.clone();
16 thread::spawn(move || {
17 tx.send(c.wait().is_leader()).unwrap();
18 });
19 }
20
21 // At this point, all spawned threads should be blocked,
22 // so we shouldn't get anything from the port
23 assert!(matches!(rx.try_recv(), Err(TryRecvError::Empty)));
24
25 let mut leader_found = barrier.wait().is_leader();
26
27 // Now, the barrier is cleared and we should get data.
28 for _ in 0..N - 1 {
29 if rx.recv().unwrap() {
30 assert!(!leader_found);
31 leader_found = true;
32 }
33 }
34 assert!(leader_found);
35 }