Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit dba869e

Browse files
committed
Merge pull request #2638 from Priya91/process
Fix for Process.TestStartTime flakiness.
2 parents b5aa230 + ec93439 commit dba869e

File tree

1 file changed

+19
-4
lines changed
  • src/System.Diagnostics.Process/tests/System.Diagnostics.Process.Tests

1 file changed

+19
-4
lines changed

src/System.Diagnostics.Process/tests/System.Diagnostics.Process.Tests/ProcessTests.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,13 @@ public void TestExitTime()
133133
{
134134
DateTime timeBeforeProcessStart = DateTime.UtcNow;
135135
Process p = CreateProcessInfinite();
136-
StartAndKillProcessWithDelay(p);
136+
p.Start();
137+
Assert.Throws<InvalidOperationException>(() => p.ExitTime);
138+
p.Kill();
139+
Assert.True(p.WaitForExit(WaitInMS));
137140
Assert.True(p.ExitTime.ToUniversalTime() > timeBeforeProcessStart, "TestExitTime is incorrect.");
138141
}
139142

140-
141143
[Fact]
142144
public void TestId()
143145
{
@@ -366,6 +368,8 @@ public void TestProcessStartTime()
366368
{
367369
DateTime timeBeforeCreatingProcess = DateTime.UtcNow;
368370
Process p = CreateProcessInfinite();
371+
372+
Assert.Throws<InvalidOperationException>(() => p.StartTime);
369373
try
370374
{
371375
p.Start();
@@ -376,8 +380,19 @@ public void TestProcessStartTime()
376380
// Allowing for error in 10 ms.
377381
long tenMSTicks = new TimeSpan(0, 0, 0, 0, 10).Ticks;
378382
long beforeTicks = timeBeforeCreatingProcess.Ticks - tenMSTicks;
379-
long afterTicks = DateTime.UtcNow.Ticks + tenMSTicks;
380-
Assert.InRange(p.StartTime.ToUniversalTime().Ticks, beforeTicks, afterTicks);
383+
384+
try
385+
{
386+
// Ensure the process has started, p.id throws InvalidOperationException, if the process has not yet started.
387+
Assert.Equal(p.Id, Process.GetProcessById(p.Id).Id);
388+
389+
long afterTicks = DateTime.UtcNow.Ticks + tenMSTicks;
390+
Assert.InRange(p.StartTime.ToUniversalTime().Ticks, beforeTicks, afterTicks);
391+
}
392+
catch (InvalidOperationException)
393+
{
394+
Assert.True(p.StartTime.ToUniversalTime().Ticks > beforeTicks);
395+
}
381396
}
382397
finally
383398
{

0 commit comments

Comments
 (0)