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