]>
Commit | Line | Data |
---|---|---|
1a4d82fc | 1 | // Copyright 2012-2013-2014 The Rust Project Developers. See the COPYRIGHT |
970d7e83 LB |
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 | ||
970d7e83 | 11 | // compile-flags:--test |
abe05a73 | 12 | // ignore-emscripten no processes |
970d7e83 LB |
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 | ||
d9579d0f | 18 | #![feature(libc, std_misc, duration)] |
1a4d82fc JJ |
19 | |
20 | extern crate libc; | |
21 | ||
9346a6ac | 22 | use std::process::{self, Command, Child, Output, Stdio}; |
970d7e83 | 23 | use std::str; |
1a4d82fc | 24 | use std::sync::mpsc::channel; |
c34b1796 | 25 | use std::thread; |
9346a6ac | 26 | use std::time::Duration; |
1a4d82fc | 27 | |
9346a6ac AL |
28 | macro_rules! t { |
29 | ($e:expr) => (match $e { Ok(e) => e, Err(e) => panic!("error: {}", e) }) | |
30 | } | |
970d7e83 | 31 | |
b039eaaf | 32 | #[test] |
970d7e83 | 33 | fn test_destroy_once() { |
1a4d82fc | 34 | let mut p = sleeper(); |
b039eaaf | 35 | t!(p.kill()); |
1a4d82fc JJ |
36 | } |
37 | ||
38 | #[cfg(unix)] | |
9346a6ac | 39 | pub fn sleeper() -> Child { |
b039eaaf | 40 | t!(Command::new("sleep").arg("1000").spawn()) |
1a4d82fc JJ |
41 | } |
42 | #[cfg(windows)] | |
9346a6ac | 43 | pub fn sleeper() -> Child { |
1a4d82fc JJ |
44 | // There's a `timeout` command on windows, but it doesn't like having |
45 | // its output piped, so instead just ping ourselves a few times with | |
46 | // gaps in between so we're sure this process is alive for awhile | |
b039eaaf | 47 | t!(Command::new("ping").arg("127.0.0.1").arg("-n").arg("1000").spawn()) |
970d7e83 LB |
48 | } |
49 | ||
b039eaaf | 50 | #[test] |
970d7e83 | 51 | fn test_destroy_twice() { |
1a4d82fc | 52 | let mut p = sleeper(); |
9346a6ac AL |
53 | t!(p.kill()); // this shouldn't crash... |
54 | let _ = p.kill(); // ...and nor should this (and nor should the destructor) | |
970d7e83 LB |
55 | } |
56 | ||
9346a6ac AL |
57 | #[test] |
58 | fn test_destroy_actually_kills() { | |
b039eaaf SL |
59 | let cmd = if cfg!(windows) { |
60 | "cmd" | |
61 | } else if cfg!(target_os = "android") { | |
62 | "/system/bin/cat" | |
63 | } else { | |
64 | "cat" | |
65 | }; | |
970d7e83 LB |
66 | |
67 | // this process will stay alive indefinitely trying to read from stdin | |
b039eaaf SL |
68 | let mut p = t!(Command::new(cmd) |
69 | .stdin(Stdio::piped()) | |
70 | .spawn()); | |
970d7e83 | 71 | |
b039eaaf | 72 | t!(p.kill()); |
970d7e83 | 73 | |
1a4d82fc | 74 | // Don't let this test time out, this should be quick |
9346a6ac | 75 | let (tx, rx) = channel(); |
c34b1796 | 76 | thread::spawn(move|| { |
9346a6ac AL |
77 | thread::sleep_ms(1000); |
78 | if rx.try_recv().is_err() { | |
79 | process::exit(1); | |
1a4d82fc JJ |
80 | } |
81 | }); | |
b039eaaf | 82 | let code = t!(p.wait()).code(); |
9346a6ac AL |
83 | if cfg!(windows) { |
84 | assert!(code.is_some()); | |
85 | } else { | |
86 | assert!(code.is_none()); | |
1a4d82fc | 87 | } |
9346a6ac | 88 | tx.send(()); |
970d7e83 | 89 | } |