Files
tokio/tokio-signal/tests/support.rs
T

39 lines
1.0 KiB
Rust
Raw Normal View History

2018-09-02 11:32:50 +02:00
#![cfg(unix)]
2018-07-28 12:30:46 -07:00
extern crate libc;
extern crate futures;
extern crate tokio;
extern crate tokio_signal;
use self::libc::{c_int, getpid, kill};
2018-09-02 11:32:50 +02:00
use std::time::Duration;
use self::tokio::timer::Timeout;
2018-07-28 12:30:46 -07:00
pub use self::futures::{Future, Stream};
2018-09-02 11:32:50 +02:00
pub use self::tokio::runtime::current_thread::{self, Runtime as CurrentThreadRuntime};
2018-07-28 12:30:46 -07:00
pub use self::tokio_signal::unix::Signal;
2018-09-02 11:32:50 +02:00
pub fn with_timeout<F: Future>(future: F) -> impl Future<Item = F::Item, Error = F::Error> {
Timeout::new(future, Duration::from_secs(1))
2018-07-28 12:30:46 -07:00
.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")
2018-09-02 11:32:50 +02:00
})
}
2018-07-28 12:30:46 -07:00
2018-09-02 11:32:50 +02:00
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))
2018-07-28 12:30:46 -07:00
}
#[cfg(unix)]
pub fn send_signal(signal: c_int) {
unsafe {
assert_eq!(kill(getpid(), signal), 0);
}
}