public void RecurringTimer_Disabled() { RecurringTimer timer; timer = new RecurringTimer(RecurringTimerType.Disabled, TimeSpan.FromSeconds(1)); Assert.IsFalse(timer.HasFired(new DateTime(2010, 10, 23, 10, 10, 0))); // Should never fire when disabled Assert.IsFalse(timer.HasFired(new DateTime(2010, 10, 24, 9, 0, 0))); Assert.IsFalse(timer.HasFired(new DateTime(2010, 10, 24, 10, 1, 0))); Assert.IsFalse(timer.HasFired(new DateTime(2010, 10, 24, 10, 1, 0))); Assert.IsFalse(timer.HasFired(new DateTime(2010, 10, 25, 10, 1, 0))); }
public void RecurringTimer_Interval() { RecurringTimer timer; timer = new RecurringTimer(RecurringTimerType.Interval, TimeSpan.FromSeconds(10)); timer.Start(new DateTime(2011, 8, 26, 0, 0, 0)); Assert.IsFalse(timer.HasFired(new DateTime(2011, 8, 26, 0, 0, 0))); Assert.IsFalse(timer.HasFired(new DateTime(2011, 8, 26, 0, 0, 9))); Assert.IsTrue(timer.HasFired(new DateTime(2011, 8, 26, 0, 0, 10))); Assert.IsFalse(timer.HasFired(new DateTime(2011, 8, 26, 0, 0, 19))); Assert.IsTrue(timer.HasFired(new DateTime(2011, 8, 26, 0, 0, 21))); }
public async Task RecurringTimer_Set() { var timer = new RecurringTimer("Interval:00:00:05"); Assert.False(timer.HasFired()); timer.Set(); Assert.True(timer.HasFired()); Assert.False(timer.HasFired()); var startUtc = DateTime.UtcNow; await timer.WaitAsync(TimeSpan.FromMilliseconds(50)); Assert.True(DateTime.UtcNow - startUtc >= TimeSpan.FromSeconds(5)); }
public void RecurringTimer_Interval() { RecurringTimer timer; timer = new RecurringTimer(RecurringTimerType.Interval, TimeSpan.FromSeconds(10)); timer.Start(new DateTime(2011, 8, 26, 0, 0, 0)); Assert.False(timer.HasFired(new DateTime(2011, 8, 26, 0, 0, 0))); Assert.False(timer.HasFired(new DateTime(2011, 8, 26, 0, 0, 9))); Assert.True(timer.HasFired(new DateTime(2011, 8, 26, 0, 0, 10))); Assert.False(timer.HasFired(new DateTime(2011, 8, 26, 0, 0, 19))); Assert.True(timer.HasFired(new DateTime(2011, 8, 26, 0, 0, 21))); Assert.Equal("Interval:00:00:10", timer.ToString()); timer = new RecurringTimer("Interval:48:11:12"); Assert.Equal("Interval:48:11:12", timer.ToString()); }
public void RecurringTimer_Daily() { RecurringTimer timer; timer = new RecurringTimer(new TimeOfDay("10:00:00")); Assert.IsFalse(timer.HasFired(new DateTime(2011, 10, 23, 10, 10, 0))); // Verify that we don't fire until we see the transition Assert.IsFalse(timer.HasFired(new DateTime(2011, 10, 24, 9, 0, 0))); // Still before the scheduled time Assert.IsTrue(timer.HasFired(new DateTime(2011, 10, 24, 10, 1, 0))); // Should have seen the transition Assert.IsFalse(timer.HasFired(new DateTime(2011, 10, 24, 10, 1, 0))); // Should be false now because we already handled this time Assert.IsTrue(timer.HasFired(new DateTime(2011, 10, 25, 10, 1, 0))); // Should fire for the next day timer = new RecurringTimer(RecurringTimerType.Daily, new TimeSpan(10, 0, 0)); Assert.IsFalse(timer.HasFired(new DateTime(2011, 10, 23, 10, 10, 0))); // Verify that we don't fire until we see the transition Assert.IsFalse(timer.HasFired(new DateTime(2011, 10, 24, 9, 0, 0))); // Still before the scheduled time Assert.IsTrue(timer.HasFired(new DateTime(2011, 10, 24, 10, 1, 0))); // Should have seen the transition Assert.IsFalse(timer.HasFired(new DateTime(2011, 10, 24, 10, 1, 0))); // Should be false now because we already handled this time Assert.IsTrue(timer.HasFired(new DateTime(2011, 10, 25, 10, 1, 0))); // Should fire for the next day }
public async Task RecurringTimer_Async() { var timer = new RecurringTimer(RecurringTimerType.Interval, TimeSpan.FromSeconds(1)); var sysNow = SysTime.Now; timer.Reset(); Assert.False(timer.HasFired()); await timer.WaitAsync(TimeSpan.FromMilliseconds(500)); Assert.True(SysTime.Now + TimeSpan.FromMilliseconds(50) - sysNow > TimeSpan.FromSeconds(1)); }
public void RecurringTimer_Hourly() { RecurringTimer timer; timer = new RecurringTimer(RecurringTimerType.Hourly, TimeSpan.FromMinutes(4)); Assert.False(timer.HasFired(new DateTime(2011, 08, 20, 10, 0, 0))); // Never fires on the first poll Assert.True(timer.HasFired(new DateTime(2011, 08, 20, 10, 5, 0))); Assert.False(timer.HasFired(new DateTime(2011, 08, 20, 11, 1, 0))); // Still before offset Assert.False(timer.HasFired(new DateTime(2011, 08, 20, 11, 3, 0))); // Still before offset Assert.True(timer.HasFired(new DateTime(2011, 08, 20, 11, 4, 0))); // Right at offset Assert.False(timer.HasFired(new DateTime(2011, 08, 20, 11, 4, 0))); // Doesn't fire until the next hour Assert.False(timer.HasFired(new DateTime(2011, 08, 20, 11, 15, 0))); Assert.False(timer.HasFired(new DateTime(2011, 08, 20, 11, 30, 0))); Assert.False(timer.HasFired(new DateTime(2011, 08, 20, 11, 55, 0))); Assert.False(timer.HasFired(new DateTime(2011, 08, 20, 12, 0, 0))); Assert.True(timer.HasFired(new DateTime(2011, 08, 20, 12, 5, 0))); // Just past the next firing time Assert.Equal("Hourly:04:00", timer.ToString()); timer = new RecurringTimer("Hourly:10:11"); Assert.Equal("Hourly:10:11", timer.ToString()); }