-
-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Tests] Add test to monitor HighPrecisionTickGenerator interval #106
- Loading branch information
1 parent
270974d
commit 5e1b82d
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
DryWetMidi.Tests/Devices/Clock/TickGenerator/HighPrecisionTickGeneratorTests.cs
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,49 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Threading; | ||
using Melanchall.DryWetMidi.Devices; | ||
using NUnit.Framework; | ||
|
||
namespace Melanchall.DryWetMidi.Tests.Devices | ||
{ | ||
[TestFixture] | ||
public sealed class HighPrecisionTickGeneratorTests | ||
{ | ||
#region Test methods | ||
|
||
[Retry(5)] | ||
[Test] | ||
public void CheckInterval([Values(1, 10, 100)] int interval) | ||
{ | ||
using (var tickGenerator = new HighPrecisionTickGenerator()) | ||
{ | ||
var stopwatch = new Stopwatch(); | ||
var elapsedTimes = new List<long>(150); | ||
|
||
tickGenerator.TickGenerated += (_, __) => elapsedTimes.Add(stopwatch.ElapsedMilliseconds); | ||
|
||
tickGenerator.TryStart(TimeSpan.FromMilliseconds(interval)); | ||
stopwatch.Start(); | ||
|
||
SpinWait.SpinUntil(() => elapsedTimes.Count >= 100); | ||
tickGenerator.TryStop(); | ||
stopwatch.Stop(); | ||
|
||
var time = elapsedTimes[0]; | ||
var maxDelta = 0L; | ||
var tolerance = interval * 0.2; | ||
|
||
foreach (var t in elapsedTimes) | ||
{ | ||
maxDelta = Math.Max(t - time, maxDelta); | ||
time = t; | ||
} | ||
|
||
Assert.Less(maxDelta, interval + tolerance, "Max time delta is too big."); | ||
} | ||
} | ||
|
||
#endregion | ||
} | ||
} |