public void WatchDogWontDisposeTheTargetIfSessionIsAborted() { var mock = new MockUp(); using (var watchDog = new WatchDog(mock.TimerFactory.Object, mock.MockClock.Object)) { watchDog.MaxSessionDuration = TimeSpan.FromSeconds(30); var session = watchDog.Watch(mock.Target.Object); // First tick mock.CurrentTime += mock.TimerInterval.TotalSeconds; mock.Timer.Raise(t => t.Ticked += null, EventArgs.Empty); // Cancel the session session.Dispose(); // Tick until ~1minutes while (mock.CurrentTime <= 60) { mock.CurrentTime += mock.TimerInterval.TotalSeconds; mock.Timer.Raise(t => t.Ticked += null, EventArgs.Empty); } // The target object must still alive mock.Target.Verify(inst => inst.Dispose(), Times.Never); } }
public void WatchDogCorrectlyDisposeTargets(double checkTime, bool expectDisposed, double refreshSessionAt = -1) { var mock = new MockUp(); // Begin test using (var watchDog = new WatchDog(mock.TimerFactory.Object, mock.MockClock.Object)) using (var session = watchDog.Watch(mock.Target.Object)) { watchDog.MaxSessionDuration = TimeSpan.FromSeconds(30); Assert.True(mock.TimerInterval.TotalSeconds > 1); // 'Tick' the clock, with increased current time, // until we reach the check time while (mock.CurrentTime <= checkTime) { // Increase the time mock.CurrentTime += mock.TimerInterval.TotalSeconds; // Should we 'refresh' the session? if (refreshSessionAt > 0 && mock.CurrentTime >= refreshSessionAt) { refreshSessionAt = -1; session.Refresh(); } mock.Timer.Raise(t => t.Ticked += null, EventArgs.Empty); } // Reached the check time, let's check. mock.Target.Verify( inst => inst.Dispose(), expectDisposed ? Times.Once() : Times.Never()); } }