Exemple #1
0
        public void Change_Int32_HasInitialGreaterThanInterval()
        {
            using (var delorean = new Delorean(true))
            {
                delorean.Freeze();

                int fired = 0;

                using (var timer = TimeProvider.Timer((state) => fired++))
                {
                    timer.Change(100, 50);

                    delorean.Advance(50);

                    Assert.AreEqual(0, fired);

                    delorean.Advance(50);

                    Assert.AreEqual(1, fired);

                    delorean.Advance(50);

                    Assert.AreEqual(2, fired);
                }
            }
        }
Exemple #2
0
        public void Callback_UInt32_HasInitialButNotInterval(uint period)
        {
            using (var delorean = new Delorean(true))
            {
                delorean.Freeze();

                object expectedState = new object();

                var    fired       = 0;
                object actualState = null;

                using (var timer = TimeProvider.Timer((state) => { actualState = state; fired++; }, expectedState, 100u, period))
                {
                    delorean.Advance(100);

                    Assert.AreEqual(1, fired);
                    Assert.AreSame(expectedState, actualState);

                    actualState = null;

                    delorean.Advance(100);

                    Assert.AreEqual(1, fired);
                    Assert.IsNull(actualState);
                }
            }
        }
Exemple #3
0
        public void Callback_Int32_HasInitialAndInterval()
        {
            using (var delorean = new Delorean(true))
            {
                delorean.Freeze();

                object expectedState = new object();

                int    fired       = 0;
                object actualState = null;

                using (var timer = TimeProvider.Timer((state) => { actualState = state; fired++; }, expectedState, 100, 100))
                {
                    delorean.Advance(100);

                    Assert.AreEqual(1, fired);
                    Assert.AreSame(expectedState, actualState);

                    actualState = null;

                    delorean.Advance(100);

                    Assert.AreEqual(2, fired);
                    Assert.AreSame(expectedState, actualState);

                    actualState = null;

                    delorean.Advance(100);

                    Assert.AreEqual(3, fired);
                    Assert.AreSame(expectedState, actualState);
                }
            }
        }
Exemple #4
0
 public void Callback_Int32_Invalid(int initial, int period)
 {
     using (var delorean = new Delorean(true))
     {
         delorean.Freeze();
         Assert.ThrowsException <ArgumentOutOfRangeException>(() => TimeProvider.Timer((state) => { }, null, initial, period));
     }
 }
Exemple #5
0
 public void Callback_TimeSpan_Invalid(long initial, long period)
 {
     using (var delorean = new Delorean(true))
     {
         delorean.Freeze();
         Assert.ThrowsException <ArgumentOutOfRangeException>(() => TimeProvider.Timer((state) => { }, null, TimeSpan.FromMilliseconds(initial), TimeSpan.FromMilliseconds(period)));
     }
 }
Exemple #6
0
 public void Change_Int64_Invalid(long initial, long period)
 {
     using (var delorean = new Delorean(true))
     {
         delorean.Freeze();
         var timer = TimeProvider.Timer((state) => { });
         Assert.ThrowsException <ArgumentOutOfRangeException>(() => timer.Change(initial, period));
     }
 }
        public void Frozen_TimeSpan_InvalidWait(double milliseconds)
        {
            using (var delorean = new Delorean(true))
            {
                delorean.Freeze();

                Assert.ThrowsException <ArgumentOutOfRangeException>(() => TimeProvider.Delay(TimeSpan.FromMilliseconds(milliseconds)));
            }
        }
        public void Frozen_Int32_InvalidWait()
        {
            using (var delorean = new Delorean(true))
            {
                delorean.Freeze();

                Assert.ThrowsException <ArgumentOutOfRangeException>(() => TimeProvider.Delay(-2));
            }
        }
        public async Task Frozen_Int32_NoDelay_ReturnsImmediately()
        {
            using (var delorean = new Delorean(true))
            {
                delorean.Freeze();

                var delay = TimeProvider.Delay(0);

                Assert.IsTrue(delay.Wait(1));

                await delay;
            }
        }
        public void Frozen_Int32_Infinite_NeverReturns()
        {
            using (var delorean = new Delorean(true))
            {
                delorean.Freeze();

                var delay = TimeProvider.Delay(-1);

                delorean.Advance(DateTime.MaxValue - TimeProvider.UtcNow);

                Assert.IsFalse(delay.Wait(1));
            }
        }
        public void Frozen_TimeSpan_Infinite_NeverReturns(double milliseconds)
        {
            using (var delorean = new Delorean(true))
            {
                delorean.Freeze();

                var delay = TimeProvider.Delay(TimeSpan.FromMilliseconds(milliseconds));

                delorean.Advance(DateTime.MaxValue - TimeProvider.UtcNow);

                Assert.IsFalse(delay.Wait(1));
            }
        }
        public async Task Frozen_TimeSpan_NoDelay_ReturnsImmediately(double milliseconds)
        {
            using (var delorean = new Delorean(true))
            {
                delorean.Freeze();

                var delay = TimeProvider.Delay(TimeSpan.FromMilliseconds(milliseconds));

                Assert.IsTrue(delay.Wait(1));

                await delay;
            }
        }
        public async Task Frozen_Int32_CancellationTokenStartsCancelled()
        {
            using (var delorean = new Delorean(true))
            {
                delorean.Freeze();

                var token = new CancellationToken(true);

                var assert = Assert.ThrowsExceptionAsync <TaskCanceledException>(
                    async() => await TimeProvider.Delay(5, token));

                Assert.IsTrue(assert.Wait(100));

                await assert;
            }
        }
