private async Task RunTest(int count) { DateTimeOffset date = new DateTimeOffset(2000, 01, 01, 12, 12, 12, TimeSpan.FromHours(2)); date = date.AddDays(count); using (ClockOffset.Pin(date)) { Task <DateTimeOffset> task1 = this.DelayedNow(true); Task <DateTimeOffset> task2 = this.DelayedNow(false); Task <DateTimeOffset> task3 = this.DelayedNow(true); DateTimeOffset[] dates = await Task.WhenAll(task1, task2, task3); Assert.All(dates, x => { Assert.Equal(date, x); }); } // we move away from the pinned after the using statement Assert.NotEqual(date, Clock.Now); using (ClockOffset.Pin(date)) { Task <DateTimeOffset> task1 = this.DelayedNow(true); Task <DateTimeOffset> task2 = this.DelayedNow(false); Task <DateTimeOffset> task3 = this.DelayedNow(true); DateTimeOffset[] dates = await Task.WhenAll(task1, task2, task3); Assert.All(dates, x => { Assert.Equal(date, x); }); } }
public async Task <DateTimeOffset> DelayedDate(DateTimeOffset pinnedDate) { using (ClockOffset.Pin(pinnedDate)) { await Task.Delay(1); // to force a proper delay return(ClockOffset.Now); } }
public void PinStaticDateCausesStaticDateTimeProviderToBeUsed() { var targetDate = new DateTimeOffset(new DateTime(2000, 01, 01, 1, 2, 3, DateTimeKind.Local)); using (ClockOffset.Pin(targetDate)) { var staticProvider = Assert.IsType <StaticDateTimeOffsetProvider>(ClockOffset.CurrentProvider); Assert.Equal(targetDate.ToUniversalTime(), staticProvider.UtcNow()); } }
public void PinnedDelegateCausesDelegateDateTimeProviderToBeUsed() { var targetDate = new DateTimeOffset(new DateTime(2000, 01, 01, 1, 2, 5, DateTimeKind.Local)); Func <DateTimeOffset> targetDateFunc = () => { return(targetDate); }; using (ClockOffset.Pin(targetDateFunc)) { var provider = Assert.IsType <DelegateDateTimeOffsetProvider>(ClockOffset.CurrentProvider); Assert.Equal(targetDate.ToUniversalTime(), provider.UtcNow()); } }
public async Task PinSubContext() { DateTimeOffset date = new DateTimeOffset(2000, 01, 01, 1, 2, 3, TimeSpan.FromHours(1)); DateTimeOffset date1 = new DateTimeOffset(2000, 01, 02, 1, 2, 3, TimeSpan.FromHours(2)); DateTimeOffset date2 = new DateTimeOffset(2000, 01, 03, 1, 2, 3, TimeSpan.FromHours(3)); using (ClockOffset.Pin(date)) { Task <DateTimeOffset> task1 = this.DelayedDate(date1); Task <DateTimeOffset> task2 = this.DelayedDate(date2); DateTimeOffset[] dates = await Task.WhenAll(task1, task2); DateTimeOffset ambiantDate = ClockOffset.Now; Assert.Contains(date1, dates); Assert.Contains(date2, dates); Assert.Equal(date, ambiantDate); } // we move away from the pinned after the using statement Assert.NotEqual(date, Clock.Now); }