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); } } }
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); } } }
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); } } }
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)); } }
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))); } }
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; } }
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); } } }
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); } } }
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); } }
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)); } } }
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); } } }
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); } } }
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); } } }
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); } } }
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)); } } }
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); } } }
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); } } }
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); } }
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); } }