|
| 1 | +// Copyright 2012-2013 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 | +// xfail-fast |
| 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 core. |
| 17 | + |
| 18 | +use core::run; |
| 19 | +use core::run::*; |
| 20 | + |
| 21 | +#[test] |
| 22 | +fn test_destroy_once() { |
| 23 | + let mut p = run::start_program("echo", []); |
| 24 | + p.destroy(); // this shouldn't crash (and nor should the destructor) |
| 25 | +} |
| 26 | + |
| 27 | +#[test] |
| 28 | +fn test_destroy_twice() { |
| 29 | + let mut p = run::start_program("echo", []); |
| 30 | + p.destroy(); // this shouldnt crash... |
| 31 | + p.destroy(); // ...and nor should this (and nor should the destructor) |
| 32 | +} |
| 33 | + |
| 34 | +fn test_destroy_actually_kills(force: bool) { |
| 35 | + |
| 36 | + #[cfg(unix)] |
| 37 | + static BLOCK_COMMAND: &'static str = "cat"; |
| 38 | + |
| 39 | + #[cfg(windows)] |
| 40 | + static BLOCK_COMMAND: &'static str = "cmd"; |
| 41 | + |
| 42 | + #[cfg(unix)] |
| 43 | + fn process_exists(pid: libc::pid_t) -> bool { |
| 44 | + run::program_output("ps", [~"-p", pid.to_str()]).out.contains(pid.to_str()) |
| 45 | + } |
| 46 | + |
| 47 | + #[cfg(windows)] |
| 48 | + fn process_exists(pid: libc::pid_t) -> bool { |
| 49 | + |
| 50 | + use core::libc::types::os::arch::extra::DWORD; |
| 51 | + use core::libc::funcs::extra::kernel32::{CloseHandle, GetExitCodeProcess, OpenProcess}; |
| 52 | + use core::libc::consts::os::extra::{FALSE, PROCESS_QUERY_INFORMATION, STILL_ACTIVE }; |
| 53 | + |
| 54 | + unsafe { |
| 55 | + let proc = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid as DWORD); |
| 56 | + if proc.is_null() { |
| 57 | + return false; |
| 58 | + } |
| 59 | + // proc will be non-null if the process is alive, or if it died recently |
| 60 | + let mut status = 0; |
| 61 | + GetExitCodeProcess(proc, &mut status); |
| 62 | + CloseHandle(proc); |
| 63 | + return status == STILL_ACTIVE; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + // this program will stay alive indefinitely trying to read from stdin |
| 68 | + let mut p = run::start_program(BLOCK_COMMAND, []); |
| 69 | + |
| 70 | + assert!(process_exists(p.get_id())); |
| 71 | + |
| 72 | + if force { |
| 73 | + p.force_destroy(); |
| 74 | + } else { |
| 75 | + p.destroy(); |
| 76 | + } |
| 77 | + |
| 78 | + assert!(!process_exists(p.get_id())); |
| 79 | +} |
| 80 | + |
| 81 | +#[test] |
| 82 | +fn test_unforced_destroy_actually_kills() { |
| 83 | + test_destroy_actually_kills(false); |
| 84 | +} |
| 85 | + |
| 86 | +#[test] |
| 87 | +fn test_forced_destroy_actually_kills() { |
| 88 | + test_destroy_actually_kills(true); |
| 89 | +} |
0 commit comments