public void SingleRecurringEventDontStartAtFirstTest() { TimerEvent event1 = new TimerEvent(); // Should not do anything if not started. int id = this.uut.ScheduleRecurringEvent(defaultTimeout, event1.Post, false); Assert.IsFalse(event1.TryWait(defaultTimeout * 2)); // Now start it. Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); this.uut.StartEvent(id); Assert.IsTrue(event1.TryWait(defaultTimeout * 2)); stopWatch.Stop(); this.uut.StopEvent(id); // The event should not have been fired at least until our delay happened. Assert.GreaterOrEqual(stopWatch.Elapsed, defaultTimeout); // The event should have been fired waaaayyy before double our timeout. Assert.LessOrEqual(stopWatch.Elapsed, defaultTimeout * 2); // Now that we've stopped, we should return false. Assert.IsFalse(event1.TryWait(defaultTimeout * 2)); // Dispose. Should remove timer. Assert.IsTrue(uut.ContainsEvent(id)); this.uut.DisposeEvent(id); Assert.AreEqual(0, this.uut.ActiveEventIds.Count()); }
public void SingleRecurringEventStartRightAway() { TimerEvent event1 = new TimerEvent(); Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); int id = this.uut.ScheduleRecurringEvent(defaultTimeout, event1.Post, true); Assert.IsTrue(event1.TryWait(defaultTimeout * 2)); TimeSpan timeStamp1 = stopWatch.Elapsed; Assert.IsTrue(event1.TryWait(defaultTimeout * 2)); TimeSpan timeStamp2 = stopWatch.Elapsed; stopWatch.Stop(); this.uut.StopEvent(id); // The first event should not have been fired until first interval. Assert.GreaterOrEqual(timeStamp1, defaultTimeout); Assert.LessOrEqual(timeStamp1, defaultTimeout * 2); // The second event should not have been fired until first interval. Assert.GreaterOrEqual(timeStamp2, defaultTimeout * 2); Assert.LessOrEqual(timeStamp2, defaultTimeout * 3); }