Skip to content

Commit

Permalink
[Tests] Add test to monitor HighPrecisionTickGenerator interval #106
Browse files Browse the repository at this point in the history
  • Loading branch information
melanchall committed Mar 22, 2021
1 parent 270974d commit 5e1b82d
Showing 1 changed file with 49 additions and 0 deletions.
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
}
}

0 comments on commit 5e1b82d

Please sign in to comment.