1 // Copyright 2012-2013-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 // compile-flags:--test
14 // NB: These tests kill child processes. Valgrind sees these children as leaking
15 // memory, which makes for some *confusing* logs. That's why these are here
18 #![feature(libc, std_misc, duration)]
22 use std::process::{self, Command, Child, Output, Stdio};
24 use std::sync::mpsc::channel;
26 use std::time::Duration;
29 ($e:expr) => (match $e { Ok(e) => e, Err(e) => panic!("error: {}", e) })
33 fn test_destroy_once() {
34 let mut p = sleeper();
39 pub fn sleeper() -> Child {
40 t!(Command::new("sleep").arg("1000").spawn())
43 pub fn sleeper() -> Child {
44 // There's a `timeout` command on windows, but it doesn't like having
45 // its output piped, so instead just ping ourselves a few times with
46 // gaps in between so we're sure this process is alive for awhile
47 t!(Command::new("ping").arg("127.0.0.1").arg("-n").arg("1000").spawn())
51 fn test_destroy_twice() {
52 let mut p = sleeper();
53 t!(p.kill()); // this shouldn't crash...
54 let _ = p.kill(); // ...and nor should this (and nor should the destructor)
58 fn test_destroy_actually_kills() {
59 let cmd = if cfg!(windows) {
61 } else if cfg!(target_os = "android") {
67 // this process will stay alive indefinitely trying to read from stdin
68 let mut p = t!(Command::new(cmd)
69 .stdin(Stdio::piped())
74 // Don't let this test time out, this should be quick
75 let (tx, rx) = channel();
76 thread::spawn(move|| {
77 thread::sleep_ms(1000);
78 if rx.try_recv().is_err() {
82 let code = t!(p.wait()).code();
84 assert!(code.is_some());
86 assert!(code.is_none());