private void TestScheduleTaskWithFixedDelay(IEventLoop loopA) { var timestamps = new BlockingCollection <long>(); int expectedTimeStamps = 3; var allTimeStampsLatch = new CountdownEvent(expectedTimeStamps); var f = loopA.ScheduleWithFixedDelay(() => { timestamps.Add(Stopwatch.GetTimestamp()); try { Thread.Sleep(51); } catch { } allTimeStampsLatch.Signal(); }, TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(100)); Assert.True(allTimeStampsLatch.Wait(TimeSpan.FromMinutes(1))); Assert.True(f.Cancel()); Thread.Sleep(300); Assert.Equal(expectedTimeStamps, timestamps.Count); // Check if the task was run without a lag. long?previousTimestamp = null; foreach (long t in timestamps) { if (previousTimestamp is null) { previousTimestamp = t; continue; } Assert.True(t - previousTimestamp.Value >= PreciseTime.ToDelayNanos(TimeSpan.FromMilliseconds(150))); previousTimestamp = t; } }