]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/core-run-destroy.rs
run EndRegion when unwinding otherwise-empty scopes
[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 // compile-flags:--test
12 // ignore-emscripten
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
18 #![feature(libc, std_misc, duration)]
19
20 extern crate libc;
21
22 use std::process::{self, Command, Child, Output, Stdio};
23 use std::str;
24 use std::sync::mpsc::channel;
25 use std::thread;
26 use std::time::Duration;
27
28 macro_rules! t {
29     ($e:expr) => (match $e { Ok(e) => e, Err(e) => panic!("error: {}", e) })
30 }
31
32 #[test]
33 fn test_destroy_once() {
34     let mut p = sleeper();
35     t!(p.kill());
36 }
37
38 #[cfg(unix)]
39 pub fn sleeper() -> Child {
40     t!(Command::new("sleep").arg("1000").spawn())
41 }
42 #[cfg(windows)]
43 pub fn sleeper() -> Child {
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
47     t!(Command::new("ping").arg("127.0.0.1").arg("-n").arg("1000").spawn())
48 }
49
50 #[test]
51 fn test_destroy_twice() {
52     let mut p = sleeper();
53     t!(p.kill()); // this shouldn't crash...
54     let _ = p.kill(); // ...and nor should this (and nor should the destructor)
55 }
56
57 #[test]
58 fn test_destroy_actually_kills() {
59     let cmd = if cfg!(windows) {
60         "cmd"
61     } else if cfg!(target_os = "android") {
62         "/system/bin/cat"
63     } else {
64         "cat"
65     };
66
67     // this process will stay alive indefinitely trying to read from stdin
68     let mut p = t!(Command::new(cmd)
69                            .stdin(Stdio::piped())
70                            .spawn());
71
72     t!(p.kill());
73
74     // Don't let this test time out, this should be quick
75     let (tx, rx) = channel();
76     thread::spawn(move|| {
77         thread::sleep_ms(1000);
78         if rx.try_recv().is_err() {
79             process::exit(1);
80         }
81     });
82     let code = t!(p.wait()).code();
83     if cfg!(windows) {
84         assert!(code.is_some());
85     } else {
86         assert!(code.is_none());
87     }
88     tx.send(());
89 }