From 2f69acbe9f5b4cb901180b7c1c028c7673082c52 Mon Sep 17 00:00:00 2001 From: Michal 'vorner' Vaner Date: Sun, 2 Sep 2018 11:32:50 +0200 Subject: [PATCH] signal: Fix tests after importing & linking * Don't use tokio-core any more for tests. That one brings tokio from crates.io instead of the current workspace and two versions of that don't want to cooperate. * Guard unix-specific examples on windows. * Leave CI setup to top-level directory. --- tokio-signal/.travis.yml | 29 --------- tokio-signal/Cargo.toml | 3 +- tokio-signal/appveyor.yml | 35 ---------- tokio-signal/examples/ctrl-c.rs | 8 +-- tokio-signal/examples/multiple.rs | 62 +++++++++++------- tokio-signal/examples/sighup-example.rs | 67 ++++++++++++-------- tokio-signal/src/lib.rs | 16 ++--- tokio-signal/tests/drop_then_get_a_signal.rs | 15 ++--- tokio-signal/tests/multi_loop.rs | 6 +- tokio-signal/tests/notify_both.rs | 20 ++---- tokio-signal/tests/simple.rs | 7 +- tokio-signal/tests/support.rs | 38 ++++------- tokio-signal/tests/twice.rs | 8 +-- 13 files changed, 120 insertions(+), 194 deletions(-) delete mode 100644 tokio-signal/.travis.yml delete mode 100644 tokio-signal/appveyor.yml diff --git a/tokio-signal/.travis.yml b/tokio-signal/.travis.yml deleted file mode 100644 index fd408ebf0..000000000 --- a/tokio-signal/.travis.yml +++ /dev/null @@ -1,29 +0,0 @@ -language: rust -sudo: false - -matrix: - include: - - rust: stable - - os: osx - - rust: beta - - rust: nightly - - - rust: nightly - before_script: - - pip install 'travis-cargo<0.2' --user && export PATH=$HOME/.local/bin:$PATH - script: - - cargo doc --no-deps --all-features - after_success: - - travis-cargo --only nightly doc-upload - -script: - - cargo test --no-fail-fast - - rustdoc --test README.md -L target/debug/deps - -env: - global: - - secure: "SXXK7Znvm1s5WWQ94l9IP25mXA0uGIQ7ghBuumZz3nSAfxhhJQnYi5hCAbl2/cOfSbpgEtE137dgk6Nd9UDx7rIkLCSe8TWyYjzraX/vvX3xNtLh/fjsayYYRK9a6qU2HIJegZdxPgyF5h2DeBgeLks0Ue8drrFQ1s9bYZVUO0yeuZ3aLkL1FkIG6RXGItUFpb6srEYL1NLizYLxXFEG3cL+kKoFIWc2qPx3EwOqv/eii134nQsuObhWZvPqfTo7zfNP8W/6TnoiggpRH1nrZc3DI3CynTICIOJ2Ogn9gFX9LftYKuJysSwUNVN3WF5aOuLP/XjRSBLYc+PW3v0iqiGzMX3n1VpcyhcbsSNA7ZckGn1HZsWYwspAxkN3idSuVie9Mezm7IV4005juiYKEWEr6hlkv1lzd49QZkWOvLCFCMRiwOOGp4NyzilG1Q1Zs3G1wrcvstmasNpK+QUFNdOFvT2sm34rI4x2rQUvjC/OyqbAK+PjYmTHL47YKON5ymfUL3mAcwgUfBUSd4Wpx8G3VKg3gMcmQm27ah1knOGJWH6XulYTnfGfx6bLo5t2NGx+vZk0naqajD3auWnseobMDsFjhUIRrt6GlnfPqeFoJSm0unu3riAX+RDF/iqZdDfjhX4evETIw3SaTl8EQtVLwz7kJTnxSbTU4XTi+0M=" - -notifications: - email: - on_success: never diff --git a/tokio-signal/Cargo.toml b/tokio-signal/Cargo.toml index a03901e8f..0974f2420 100644 --- a/tokio-signal/Cargo.toml +++ b/tokio-signal/Cargo.toml @@ -28,8 +28,7 @@ mio-uds = "0.6" signal-hook = "0.1" [dev-dependencies] -tokio-core = "0.1.17" -tokio = { version = "0.1.6", path = ".." } +tokio = { version = "0.1.8", path = ".." } [target.'cfg(windows)'.dependencies.winapi] version = "0.3" diff --git a/tokio-signal/appveyor.yml b/tokio-signal/appveyor.yml deleted file mode 100644 index 6067b9d26..000000000 --- a/tokio-signal/appveyor.yml +++ /dev/null @@ -1,35 +0,0 @@ -environment: - matrix: - # Stable channel - - TARGET: x86_64-pc-windows-gnu - CHANNEL: stable - - TARGET: x86_64-pc-windows-msvc - CHANNEL: stable - # Beta channel - - TARGET: x86_64-pc-windows-msvc - CHANNEL: beta - # Nightly channel - - TARGET: x86_64-pc-windows-msvc - CHANNEL: nightly - -# Install Rust and Cargo -# (Based on from https://github.com/rust-lang/libc/blob/master/appveyor.yml) -install: - - curl -sSf -o rustup-init.exe https://win.rustup.rs - - rustup-init.exe --default-host %TARGET% --default-toolchain %CHANNEL% -y - - set PATH=%PATH%;C:\Users\appveyor\.cargo\bin - - rustc -Vv - - cargo -V - -# 'cargo test' takes care of building for us, so disable Appveyor's build stage. This prevents -# the "directory does not contain a project or solution file" error. -# source: https://github.com/starkat99/appveyor-rust/blob/master/appveyor.yml#L113 -build: false - -test_script: - - cargo build --example ctrl-c - - rustdoc --test README.md -L target/debug/deps - -branches: - only: - - master diff --git a/tokio-signal/examples/ctrl-c.rs b/tokio-signal/examples/ctrl-c.rs index df821a3f5..be83c4258 100644 --- a/tokio-signal/examples/ctrl-c.rs +++ b/tokio-signal/examples/ctrl-c.rs @@ -1,17 +1,13 @@ extern crate futures; -extern crate tokio_core; +extern crate tokio; extern crate tokio_signal; use futures::{Future, Stream}; -use tokio_core::reactor::Core; /// how many signals to handle before exiting const STOP_AFTER: u64 = 10; fn main() { - // set up a Tokio event loop - let mut core = Core::new().unwrap(); - // tokio_signal provides a convenience builder for Ctrl+C // this even works cross-platform: linux and windows! // @@ -57,7 +53,7 @@ fn main() { // Up until now, we haven't really DONE anything, just prepared // now it's time to actually schedule, and thus execute, the stream // on our event loop - core.run(future).unwrap(); + tokio::runtime::current_thread::block_on_all(future).unwrap(); println!("Stream ended, quiting the program."); } diff --git a/tokio-signal/examples/multiple.rs b/tokio-signal/examples/multiple.rs index 380523188..b385d54cd 100644 --- a/tokio-signal/examples/multiple.rs +++ b/tokio-signal/examples/multiple.rs @@ -1,38 +1,52 @@ //! A small example of how to listen for two signals at the same time extern crate futures; -extern crate tokio_core; +extern crate tokio; extern crate tokio_signal; -use futures::{Future, Stream}; -use tokio_core::reactor::Core; -use tokio_signal::unix::{Signal, SIGINT, SIGTERM}; +// A trick to not fail build on non-unix platforms when using unix-specific features. +#[cfg(unix)] +mod platform { -fn main() { - let mut core = Core::new().unwrap(); + use futures::{Future, Stream}; + use tokio_signal::unix::{Signal, SIGINT, SIGTERM}; - // Create a stream for each of the signals we'd like to handle. - let sigint = Signal::new(SIGINT).flatten_stream(); - let sigterm = Signal::new(SIGTERM).flatten_stream(); + pub fn main() { + // Create a stream for each of the signals we'd like to handle. + let sigint = Signal::new(SIGINT).flatten_stream(); + let sigterm = Signal::new(SIGTERM).flatten_stream(); - // Use the `select` combinator to merge these two streams into one - let stream = sigint.select(sigterm); + // Use the `select` combinator to merge these two streams into one + let stream = sigint.select(sigterm); - // Wait for a signal to arrive - println!("Waiting for SIGINT or SIGTERM"); - println!( - " TIP: use `pkill -sigint multiple` from a second terminal \ + // Wait for a signal to arrive + println!("Waiting for SIGINT or SIGTERM"); + println!( + " TIP: use `pkill -sigint multiple` from a second terminal \ to send a SIGINT to all processes named 'multiple' \ (i.e. this binary)" - ); - let (item, _rest) = core.run(stream.into_future()).ok().unwrap(); + ); + let (item, _rest) = ::tokio::runtime::current_thread::block_on_all(stream.into_future()) + .ok() + .unwrap(); - // Figure out which signal we received - let item = item.unwrap(); - if item == SIGINT { - println!("received SIGINT"); - } else { - assert_eq!(item, SIGTERM); - println!("received SIGTERM"); + // Figure out which signal we received + let item = item.unwrap(); + if item == SIGINT { + println!("received SIGINT"); + } else { + assert_eq!(item, SIGTERM); + println!("received SIGTERM"); + } } + +} + +#[cfg(not(unix))] +mod platform { + pub fn main() {} +} + +fn main() { + platform::main() } diff --git a/tokio-signal/examples/sighup-example.rs b/tokio-signal/examples/sighup-example.rs index e48e23ea3..dc8c32524 100644 --- a/tokio-signal/examples/sighup-example.rs +++ b/tokio-signal/examples/sighup-example.rs @@ -1,39 +1,50 @@ extern crate futures; -extern crate tokio_core; +extern crate tokio; extern crate tokio_signal; -use futures::{Future, Stream}; -use tokio_core::reactor::Core; -use tokio_signal::unix::{Signal, SIGHUP}; +// A trick to not fail build on non-unix platforms when using unix-specific features. +#[cfg(unix)] +mod platform { -fn main() { - // set up a Tokio event loop - let mut core = Core::new().unwrap(); + use futures::{Future, Stream}; + use tokio_signal::unix::{Signal, SIGHUP}; - // on Unix, we can listen to whatever signal we want, in this case: SIGHUP - let stream = Signal::new(SIGHUP).flatten_stream(); + pub fn main() { + // on Unix, we can listen to whatever signal we want, in this case: SIGHUP + let stream = Signal::new(SIGHUP).flatten_stream(); - println!("Waiting for SIGHUPS (Ctrl+C to quit)"); - println!( - " TIP: use `pkill -sighup sighup-example` from a second terminal \ + println!("Waiting for SIGHUPS (Ctrl+C to quit)"); + println!( + " TIP: use `pkill -sighup sighup-example` from a second terminal \ to send a SIGHUP to all processes named 'sighup-example' \ (i.e. this binary)" - ); - - // for_each is a powerful primitive provided by the Futures crate - // it turns a Stream into a Future that completes after all stream-items - // have been completed. - let future = stream.for_each(|the_signal| { - println!( - "*Got signal {:#x}* I should probably reload my config \ - or something", - the_signal ); - Ok(()) - }); - // Up until now, we haven't really DONE anything, just prepared - // now it's time to actually schedule, and thus execute, the stream - // on our event loop, and loop forever - core.run(future).unwrap(); + // for_each is a powerful primitive provided by the Futures crate + // it turns a Stream into a Future that completes after all stream-items + // have been completed. + let future = stream.for_each(|the_signal| { + println!( + "*Got signal {:#x}* I should probably reload my config \ + or something", + the_signal + ); + Ok(()) + }); + + // Up until now, we haven't really DONE anything, just prepared + // now it's time to actually schedule, and thus execute, the stream + // on our event loop, and loop forever + ::tokio::runtime::current_thread::block_on_all(future).unwrap(); + } + +} + +#[cfg(not(unix))] +mod platform { + pub fn main() {} +} + +fn main() { + platform::main() } diff --git a/tokio-signal/src/lib.rs b/tokio-signal/src/lib.rs index 29dc914c2..8062ca39f 100644 --- a/tokio-signal/src/lib.rs +++ b/tokio-signal/src/lib.rs @@ -19,15 +19,12 @@ //! //! ```rust,no_run //! extern crate futures; -//! extern crate tokio_core; +//! extern crate tokio; //! extern crate tokio_signal; //! -//! use tokio_core::reactor::Core; //! use futures::{Future, Stream}; //! //! fn main() { -//! let mut core = Core::new().unwrap(); -//! //! // Create an infinite stream of "Ctrl+C" notifications. Each item received //! // on this stream may represent multiple ctrl-c signals. //! let ctrl_c = tokio_signal::ctrl_c().flatten_stream(); @@ -38,7 +35,7 @@ //! Ok(()) //! }); //! -//! core.run(prog).unwrap(); +//! tokio::runtime::current_thread::block_on_all(prog).unwrap(); //! } //! ``` //! @@ -46,28 +43,25 @@ //! //! ```rust,no_run //! # extern crate futures; -//! # extern crate tokio_core; +//! # extern crate tokio; //! # extern crate tokio_signal; //! # #[cfg(unix)] //! # mod foo { //! # //! extern crate futures; -//! extern crate tokio_core; +//! extern crate tokio; //! extern crate tokio_signal; //! -//! use tokio_core::reactor::Core; //! use futures::{Future, Stream}; //! use tokio_signal::unix::{Signal, SIGHUP}; //! //! fn main() { -//! let mut core = Core::new().unwrap(); -//! //! // Like the previous example, this is an infinite stream of signals //! // being received, and signals may be coalesced while pending. //! let stream = Signal::new(SIGHUP).flatten_stream(); //! //! // Convert out stream into a future and block the program -//! core.run(stream.into_future()).ok().unwrap(); +//! tokio::runtime::current_thread::block_on_all(stream.into_future()).ok().unwrap(); //! } //! # } //! # fn main() {} diff --git a/tokio-signal/tests/drop_then_get_a_signal.rs b/tokio-signal/tests/drop_then_get_a_signal.rs index 4323e38b7..8b58c0c20 100644 --- a/tokio-signal/tests/drop_then_get_a_signal.rs +++ b/tokio-signal/tests/drop_then_get_a_signal.rs @@ -7,22 +7,17 @@ use support::*; #[test] fn drop_then_get_a_signal() { - let mut lp = Core::new().unwrap(); - let handle = lp.handle(); - - let signal = run_core_with_timeout(&mut lp, Signal::with_handle( - libc::SIGUSR1, - &handle.new_tokio_handle(), - )).expect("failed to create first signal"); + let mut rt = CurrentThreadRuntime::new().unwrap(); + let signal = run_with_timeout(&mut rt, Signal::new(libc::SIGUSR1)) + .expect("failed to create first signal"); drop(signal); send_signal(libc::SIGUSR1); - let signal = lp.run(Signal::with_handle(libc::SIGUSR1, &handle.new_tokio_handle())) + let signal = run_with_timeout(&mut rt, Signal::new(libc::SIGUSR1)) .expect("failed to create signal") .into_future() .map(|_| ()) .map_err(|(e, _)| panic!("{}", e)); - run_core_with_timeout(&mut lp, signal) - .expect("failed to get signal"); + run_with_timeout(&mut rt, signal).expect("failed to get signal"); } diff --git a/tokio-signal/tests/multi_loop.rs b/tokio-signal/tests/multi_loop.rs index 40facc191..31522abae 100644 --- a/tokio-signal/tests/multi_loop.rs +++ b/tokio-signal/tests/multi_loop.rs @@ -19,10 +19,10 @@ fn multi_loop() { .map(|_| { let sender = sender.clone(); thread::spawn(move || { - let mut lp = Core::new().unwrap(); - let signal = lp.run(Signal::new(libc::SIGHUP)).unwrap(); + let mut rt = CurrentThreadRuntime::new().unwrap(); + let signal = run_with_timeout(&mut rt, Signal::new(libc::SIGHUP)).unwrap(); sender.send(()).unwrap(); - run_core_with_timeout(&mut lp, signal.into_future()).ok().unwrap(); + run_with_timeout(&mut rt, signal.into_future()).ok().unwrap(); }) }) .collect(); diff --git a/tokio-signal/tests/notify_both.rs b/tokio-signal/tests/notify_both.rs index ddcec33b7..b45ff6701 100644 --- a/tokio-signal/tests/notify_both.rs +++ b/tokio-signal/tests/notify_both.rs @@ -7,21 +7,15 @@ use support::*; #[test] fn notify_both() { - let mut lp = Core::new().unwrap(); - let handle = lp.handle(); + let mut rt = CurrentThreadRuntime::new().unwrap(); + let signal1 = run_with_timeout(&mut rt, Signal::new(libc::SIGUSR2)) + .expect("failed to create signal1"); - let signal1 = run_core_with_timeout(&mut lp, Signal::with_handle( - libc::SIGUSR2, - &handle.new_tokio_handle(), - )).expect("failed to create signal1"); - - let signal2 = run_core_with_timeout(&mut lp, Signal::with_handle( - libc::SIGUSR2, - &handle.new_tokio_handle(), - )).expect("failed to create signal2"); + let signal2 = run_with_timeout(&mut rt, Signal::new(libc::SIGUSR2)) + .expect("failed to create signal2"); send_signal(libc::SIGUSR2); - run_core_with_timeout(&mut lp, signal1.into_future().join(signal2.into_future())) + run_with_timeout(&mut rt, signal1.into_future().join(signal2.into_future())) .ok() - .expect("failed to create signal2"); + .expect("failed to receive"); } diff --git a/tokio-signal/tests/simple.rs b/tokio-signal/tests/simple.rs index 117cdce19..d7b5f7920 100644 --- a/tokio-signal/tests/simple.rs +++ b/tokio-signal/tests/simple.rs @@ -7,14 +7,13 @@ use support::*; #[test] fn simple() { - let mut lp = Core::new().unwrap(); - - let signal = run_core_with_timeout(&mut lp, Signal::new(libc::SIGUSR1)) + let mut rt = CurrentThreadRuntime::new().unwrap(); + let signal = run_with_timeout(&mut rt, Signal::new(libc::SIGUSR1)) .expect("failed to create signal"); send_signal(libc::SIGUSR1); - run_core_with_timeout(&mut lp, signal.into_future()) + run_with_timeout(&mut rt, signal.into_future()) .ok() .expect("failed to get signal"); } diff --git a/tokio-signal/tests/support.rs b/tokio-signal/tests/support.rs index c9c7fd620..942b6a09a 100644 --- a/tokio-signal/tests/support.rs +++ b/tokio-signal/tests/support.rs @@ -1,45 +1,33 @@ +#![cfg(unix)] + extern crate libc; extern crate futures; extern crate tokio; -extern crate tokio_core; extern crate tokio_signal; use self::libc::{c_int, getpid, kill}; -use std::time::{Duration, Instant}; -use self::tokio::timer::Deadline; -use self::tokio_core::reactor::Timeout; +use std::time::Duration; +use self::tokio::timer::Timeout; pub use self::futures::{Future, Stream}; -pub use self::tokio_core::reactor::Core; -pub use self::tokio::runtime::current_thread::Runtime as CurrentThreadRuntime; +pub use self::tokio::runtime::current_thread::{self, Runtime as CurrentThreadRuntime}; pub use self::tokio_signal::unix::Signal; -pub fn run_core_with_timeout(lp: &mut Core, future: F) -> Result - where F: Future -{ - let timeout = Timeout::new(Duration::from_secs(1), &lp.handle()) - .expect("failed to register timeout") - .map(|()| panic!("timeout exceeded")) - .map_err(|e| panic!("timeout error: {}", e)); - - lp.run(future.select(timeout)) - .map(|(r, _)| r) - .map_err(|(e, _)| e) -} - -pub fn run_with_timeout(rt: &mut CurrentThreadRuntime, future: F) -> Result - where F: Future -{ - let deadline = Deadline::new(future, Instant::now() + Duration::from_secs(1)) +pub fn with_timeout(future: F) -> impl Future { + Timeout::new(future, Duration::from_secs(1)) .map_err(|e| if e.is_timer() { panic!("failed to register timer"); } else if e.is_elapsed() { panic!("timed out") } else { e.into_inner().expect("missing inner error") - }); + }) +} - rt.block_on(deadline) +pub fn run_with_timeout(rt: &mut CurrentThreadRuntime, future: F) -> Result + where F: Future +{ + rt.block_on(with_timeout(future)) } #[cfg(unix)] diff --git a/tokio-signal/tests/twice.rs b/tokio-signal/tests/twice.rs index 3ddefa58a..33e36511d 100644 --- a/tokio-signal/tests/twice.rs +++ b/tokio-signal/tests/twice.rs @@ -7,13 +7,13 @@ use support::*; #[test] fn twice() { - let mut lp = Core::new().unwrap(); - let signal = run_core_with_timeout(&mut lp, Signal::new(libc::SIGUSR1)).unwrap(); + let mut rt = CurrentThreadRuntime::new().unwrap(); + let signal = run_with_timeout(&mut rt, Signal::new(libc::SIGUSR1)).unwrap(); send_signal(libc::SIGUSR1); - let (num, signal) = run_core_with_timeout(&mut lp, signal.into_future()).ok().unwrap(); + let (num, signal) = run_with_timeout(&mut rt, signal.into_future()).ok().unwrap(); assert_eq!(num, Some(libc::SIGUSR1)); send_signal(libc::SIGUSR1); - run_core_with_timeout(&mut lp, signal.into_future()).ok().unwrap(); + run_with_timeout(&mut rt, signal.into_future()).ok().unwrap(); }