File: condvar.rs

package info (click to toggle)
rust-async-std 1.13.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,992 kB
  • sloc: sh: 13; makefile: 8
file content (103 lines) | stat: -rw-r--r-- 2,848 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#![cfg(feature = "unstable")]
use std::sync::Arc;
use std::time::Duration;

use async_std::sync::{Condvar, Mutex};
use async_std::task::{self, JoinHandle};

#[cfg(not(target_os = "unknown"))]
use async_std::task::spawn;
#[cfg(target_os = "unknown")]
use async_std::task::spawn_local as spawn;

#[cfg(target_arch = "wasm32")]
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn wait_timeout_with_lock() {
    task::block_on(async {
        let pair = Arc::new((Mutex::new(false), Condvar::new()));
        let pair2 = pair.clone();

        spawn(async move {
            let (m, c) = &*pair2;
            let _g = m.lock().await;
            task::sleep(Duration::from_millis(200)).await;
            c.notify_one();
        });

        let (m, c) = &*pair;
        let (_, wait_result) = c
            .wait_timeout(m.lock().await, Duration::from_millis(50))
            .await;
        assert!(wait_result.timed_out());
    })
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn wait_timeout_without_lock() {
    task::block_on(async {
        let m = Mutex::new(false);
        let c = Condvar::new();

        let (_, wait_result) = c
            .wait_timeout(m.lock().await, Duration::from_millis(10))
            .await;
        assert!(wait_result.timed_out());
    })
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn wait_timeout_until_timed_out() {
    task::block_on(async {
        let m = Mutex::new(false);
        let c = Condvar::new();

        let (_, wait_result) = c
            .wait_timeout_until(m.lock().await, Duration::from_millis(100), |&mut started| {
                started
            })
            .await;
        assert!(wait_result.timed_out());
    })
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
fn notify_all() {
    task::block_on(async {
        let mut tasks: Vec<JoinHandle<()>> = Vec::new();
        let pair = Arc::new((Mutex::new(0u32), Condvar::new()));

        for _ in 0..10 {
            let pair = pair.clone();
            tasks.push(spawn(async move {
                let (m, c) = &*pair;
                let mut count = m.lock().await;
                while *count == 0 {
                    count = c.wait(count).await;
                }
                *count += 1;
            }));
        }

        // Give some time for tasks to start up
        task::sleep(Duration::from_millis(50)).await;

        let (m, c) = &*pair;
        {
            let mut count = m.lock().await;
            *count += 1;
            c.notify_all();
        }

        for t in tasks {
            t.await;
        }
        let count = m.lock().await;
        assert_eq!(11, *count);
    })
}