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