@@ -16,8 +16,8 @@ public class ProcessWrapperTests
1616 {
1717 /// <summary>
1818 /// Verifies that Kill() removes the Process from the tracked dictionary
19- /// so that it doesn't leak. After Kill, WaitForProcessExit should fall
20- /// back to GetProcessById (which will throw because the process is gone) .
19+ /// so that it doesn't leak. After Kill, WaitForProcessExit should throw
20+ /// InvalidOperationException because the process is no longer tracked .
2121 /// </summary>
2222 [ TestMethod ]
2323 [ TestCategory ( "BVT" ) ]
@@ -26,17 +26,11 @@ public void Kill_RemovesProcessFromTrackedDictionary()
2626 var wrapper = new ProcessWrapper ( ) ;
2727 int pid = wrapper . Start ( GetSleepProcessStartInfo ( ) ) ;
2828
29- // Kill should remove from _trackedProcesses and dispose
3029 wrapper . Kill ( pid ) ;
3130
32- // WaitForProcessExit should now fall back to GetProcessById.
33- // This throws ArgumentException if the process no longer exists,
34- // or InvalidOperationException if the OS still has the PID but
35- // the Process object wasn't the one that started it.
31+ // WaitForProcessExit should throw because the process was removed from tracking by Kill
3632 Action act = ( ) => wrapper . WaitForProcessExit ( pid ) ;
37- act . Should ( ) . Throw < Exception > ( )
38- . Which . Should ( ) . Match < Exception > ( e =>
39- e is ArgumentException || e is InvalidOperationException ) ;
33+ act . Should ( ) . Throw < InvalidOperationException > ( ) ;
4034
4135 wrapper . Dispose ( ) ;
4236 }
@@ -60,9 +54,7 @@ public void WaitForProcessExit_ReturnsExitCodeAndCleansUp()
6054
6155 // Calling again should throw since it was removed from tracking
6256 Action act = ( ) => wrapper . WaitForProcessExit ( pid ) ;
63- act . Should ( ) . Throw < Exception > ( )
64- . Which . Should ( ) . Match < Exception > ( e =>
65- e is ArgumentException || e is InvalidOperationException ) ;
57+ act . Should ( ) . Throw < InvalidOperationException > ( ) ;
6658
6759 wrapper . Dispose ( ) ;
6860 }
@@ -123,7 +115,9 @@ public void Dispose_CleansUpRemainingTrackedProcesses()
123115 wrapper . Dispose ( ) ;
124116
125117 // Clean up the actual OS process
126- try { Process . GetProcessById ( pid ) . Kill ( true ) ; } catch { }
118+ try { Process . GetProcessById ( pid ) . Kill ( true ) ; }
119+ catch ( ArgumentException ) { /* process already exited */ }
120+ catch ( InvalidOperationException ) { /* process already exited */ }
127121 }
128122
129123 /// <summary>
@@ -145,61 +139,43 @@ public void StartWithEventHandler_KillCleansUpTrackedProcess()
145139
146140 // Process should be removed from tracking
147141 Action act = ( ) => wrapper . WaitForProcessExit ( pid ) ;
148- act . Should ( ) . Throw < Exception > ( )
149- . Which . Should ( ) . Match < Exception > ( e =>
150- e is ArgumentException || e is InvalidOperationException ) ;
142+ act . Should ( ) . Throw < InvalidOperationException > ( ) ;
151143
152144 wrapper . Dispose ( ) ;
153145 }
154146
155- private static ProcessStartInfo GetSleepProcessStartInfo ( )
156- {
157- // Cross-platform sleep: use dotnet to run a trivial inline program
158- // that sleeps, or just use a long-running process
159- if ( OperatingSystem . IsWindows ( ) )
160- {
161- return new ProcessStartInfo
147+ private static ProcessStartInfo GetSleepProcessStartInfo ( ) =>
148+ OperatingSystem . IsWindows ( )
149+ ? new ProcessStartInfo
162150 {
163151 FileName = "cmd.exe" ,
164152 Arguments = "/c timeout /t 30 /nobreak >nul" ,
165153 UseShellExecute = false ,
166154 CreateNoWindow = true
167- } ;
168- }
169- else
170- {
171- return new ProcessStartInfo
155+ }
156+ : new ProcessStartInfo
172157 {
173158 FileName = "sleep" ,
174159 Arguments = "30" ,
175160 UseShellExecute = false ,
176161 CreateNoWindow = true
177162 } ;
178- }
179- }
180163
181- private static ProcessStartInfo GetExitProcessStartInfo ( int exitCode )
182- {
183- if ( OperatingSystem . IsWindows ( ) )
184- {
185- return new ProcessStartInfo
164+ private static ProcessStartInfo GetExitProcessStartInfo ( int exitCode ) =>
165+ OperatingSystem . IsWindows ( )
166+ ? new ProcessStartInfo
186167 {
187168 FileName = "cmd.exe" ,
188169 Arguments = $ "/c exit { exitCode } ",
189170 UseShellExecute = false ,
190171 CreateNoWindow = true
191- } ;
192- }
193- else
194- {
195- return new ProcessStartInfo
172+ }
173+ : new ProcessStartInfo
196174 {
197175 FileName = "/bin/sh" ,
198176 Arguments = $ "-c \" exit { exitCode } \" ",
199177 UseShellExecute = false ,
200178 CreateNoWindow = true
201179 } ;
202- }
203- }
204180 }
205181}
0 commit comments