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