From 3a81d7746a71ba990d65b9f42ac23466e8c86106 Mon Sep 17 00:00:00 2001 From: Ivan Petkov Date: Sun, 15 Jul 2018 20:22:34 -0700 Subject: [PATCH] signal: Split up all integration tests to run in their own process * Cargo runs each integration-style-test in its own process. Since the tests use global data structures specific to the process, we should run them in an isolated manner to avoid having cross-test interactions * Fixes alexcrichton/tokio-signal#39 --- .travis.yml | 2 +- tests/drop_then_get_a_signal.rs | 28 ++++++++++ tests/multi_loop.rs | 47 ++++++++++++++++ tests/notify_both.rs | 32 +++++++++++ tests/signal.rs | 99 --------------------------------- tests/simple.rs | 20 +++++++ tests/twice.rs | 26 +++++++++ 7 files changed, 154 insertions(+), 100 deletions(-) create mode 100644 tests/drop_then_get_a_signal.rs create mode 100644 tests/multi_loop.rs create mode 100644 tests/notify_both.rs create mode 100644 tests/simple.rs create mode 100644 tests/twice.rs diff --git a/.travis.yml b/.travis.yml index 0b08695a1..fd408ebf0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,7 +17,7 @@ matrix: - travis-cargo --only nightly doc-upload script: - - cargo test + - cargo test --no-fail-fast - rustdoc --test README.md -L target/debug/deps env: diff --git a/tests/drop_then_get_a_signal.rs b/tests/drop_then_get_a_signal.rs new file mode 100644 index 000000000..0d53c5cac --- /dev/null +++ b/tests/drop_then_get_a_signal.rs @@ -0,0 +1,28 @@ +#![cfg(unix)] + +extern crate futures; +extern crate libc; +extern crate tokio; +extern crate tokio_core; +extern crate tokio_signal; + +use std::time::Duration; + +use tokio_core::reactor::{Core, Timeout}; +use tokio_signal::unix::Signal; + +#[test] +fn drop_then_get_a_signal() { + let mut lp = Core::new().unwrap(); + let handle = lp.handle(); + let signal = lp.run(Signal::with_handle( + libc::SIGUSR1, + &handle.new_tokio_handle(), + )).unwrap(); + drop(signal); + unsafe { + assert_eq!(libc::kill(libc::getpid(), libc::SIGUSR1), 0); + } + let timeout = Timeout::new(Duration::from_millis(1), &lp.handle()).unwrap(); + lp.run(timeout).unwrap(); +} diff --git a/tests/multi_loop.rs b/tests/multi_loop.rs new file mode 100644 index 000000000..e899b2338 --- /dev/null +++ b/tests/multi_loop.rs @@ -0,0 +1,47 @@ +#![cfg(unix)] + +extern crate futures; +extern crate libc; +extern crate tokio; +extern crate tokio_core; +extern crate tokio_signal; + +use std::sync::mpsc::channel; +use std::thread; + +use futures::stream::Stream; +use tokio_core::reactor::Core; +use tokio_signal::unix::Signal; + +#[test] +fn multi_loop() { + // An "ordinary" (non-future) channel + let (sender, receiver) = channel(); + // Run multiple times, to make sure there are no race conditions + for _ in 0..10 { + // Run multiple event loops, each one in its own thread + let threads: Vec<_> = (0..4) + .map(|_| { + let sender = sender.clone(); + thread::spawn(move || { + let mut lp = Core::new().unwrap(); + let signal = lp.run(Signal::new(libc::SIGHUP)).unwrap(); + sender.send(()).unwrap(); + lp.run(signal.into_future()).ok().unwrap(); + }) + }) + .collect(); + // Wait for them to declare they're ready + for &_ in threads.iter() { + receiver.recv().unwrap(); + } + // Send a signal + unsafe { + assert_eq!(libc::kill(libc::getpid(), libc::SIGHUP), 0); + } + // Make sure the threads terminated correctly + for t in threads { + t.join().unwrap(); + } + } +} diff --git a/tests/notify_both.rs b/tests/notify_both.rs new file mode 100644 index 000000000..b80259162 --- /dev/null +++ b/tests/notify_both.rs @@ -0,0 +1,32 @@ +#![cfg(unix)] + +extern crate futures; +extern crate libc; +extern crate tokio; +extern crate tokio_core; +extern crate tokio_signal; + +use futures::stream::Stream; +use futures::Future; +use tokio_core::reactor::Core; +use tokio_signal::unix::Signal; + +#[test] +fn notify_both() { + let mut lp = Core::new().unwrap(); + let handle = lp.handle(); + let signal1 = lp.run(Signal::with_handle( + libc::SIGUSR2, + &handle.new_tokio_handle(), + )).unwrap(); + let signal2 = lp.run(Signal::with_handle( + libc::SIGUSR2, + &handle.new_tokio_handle(), + )).unwrap(); + unsafe { + assert_eq!(libc::kill(libc::getpid(), libc::SIGUSR2), 0); + } + lp.run(signal1.into_future().join(signal2.into_future())) + .ok() + .unwrap(); +} diff --git a/tests/signal.rs b/tests/signal.rs index 54b6916b2..3a3093bce 100644 --- a/tests/signal.rs +++ b/tests/signal.rs @@ -6,25 +6,10 @@ extern crate tokio; extern crate tokio_core; extern crate tokio_signal; -use std::sync::mpsc::channel; -use std::thread; -use std::time::Duration; - use futures::stream::Stream; use futures::{Future, IntoFuture}; -use tokio_core::reactor::{Core, Timeout}; use tokio_signal::unix::Signal; -#[test] -fn simple() { - let mut lp = Core::new().unwrap(); - let signal = lp.run(Signal::new(libc::SIGUSR1)).unwrap(); - unsafe { - assert_eq!(libc::kill(libc::getpid(), libc::SIGUSR1), 0); - } - lp.run(signal.into_future()).ok().unwrap(); -} - #[test] fn tokio_simple() { tokio::run( @@ -39,87 +24,3 @@ fn tokio_simple() { .map_err(|err| panic!("{}", err)), ) } - -#[test] -fn notify_both() { - let mut lp = Core::new().unwrap(); - let handle = lp.handle(); - let signal1 = lp.run(Signal::with_handle( - libc::SIGUSR2, - &handle.new_tokio_handle(), - )).unwrap(); - let signal2 = lp.run(Signal::with_handle( - libc::SIGUSR2, - &handle.new_tokio_handle(), - )).unwrap(); - unsafe { - assert_eq!(libc::kill(libc::getpid(), libc::SIGUSR2), 0); - } - lp.run(signal1.into_future().join(signal2.into_future())) - .ok() - .unwrap(); -} - -#[test] -fn drop_then_get_a_signal() { - let mut lp = Core::new().unwrap(); - let handle = lp.handle(); - let signal = lp.run(Signal::with_handle( - libc::SIGUSR1, - &handle.new_tokio_handle(), - )).unwrap(); - drop(signal); - unsafe { - assert_eq!(libc::kill(libc::getpid(), libc::SIGUSR1), 0); - } - let timeout = Timeout::new(Duration::from_millis(1), &lp.handle()).unwrap(); - lp.run(timeout).unwrap(); -} - -#[test] -fn twice() { - let mut lp = Core::new().unwrap(); - let signal = lp.run(Signal::new(libc::SIGUSR1)).unwrap(); - unsafe { - assert_eq!(libc::kill(libc::getpid(), libc::SIGUSR1), 0); - } - let (num, signal) = lp.run(signal.into_future()).ok().unwrap(); - assert_eq!(num, Some(libc::SIGUSR1)); - unsafe { - assert_eq!(libc::kill(libc::getpid(), libc::SIGUSR1), 0); - } - lp.run(signal.into_future()).ok().unwrap(); -} - -#[test] -fn multi_loop() { - // An "ordinary" (non-future) channel - let (sender, receiver) = channel(); - // Run multiple times, to make sure there are no race conditions - for _ in 0..10 { - // Run multiple event loops, each one in its own thread - let threads: Vec<_> = (0..4) - .map(|_| { - let sender = sender.clone(); - thread::spawn(move || { - let mut lp = Core::new().unwrap(); - let signal = lp.run(Signal::new(libc::SIGHUP)).unwrap(); - sender.send(()).unwrap(); - lp.run(signal.into_future()).ok().unwrap(); - }) - }) - .collect(); - // Wait for them to declare they're ready - for &_ in threads.iter() { - receiver.recv().unwrap(); - } - // Send a signal - unsafe { - assert_eq!(libc::kill(libc::getpid(), libc::SIGHUP), 0); - } - // Make sure the threads terminated correctly - for t in threads { - t.join().unwrap(); - } - } -} diff --git a/tests/simple.rs b/tests/simple.rs new file mode 100644 index 000000000..f24fb019b --- /dev/null +++ b/tests/simple.rs @@ -0,0 +1,20 @@ +#![cfg(unix)] + +extern crate futures; +extern crate libc; +extern crate tokio_core; +extern crate tokio_signal; + +use futures::stream::Stream; +use tokio_core::reactor::Core; +use tokio_signal::unix::Signal; + +#[test] +fn simple() { + let mut lp = Core::new().unwrap(); + let signal = lp.run(Signal::new(libc::SIGUSR1)).unwrap(); + unsafe { + assert_eq!(libc::kill(libc::getpid(), libc::SIGUSR1), 0); + } + lp.run(signal.into_future()).ok().unwrap(); +} diff --git a/tests/twice.rs b/tests/twice.rs new file mode 100644 index 000000000..4eb5804f0 --- /dev/null +++ b/tests/twice.rs @@ -0,0 +1,26 @@ +#![cfg(unix)] + +extern crate futures; +extern crate libc; +extern crate tokio; +extern crate tokio_core; +extern crate tokio_signal; + +use futures::stream::Stream; +use tokio_core::reactor::Core; +use tokio_signal::unix::Signal; + +#[test] +fn twice() { + let mut lp = Core::new().unwrap(); + let signal = lp.run(Signal::new(libc::SIGUSR1)).unwrap(); + unsafe { + assert_eq!(libc::kill(libc::getpid(), libc::SIGUSR1), 0); + } + let (num, signal) = lp.run(signal.into_future()).ok().unwrap(); + assert_eq!(num, Some(libc::SIGUSR1)); + unsafe { + assert_eq!(libc::kill(libc::getpid(), libc::SIGUSR1), 0); + } + lp.run(signal.into_future()).ok().unwrap(); +}