tokio/tokio-net/tests/support.rs
2019-08-19 19:42:54 -07:00

35 lines
871 B
Rust

#![warn(rust_2018_idioms)]
#![allow(dead_code)]
pub use tokio::runtime::current_thread::{self, Runtime as CurrentThreadRuntime};
use tokio::timer::Timeout;
#[cfg(all(unix, feature = "signal"))]
pub use tokio_net::signal::unix::{signal, SignalKind};
pub use futures_util::future;
use futures_util::future::FutureExt;
pub use futures_util::stream::StreamExt;
#[cfg(unix)]
use libc::{c_int, getpid, kill};
use std::future::Future;
use std::time::Duration;
pub fn with_timeout<F: Future>(future: F) -> impl Future<Output = F::Output> {
Timeout::new(future, Duration::from_secs(3)).map(Result::unwrap)
}
pub fn run_with_timeout<F>(rt: &mut CurrentThreadRuntime, future: F) -> F::Output
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);
}
}