static void Main(string[] args) { Console.WriteLine("Real time"); for (var i = 0; i < 10; i++) { logTime(); } Console.WriteLine(); Console.WriteLine("Pinned"); using (ClockTimer.Pin(TimeSpan.FromMilliseconds(5))) using (Clock.Pin(new DateTime(2000, 01, 01))) { for (var i = 0; i < 10; i++) { logTime(); } } Console.WriteLine(); Console.WriteLine("Real time"); for (var i = 0; i < 10; i++) { logTime(); } Console.WriteLine(); Console.ReadLine(); }
public async Task PinSubContext() { var targetTime = TimeSpan.FromMilliseconds(12312456); var targetTime1 = targetTime + TimeSpan.FromMinutes(1); var targetTime2 = targetTime + TimeSpan.FromMinutes(2); using (ClockTimer.Pin(targetTime)) { Task <TimeSpan> task1 = this.DelayedDate(targetTime1); Task <TimeSpan> task2 = this.DelayedDate(targetTime2); TimeSpan[] dates = await Task.WhenAll(task1, task2); var timer = ClockTimer.StartNew(); timer.Stop(); var ambiantDate = timer.Elapsed; Assert.Contains(targetTime1, dates); Assert.Contains(targetTime2, dates); Assert.Equal(targetTime, ambiantDate); } var nowtimer = ClockTimer.StartNew(); nowtimer.Stop(); var now = nowtimer.Elapsed; // we move away from the pinned after the using statement Assert.NotEqual(targetTime, now); }
public void PinStaticDateCausesStaticDateTimeProviderToBeUsed() { var targetTime = TimeSpan.FromMilliseconds(123456); using (ClockTimer.Pin(targetTime)) { var staticProvider = Assert.IsType <StaticElapsedClockTimerProvider>(ClockTimer.CurrentProvider); Assert.Equal(targetTime, staticProvider.Create().Elapsed); } }
public async Task <TimeSpan> DelayedDate(TimeSpan pinnedDate) { using (ClockTimer.Pin(pinnedDate)) { var timer = ClockTimer.StartNew(); await Task.Delay(1); // to force a proper delay timer.Stop(); return(timer.Elapsed); } }
public void PinnedDelegateCausesDelegateDateTimeProviderToBeUsed() { var targetTime = TimeSpan.FromMilliseconds(12312456); Func <TimeSpan> targetDateFunc = () => { return(targetTime); }; using (ClockTimer.Pin(targetDateFunc)) { var provider = Assert.IsType <DelegateClockTimerProvider>(ClockTimer.CurrentProvider); Assert.Equal(targetTime, provider.Create().Elapsed); } }
private async Task RunTest(int count) { var targetTime = TimeSpan.FromMilliseconds(12312456); targetTime = targetTime + TimeSpan.FromMinutes(count); using (ClockTimer.Pin(targetTime)) { Task <TimeSpan> task1 = this.DelayedNow(true); Task <TimeSpan> task2 = this.DelayedNow(false); Task <TimeSpan> task3 = this.DelayedNow(true); TimeSpan[] dates = await Task.WhenAll(task1, task2, task3); Assert.All(dates, x => { Assert.Equal(targetTime, x); }); } // we move away from the pinned after the using statement var timer = ClockTimer.StartNew(); timer.Stop(); var now = timer.Elapsed; Assert.NotEqual(targetTime, now); using (ClockTimer.Pin(targetTime)) { Task <TimeSpan> task1 = this.DelayedNow(true); Task <TimeSpan> task2 = this.DelayedNow(false); Task <TimeSpan> task3 = this.DelayedNow(true); TimeSpan[] dates = await Task.WhenAll(task1, task2, task3); Assert.All(dates, x => { Assert.Equal(targetTime, x); }); } }