private void TestScheduleLaggyTaskAtFixedRate(IEventLoop loopA)
        {
            var timestamps         = new BlockingCollection <long>();
            int expectedTimeStamps = 5;
            var allTimeStampsLatch = new CountdownEvent(expectedTimeStamps);
            var f = loopA.ScheduleAtFixedRate(() =>
            {
                var empty = timestamps.Count == 0;
                timestamps.Add(Stopwatch.GetTimestamp());
                if (empty)
                {
                    try
                    {
                        Thread.Sleep(401);
                    }
                    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 with lag.
            int  i = 0;
            long?previousTimestamp = null;

            foreach (long t in timestamps)
            {
                if (previousTimestamp == null)
                {
                    previousTimestamp = t;
                    continue;
                }

                long diff = t - previousTimestamp.Value;
                if (i == 0)
                {
                    Assert.True(diff >= PreciseTime.ToDelayNanos(TimeSpan.FromMilliseconds(400)));
                }
                else
                {
                    Assert.True(diff <= PreciseTime.ToDelayNanos(TimeSpan.FromMilliseconds(10)));
                }
                previousTimestamp = t;
                i++;
            }
        }
        private void TestScheduleTaskAtFixedRate(IEventLoop loopA)
        {
            var timestamps         = new BlockingCollection <long>();
            int expectedTimeStamps = 5;
            var allTimeStampsLatch = new CountdownEvent(expectedTimeStamps);
            var f = loopA.ScheduleAtFixedRate(() =>
            {
                timestamps.Add(Stopwatch.GetTimestamp());
                try
                {
                    Thread.Sleep(50);
                }
                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?firstTimestamp = null;
            int  cnt            = 0;

            foreach (long t in timestamps)
            {
                if (firstTimestamp == null)
                {
                    firstTimestamp = t;
                    continue;
                }

                long timepoint = t - firstTimestamp.Value;
                Assert.True(timepoint >= PreciseTime.ToDelayNanos(TimeSpan.FromMilliseconds(100 * cnt + 80)));
                Assert.True(timepoint <= PreciseTime.ToDelayNanos(TimeSpan.FromMilliseconds(100 * (cnt + 1) + 20)));

                cnt++;
            }
        }