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

32 lines
802 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-07-03 10:40:59 -07:00
use futures_util::future::FutureExt;
2019-05-14 10:27:36 -07:00
use libc::{c_int, getpid, kill};
2019-07-03 10:40:59 -07:00
use std::future::Future;
2019-02-21 11:56:15 -08:00
use std::time::Duration;
2019-07-03 10:40:59 -07:00
use tokio_timer::Timeout;
2018-07-28 12:30:46 -07:00
2019-07-03 10:40:59 -07:00
pub use futures_util::future;
pub use futures_util::stream::StreamExt;
2019-05-14 10:27:36 -07:00
pub use tokio::runtime::current_thread::{self, Runtime as CurrentThreadRuntime};
pub use tokio_signal::unix::Signal;
2018-07-28 12:30:46 -07:00
2019-07-03 10:40:59 -07:00
pub fn with_timeout<F: Future>(future: F) -> impl Future<Output = F::Output> {
Timeout::new(future, Duration::from_secs(1)).map(Result::unwrap)
2018-09-02 11:32:50 +02:00
}
2018-07-28 12:30:46 -07:00
2019-07-03 10:40:59 -07:00
pub fn run_with_timeout<F>(rt: &mut CurrentThreadRuntime, future: F) -> F::Output
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);
}
}