Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiasnordqvist committed Jun 16, 2024
1 parent 4f82709 commit 3b80f48
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions DotNetThoughts.TimeKeeping.Tests/TimeTravelersClockTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace DotNetThoughts.TimeKeeping.Tests;
public class TimeTravelersClockTests
{
private static TimeSpan _allowedDeviation = TimeSpan.FromMilliseconds(10);
[Fact]
public void FreezeTime()
{
Expand Down Expand Up @@ -34,7 +35,7 @@ public void FreezeCurrentTime()
var sut = new TimeTravelersClock();
sut.Freeze();
sut.IsFrozen().Should().BeTrue();
sut.UtcNow().Should().BeCloseTo(DateTimeOffset.Now, TimeSpan.FromMilliseconds(5));
sut.UtcNow().Should().BeCloseTo(DateTimeOffset.Now, _allowedDeviation);
sut.UtcNow().Should().NotBeAfter(DateTimeOffset.Now);
}

Expand All @@ -52,7 +53,7 @@ public void ResetFrozenTimeShouldRevertEverythingToNormal()
sut.Reset();
// Assert
sut.IsFrozen().Should().BeFalse();
sut.UtcNow().Should().BeCloseTo(DateTimeOffset.Now, TimeSpan.FromMilliseconds(5));
sut.UtcNow().Should().BeCloseTo(DateTimeOffset.Now, _allowedDeviation);
}

[Fact]
Expand All @@ -62,9 +63,9 @@ public void BaselineMocksNow()
var now = DateTimeOffset.Now;
var baseLine = now.AddDays(-1);
sut.SetBaseline(baseLine);
sut.UtcNow().Should().BeCloseTo(baseLine, TimeSpan.FromMilliseconds(5));
sut.UtcNow().Should().BeCloseTo(baseLine, _allowedDeviation);
var slept = Sleep(20);
sut.UtcNow().Should().BeCloseTo(baseLine.Add(slept), TimeSpan.FromMilliseconds(5));
sut.UtcNow().Should().BeCloseTo(baseLine.Add(slept), _allowedDeviation);
}

[Fact]
Expand All @@ -82,7 +83,7 @@ public void AdvanceLiveTime()
{
var sut = new TimeTravelersClock();
sut.Advance(TimeSpan.FromDays(1));
sut.UtcNow().Should().BeCloseTo(DateTimeOffset.Now.AddDays(1), TimeSpan.FromMilliseconds(5));
sut.UtcNow().Should().BeCloseTo(DateTimeOffset.Now.AddDays(1), _allowedDeviation);
}

[Fact]
Expand All @@ -94,7 +95,7 @@ public void AdvanceFrozenTimeAndThenThaw()
sut.Advance(TimeSpan.FromDays(1));
sut.Thaw();
var slept = Sleep(20);
sut.UtcNow().Should().BeCloseTo(frozenTime.AddDays(1).Add(slept), TimeSpan.FromMilliseconds(5));
sut.UtcNow().Should().BeCloseTo(frozenTime.AddDays(1).Add(slept), _allowedDeviation);
}

[Fact]
Expand All @@ -104,15 +105,15 @@ public void AComplicatedTest()
var baseLine = new DateTimeOffset(2021, 1, 1, 0, 0, 0, TimeSpan.Zero);
var timer = new Stopwatch(); timer.Start();
sut.SetBaseline(baseLine);
sut.UtcNow().Should().BeCloseTo(baseLine.Add(timer.Elapsed), TimeSpan.FromMilliseconds(5));
sut.UtcNow().Should().BeCloseTo(baseLine.Add(timer.Elapsed), _allowedDeviation);
Sleep(20);
sut.UtcNow().Should().BeCloseTo(baseLine.Add(timer.Elapsed), TimeSpan.FromMilliseconds(5));
sut.UtcNow().Should().BeCloseTo(baseLine.Add(timer.Elapsed), _allowedDeviation);
var frozen = sut.Freeze();
sut.UtcNow().Should().Be(frozen);
var sleptWhileFrozen = Sleep(200);
sut.Thaw();
Sleep(20);
sut.UtcNow().Should().BeCloseTo(baseLine.Add(timer.Elapsed).Add(-sleptWhileFrozen), TimeSpan.FromMilliseconds(5));
sut.UtcNow().Should().BeCloseTo(baseLine.Add(timer.Elapsed).Add(-sleptWhileFrozen), _allowedDeviation);
}

private static TimeSpan Sleep(int milliseconds)
Expand Down

0 comments on commit 3b80f48

Please sign in to comment.