From 758ce3cbe695bd65a41fd5d625d0d0e0408ba356 Mon Sep 17 00:00:00 2001 From: Egil Hansen Date: Tue, 30 May 2023 16:16:55 +0000 Subject: [PATCH] Refactor tests for improved readability --- .../FakeTimeProviderTests.cs | 22 +++++++------------ .../FakeTimeProviderTimerTests.cs | 2 ++ 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/test/Libraries/Microsoft.Extensions.TimeProvider.Testing.Tests/FakeTimeProviderTests.cs b/test/Libraries/Microsoft.Extensions.TimeProvider.Testing.Tests/FakeTimeProviderTests.cs index cca94a3e5b1..0fd6d01af13 100644 --- a/test/Libraries/Microsoft.Extensions.TimeProvider.Testing.Tests/FakeTimeProviderTests.cs +++ b/test/Libraries/Microsoft.Extensions.TimeProvider.Testing.Tests/FakeTimeProviderTests.cs @@ -32,17 +32,11 @@ public void DefaultCtor() var timestamp2 = timeProvider.GetTimestamp(); var frequency2 = timeProvider.TimestampFrequency; - now = timeProvider.GetUtcNow(); + var now2 = timeProvider.GetUtcNow(); - Assert.Equal(2000, now.Year); - Assert.Equal(1, now.Month); - Assert.Equal(1, now.Day); - Assert.Equal(0, now.Hour); - Assert.Equal(0, now.Minute); - Assert.Equal(0, now.Second); - Assert.Equal(0, now.Millisecond); - Assert.Equal(10_000_000, frequency2); - Assert.Equal(timestamp2, timestamp); + Assert.Equal(now, now2); + Assert.Equal(frequency, frequency2); + Assert.Equal(timestamp, timestamp2); } [Fact] @@ -77,7 +71,7 @@ public void RichCtor() Assert.Equal(5, now.Minute); Assert.Equal(6, now.Second); Assert.Equal(16, now.Millisecond); - Assert.Equal(10_000_000, frequency2); + Assert.Equal(frequency, frequency2); Assert.True(pnow2 > pnow); } @@ -173,7 +167,7 @@ public async Task Delay_Timeout() var timeProvider = new FakeTimeProvider(); var delay = timeProvider.Delay(TimeSpan.FromMilliseconds(1), CancellationToken.None); - timeProvider.Advance(); + timeProvider.Advance(TimeSpan.FromMilliseconds(1)); await delay; Assert.True(delay.IsCompleted); @@ -203,7 +197,7 @@ public async Task CreateSource() var timeProvider = new FakeTimeProvider(); using var cts = timeProvider.CreateCancellationTokenSource(TimeSpan.FromMilliseconds(1)); - timeProvider.Advance(); + timeProvider.Advance(TimeSpan.FromMilliseconds(1)); await Assert.ThrowsAsync(() => timeProvider.Delay(TimeSpan.FromTicks(1), cts.Token)); } @@ -224,7 +218,7 @@ public async Task WaitAsync() var t = source.Task.WaitAsync(TimeSpan.FromSeconds(100000), timeProvider, CancellationToken.None); while (!t.IsCompleted) { - timeProvider.Advance(); + timeProvider.Advance(TimeSpan.FromMilliseconds(1)); await Task.Delay(1); _ = source.TrySetResult(true); } diff --git a/test/Libraries/Microsoft.Extensions.TimeProvider.Testing.Tests/FakeTimeProviderTimerTests.cs b/test/Libraries/Microsoft.Extensions.TimeProvider.Testing.Tests/FakeTimeProviderTimerTests.cs index 64cb4aececc..c605235104d 100644 --- a/test/Libraries/Microsoft.Extensions.TimeProvider.Testing.Tests/FakeTimeProviderTimerTests.cs +++ b/test/Libraries/Microsoft.Extensions.TimeProvider.Testing.Tests/FakeTimeProviderTimerTests.cs @@ -24,6 +24,7 @@ public void TimerNonPeriodicPeriodZero() using var timer = timeProvider.CreateTimer(_ => { counter++; }, null, TimeSpan.FromMilliseconds(10), TimeSpan.Zero); var value1 = counter; + timeProvider.Advance(TimeSpan.FromMilliseconds(20)); var value2 = counter; @@ -45,6 +46,7 @@ public void TimerNonPeriodicPeriodInfinite() using var timer = timeProvider.CreateTimer(_ => { counter++; }, null, TimeSpan.FromMilliseconds(10), Timeout.InfiniteTimeSpan); var value1 = counter; + timeProvider.Advance(TimeSpan.FromMilliseconds(20)); var value2 = counter;