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