@@ -46,38 +46,49 @@ public async Task RunAsync_CancellationCleanupIsBounded_WhenProcessTreeKillDoesN
4646 return ;
4747
4848 var launcherPid = 0 ;
49+ var killAttempted = false ;
50+ Process ? launcher = null ;
4951 var cmdPath = Path . Combine (
5052 Environment . GetFolderPath ( Environment . SpecialFolder . System ) ,
5153 "cmd.exe" ) ;
5254 var executor = new MxcExecutor (
5355 cmdPath ,
5456 stdoutCapBytes : null ,
5557 stderrCapBytes : null ,
56- processFactory : _ => CreateProcess (
58+ processFactory : _ => launcher = CreateProcess (
5759 cmdPath ,
5860 "echo launcher-started & ping -n 31 127.0.0.1 >nul" ) ,
59- processTreeKiller : process => launcherPid = process . Id ,
61+ processTreeKiller : process =>
62+ {
63+ killAttempted = true ;
64+ launcherPid = process . Id ;
65+ } ,
6066 cleanupTimeout : TimeSpan . FromMilliseconds ( 100 ) ) ;
61- using var cancellation = new CancellationTokenSource ( TimeSpan . FromMilliseconds ( 100 ) ) ;
62- var stopwatch = Stopwatch . StartNew ( ) ;
67+ using var cancellation = new CancellationTokenSource ( ) ;
6368
6469 try
6570 {
66- var result = await executor . RunAsync (
71+ var run = executor . RunAsync (
6772 new MxcConfig
6873 {
6974 ContainerId = "bounded-cleanup-test" ,
7075 Process = new MxcProcess { CommandLine = "ignored-by-test-process" } ,
7176 } ,
72- cancellation . Token ) . WaitAsync ( TimeSpan . FromSeconds ( 2 ) ) ;
73-
77+ cancellation . Token ) ;
78+ await WaitForProcessStartAsync ( ( ) => launcher , TimeSpan . FromSeconds ( 5 ) ) ;
79+ launcherPid = launcher ! . Id ;
80+ var stopwatch = Stopwatch . StartNew ( ) ;
81+ cancellation . Cancel ( ) ;
82+ var result = await run . WaitAsync ( TimeSpan . FromSeconds ( 5 ) ) ;
7483 stopwatch . Stop ( ) ;
84+
85+ Assert . True ( killAttempted ) ;
7586 Assert . True ( result . TimedOut ) ;
7687 Assert . Equal ( - 1 , result . ExitCode ) ;
7788 Assert . Contains ( "cancelled" , result . Error , StringComparison . OrdinalIgnoreCase ) ;
7889 Assert . True (
79- stopwatch . Elapsed < TimeSpan . FromSeconds ( 1 ) ,
80- $ "Cancellation cleanup took { stopwatch . ElapsedMilliseconds } ms.") ;
90+ stopwatch . Elapsed < TimeSpan . FromSeconds ( 2 ) ,
91+ $ "Post-cancel cleanup took { stopwatch . ElapsedMilliseconds } ms.") ;
8192 }
8293 finally
8394 {
@@ -99,6 +110,29 @@ public async Task WaitForCleanupAsync_ReturnsTrue_WhenProcessAndPipesComplete()
99110 Assert . True ( result ) ;
100111 }
101112
113+ private static async Task WaitForProcessStartAsync (
114+ Func < Process ? > processProvider ,
115+ TimeSpan timeout )
116+ {
117+ var deadline = DateTime . UtcNow + timeout ;
118+ while ( DateTime . UtcNow < deadline )
119+ {
120+ try
121+ {
122+ if ( processProvider ( ) ? . Id > 0 )
123+ return ;
124+ }
125+ catch ( InvalidOperationException )
126+ {
127+ // Process.Start has not completed yet.
128+ }
129+
130+ await Task . Delay ( 10 ) ;
131+ }
132+
133+ throw new TimeoutException ( "Test launcher did not start within the expected time." ) ;
134+ }
135+
102136 private static Process CreateProcess ( string cmdPath , string command )
103137 {
104138 var startInfo = new ProcessStartInfo ( cmdPath )
0 commit comments