]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/core-run-destroy.rs
Rollup merge of #61499 - varkor:issue-53457, r=oli-obk
[rust.git] / src / test / run-pass / core-run-destroy.rs
1 #![allow(unused_must_use)]
2 #![allow(stable_features)]
3 #![allow(deprecated)]
4 #![allow(unused_imports)]
5 // compile-flags:--test
6 // ignore-cloudabi no processes
7 // ignore-emscripten no processes
8 // ignore-sgx no processes
9
10 // N.B., these tests kill child processes. Valgrind sees these children as leaking
11 // memory, which makes for some *confusing* logs. That's why these are here
12 // instead of in std.
13
14 #![feature(rustc_private, duration)]
15
16 extern crate libc;
17
18 use std::process::{self, Command, Child, Output, Stdio};
19 use std::str;
20 use std::sync::mpsc::channel;
21 use std::thread;
22 use std::time::Duration;
23
24 macro_rules! t {
25     ($e:expr) => (match $e { Ok(e) => e, Err(e) => panic!("error: {}", e) })
26 }
27
28 #[test]
29 fn test_destroy_once() {
30     let mut p = sleeper();
31     t!(p.kill());
32 }
33
34 #[cfg(unix)]
35 pub fn sleeper() -> Child {
36     t!(Command::new("sleep").arg("1000").spawn())
37 }
38 #[cfg(windows)]
39 pub fn sleeper() -> Child {
40     // There's a `timeout` command on windows, but it doesn't like having
41     // its output piped, so instead just ping ourselves a few times with
42     // gaps in between so we're sure this process is alive for awhile
43     t!(Command::new("ping").arg("127.0.0.1").arg("-n").arg("1000").spawn())
44 }
45
46 #[test]
47 fn test_destroy_twice() {
48     let mut p = sleeper();
49     t!(p.kill()); // this shouldn't crash...
50     let _ = p.kill(); // ...and nor should this (and nor should the destructor)
51 }
52
53 #[test]
54 fn test_destroy_actually_kills() {
55     let cmd = if cfg!(windows) {
56         "cmd"
57     } else if cfg!(target_os = "android") {
58         "/system/bin/cat"
59     } else {
60         "cat"
61     };
62
63     // this process will stay alive indefinitely trying to read from stdin
64     let mut p = t!(Command::new(cmd)
65                            .stdin(Stdio::piped())
66                            .spawn());
67
68     t!(p.kill());
69
70     // Don't let this test time out, this should be quick
71     let (tx, rx) = channel();
72     thread::spawn(move|| {
73         thread::sleep_ms(1000);
74         if rx.try_recv().is_err() {
75             process::exit(1);
76         }
77     });
78     let code = t!(p.wait()).code();
79     if cfg!(windows) {
80         assert!(code.is_some());
81     } else {
82         assert!(code.is_none());
83     }
84     tx.send(());
85 }