]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/core-run-destroy.rs
cleanup: s/impl Copy/#[derive(Copy)]/g
[rust.git] / src / test / run-pass / core-run-destroy.rs
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.
4 //
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.
10
11 // ignore-pretty
12 // compile-flags:--test
13
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
16 // instead of in std.
17
18 #![reexport_test_harness_main = "test_main"]
19
20 extern crate libc;
21
22 use std::io::{Process, Command, timer};
23 use std::time::Duration;
24 use std::str;
25 use std::sync::mpsc::channel;
26 use std::thread::Thread;
27
28 macro_rules! succeed { ($e:expr) => (
29     match $e { Ok(..) => {}, Err(e) => panic!("panic: {}", e) }
30 ) }
31
32 fn test_destroy_once() {
33     let mut p = sleeper();
34     match p.signal_exit() {
35         Ok(()) => {}
36         Err(e) => panic!("error: {}", e),
37     }
38 }
39
40 #[cfg(unix)]
41 pub fn sleeper() -> Process {
42     Command::new("sleep").arg("1000").spawn().unwrap()
43 }
44 #[cfg(windows)]
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()
50 }
51
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)
56 }
57
58 pub fn test_destroy_actually_kills(force: bool) {
59     use std::io::process::{Command, ProcessOutput, ExitStatus, ExitSignal};
60     use std::io::timer;
61     use libc;
62     use std::str;
63
64     #[cfg(all(unix,not(target_os="android")))]
65     static BLOCK_COMMAND: &'static str = "cat";
66
67     #[cfg(all(unix,target_os="android"))]
68     static BLOCK_COMMAND: &'static str = "/system/bin/cat";
69
70     #[cfg(windows)]
71     static BLOCK_COMMAND: &'static str = "cmd";
72
73     // this process will stay alive indefinitely trying to read from stdin
74     let mut p = Command::new(BLOCK_COMMAND).spawn().unwrap();
75
76     assert!(p.signal(0).is_ok());
77
78     if force {
79         p.signal_kill().unwrap();
80     } else {
81         p.signal_exit().unwrap();
82     }
83
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|| {
89         select! {
90             _ = rx2.recv() => unsafe { libc::exit(1) },
91             _ = rx1.recv() => {}
92         }
93     });
94     match p.wait().unwrap() {
95         ExitStatus(..) => panic!("expected a signal"),
96         ExitSignal(..) => tx.send(()).unwrap(),
97     }
98 }
99
100 fn test_unforced_destroy_actually_kills() {
101     test_destroy_actually_kills(false);
102 }
103
104 fn test_forced_destroy_actually_kills() {
105     test_destroy_actually_kills(true);
106 }