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.
12 // 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 #![reexport_test_harness_main = "test_main"]
22 use std::io::{Process, Command, timer};
23 use std::time::Duration;
25 use std::sync::mpsc::channel;
26 use std::thread::Thread;
28 macro_rules! succeed { ($e:expr) => (
29 match $e { Ok(..) => {}, Err(e) => panic!("panic: {}", e) }
32 fn test_destroy_once() {
33 let mut p = sleeper();
34 match p.signal_exit() {
36 Err(e) => panic!("error: {}", e),
41 pub fn sleeper() -> Process {
42 Command::new("sleep").arg("1000").spawn().unwrap()
45 pub fn sleeper() -> Process {
46 // There's a `timeout` command on windows, but it doesn't like having
47 // its output piped, so instead just ping ourselves a few times with
48 // gaps in between so we're sure this process is alive for awhile
49 Command::new("ping").arg("127.0.0.1").arg("-n").arg("1000").spawn().unwrap()
52 fn test_destroy_twice() {
53 let mut p = sleeper();
54 succeed!(p.signal_exit()); // this shouldnt crash...
55 let _ = p.signal_exit(); // ...and nor should this (and nor should the destructor)
58 pub fn test_destroy_actually_kills(force: bool) {
59 use std::io::process::{Command, ProcessOutput, ExitStatus, ExitSignal};
64 #[cfg(all(unix,not(target_os="android")))]
65 static BLOCK_COMMAND: &'static str = "cat";
67 #[cfg(all(unix,target_os="android"))]
68 static BLOCK_COMMAND: &'static str = "/system/bin/cat";
71 static BLOCK_COMMAND: &'static str = "cmd";
73 // this process will stay alive indefinitely trying to read from stdin
74 let mut p = Command::new(BLOCK_COMMAND).spawn().unwrap();
76 assert!(p.signal(0).is_ok());
79 p.signal_kill().unwrap();
81 p.signal_exit().unwrap();
84 // Don't let this test time out, this should be quick
85 let (tx, rx1) = channel();
86 let mut t = timer::Timer::new().unwrap();
87 let rx2 = t.oneshot(Duration::milliseconds(1000));
88 Thread::spawn(move|| {
90 _ = rx2.recv() => unsafe { libc::exit(1) },
94 match p.wait().unwrap() {
95 ExitStatus(..) => panic!("expected a signal"),
96 ExitSignal(..) => tx.send(()).unwrap(),
100 fn test_unforced_destroy_actually_kills() {
101 test_destroy_actually_kills(false);
104 fn test_forced_destroy_actually_kills() {
105 test_destroy_actually_kills(true);