public async Task ExecuteAsync_ShouldCancelAction_WhenCancellationToken_IsCancelled() { // Arrange var policy = new ServiceRetryPolicy(0, TimeSpan.Zero); using (var cancellationSource = new CancellationTokenSource()) using (var breakpoint = new Breakpoint()) { var cancelled = false; // Act var task = policy.ExecuteAsync( async ct => { // ReSharper disable once AccessToDisposedClosure await breakpoint.Hit().WaitForContinue(); cancelled = ct.IsCancellationRequested; ct.ThrowIfCancellationRequested(); return(Result.Ok()); }, cancellationSource.Token); var hit = await breakpoint.WaitForHit().TimeoutAfter(TestTimeout); cancellationSource.Cancel(); hit.Continue(); var result = await task.TimeoutAfter(TestTimeout); // Assert result.Success.Should().BeFalse(); cancelled.Should().BeTrue(); } }
public async Task ExecuteAsync_ShouldBeFailure_WhenActionReturnsFailure() { // Arrange var policy = new ServiceRetryPolicy(0, TimeSpan.Zero); // Act var result = await policy.ExecuteAsync(ct => Task.FromResult(Result.Fail()), CancellationToken.None); // Assert result.Failure.Should().BeTrue(); }
public async Task ExecuteAsync_ShouldBe_ActionResult() { // Arrange var expected = Result.Ok(); var policy = new ServiceRetryPolicy(2, TimeSpan.Zero); // Act var result = await policy.ExecuteAsync(ct => Task.FromResult(expected), CancellationToken.None); // Assert result.Should().Be(expected); }
public async Task ExecuteAsync_ShouldCallAction_RetryCountPlusOneTime_WhenActionReturnsFailure() { // Arrange const int retryCount = 2; var timesCalled = 0; var policy = new ServiceRetryPolicy(retryCount, TimeSpan.Zero); // Act await policy.ExecuteAsync( ct => { timesCalled++; return(Task.FromResult(Result.Fail())); }, CancellationToken.None); // Assert timesCalled.Should().Be(1 + retryCount); }