@@ -51,3 +51,71 @@ impl Deref for SafeHandle {
5151 & self . 0
5252 }
5353}
54+
55+ #[ cfg( test) ]
56+ #[ cfg( windows) ]
57+ mod tests {
58+ use std:: process:: Command ;
59+ use std:: time:: Duration ;
60+
61+ use super :: * ;
62+
63+ // Helper to create a long-running process for testing
64+ fn spawn_test_process ( ) -> std:: process:: Child {
65+ let mut command = Command :: new ( "cmd" ) ;
66+ command. args ( [ "/C" , "ping 127.0.0.1 -n 30 > nul" ] ) ;
67+ command. spawn ( ) . expect ( "Failed to spawn test process" )
68+ }
69+
70+ #[ test]
71+ fn test_terminate_process ( ) {
72+ // Spawn a test process
73+ let mut child = spawn_test_process ( ) ;
74+ let pid = Pid :: from_u32 ( child. id ( ) ) ;
75+
76+ // Terminate the process
77+ let result = terminate_process ( pid) ;
78+
79+ // Verify termination was successful
80+ assert ! ( result. is_ok( ) , "Process termination failed: {:?}" , result. err( ) ) ;
81+
82+ // Give it a moment to terminate
83+ std:: thread:: sleep ( Duration :: from_millis ( 100 ) ) ;
84+
85+ // Verify the process is actually terminated
86+ match child. try_wait ( ) {
87+ Ok ( Some ( _) ) => {
88+ // Process exited, which is what we expect
89+ } ,
90+ Ok ( None ) => {
91+ panic ! ( "Process is still running after termination" ) ;
92+ } ,
93+ Err ( e) => {
94+ panic ! ( "Error checking process status: {}" , e) ;
95+ } ,
96+ }
97+ }
98+
99+ #[ test]
100+ fn test_terminate_nonexistent_process ( ) {
101+ // Use a likely invalid PID
102+ let invalid_pid = Pid :: from_u32 ( u32:: MAX - 1 ) ;
103+
104+ // Attempt to terminate a non-existent process
105+ let result = terminate_process ( invalid_pid) ;
106+
107+ // Should return an error
108+ assert ! ( result. is_err( ) , "Terminating non-existent process should fail" ) ;
109+ }
110+
111+ #[ test]
112+ fn test_safe_handle ( ) {
113+ // Test creating a SafeHandle with an invalid handle
114+ let invalid_handle = HANDLE ( 0 ) ;
115+ let safe_handle = SafeHandle :: new ( invalid_handle) ;
116+ assert ! ( safe_handle. is_none( ) , "SafeHandle should be None for invalid handle" ) ;
117+
118+ // We can't easily test a valid handle without actually opening a process,
119+ // which would require additional setup and teardown
120+ }
121+ }
0 commit comments