Exemple #14
0
        public void Callback_UInt32_HasInitialAndIntervalHitInSingleAdvance()
        {
            using (var delorean = new Delorean(true))
            {
                delorean.Freeze();

                int fired = 0;

                using (var timer = TimeProvider.Timer((state) => { fired++; }, null, 100u, 50u))
                {
                    delorean.Advance(200);

                    Assert.AreEqual(3, fired);
                }
            }
        }
Exemple #15
0
        public void Callback_UInt32_HasIntervalButNotInitial()
        {
            using (var delorean = new Delorean(true))
            {
                delorean.Freeze();

                int fired = 0;

                using (var timer = TimeProvider.Timer((state) => { fired++; }, null, uint.MaxValue, 50u))
                {
                    delorean.Advance(1000);

                    Assert.AreEqual(0, fired);
                }
            }
        }
Exemple #16
0
        public void Callback_TimeSpan_HasIntervalButNotInitial()
        {
            using (var delorean = new Delorean(true))
            {
                delorean.Freeze();

                int fired = 0;

                using (var timer = TimeProvider.Timer((state) => { fired++; }, null, TimeSpan.FromMilliseconds(-1), TimeSpan.FromMilliseconds(50)))
                {
                    delorean.Advance(1000);

                    Assert.AreEqual(0, fired);
                }
            }
        }
        public async Task Frozen_TimeSpan_CancellationTokenStartsCancelled(double milliseconds)
        {
            using (var delorean = new Delorean(true))
            {
                delorean.Freeze();

                var token = new CancellationToken(true);

                var assert = Assert.ThrowsExceptionAsync <TaskCanceledException>(
                    async() => await TimeProvider.Delay(TimeSpan.FromMilliseconds(milliseconds), token));

                Assert.IsTrue(assert.Wait(100));

                await assert;
            }
        }
        public async Task Frozen_TimeSpan_FollowsTimeAdvances()
        {
            using (var delorean = new Delorean(true))
            {
                delorean.Freeze();

                var watch = Stopwatch.StartNew();

                var controlledDelay = TimeProvider.Delay(TimeSpan.FromMilliseconds(500));
                delorean.Advance(500);
                await controlledDelay;

                watch.Stop();

                Assert.IsTrue(watch.ElapsedMilliseconds < 100);
            }
        }
Exemple #19
0
        public void Thawed_InitialNotHit_ContinuesRealtime()
        {
            using (var delorean = new Delorean(true))
            {
                delorean.Freeze();

                using (var evt = new AutoResetEvent(false))
                    using (var timer = TimeProvider.Timer((state) => evt.Set(), null, 25, -1))
                    {
                        delorean.Thaw();

                        Assert.IsTrue(evt.WaitOne(50));

                        Assert.IsFalse(evt.WaitOne(25));
                    }
            }
        }
Exemple #20
0
        public void Change_Int32_HasInitialAndIntervalHitInSingleAdvance()
        {
            using (var delorean = new Delorean(true))
            {
                delorean.Freeze();

                int fired = 0;

                using (var timer = TimeProvider.Timer((state) => fired++))
                {
                    timer.Change(100, 50);

                    delorean.Advance(200);

                    Assert.AreEqual(3, fired);
                }
            }
        }
Exemple #21
0
        public void Change_UInt32_HasInitialButNotInterval(uint period)
        {
            using (var delorean = new Delorean(true))
            {
                delorean.Freeze();

                var fired = 0;

                using (var timer = TimeProvider.Timer((state) => fired++))
                {
                    timer.Change(100u, period);

                    delorean.Advance(1000);

                    Assert.AreEqual(1, fired);
                }
            }
        }
