@@ -32,6 +32,7 @@ use crate::sync::Mutex;
3232/// # });
3333/// # }
3434/// ```
35+ #[ cfg_attr( feature = "docs" , doc( cfg( unstable) ) ) ]
3536#[ derive( Debug ) ]
3637pub struct Barrier {
3738 state : Mutex < BarrierState > ,
@@ -60,6 +61,7 @@ struct BarrierState {
6061/// let barrier = Barrier::new(1);
6162/// let barrier_wait_result = barrier.wait();
6263/// ```
64+ #[ cfg_attr( feature = "docs" , doc( cfg( unstable) ) ) ]
6365#[ derive( Debug , Clone ) ]
6466pub struct BarrierWaitResult ( bool ) ;
6567
@@ -172,3 +174,58 @@ impl BarrierWaitResult {
172174 self . 0
173175 }
174176}
177+
178+ #[ cfg( test) ]
179+ mod test {
180+ use futures_channel:: mpsc:: unbounded;
181+ use futures_util:: sink:: SinkExt ;
182+ use futures_util:: stream:: StreamExt ;
183+
184+ use crate :: sync:: { Arc , Barrier } ;
185+ use crate :: task;
186+
187+ #[ test]
188+ fn test_barrier ( ) {
189+ // NOTE(dignifiedquire): Based on the test in std, I was seeing some
190+ // race conditions, so running it in a loop to make sure things are
191+ // solid.
192+
193+ for _ in 0 ..1_000 {
194+ task:: block_on ( async move {
195+ const N : usize = 10 ;
196+
197+ let barrier = Arc :: new ( Barrier :: new ( N ) ) ;
198+ let ( tx, mut rx) = unbounded ( ) ;
199+
200+ for _ in 0 ..N - 1 {
201+ let c = barrier. clone ( ) ;
202+ let mut tx = tx. clone ( ) ;
203+ task:: spawn ( async move {
204+ let res = c. wait ( ) . await ;
205+
206+ tx. send ( res. is_leader ( ) ) . await . unwrap ( ) ;
207+ } ) ;
208+ }
209+
210+ // At this point, all spawned threads should be blocked,
211+ // so we shouldn't get anything from the port
212+ let res = rx. try_next ( ) ;
213+ assert ! ( match res {
214+ Err ( _err) => true ,
215+ _ => false ,
216+ } ) ;
217+
218+ let mut leader_found = barrier. wait ( ) . await . is_leader ( ) ;
219+
220+ // Now, the barrier is cleared and we should get data.
221+ for _ in 0 ..N - 1 {
222+ if rx. next ( ) . await . unwrap ( ) {
223+ assert ! ( !leader_found) ;
224+ leader_found = true ;
225+ }
226+ }
227+ assert ! ( leader_found) ;
228+ } ) ;
229+ }
230+ }
231+ }
0 commit comments