Skip to content

Commit bd979c1

Browse files
garethgareth
gareth
authored and
gareth
committed
Fix some issues with test_destroy_actually_kills:
- it is now cross platform, instead of just unix - it now avoids sleeping (fixing issue #6156) - it now calls force_destroy() when force = true (was a bug)
1 parent 23e97ae commit bd979c1

File tree

1 file changed

+38
-14
lines changed

1 file changed

+38
-14
lines changed

src/libcore/run.rs

+38-14
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,6 @@ mod tests {
781781
use libc;
782782
use option::None;
783783
use os;
784-
use path::Path;
785784
use run::{readclose, writeclose};
786785
use run;
787786

@@ -870,34 +869,59 @@ mod tests {
870869
p.destroy(); // ...and nor should this (and nor should the destructor)
871870
}
872871
873-
#[cfg(unix)] // there is no way to sleep on windows from inside libcore...
874872
fn test_destroy_actually_kills(force: bool) {
875-
let path = Path(fmt!("test/core-run-test-destroy-actually-kills-%?.tmp", force));
876873
877-
os::remove_file(&path);
874+
#[cfg(unix)]
875+
static BLOCK_COMMAND: &'static str = "cat";
878876
879-
let cmd = fmt!("sleep 5 && echo MurderDeathKill > %s", path.to_str());
880-
let mut p = run::start_program("sh", [~"-c", cmd]);
877+
#[cfg(windows)]
878+
static BLOCK_COMMAND: &'static str = "cmd";
881879
882-
p.destroy(); // destroy the program before it has a chance to echo its message
880+
#[cfg(unix)]
881+
fn process_exists(pid: libc::pid_t) -> bool {
882+
run::program_output("ps", [~"-p", pid.to_str()]).out.contains(pid.to_str())
883+
}
883884

884-
unsafe {
885-
// wait to ensure the program is really destroyed and not just waiting itself
886-
libc::sleep(10);
885+
#[cfg(windows)]
886+
fn process_exists(pid: libc::pid_t) -> bool {
887+
888+
use libc::types::os::arch::extra::DWORD;
889+
use libc::funcs::extra::kernel32::{CloseHandle, GetExitCodeProcess, OpenProcess};
890+
use libc::consts::os::extra::{FALSE, PROCESS_QUERY_INFORMATION, STILL_ACTIVE };
891+
892+
unsafe {
893+
let proc = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid as DWORD);
894+
if proc.is_null() {
895+
return false;
896+
}
897+
// proc will be non-null if the process is alive, or if it died recently
898+
let mut status = 0;
899+
GetExitCodeProcess(proc, &mut status);
900+
CloseHandle(proc);
901+
return status == STILL_ACTIVE;
902+
}
887903
}
888904

889-
// the program should not have had chance to echo its message
890-
assert!(!path.exists());
905+
// this program will stay alive indefinitely trying to read from stdin
906+
let mut p = run::start_program(BLOCK_COMMAND, []);
907+
908+
assert!(process_exists(p.get_id()));
909+
910+
if force {
911+
p.force_destroy();
912+
} else {
913+
p.destroy();
914+
}
915+
916+
assert!(!process_exists(p.get_id()));
891917
}
892918

893919
#[test]
894-
#[cfg(unix)]
895920
fn test_unforced_destroy_actually_kills() {
896921
test_destroy_actually_kills(false);
897922
}
898923

899924
#[test]
900-
#[cfg(unix)]
901925
fn test_forced_destroy_actually_kills() {
902926
test_destroy_actually_kills(true);
903927
}

0 commit comments

Comments
 (0)