Skip to content

Commit e810df7

Browse files
committed
WaitForExitAsync: Handle timeout exception when timeout parameter set
1 parent abe6a1a commit e810df7

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/VirtualClient/VirtualClient.Common.UnitTests/ProcessProxyTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,5 +116,34 @@ public async Task ProcessProxyExitTimesAreNotAffectedByTheProcessHavingBeenDispo
116116
DateTime exitTime = process.ExitTime;
117117
Assert.IsTrue(exitTime != DateTime.MinValue);
118118
}
119+
120+
[Test]
121+
public async Task ProcessProxyWaitForExitAsyncHandlesTimeoutExceptionAsExpected()
122+
{
123+
IProcessProxy process = null;
124+
ProcessStartInfo startInfo = new ProcessStartInfo
125+
{
126+
FileName = "ping",
127+
Arguments = "localhost -n 2", // This will run for about 2 seconds
128+
CreateNoWindow = true,
129+
UseShellExecute = false,
130+
RedirectStandardError = true,
131+
RedirectStandardOutput = true
132+
};
133+
134+
using (process = new ProcessProxy(new Process { StartInfo = startInfo }))
135+
{
136+
// Test Case 1: When timeout is null, no TimeoutException should be thrown
137+
// The process will complete normally
138+
await process.StartAndWaitAsync(CancellationToken.None);
139+
Assert.IsTrue(process.HasExited);
140+
141+
// Test Case 2: When timeout is specified and process takes longer, TimeoutException should be caught
142+
process = new ProcessProxy(new Process { StartInfo = startInfo });
143+
await process.StartAndWaitAsync(CancellationToken.None, TimeSpan.FromMilliseconds(100));
144+
// If we get here, the TimeoutException was caught as expected
145+
Assert.IsTrue(true);
146+
}
147+
}
119148
}
120149
}

src/VirtualClient/VirtualClient.Common/ProcessProxy.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,10 @@ public virtual async Task WaitForExitAsync(CancellationToken cancellationToken,
365365
{
366366
// Expected whenever the CancellationToken receives a cancellation request.
367367
}
368+
catch (TimeoutException) when (timeout != null)
369+
{
370+
// Expected timeout when timeout was specified.
371+
}
368372
finally
369373
{
370374
this.exitTime = DateTime.UtcNow;

0 commit comments

Comments
 (0)