1 #![cfg(feature = "unstable")]
2 use std::sync::Arc;
3 use std::time::Duration;
4 
5 use async_std::sync::{Condvar, Mutex};
6 use async_std::task::{self, JoinHandle};
7 
8 #[cfg(not(target_os = "unknown"))]
9 use async_std::task::spawn;
10 #[cfg(target_os = "unknown")]
11 use async_std::task::spawn_local as spawn;
12 
13 #[cfg(target_arch = "wasm32")]
14 wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
15 
16 #[test]
17 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
wait_timeout_with_lock()18 fn wait_timeout_with_lock() {
19     task::block_on(async {
20         let pair = Arc::new((Mutex::new(false), Condvar::new()));
21         let pair2 = pair.clone();
22 
23         spawn(async move {
24             let (m, c) = &*pair2;
25             let _g = m.lock().await;
26             task::sleep(Duration::from_millis(200)).await;
27             c.notify_one();
28         });
29 
30         let (m, c) = &*pair;
31         let (_, wait_result) = c
32             .wait_timeout(m.lock().await, Duration::from_millis(50))
33             .await;
34         assert!(wait_result.timed_out());
35     })
36 }
37 
38 #[test]
39 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
wait_timeout_without_lock()40 fn wait_timeout_without_lock() {
41     task::block_on(async {
42         let m = Mutex::new(false);
43         let c = Condvar::new();
44 
45         let (_, wait_result) = c
46             .wait_timeout(m.lock().await, Duration::from_millis(10))
47             .await;
48         assert!(wait_result.timed_out());
49     })
50 }
51 
52 #[test]
53 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
wait_timeout_until_timed_out()54 fn wait_timeout_until_timed_out() {
55     task::block_on(async {
56         let m = Mutex::new(false);
57         let c = Condvar::new();
58 
59         let (_, wait_result) = c
60             .wait_timeout_until(m.lock().await, Duration::from_millis(100), |&mut started| {
61                 started
62             })
63             .await;
64         assert!(wait_result.timed_out());
65     })
66 }
67 
68 #[test]
69 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
notify_all()70 fn notify_all() {
71     task::block_on(async {
72         let mut tasks: Vec<JoinHandle<()>> = Vec::new();
73         let pair = Arc::new((Mutex::new(0u32), Condvar::new()));
74 
75         for _ in 0..10 {
76             let pair = pair.clone();
77             tasks.push(spawn(async move {
78                 let (m, c) = &*pair;
79                 let mut count = m.lock().await;
80                 while *count == 0 {
81                     count = c.wait(count).await;
82                 }
83                 *count += 1;
84             }));
85         }
86 
87         // Give some time for tasks to start up
88         task::sleep(Duration::from_millis(50)).await;
89 
90         let (m, c) = &*pair;
91         {
92             let mut count = m.lock().await;
93             *count += 1;
94             c.notify_all();
95         }
96 
97         for t in tasks {
98             t.await;
99         }
100         let count = m.lock().await;
101         assert_eq!(11, *count);
102     })
103 }
104