1#![allow(clippy::unnecessary_operation)]
2#![warn(rust_2018_idioms)]
3#![cfg(feature = "sync")]
4
5#[cfg(all(target_family = "wasm", not(target_os = "wasi")))]
6use wasm_bindgen_test::wasm_bindgen_test as test;
7
8use tokio::sync::Barrier;
9
10use tokio_test::task::spawn;
11use tokio_test::{assert_pending, assert_ready};
12
13struct IsSend<T: Send>(T);
14#[test]
15fn barrier_future_is_send() {
16 let b = Barrier::new(0);
17 IsSend(b.wait());
18}
19
20#[test]
21fn zero_does_not_block() {
22 let b = Barrier::new(0);
23
24 {
25 let mut w = spawn(b.wait());
26 let wr = assert_ready!(w.poll());
27 assert!(wr.is_leader());
28 }
29 {
30 let mut w = spawn(b.wait());
31 let wr = assert_ready!(w.poll());
32 assert!(wr.is_leader());
33 }
34}
35
36#[test]
37fn single() {
38 let b = Barrier::new(1);
39
40 {
41 let mut w = spawn(b.wait());
42 let wr = assert_ready!(w.poll());
43 assert!(wr.is_leader());
44 }
45 {
46 let mut w = spawn(b.wait());
47 let wr = assert_ready!(w.poll());
48 assert!(wr.is_leader());
49 }
50 {
51 let mut w = spawn(b.wait());
52 let wr = assert_ready!(w.poll());
53 assert!(wr.is_leader());
54 }
55}
56
57#[test]
58fn tango() {
59 let b = Barrier::new(2);
60
61 let mut w1 = spawn(b.wait());
62 assert_pending!(w1.poll());
63
64 let mut w2 = spawn(b.wait());
65 let wr2 = assert_ready!(w2.poll());
66 let wr1 = assert_ready!(w1.poll());
67
68 assert!(wr1.is_leader() || wr2.is_leader());
69 assert!(!(wr1.is_leader() && wr2.is_leader()));
70}
71
72#[test]
73fn lots() {
74 let b = Barrier::new(100);
75
76 for _ in 0..10 {
77 let mut wait = Vec::new();
78 for _ in 0..99 {
79 let mut w = spawn(b.wait());
80 assert_pending!(w.poll());
81 wait.push(w);
82 }
83 for w in &mut wait {
84 assert_pending!(w.poll());
85 }
86
87 // pass the barrier
88 let mut w = spawn(b.wait());
89 let mut found_leader = assert_ready!(w.poll()).is_leader();
90 for mut w in wait {
91 let wr = assert_ready!(w.poll());
92 if wr.is_leader() {
93 assert!(!found_leader);
94 found_leader = true;
95 }
96 }
97 assert!(found_leader);
98 }
99}
100