]> git.proxmox.com Git - rustc.git/blob - vendor/crossbeam-utils/tests/wait_group.rs
New upstream version 1.43.0+dfsg1
[rustc.git] / vendor / crossbeam-utils / tests / wait_group.rs
1 extern crate crossbeam_utils;
2
3 use std::sync::mpsc;
4 use std::thread;
5 use std::time::Duration;
6
7 use crossbeam_utils::sync::WaitGroup;
8
9 const THREADS: usize = 10;
10
11 #[test]
12 fn wait() {
13 let wg = WaitGroup::new();
14 let (tx, rx) = mpsc::channel();
15
16 for _ in 0..THREADS {
17 let wg = wg.clone();
18 let tx = tx.clone();
19
20 thread::spawn(move || {
21 wg.wait();
22 tx.send(()).unwrap();
23 });
24 }
25
26 thread::sleep(Duration::from_millis(100));
27
28 // At this point, all spawned threads should be blocked, so we shouldn't get anything from the
29 // channel.
30 assert!(rx.try_recv().is_err());
31
32 wg.wait();
33
34 // Now, the wait group is cleared and we should receive messages.
35 for _ in 0..THREADS {
36 rx.recv().unwrap();
37 }
38 }
39
40 #[test]
41 fn wait_and_drop() {
42 let wg = WaitGroup::new();
43 let (tx, rx) = mpsc::channel();
44
45 for _ in 0..THREADS {
46 let wg = wg.clone();
47 let tx = tx.clone();
48
49 thread::spawn(move || {
50 thread::sleep(Duration::from_millis(100));
51 tx.send(()).unwrap();
52 drop(wg);
53 });
54 }
55
56 // At this point, all spawned threads should be sleeping, so we shouldn't get anything from the
57 // channel.
58 assert!(rx.try_recv().is_err());
59
60 wg.wait();
61
62 // Now, the wait group is cleared and we should receive messages.
63 for _ in 0..THREADS {
64 rx.try_recv().unwrap();
65 }
66 }