tokio/tokio-signal/tests/support.rs
Michal 'vorner' Vaner 2f69acbe9f
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.
2018-09-14 23:28:47 +02:00

39 lines
1.0 KiB
Rust

#![cfg(unix)]
extern crate libc;
extern crate futures;
extern crate tokio;
extern crate tokio_signal;
use self::libc::{c_int, getpid, kill};
use std::time::Duration;
use self::tokio::timer::Timeout;
pub use self::futures::{Future, Stream};
pub use self::tokio::runtime::current_thread::{self, Runtime as CurrentThreadRuntime};
pub use self::tokio_signal::unix::Signal;
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")
})
}
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)]
pub fn send_signal(signal: c_int) {
unsafe {
assert_eq!(kill(getpid(), signal), 0);
}
}