Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,13 @@ public static class StopwatchTests
{
private static readonly ManualResetEvent s_sleepEvent = new ManualResetEvent(false);

private static readonly int s_defaultSleepTimeMs = PlatformDetection.IsBrowser ? 5 : 1;

[Fact]
public static void GetTimestamp()
{
long ts1 = Stopwatch.GetTimestamp();
if (PlatformDetection.IsBrowser)
{
// workaround for issue: https://github.com/dotnet/runtime/issues/62021
Sleep(5);
}
else
{
Sleep();
}
Sleep(s_defaultSleepTimeMs);
long ts2 = Stopwatch.GetTimestamp();
Assert.NotEqual(ts1, ts2);
}
Expand All @@ -38,20 +32,20 @@ public static void ConstructStartAndStop()
Assert.Equal(0, watch.ElapsedMilliseconds);
watch.Start();
Assert.True(watch.IsRunning);
Sleep();
Sleep(s_defaultSleepTimeMs);
Assert.True(watch.Elapsed > TimeSpan.Zero);

watch.Stop();
Assert.False(watch.IsRunning);

var e1 = watch.Elapsed;
Sleep();
Sleep(s_defaultSleepTimeMs);
var e2 = watch.Elapsed;
Assert.Equal(e1, e2);
Assert.Equal((long)e1.TotalMilliseconds, watch.ElapsedMilliseconds);

var t1 = watch.ElapsedTicks;
Sleep();
Sleep(s_defaultSleepTimeMs);
var t2 = watch.ElapsedTicks;
Assert.Equal(t1, t2);
}
Expand All @@ -63,7 +57,7 @@ public static void StartNewAndReset()
Assert.True(watch.IsRunning);
watch.Start(); // should be no-op
Assert.True(watch.IsRunning);
Sleep();
Sleep(s_defaultSleepTimeMs);
Assert.True(watch.Elapsed > TimeSpan.Zero);

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

private static void Sleep(int milliseconds = 1)
private static void Sleep(int milliseconds)
{
s_sleepEvent.WaitOne(milliseconds);
}
Expand Down