@@ -24,12 +24,84 @@ public async Task AwaitRedirectedOutput_ReturnsNullWhenStdoutNeverCloses()
2424 } ) ;
2525 Assert . NotNull ( process ) ;
2626
27- var never = new TaskCompletionSource < string > ( ) . Task ;
27+ var never = new TaskCompletionSource < string > (
28+ TaskCreationOptions . RunContinuationsAsynchronously ) . Task ;
2829 var helper = Task . Run ( ( ) =>
2930 WindowsTcpListenerSnapshot . AwaitRedirectedOutput ( process , never , timeoutMs : 400 ) ) ;
3031
3132 var completed = await Task . WhenAny ( helper , Task . Delay ( TimeSpan . FromSeconds ( 3 ) ) ) ;
3233 Assert . Same ( helper , completed ) ;
3334 Assert . Null ( await helper ) ;
3435 }
36+
37+ [ Fact ]
38+ public void AwaitRedirectedOutput_PreservesOutputCompletedDuringDrainGrace ( )
39+ {
40+ using var process = StartExitingProcess ( ) ;
41+ Assert . True ( process . WaitForExit ( 3_000 ) ) ;
42+ var outputTask = Task . Run ( async ( ) =>
43+ {
44+ await Task . Delay ( 100 ) ;
45+ return "complete output" ;
46+ } ) ;
47+
48+ var output = WindowsTcpListenerSnapshot . AwaitRedirectedOutput (
49+ process ,
50+ outputTask ,
51+ timeoutMs : 400 ) ;
52+
53+ Assert . Equal ( "complete output" , output ) ;
54+ }
55+
56+ [ Fact ]
57+ public async Task AwaitRedirectedOutput_ReturnsNullWhenDescendantKeepsStdoutOpen ( )
58+ {
59+ var ( fileName , arguments ) = DescendantPipeHolderCommand ( ) ;
60+ using var process = Process . Start ( new ProcessStartInfo
61+ {
62+ FileName = fileName ,
63+ Arguments = arguments ,
64+ RedirectStandardOutput = true ,
65+ UseShellExecute = false ,
66+ CreateNoWindow = true ,
67+ } ) ;
68+ Assert . NotNull ( process ) ;
69+
70+ var readTask = process . StandardOutput . ReadToEndAsync ( ) ;
71+ var stopwatch = Stopwatch . StartNew ( ) ;
72+ var output = WindowsTcpListenerSnapshot . AwaitRedirectedOutput (
73+ process ,
74+ readTask ,
75+ timeoutMs : 400 ) ;
76+
77+ Assert . Null ( output ) ;
78+ Assert . InRange ( stopwatch . Elapsed , TimeSpan . Zero , TimeSpan . FromSeconds ( 2 ) ) ;
79+ await readTask . WaitAsync ( TimeSpan . FromSeconds ( 5 ) ) ;
80+ }
81+
82+ [ Fact ]
83+ public void GetProcessCommandLine_CurrentProcess_IsBoundedOnWindows ( )
84+ {
85+ if ( ! OperatingSystem . IsWindows ( ) )
86+ return ;
87+
88+ var stopwatch = Stopwatch . StartNew ( ) ;
89+ _ = WindowsTcpListenerSnapshot . GetProcessCommandLine ( Environment . ProcessId ) ;
90+
91+ Assert . InRange ( stopwatch . Elapsed , TimeSpan . Zero , TimeSpan . FromSeconds ( 7 ) ) ;
92+ }
93+
94+ private static Process StartExitingProcess ( ) =>
95+ Process . Start ( new ProcessStartInfo
96+ {
97+ FileName = OperatingSystem . IsWindows ( ) ? "cmd.exe" : "/bin/sh" ,
98+ Arguments = OperatingSystem . IsWindows ( ) ? "/d /c exit 0" : "-c \" exit 0\" " ,
99+ UseShellExecute = false ,
100+ CreateNoWindow = true ,
101+ } ) ! ;
102+
103+ private static ( string FileName , string Arguments ) DescendantPipeHolderCommand ( ) =>
104+ OperatingSystem . IsWindows ( )
105+ ? ( "cmd.exe" , "/d /s /c \" start /b ping 127.0.0.1 -n 3\" " )
106+ : ( "/bin/sh" , "-c \" sleep 2 & exit 0\" " ) ;
35107}
0 commit comments