public async Task ShouldCancelTheTaskIfTheDeadManSwitchWasPausedAndResumedAndTheTaskTakesTooLong() { using (var cts = new CancellationTokenSource()) { // Arrange double?pi = null, e = null; var options = new DeadManSwitchOptions { Timeout = TimeSpan.FromSeconds(2) }; var workItems = WorkItems( Work( Pause(), Sleep(TimeSpan.FromSeconds(3)), Do(_ => pi = Math.PI), Resume(), Sleep(TimeSpan.FromSeconds(3)), Do(_ => e = Math.E) ), Work( Do(_ => _logger.LogInformation("Cancelling infinite worker")), Do(_ => cts.Cancel()) ) ); var worker = InfiniteWorker(workItems); // Act await _runner.RunAsync(worker, options, cts.Token).ConfigureAwait(false); // Assert pi.Should().NotBeNull(); e.Should().BeNull(); } }
public async Task ShouldLetTaskFinishIfItCompletesImmediately() { using (var cts = new CancellationTokenSource()) { // Arrange double?pi = null; var options = new DeadManSwitchOptions { Timeout = TimeSpan.FromSeconds(2) }; var workItems = WorkItems( Work(Do(_ => pi = Math.PI)), Do(_ => _logger.LogInformation("Cancelling infinite worker")), Work(Do(_ => cts.Cancel())) ); var worker = Worker(workItems); // Act await _runner.RunAsync(worker, options, cts.Token).ConfigureAwait(false); // Assert pi.Should().Be(Math.PI); } }