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)]
27 macro_rules! succeed( ($e:expr) => (
28 match $e { Ok(..) => {}, Err(e) => fail!("failure: {}", e) }
32 { fn $name:ident() $b:block $($a:attr)* } => (
34 #![allow(unused_imports)]
39 use std::io::process::{Process, ProcessOutput};
45 $($a)* #[test] fn green() { f() }
46 $($a)* #[test] fn native() {
48 let (tx, rx) = channel();
49 native::task::spawn(proc() { tx.send(f()) });
57 fn start(argc: int, argv: **u8) -> int {
58 green::start(argc, argv, rustuv::event_loop, __test::main)
61 iotest!(fn test_destroy_once() {
62 let mut p = sleeper();
63 match p.signal_exit() {
65 Err(e) => fail!("error: {}", e),
70 pub fn sleeper() -> Process {
71 Process::new("sleep", ["1000".to_owned()]).unwrap()
74 pub fn sleeper() -> Process {
75 // There's a `timeout` command on windows, but it doesn't like having
76 // its output piped, so instead just ping ourselves a few times with
77 // gaps inbetweeen so we're sure this process is alive for awhile
78 Process::new("ping", ["127.0.0.1".to_owned(), "-n".to_owned(), "1000".to_owned()]).unwrap()
81 iotest!(fn test_destroy_twice() {
82 let mut p = sleeper();
83 succeed!(p.signal_exit()); // this shouldnt crash...
84 let _ = p.signal_exit(); // ...and nor should this (and nor should the destructor)
87 pub fn test_destroy_actually_kills(force: bool) {
88 use std::io::process::{Process, ProcessOutput, ExitStatus, ExitSignal};
93 #[cfg(unix,not(target_os="android"))]
94 static BLOCK_COMMAND: &'static str = "cat";
96 #[cfg(unix,target_os="android")]
97 static BLOCK_COMMAND: &'static str = "/system/bin/cat";
100 static BLOCK_COMMAND: &'static str = "cmd";
102 // this process will stay alive indefinitely trying to read from stdin
103 let mut p = Process::new(BLOCK_COMMAND, []).unwrap();
105 assert!(p.signal(0).is_ok());
108 p.signal_kill().unwrap();
110 p.signal_exit().unwrap();
113 // Don't let this test time out, this should be quick
114 let (tx, rx1) = channel();
115 let mut t = timer::Timer::new().unwrap();
116 let rx2 = t.oneshot(1000);
119 () = rx2.recv() => unsafe { libc::exit(1) },
120 () = rx1.recv() => {}
124 ExitStatus(..) => fail!("expected a signal"),
125 ExitSignal(..) => tx.send(()),
129 iotest!(fn test_unforced_destroy_actually_kills() {
130 test_destroy_actually_kills(false);
133 iotest!(fn test_forced_destroy_actually_kills() {
134 test_destroy_actually_kills(true);