process: Share init in tests

This commit is contained in:
Alex Crichton
2019-06-24 16:56:48 -07:00
committed by Ivan Petkov
parent 124391e42b
commit 9680ecc109
3 changed files with 52 additions and 47 deletions
+3 -36
View File
@@ -1,50 +1,17 @@
extern crate futures;
extern crate tokio_core;
extern crate tokio_process;
use std::env;
use std::sync::mpsc::channel;
use std::sync::{Once, ONCE_INIT};
use std::thread;
use std::process::Command;
use tokio_core::reactor::Core;
use tokio_process::CommandExt;
static INIT: Once = ONCE_INIT;
fn init() {
INIT.call_once(|| {
let (tx, rx) = channel();
thread::spawn(move || {
let mut lp = Core::new().unwrap();
let mut cmd = exit();
let mut child = cmd.spawn_async(&lp.handle()).unwrap();
drop(child.kill());
lp.run(child).unwrap();
tx.send(()).unwrap();
drop(lp.run(futures::empty::<(), ()>()));
});
rx.recv().unwrap();
});
}
fn exit() -> Command {
let mut me = env::current_exe().unwrap();
me.pop();
if me.ends_with("deps") {
me.pop();
}
me.push("exit");
Command::new(me)
}
mod support;
#[test]
fn simple() {
init();
support::init();
let mut lp = Core::new().unwrap();
let mut cmd = exit();
let mut cmd = support::cmd("exit");
cmd.arg("2");
let mut child = cmd.spawn_async(&lp.handle()).unwrap();
let id = child.id();