-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cdfbf7d
commit bafbb2f
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |