]> git.proxmox.com Git - rustc.git/blob - library/std/src/sync/mpsc/mpsc_queue/tests.rs
New upstream version 1.63.0+dfsg1
[rustc.git] / library / std / src / sync / mpsc / mpsc_queue / tests.rs
1 use super::{Data, Empty, Inconsistent, Queue};
2 use crate::sync::mpsc::channel;
3 use crate::sync::Arc;
4 use crate::thread;
5
6 #[test]
7 fn test_full() {
8 let q: Queue<Box<_>> = Queue::new();
9 q.push(Box::new(1));
10 q.push(Box::new(2));
11 }
12
13 #[test]
14 fn test() {
15 let nthreads = 8;
16 let nmsgs = 1000;
17 let q = Queue::new();
18 match q.pop() {
19 Empty => {}
20 Inconsistent | Data(..) => panic!(),
21 }
22 let (tx, rx) = channel();
23 let q = Arc::new(q);
24
25 for _ in 0..nthreads {
26 let tx = tx.clone();
27 let q = q.clone();
28 thread::spawn(move || {
29 for i in 0..nmsgs {
30 q.push(i);
31 }
32 tx.send(()).unwrap();
33 });
34 }
35
36 let mut i = 0;
37 while i < nthreads * nmsgs {
38 match q.pop() {
39 Empty | Inconsistent => {}
40 Data(_) => i += 1,
41 }
42 }
43 drop(tx);
44 for _ in 0..nthreads {
45 rx.recv().unwrap();
46 }
47 }