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

Commit 1ed5cf1

Browse files
alexperovichdanmoseley
authored andcommitted
Use a monotonic clock for test timing (#10812)
Fixes #8348
1 parent 4d6398e commit 1ed5cf1

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

tests/src/CoreMangLib/cti/system/threading/autoresetevent/autoreseteventctor.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
4+
45
using System;
6+
using System.Diagnostics;
57
using System.Threading;
68

79
public class AutoResetEventCtor
@@ -70,8 +72,8 @@ public bool PosTest2()
7072
{
7173
bool retVal = true;
7274
AutoResetEvent are;
73-
long ticksBefore;
74-
long ticksAfter;
75+
Stopwatch sw;
76+
long elapsedTicks;
7577

7678
TestLibrary.TestFramework.BeginScenario("PosTest1: AutoResetEvent.Ctor(false)");
7779

@@ -80,19 +82,20 @@ public bool PosTest2()
8082
// true means that the initial state should be signaled
8183
are = new AutoResetEvent(false);
8284

83-
ticksBefore = DateTime.Now.Ticks;
85+
sw = Stopwatch.StartNew();
8486

8587
// verify that the autoreset event is signaled
8688
// if it is not signaled the following call will block for ever
8789
TestLibrary.TestFramework.LogInformation("Calling AutoResetEvent.WaitOne()... if the event is signaled it will not wait long enough");
8890
are.WaitOne(c_MILLISECONDS_TOWAIT);
8991

90-
ticksAfter = DateTime.Now.Ticks;
92+
sw.Stop();
93+
elapsedTicks = sw.Elapsed.Ticks;
9194

92-
if (c_DELTA < Math.Abs((ticksAfter - ticksBefore) - (c_MILLISECONDS_TOWAIT*10000)))
95+
if (c_DELTA < Math.Abs(elapsedTicks - (c_MILLISECONDS_TOWAIT*10000)))
9396
{
9497
TestLibrary.TestFramework.LogError("002", "AutoResetEvent did not wait long enough... this implies that the parameter was not respected.");
95-
TestLibrary.TestFramework.LogError("002", " WaitTime=" + (ticksAfter-ticksBefore) + " (ticks)");
98+
TestLibrary.TestFramework.LogError("002", " WaitTime=" + elapsedTicks + " (ticks)");
9699
TestLibrary.TestFramework.LogError("002", " Execpted=" + (c_MILLISECONDS_TOWAIT*10000) + " (ticks)");
97100
TestLibrary.TestFramework.LogError("002", " Acceptable Delta=" + c_DELTA + " (ticks)");
98101
retVal = false;

0 commit comments

Comments
 (0)