xref: /relibc/ralloc/tests/mpsc.rs (revision 92ca1d73ef9a68d6508c46e2f6ad99fc65e8bc68)
1 extern crate ralloc;
2 
3 mod util;
4 
5 use std::sync::mpsc;
6 use std::thread;
7 
8 #[test]
9 fn mpsc_queue() {
10     util::multiply(|| {
11         {
12             let (tx, rx) = mpsc::channel::<Box<u64>>();
13 
14             let handle = thread::spawn(move || {
15                 util::acid(|| {
16                     tx.send(Box::new(0xBABAFBABAF)).unwrap();
17                     tx.send(Box::new(0xDEADBEAF)).unwrap();
18                     tx.send(Box::new(0xDECEA5E)).unwrap();
19                     tx.send(Box::new(0xDEC1A551F1E5)).unwrap();
20                 });
21             });
22             assert_eq!(*rx.recv().unwrap(), 0xBABAFBABAF);
23             assert_eq!(*rx.recv().unwrap(), 0xDEADBEAF);
24             assert_eq!(*rx.recv().unwrap(), 0xDECEA5E);
25             assert_eq!(*rx.recv().unwrap(), 0xDEC1A551F1E5);
26 
27             handle.join().unwrap();
28         }
29 
30         let (tx, rx) = mpsc::channel();
31         let mut handles = Vec::new();
32 
33         for _ in 0..10 {
34             util::acid(|| {
35                 let tx = tx.clone();
36                 handles.push(thread::spawn(move || {
37                     tx.send(Box::new(0xFA11BAD)).unwrap();
38                 }));
39             });
40         }
41 
42         for _ in 0..10 {
43             assert_eq!(*rx.recv().unwrap(), 0xFA11BAD);
44         }
45 
46         for i in handles {
47             i.join().unwrap()
48         }
49     });
50 }
51