Skip to content

Commit 47b9cb5

Browse files
authored
[wasm] Work around the browser having a lower timer resolution than the (#62411)
.. desktop. Earlier we did this for one test, but others are hitting this issue randomly too. New one: ``` [00:32:41] fail: [FAIL] System.Diagnostics.Tests.StopwatchTests.StartNewAndReset [00:32:41] info: Assert.True() Failure [00:32:41] info: Expected: True [00:32:41] info: Actual: False [00:32:41] info: at System.Diagnostics.Tests.StopwatchTests.StartNewAndReset() [00:32:41] info: at System.Reflection.RuntimeMethodInfo.InvokeWorker(Object obj, BindingFlags invokeAttr, Span`1 parameters) ``` Issue: #62021
1 parent a9947a1 commit 47b9cb5

File tree

1 file changed

+8
-14
lines changed
  • src/libraries/System.Runtime.Extensions/tests/System/Diagnostics

1 file changed

+8
-14
lines changed

src/libraries/System.Runtime.Extensions/tests/System/Diagnostics/Stopwatch.cs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,13 @@ public static class StopwatchTests
1111
{
1212
private static readonly ManualResetEvent s_sleepEvent = new ManualResetEvent(false);
1313

14+
private static readonly int s_defaultSleepTimeMs = PlatformDetection.IsBrowser ? 5 : 1;
15+
1416
[Fact]
1517
public static void GetTimestamp()
1618
{
1719
long ts1 = Stopwatch.GetTimestamp();
18-
if (PlatformDetection.IsBrowser)
19-
{
20-
// workaround for issue: https://github.com/dotnet/runtime/issues/62021
21-
Sleep(5);
22-
}
23-
else
24-
{
25-
Sleep();
26-
}
20+
Sleep(s_defaultSleepTimeMs);
2721
long ts2 = Stopwatch.GetTimestamp();
2822
Assert.NotEqual(ts1, ts2);
2923
}
@@ -38,20 +32,20 @@ public static void ConstructStartAndStop()
3832
Assert.Equal(0, watch.ElapsedMilliseconds);
3933
watch.Start();
4034
Assert.True(watch.IsRunning);
41-
Sleep();
35+
Sleep(s_defaultSleepTimeMs);
4236
Assert.True(watch.Elapsed > TimeSpan.Zero);
4337

4438
watch.Stop();
4539
Assert.False(watch.IsRunning);
4640

4741
var e1 = watch.Elapsed;
48-
Sleep();
42+
Sleep(s_defaultSleepTimeMs);
4943
var e2 = watch.Elapsed;
5044
Assert.Equal(e1, e2);
5145
Assert.Equal((long)e1.TotalMilliseconds, watch.ElapsedMilliseconds);
5246

5347
var t1 = watch.ElapsedTicks;
54-
Sleep();
48+
Sleep(s_defaultSleepTimeMs);
5549
var t2 = watch.ElapsedTicks;
5650
Assert.Equal(t1, t2);
5751
}
@@ -63,7 +57,7 @@ public static void StartNewAndReset()
6357
Assert.True(watch.IsRunning);
6458
watch.Start(); // should be no-op
6559
Assert.True(watch.IsRunning);
66-
Sleep();
60+
Sleep(s_defaultSleepTimeMs);
6761
Assert.True(watch.Elapsed > TimeSpan.Zero);
6862

6963
watch.Reset();
@@ -130,7 +124,7 @@ public static void ElapsedMilliseconds_WithinExpectedWindow()
130124
Assert.True(false, $"All {AllowedTries} fell outside of {WindowFactor} window of {SleepTime} sleep time: {string.Join(", ", results)}");
131125
}
132126

133-
private static void Sleep(int milliseconds = 1)
127+
private static void Sleep(int milliseconds)
134128
{
135129
s_sleepEvent.WaitOne(milliseconds);
136130
}

0 commit comments

Comments
 (0)