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