| 1 | #![warn (rust_2018_idioms)] |
| 2 | #![cfg (feature = "full" )] |
| 3 | #![cfg (unix)] |
| 4 | |
| 5 | mod support { |
| 6 | pub mod signal; |
| 7 | } |
| 8 | use support::signal::send_signal; |
| 9 | |
| 10 | use tokio::runtime::Runtime; |
| 11 | use tokio::signal::unix::{signal, SignalKind}; |
| 12 | |
| 13 | use std::sync::mpsc::channel; |
| 14 | use std::thread; |
| 15 | |
| 16 | #[test] |
| 17 | fn multi_loop() { |
| 18 | // An "ordinary" (non-future) channel |
| 19 | let (sender, receiver) = channel(); |
| 20 | // Run multiple times, to make sure there are no race conditions |
| 21 | for _ in 0..10 { |
| 22 | // Run multiple event loops, each one in its own thread |
| 23 | let threads: Vec<_> = (0..4) |
| 24 | .map(|_| { |
| 25 | let sender = sender.clone(); |
| 26 | thread::spawn(move || { |
| 27 | let rt = rt(); |
| 28 | let _ = rt.block_on(async { |
| 29 | let mut signal = signal(SignalKind::hangup()).unwrap(); |
| 30 | sender.send(()).unwrap(); |
| 31 | signal.recv().await |
| 32 | }); |
| 33 | }) |
| 34 | }) |
| 35 | .collect(); |
| 36 | // Wait for them to declare they're ready |
| 37 | for &_ in threads.iter() { |
| 38 | receiver.recv().unwrap(); |
| 39 | } |
| 40 | // Send a signal |
| 41 | send_signal(libc::SIGHUP); |
| 42 | // Make sure the threads terminated correctly |
| 43 | for t in threads { |
| 44 | t.join().unwrap(); |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | fn rt() -> Runtime { |
| 50 | tokio::runtime::Builder::new_current_thread() |
| 51 | .enable_all() |
| 52 | .build() |
| 53 | .unwrap() |
| 54 | } |
| 55 | |