Skip to content

Commit

Permalink
SystemClockTimer test
Browse files Browse the repository at this point in the history
  • Loading branch information
danielchalmers committed Jul 17, 2024
1 parent cdfbf7d commit bafbb2f
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions DesktopClock.Tests/SystemClockTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace DesktopClock.Tests;

public class SystemClockTimerTests
{
private readonly SystemClockTimer _timer;

public SystemClockTimerTests()
{
_timer?.Dispose();
_timer = new SystemClockTimer();
}

[Theory]
[InlineData(3)]
public async Task ShouldTickEverySecondAccurately(int seconds)
{
// Ensure the timer is started at an unclean time to test accuracy.
await Task.Delay(1000 - DateTimeOffset.Now.Millisecond + 234);

Assert.NotInRange(DateTimeOffset.Now.Millisecond, 0, 100);

var tickTimes = new List<DateTimeOffset>();

_timer.SecondChanged += (sender, args) =>
{
tickTimes.Add(DateTimeOffset.Now);
};

_timer.Start();

await Task.Delay(TimeSpan.FromSeconds(seconds));

_timer.Stop();

Assert.Equal(seconds, tickTimes.Count);

// Check that each tick is close to the exact second.
foreach (var tickTime in tickTimes)
{
Assert.InRange(tickTime.Millisecond, 0, 100);
}
}
}

0 comments on commit bafbb2f

Please sign in to comment.