2019-07-26 21:54:25 +00:00
|
|
|
//@ run-pass
|
|
|
|
|
2018-09-14 10:20:28 +00:00
|
|
|
#![allow(unused_must_use)]
|
|
|
|
#![allow(stable_features)]
|
|
|
|
#![allow(deprecated)]
|
|
|
|
#![allow(unused_imports)]
|
2013-05-03 23:08:43 +00:00
|
|
|
//@ compile-flags:--test
|
2024-03-06 20:44:54 +00:00
|
|
|
//@ ignore-wasm32 no processes
|
2019-04-24 16:26:33 +00:00
|
|
|
//@ ignore-sgx no processes
|
2019-07-28 16:15:43 +00:00
|
|
|
//@ ignore-vxworks no 'cat' and 'sleep'
|
2022-09-12 22:38:49 +00:00
|
|
|
//@ ignore-fuchsia no 'cat'
|
2013-05-03 23:08:43 +00:00
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// N.B., these tests kill child processes. Valgrind sees these children as leaking
|
2013-05-03 23:08:43 +00:00
|
|
|
// memory, which makes for some *confusing* logs. That's why these are here
|
2013-05-21 00:07:24 +00:00
|
|
|
// instead of in std.
|
2013-05-03 23:08:43 +00:00
|
|
|
|
2015-04-10 20:51:53 +00:00
|
|
|
use std::process::{self, Command, Child, Output, Stdio};
|
2014-10-01 04:09:29 +00:00
|
|
|
use std::str;
|
2014-12-23 19:53:35 +00:00
|
|
|
use std::sync::mpsc::channel;
|
2015-03-30 18:00:05 +00:00
|
|
|
use std::thread;
|
2015-04-10 18:12:43 +00:00
|
|
|
use std::time::Duration;
|
2014-10-01 04:09:29 +00:00
|
|
|
|
2015-04-10 18:12:43 +00:00
|
|
|
macro_rules! t {
|
|
|
|
($e:expr) => (match $e { Ok(e) => e, Err(e) => panic!("error: {}", e) })
|
|
|
|
}
|
2014-03-25 15:44:40 +00:00
|
|
|
|
2015-09-18 17:19:23 +00:00
|
|
|
#[test]
|
2014-10-01 04:09:29 +00:00
|
|
|
fn test_destroy_once() {
|
2014-03-25 15:44:40 +00:00
|
|
|
let mut p = sleeper();
|
2015-09-18 17:19:23 +00:00
|
|
|
t!(p.kill());
|
2014-10-01 04:09:29 +00:00
|
|
|
}
|
2013-05-03 23:08:43 +00:00
|
|
|
|
2014-03-25 15:44:40 +00:00
|
|
|
#[cfg(unix)]
|
2015-04-10 18:12:43 +00:00
|
|
|
pub fn sleeper() -> Child {
|
2015-09-18 17:19:23 +00:00
|
|
|
t!(Command::new("sleep").arg("1000").spawn())
|
2014-03-25 15:44:40 +00:00
|
|
|
}
|
|
|
|
#[cfg(windows)]
|
2015-04-10 18:12:43 +00:00
|
|
|
pub fn sleeper() -> Child {
|
2014-03-25 15:44:40 +00:00
|
|
|
// There's a `timeout` command on windows, but it doesn't like having
|
|
|
|
// its output piped, so instead just ping ourselves a few times with
|
2014-08-01 23:42:13 +00:00
|
|
|
// gaps in between so we're sure this process is alive for awhile
|
2015-09-18 17:19:23 +00:00
|
|
|
t!(Command::new("ping").arg("127.0.0.1").arg("-n").arg("1000").spawn())
|
2014-03-25 15:44:40 +00:00
|
|
|
}
|
|
|
|
|
2015-09-18 17:19:23 +00:00
|
|
|
#[test]
|
2014-10-01 04:09:29 +00:00
|
|
|
fn test_destroy_twice() {
|
2014-03-25 15:44:40 +00:00
|
|
|
let mut p = sleeper();
|
2015-04-10 18:12:43 +00:00
|
|
|
t!(p.kill()); // this shouldn't crash...
|
|
|
|
let _ = p.kill(); // ...and nor should this (and nor should the destructor)
|
2014-10-01 04:09:29 +00:00
|
|
|
}
|
2013-05-03 23:08:43 +00:00
|
|
|
|
2015-04-10 18:12:43 +00:00
|
|
|
#[test]
|
|
|
|
fn test_destroy_actually_kills() {
|
2015-09-18 17:19:23 +00:00
|
|
|
let cmd = if cfg!(windows) {
|
|
|
|
"cmd"
|
|
|
|
} else if cfg!(target_os = "android") {
|
|
|
|
"/system/bin/cat"
|
|
|
|
} else {
|
|
|
|
"cat"
|
|
|
|
};
|
2013-05-03 23:08:43 +00:00
|
|
|
|
2013-05-12 12:58:00 +00:00
|
|
|
// this process will stay alive indefinitely trying to read from stdin
|
2015-09-18 17:19:23 +00:00
|
|
|
let mut p = t!(Command::new(cmd)
|
|
|
|
.stdin(Stdio::piped())
|
|
|
|
.spawn());
|
2013-05-03 23:08:43 +00:00
|
|
|
|
2015-09-18 17:19:23 +00:00
|
|
|
t!(p.kill());
|
2013-05-03 23:08:43 +00:00
|
|
|
|
2014-03-11 20:38:36 +00:00
|
|
|
// Don't let this test time out, this should be quick
|
2015-04-10 18:12:43 +00:00
|
|
|
let (tx, rx) = channel();
|
2015-03-30 18:00:05 +00:00
|
|
|
thread::spawn(move|| {
|
2015-04-10 18:12:43 +00:00
|
|
|
thread::sleep_ms(1000);
|
|
|
|
if rx.try_recv().is_err() {
|
|
|
|
process::exit(1);
|
2014-03-11 20:38:36 +00:00
|
|
|
}
|
2015-01-06 05:59:45 +00:00
|
|
|
});
|
2015-09-18 17:19:23 +00:00
|
|
|
let code = t!(p.wait()).code();
|
2015-04-10 20:51:53 +00:00
|
|
|
if cfg!(windows) {
|
|
|
|
assert!(code.is_some());
|
|
|
|
} else {
|
|
|
|
assert!(code.is_none());
|
|
|
|
}
|
2015-04-10 18:12:43 +00:00
|
|
|
tx.send(());
|
2014-10-01 04:09:29 +00:00
|
|
|
}
|