]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/core-run-destroy.rs
Replace all ~"" with "".to_owned()
[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 // ignore-pretty
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
18 #![feature(macro_rules)]
19 extern crate libc;
20
21 extern crate native;
22 extern crate green;
23 extern crate rustuv;
24
25 use std::io::Process;
26
27 macro_rules! succeed( ($e:expr) => (
28     match $e { Ok(..) => {}, Err(e) => fail!("failure: {}", e) }
29 ) )
30
31 macro_rules! iotest (
32     { fn $name:ident() $b:block $($a:attr)* } => (
33         mod $name {
34             #![allow(unused_imports)]
35
36             use std::io::timer;
37             use libc;
38             use std::str;
39             use std::io::process::{Process, ProcessOutput};
40             use native;
41             use super::*;
42
43             fn f() $b
44
45             $($a)* #[test] fn green() { f() }
46             $($a)* #[test] fn native() {
47                 use native;
48                 let (tx, rx) = channel();
49                 native::task::spawn(proc() { tx.send(f()) });
50                 rx.recv();
51             }
52         }
53     )
54 )
55
56 #[cfg(test)] #[start]
57 fn start(argc: int, argv: **u8) -> int {
58     green::start(argc, argv, rustuv::event_loop, __test::main)
59 }
60
61 iotest!(fn test_destroy_once() {
62     let mut p = sleeper();
63     match p.signal_exit() {
64         Ok(()) => {}
65         Err(e) => fail!("error: {}", e),
66     }
67 })
68
69 #[cfg(unix)]
70 pub fn sleeper() -> Process {
71     Process::new("sleep", ["1000".to_owned()]).unwrap()
72 }
73 #[cfg(windows)]
74 pub fn sleeper() -> Process {
75     // There's a `timeout` command on windows, but it doesn't like having
76     // its output piped, so instead just ping ourselves a few times with
77     // gaps inbetweeen so we're sure this process is alive for awhile
78     Process::new("ping", ["127.0.0.1".to_owned(), "-n".to_owned(), "1000".to_owned()]).unwrap()
79 }
80
81 iotest!(fn test_destroy_twice() {
82     let mut p = sleeper();
83     succeed!(p.signal_exit()); // this shouldnt crash...
84     let _ = p.signal_exit(); // ...and nor should this (and nor should the destructor)
85 })
86
87 pub fn test_destroy_actually_kills(force: bool) {
88     use std::io::process::{Process, ProcessOutput, ExitStatus, ExitSignal};
89     use std::io::timer;
90     use libc;
91     use std::str;
92
93     #[cfg(unix,not(target_os="android"))]
94     static BLOCK_COMMAND: &'static str = "cat";
95
96     #[cfg(unix,target_os="android")]
97     static BLOCK_COMMAND: &'static str = "/system/bin/cat";
98
99     #[cfg(windows)]
100     static BLOCK_COMMAND: &'static str = "cmd";
101
102     // this process will stay alive indefinitely trying to read from stdin
103     let mut p = Process::new(BLOCK_COMMAND, []).unwrap();
104
105     assert!(p.signal(0).is_ok());
106
107     if force {
108         p.signal_kill().unwrap();
109     } else {
110         p.signal_exit().unwrap();
111     }
112
113     // Don't let this test time out, this should be quick
114     let (tx, rx1) = channel();
115     let mut t = timer::Timer::new().unwrap();
116     let rx2 = t.oneshot(1000);
117     spawn(proc() {
118         select! {
119             () = rx2.recv() => unsafe { libc::exit(1) },
120             () = rx1.recv() => {}
121         }
122     });
123     match p.wait() {
124         ExitStatus(..) => fail!("expected a signal"),
125         ExitSignal(..) => tx.send(()),
126     }
127 }
128
129 iotest!(fn test_unforced_destroy_actually_kills() {
130     test_destroy_actually_kills(false);
131 })
132
133 iotest!(fn test_forced_destroy_actually_kills() {
134     test_destroy_actually_kills(true);
135 })