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