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

37 lines
961 B
Rust
Raw Normal View History

2018-09-02 11:32:50 +02:00
#![cfg(unix)]
2019-05-14 10:27:36 -07:00
#![deny(warnings, rust_2018_idioms)]
2018-09-02 11:32:50 +02:00
2019-05-14 10:27:36 -07:00
use libc::{c_int, getpid, kill};
2019-02-21 11:56:15 -08:00
use std::time::Duration;
2019-05-14 10:27:36 -07:00
use tokio::timer::Timeout;
2018-07-28 12:30:46 -07:00
2019-05-14 10:27:36 -07:00
pub use futures::{Future, Stream};
pub use tokio::runtime::current_thread::{self, Runtime as CurrentThreadRuntime};
pub use tokio_signal::unix::Signal;
2018-07-28 12:30:46 -07:00
2018-09-02 11:32:50 +02:00
pub fn with_timeout<F: Future>(future: F) -> impl Future<Item = F::Item, Error = F::Error> {
2019-02-21 11:56:15 -08:00
Timeout::new(future, Duration::from_secs(1)).map_err(|e| {
if e.is_timer() {
2018-07-28 12:30:46 -07:00
panic!("failed to register timer");
} else if e.is_elapsed() {
panic!("timed out")
} else {
e.into_inner().expect("missing inner error")
2019-02-21 11:56:15 -08:00
}
})
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>
2019-02-21 11:56:15 -08:00
where
F: Future,
2018-09-02 11:32:50 +02:00
{
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);
}
}