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.
This commit is contained in:
Michal 'vorner' Vaner
2018-09-14 23:28:47 +02:00
parent 7e12f5c39e
commit 2f69acbe9f
13 changed files with 120 additions and 194 deletions
-29
View File
@@ -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
+1 -2
View File
@@ -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"
-35
View File
@@ -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
+2 -6
View File
@@ -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.");
}
+38 -24
View File
@@ -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()
}
+39 -28
View File
@@ -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()
}
+5 -11
View File
@@ -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() {}
+5 -10
View File
@@ -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");
}
+3 -3
View File
@@ -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();
+7 -13
View File
@@ -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");
}
+3 -4
View File
@@ -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");
}
+13 -25
View File
@@ -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<F>(lp: &mut Core, future: F) -> Result<F::Item, F::Error>
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<F>(rt: &mut CurrentThreadRuntime, future: F) -> Result<F::Item, F::Error>
where F: Future
{
let deadline = Deadline::new(future, Instant::now() + Duration::from_secs(1))
pub fn with_timeout<F: Future>(future: F) -> impl Future<Item = F::Item, Error = F::Error> {
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<F>(rt: &mut CurrentThreadRuntime, future: F) -> Result<F::Item, F::Error>
where F: Future
{
rt.block_on(with_timeout(future))
}
#[cfg(unix)]
+4 -4
View File
@@ -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();
}