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