@@ -781,7 +781,6 @@ mod tests {
781
781
use libc;
782
782
use option:: None ;
783
783
use os;
784
- use path:: Path ;
785
784
use run:: { readclose, writeclose} ;
786
785
use run;
787
786
@@ -870,34 +869,59 @@ mod tests {
870
869
p.destroy(); // ...and nor should this (and nor should the destructor)
871
870
}
872
871
873
- #[cfg(unix)] // there is no way to sleep on windows from inside libcore...
874
872
fn test_destroy_actually_kills(force: bool) {
875
- let path = Path(fmt!(" test/core-run-test-destroy-actually-kills-%?. tmp", force));
876
873
877
- os::remove_file(&path);
874
+ #[cfg(unix)]
875
+ static BLOCK_COMMAND: &'static str = " cat";
878
876
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" ;
881
879
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
+ }
883
884
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
+ }
887
903
}
888
904
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( ) ) ) ;
891
917
}
892
918
893
919
#[ test]
894
- #[ cfg( unix) ]
895
920
fn test_unforced_destroy_actually_kills( ) {
896
921
test_destroy_actually_kills( false ) ;
897
922
}
898
923
899
924
#[ test]
900
- #[ cfg( unix) ]
901
925
fn test_forced_destroy_actually_kills( ) {
902
926
test_destroy_actually_kills( true ) ;
903
927
}
0 commit comments