Files
tokio/tokio-process/tests/support/mod.rs
T

43 lines
1.2 KiB
Rust
Raw Normal View History

extern crate tokio;
2016-12-18 22:48:18 -08:00
2019-07-29 21:36:11 -04:00
use futures_util::future;
use futures_util::future::FutureExt;
2016-12-18 22:48:18 -08:00
use std::env;
2019-07-29 21:36:11 -04:00
use std::future::Future;
2016-12-18 22:48:18 -08:00
use std::process::Command;
use std::time::Duration;
2019-07-29 21:36:11 -04:00
use tokio::timer::Timeout;
2016-12-18 22:48:18 -08:00
pub use self::tokio::runtime::current_thread::Runtime as CurrentThreadRuntime;
2019-07-29 21:36:11 -04:00
#[allow(dead_code)]
2016-12-18 22:48:18 -08:00
pub fn cmd(s: &str) -> Command {
let mut me = env::current_exe().unwrap();
me.pop();
if me.ends_with("deps") {
me.pop();
}
me.push(s);
Command::new(me)
}
2019-07-29 21:36:11 -04:00
pub fn with_timeout<F: Future>(future: F) -> impl Future<Output = F::Output> {
Timeout::new(future, Duration::from_secs(3)).then(|r| {
if r.is_err() {
panic!("timed out {:?}", r.err());
}
2019-07-29 21:36:11 -04:00
future::ready(r.unwrap())
})
}
2019-07-29 21:36:11 -04:00
pub fn run_with_timeout<F>(future: F) -> F::Output
where
F: Future,
{
// NB: Timeout requires a timer registration which is provided by
// tokio's `current_thread::Runtime`, but isn't available by just using
// tokio's default CurrentThread executor which powers `current_thread::block_on_all`.
let mut rt = CurrentThreadRuntime::new().expect("failed to get runtime");
rt.block_on(with_timeout(future))
}