public async Task ShouldCancelTheTaskIfItTakesTooLongAfterResuming() { using (var cts = new CancellationTokenSource()) { // Arrange double?e = null; var options = new DeadManSwitchOptions { Timeout = TimeSpan.FromSeconds(2) }; var worker = Worker( Work( Pause(), Notify("Sleeping 3s"), Sleep(TimeSpan.FromSeconds(3)), Notify("Calculating PI"), Resume(), Notify("Sleeping 3s"), Sleep(TimeSpan.FromSeconds(3)), Notify("Calculating E"), Do(_ => e = Math.E) ), Result(Math.PI) ); // Act await _runner.Invoking(m => m.RunAsync(worker, options, cts.Token)).Should().ThrowAsync <OperationCanceledException>().ConfigureAwait(false); // Assert e.Should().BeNull(); } }
public async Task ShouldCancelTheTaskIfItTakesTooLongToDoSomething() { using (var cts = new CancellationTokenSource()) { // Arrange var options = new DeadManSwitchOptions { Timeout = TimeSpan.FromSeconds(2) }; var worker = Worker( Work( Notify("Sleeping for 3 seconds"), Sleep(TimeSpan.FromSeconds(3)) ), Result(Math.PI) ); // Act + Assert await _runner.Invoking(m => m.RunAsync(worker, options, cts.Token)).Should().ThrowAsync <OperationCanceledException>().ConfigureAwait(false); } }