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 #![allow(unused_must_use)]
12 #![allow(stable_features)]
14 #![allow(unused_imports)]
15 // compile-flags:--test
16 // ignore-cloudabi no processes
17 // ignore-emscripten no processes
19 // NB: These tests kill child processes. Valgrind sees these children as leaking
20 // memory, which makes for some *confusing* logs. That's why these are here
23 #![feature(libc, duration)]
27 use std::process::{self, Command, Child, Output, Stdio};
29 use std::sync::mpsc::channel;
31 use std::time::Duration;
34 ($e:expr) => (match $e { Ok(e) => e, Err(e) => panic!("error: {}", e) })
38 fn test_destroy_once() {
39 let mut p = sleeper();
44 pub fn sleeper() -> Child {
45 t!(Command::new("sleep").arg("1000").spawn())
48 pub fn sleeper() -> Child {
49 // There's a `timeout` command on windows, but it doesn't like having
50 // its output piped, so instead just ping ourselves a few times with
51 // gaps in between so we're sure this process is alive for awhile
52 t!(Command::new("ping").arg("127.0.0.1").arg("-n").arg("1000").spawn())
56 fn test_destroy_twice() {
57 let mut p = sleeper();
58 t!(p.kill()); // this shouldn't crash...
59 let _ = p.kill(); // ...and nor should this (and nor should the destructor)
63 fn test_destroy_actually_kills() {
64 let cmd = if cfg!(windows) {
66 } else if cfg!(target_os = "android") {
72 // this process will stay alive indefinitely trying to read from stdin
73 let mut p = t!(Command::new(cmd)
74 .stdin(Stdio::piped())
79 // Don't let this test time out, this should be quick
80 let (tx, rx) = channel();
81 thread::spawn(move|| {
82 thread::sleep_ms(1000);
83 if rx.try_recv().is_err() {
87 let code = t!(p.wait()).code();
89 assert!(code.is_some());
91 assert!(code.is_none());