1 #![cfg(feature = "support-v0_8")]
2
3 use std::sync::atomic::{AtomicBool, Ordering};
4 use std::sync::Arc;
5 use std::thread;
6 use std::time::Duration;
7
8 use mio_0_8::{Events, Interest, Poll, Token};
9
10 use signal_hook::consts::{SIGUSR1, SIGUSR2};
11 use signal_hook::low_level::raise;
12 use signal_hook_mio::v0_8::Signals;
13
14 use serial_test::serial;
15
16 use libc::c_int;
17
18 #[test]
19 #[serial]
mio_wakeup()20 fn mio_wakeup() {
21 let mut signals = Signals::new(&[SIGUSR1]).unwrap();
22 let mut poll = Poll::new().unwrap();
23 let token = Token(0);
24 poll.registry()
25 .register(&mut signals, token, Interest::READABLE)
26 .unwrap();
27
28 let mut events = Events::with_capacity(10);
29
30 // The self pipe shouldn't be readable yet
31 poll.poll(&mut events, Some(Duration::from_secs(0)))
32 .unwrap();
33 assert!(events.is_empty());
34
35 raise(SIGUSR1).unwrap();
36 poll.poll(&mut events, Some(Duration::from_secs(10)))
37 .unwrap();
38 let event = events.iter().next().unwrap();
39
40 assert!(event.is_readable());
41 assert_eq!(token, event.token());
42 let sig = signals.pending().next().unwrap();
43 assert_eq!(SIGUSR1, sig);
44
45 // The self pipe shouldn't be readable after consuming signals
46 poll.poll(&mut events, Some(Duration::from_secs(0)))
47 .unwrap();
48 assert!(events.is_empty());
49 }
50
51 #[test]
52 #[serial]
mio_multiple_signals()53 fn mio_multiple_signals() {
54 let mut signals = Signals::new(&[SIGUSR1, SIGUSR2]).unwrap();
55 let mut poll = Poll::new().unwrap();
56 let token = Token(0);
57 poll.registry()
58 .register(&mut signals, token, Interest::READABLE)
59 .unwrap();
60
61 let mut events = Events::with_capacity(10);
62
63 raise(SIGUSR1).unwrap();
64 raise(SIGUSR2).unwrap();
65
66 poll.poll(&mut events, Some(Duration::from_secs(10)))
67 .unwrap();
68
69 let event = events.iter().next().unwrap();
70 assert!(event.is_readable());
71
72 let sigs: Vec<c_int> = signals.pending().collect();
73 assert_eq!(2, sigs.len());
74 assert!(sigs.contains(&SIGUSR1));
75 assert!(sigs.contains(&SIGUSR2));
76
77 // The self pipe shouldn't be completely empty after calling pending()
78 poll.poll(&mut events, Some(Duration::from_secs(0)))
79 .unwrap();
80 assert!(events.is_empty());
81 }
82
83 #[test]
84 #[serial]
mio_parallel_multiple()85 fn mio_parallel_multiple() {
86 let mut signals = Signals::new(&[SIGUSR1]).unwrap();
87 let mut poll = Poll::new().unwrap();
88 let token = Token(0);
89 poll.registry()
90 .register(&mut signals, token, Interest::READABLE)
91 .unwrap();
92
93 let mut events = Events::with_capacity(10);
94
95 let thread_done = Arc::new(AtomicBool::new(false));
96
97 let done = Arc::clone(&thread_done);
98 thread::spawn(move || {
99 for _ in 0..10 {
100 // Wait some time to allow main thread to poll
101 thread::sleep(Duration::from_millis(25));
102 raise(SIGUSR1).unwrap();
103 }
104 done.store(true, Ordering::SeqCst);
105
106 // Raise a final signal so the main thread wakes up
107 // if it already called poll.
108 raise(SIGUSR1).unwrap();
109 });
110
111 while !thread_done.load(Ordering::SeqCst) {
112 poll.poll(&mut events, Some(Duration::from_secs(10)))
113 .unwrap();
114 let event = events.iter().next().unwrap();
115 assert!(event.is_readable());
116 assert_eq!(SIGUSR1, signals.pending().next().unwrap());
117 }
118 }
119