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