public async Task DelayOnExecuteWaitsForIntervalBeforeFirstTaskWithCancellationToken() { // Arrange Mock<IAsynchronousDelay> delay = new Mock<IAsynchronousDelay>(); AsynchronousIntervalTimer timer = new AsynchronousIntervalTimer(delay.Object, TimeSpan.FromMilliseconds(PretendDelayInMilliseconds), true); CancellationTokenSource source = new CancellationTokenSource(); // Act await timer.ExecuteAsync(ct => Task.FromResult(false), source.Token); // Assert delay.Verify(x => x.Delay(TimeSpan.FromMilliseconds(PretendDelayInMilliseconds), source.Token)); }
public async Task ShutdownActionInvokedWithCancellationToken() { // Arrange Mock<IAsynchronousDelay> delay = new Mock<IAsynchronousDelay>(); bool shutdownActionInvoked = false; AsynchronousIntervalTimer timer = new AsynchronousIntervalTimer(delay.Object, TimeSpan.FromMilliseconds(PretendDelayInMilliseconds), false); CancellationTokenSource source = new CancellationTokenSource(); // Act await timer.ExecuteAsync(ct => Task.FromResult(false), source.Token, () => shutdownActionInvoked = true); // Assert Assert.IsTrue(shutdownActionInvoked); source.Dispose(); }
public async Task ReturningFalseCancelsTimerWithCancellationToken() { // Arrange Mock<IAsynchronousDelay> delay = new Mock<IAsynchronousDelay>(); AsynchronousIntervalTimer timer = new AsynchronousIntervalTimer(delay.Object, TimeSpan.FromMilliseconds(PretendDelayInMilliseconds), false); CancellationTokenSource source = new CancellationTokenSource(); int repeatCount = 0; // Act await timer.ExecuteAsync(ct => { repeatCount++; return Task.FromResult(false); }, source.Token); // Assert Assert.AreEqual(1, repeatCount); source.Dispose(); }
public async Task IntervalOccursBetweenTasksWithCancellationToken() { // Arrange Mock<IAsynchronousDelay> delay = new Mock<IAsynchronousDelay>(); AsynchronousIntervalTimer timer = new AsynchronousIntervalTimer(delay.Object, TimeSpan.FromMilliseconds(PretendDelayInMilliseconds), false); CancellationTokenSource source = new CancellationTokenSource(); int repeatCount = 0; // Act await timer.ExecuteAsync(ct => { repeatCount++; return Task.FromResult(repeatCount < 2); }, source.Token); // Assert Assert.AreEqual(2, repeatCount); delay.Verify(x => x.Delay(TimeSpan.FromMilliseconds(PretendDelayInMilliseconds), source.Token)); source.Dispose(); }