Exemple #22
0
        public void Change_TimeSpan_HasInitialButNotInterval(long period)
        {
            using (var delorean = new Delorean(true))
            {
                delorean.Freeze();

                var fired = 0;

                using (var timer = TimeProvider.Timer((state) => fired++))
                {
                    timer.Change(TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(period));

                    delorean.Advance(1000);

                    Assert.AreEqual(1, fired);
                }
            }
        }
Exemple #23
0
        public void Change_Int32_HasIntervalButNotInitial()
        {
            using (var delorean = new Delorean(true))
            {
                delorean.Freeze();

                int fired = 0;

                using (var timer = TimeProvider.Timer((state) => { fired++; }))
                {
                    timer.Change(-1, 50);

                    delorean.Advance(1000);

                    Assert.AreEqual(0, fired);
                }
            }
        }
Exemple #24
0
        public void Thawed_InitialHit_HasInterval_UsesInterval()
        {
            using (var delorean = new Delorean(true))
            {
                delorean.Freeze();

                using (var evt = new AutoResetEvent(false))
                    using (var timer = TimeProvider.Timer((state) => evt.Set(), null, 250, 25))
                    {
                        delorean.Advance(250);
                        evt.Reset();

                        delorean.Thaw();

                        Assert.IsTrue(evt.WaitOne(100));
                    }
            }
        }
Exemple #25
0
        public void Callback_NeverGetsCalled()
        {
            using (var delorean = new Delorean(true))
            {
                delorean.Freeze();

                var    fired       = 0;
                object actualState = null;

                using (var timer = TimeProvider.Timer((state) => { actualState = state; fired++; }))
                {
                    delorean.Advance(int.MaxValue - 1);

                    Assert.AreEqual(0, fired);
                    Assert.IsNull(actualState);
                }
            }
        }
Exemple #26
0
        public void Thawed_InitialNotHit_HasInterval_UsesInitial()
        {
            using (var delorean = new Delorean(true))
                using (var evt = new AutoResetEvent(false))
                {
                    delorean.Freeze();

                    using (var timer = TimeProvider.Timer((state) => evt.Set(), null, 50, 500))
                    {
                        delorean.Thaw();

                        var watch = Stopwatch.StartNew();

                        Assert.IsTrue(evt.WaitOne(100));

                        Assert.IsTrue(watch.ElapsedMilliseconds > 25);
                    }
                }
        }
Exemple #27
0
        public void Thawed_Change_FunctionsAsNormalTimer()
        {
            using (var delorean = new Delorean(true))
            {
                delorean.Freeze();

                using (var evt = new AutoResetEvent(false))
                    using (var timer = TimeProvider.Timer((state) => evt.Set(), null, -1, -1))
                    {
                        evt.Reset();

                        delorean.Thaw();

                        timer.Change(25, 25);

                        Assert.IsTrue(evt.WaitOne(50));
                    }
            }
        }
        public async Task Thawed_ContinuesRealtimeBasedOnTargetTime()
        {
            using (var delorean = new Delorean(true))
            {
                delorean.Freeze();

                var first = TimeProvider.Delay(5);

                var second = TimeProvider.Delay(100);

                await Task.Delay(5);

                delorean.Thaw();

                Assert.IsTrue(first.Wait(1));

                Assert.IsFalse(second.Wait(25));
            }
        }
        public async Task Frozen_IgnoresRealTime()
        {
            using (var delorean = new Delorean(true))
            {
                delorean.Freeze();

                var watch = Stopwatch.StartNew();

                var controlledDelay = TimeProvider.Delay(5);
                var realtimeDelay   = Task.Delay(10);

                Assert.AreSame(
                    realtimeDelay,
                    await Task.WhenAny(controlledDelay, realtimeDelay));

                watch.Stop();

                Assert.IsTrue(watch.ElapsedMilliseconds >= 10);
            }
        }
Exemple #30
0
        public void UtcNowOffset_Frozen()
        {
            using (var delorean = new Delorean(true))
            {
                delorean.Freeze();

                Thread.Sleep(1);

                var before = DateTimeOffset.UtcNow;

                var firstNow = TimeProvider.UtcNowOffset;
                Thread.Sleep(1);
                var secondNow = TimeProvider.UtcNowOffset;

                var after = DateTimeOffset.UtcNow;

                Assert.IsTrue(before >= firstNow);
                Assert.IsTrue(after >= firstNow);
                Assert.AreEqual(firstNow, secondNow);
            }
        }