public static void TestEventAfterDispose() { bool wasHere = false; using (DelayedAction delayedAction = new DelayedAction(() => { wasHere = true; }, new TimeSpan(0, 0, 0, 0, 1), null)) { delayedAction.RestartIdleTimer(); Assert.That(wasHere, Is.False, "The event should not be triggered immediately."); } Thread.Sleep(50); Assert.That(wasHere, Is.False, "The event should be note be triggered once disposed."); }
public static void TestShortDelayAndImmediateStart() { bool wasHere = false; using (DelayedAction delayedAction = new DelayedAction(() => { wasHere = true; }, new TimeSpan(0, 0, 0, 0, 1), null)) { delayedAction.RestartIdleTimer(); Thread.Sleep(50); Assert.That(wasHere, Is.True, "The event should be triggered once started."); wasHere = false; Thread.Sleep(50); Assert.That(wasHere, Is.False, "The event should not be triggered more than once."); } }
public static void TestManyRestartsButOnlyOneEvent() { int eventCount = 0; using (DelayedAction delayedAction = new DelayedAction(() => { ++eventCount; }, new TimeSpan(0, 0, 0, 0, 5), null)) { for (int i = 0; i < 10; ++i) { delayedAction.RestartIdleTimer(); } Thread.Sleep(50); Assert.That(eventCount, Is.EqualTo(1), "The event should be triggered exactly once."); Thread.Sleep(50); Assert.That(eventCount, Is.EqualTo(1), "The event should still be triggered exactly once."); } }
public static void TestObjectDisposedException() { DelayedAction delayedAction = new DelayedAction(() => { }, new TimeSpan(0, 0, 0, 0, 1), null); delayedAction.Dispose(); Assert.Throws<ObjectDisposedException>(() => { delayedAction.RestartIdleTimer(); }); }