diff --git a/src/Polly.Specs/Bulkhead/BulkheadAsyncSpecs.cs b/src/Polly.Specs/Bulkhead/BulkheadAsyncSpecs.cs index 3a560e7900..b5f877bd40 100644 --- a/src/Polly.Specs/Bulkhead/BulkheadAsyncSpecs.cs +++ b/src/Polly.Specs/Bulkhead/BulkheadAsyncSpecs.cs @@ -71,7 +71,7 @@ public void Should_call_onBulkheadRejected_with_passed_context() Within(CohesionTimeLimit, () => Expect(0, () => bulkhead.BulkheadAvailableCount, nameof(bulkhead.BulkheadAvailableCount))); - bulkhead.Awaiting(async b => await b.ExecuteAsync(ctx => TaskHelper.EmptyTask, contextPassedToExecute)).Should().Throw(); + bulkhead.Awaiting(b => b.ExecuteAsync(ctx => TaskHelper.EmptyTask, contextPassedToExecute)).Should().Throw(); cancellationSource.Cancel(); tcs.SetCanceled(); diff --git a/src/Polly.Specs/Bulkhead/BulkheadTResultAsyncSpecs.cs b/src/Polly.Specs/Bulkhead/BulkheadTResultAsyncSpecs.cs index e5d9579283..58d8be585c 100644 --- a/src/Polly.Specs/Bulkhead/BulkheadTResultAsyncSpecs.cs +++ b/src/Polly.Specs/Bulkhead/BulkheadTResultAsyncSpecs.cs @@ -77,7 +77,7 @@ public void Should_call_onBulkheadRejected_with_passed_context() Within(CohesionTimeLimit, () => Expect(0, () => bulkhead.BulkheadAvailableCount, nameof(bulkhead.BulkheadAvailableCount))); - bulkhead.Awaiting(async b => await b.ExecuteAsync(ctx => Task.FromResult(1), contextPassedToExecute)).Should().Throw(); + bulkhead.Awaiting(b => b.ExecuteAsync(ctx => Task.FromResult(1), contextPassedToExecute)).Should().Throw(); cancellationSource.Cancel(); tcs.SetCanceled(); diff --git a/src/Polly.Specs/CircuitBreaker/AdvancedCircuitBreakerAsyncSpecs.cs b/src/Polly.Specs/CircuitBreaker/AdvancedCircuitBreakerAsyncSpecs.cs index 143b652a21..d79b14c263 100644 --- a/src/Polly.Specs/CircuitBreaker/AdvancedCircuitBreakerAsyncSpecs.cs +++ b/src/Polly.Specs/CircuitBreaker/AdvancedCircuitBreakerAsyncSpecs.cs @@ -25,7 +25,7 @@ public void Should_be_able_to_handle_a_duration_of_timespan_maxvalue() .Handle() .AdvancedCircuitBreakerAsync(0.5, TimeSpan.FromSeconds(10), 4, TimeSpan.MaxValue); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); } @@ -81,9 +81,9 @@ public void Should_throw_if_timeslice_duration_is_less_than_resolution_of_circui Action action = () => Policy .Handle() .AdvancedCircuitBreakerAsync( - 0.5, - TimeSpan.FromMilliseconds(20).Add(TimeSpan.FromTicks(-1)), - 4, + 0.5, + TimeSpan.FromMilliseconds(20).Add(TimeSpan.FromTicks(-1)), + 4, TimeSpan.FromSeconds(30)); action.Should().Throw() @@ -163,8 +163,8 @@ public void Should_initialise_to_closed_state() #region Tests that are independent from health metrics implementation - // Tests on the AdvancedCircuitBreakerAsync operation typically use a breaker: - // - with a failure threshold of >=50%, + // Tests on the AdvancedCircuitBreakerAsync operation typically use a breaker: + // - with a failure threshold of >=50%, // - and a throughput threshold of 4 // - across a ten-second period. // These provide easy values for testing for failure and throughput thresholds each being met and non-met, in combination. @@ -185,21 +185,21 @@ public void Should_not_open_circuit_if_failure_threshold_and_minimum_threshold_i ); // Three of three actions in this test throw handled failures. - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); // Failure threshold exceeded, but throughput threshold not yet. // Throughput threshold will be exceeded by the below successful call, but we never break on a successful call; hence don't break on this. - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().NotThrow(); breaker.CircuitState.Should().Be(CircuitState.Closed); // No adjustment to SystemClock.UtcNow, so all exceptions were raised within same timeslice @@ -222,19 +222,19 @@ public void Should_not_open_circuit_if_exceptions_raised_are_not_one_of_the_spec ); // Four of four actions in this test throw unhandled failures. - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); } @@ -243,8 +243,8 @@ public void Should_not_open_circuit_if_exceptions_raised_are_not_one_of_the_spec #region With sample duration higher than 199 ms so that multiple windows are used - // Tests on the AdvancedCircuitBreakerAsync operation typically use a breaker: - // - with a failure threshold of >=50%, + // Tests on the AdvancedCircuitBreakerAsync operation typically use a breaker: + // - with a failure threshold of >=50%, // - and a throughput threshold of 4 // - across a ten-second period. // These provide easy values for testing for failure and throughput thresholds each being met and non-met, in combination. @@ -265,26 +265,26 @@ public void Should_open_circuit_blocking_executions_and_noting_the_last_raised_e ); // Four of four actions in this test throw handled failures. - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); // No adjustment to SystemClock.UtcNow, so all exceptions raised within same timeslice - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); bool delegateExecutedWhenBroken = false; - breaker.Awaiting(async x => await x.ExecuteAsync(() => { delegateExecutedWhenBroken = true; return TaskHelper.EmptyTask; })) + breaker.Awaiting(x => x.ExecuteAsync(() => { delegateExecutedWhenBroken = true; return TaskHelper.EmptyTask; })) .Should().Throw() .WithMessage("The circuit is now open and is not allowing calls.") .WithInnerException(); @@ -311,15 +311,15 @@ public void Should_open_circuit_with_the_last_raised_exception_if_failure_thresh ); // Four of four actions in this test throw handled failures. - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); @@ -328,11 +328,11 @@ public void Should_open_circuit_with_the_last_raised_exception_if_failure_thresh // They are still placed within same timeslice. SystemClock.UtcNow = () => time.AddSeconds(samplingDuration.Seconds / 2d + 1); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw() .WithMessage("The circuit is now open and is not allowing calls.") .WithInnerException(); @@ -356,24 +356,24 @@ public void Should_open_circuit_with_the_last_raised_exception_if_failure_thresh ); // Three of four actions in this test throw handled failures. - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().NotThrow(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); // No adjustment to SystemClock.UtcNow, so all exceptions were raised within same timeslice - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw() .WithMessage("The circuit is now open and is not allowing calls.") .WithInnerException(); @@ -399,19 +399,19 @@ public void Should_open_circuit_with_the_last_raised_exception_if_failure_thresh ); // Three of four actions in this test throw handled failures. - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().NotThrow(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); @@ -420,7 +420,7 @@ public void Should_open_circuit_with_the_last_raised_exception_if_failure_thresh // They are still placed within same timeslice SystemClock.UtcNow = () => time.AddSeconds(samplingDuration.Seconds / 2d + 1); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw() .WithMessage("The circuit is now open and is not allowing calls.") .WithInnerException(); @@ -444,24 +444,24 @@ public void Should_open_circuit_with_the_last_raised_exception_if_failure_thresh ); // Two of four actions in this test throw handled failures. - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().NotThrow(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().NotThrow(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); // No adjustment to SystemClock.UtcNow, so all exceptions were raised within same timeslice - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw() .WithMessage("The circuit is now open and is not allowing calls.") .WithInnerException(); @@ -487,19 +487,19 @@ public void Should_open_circuit_with_the_last_raised_exception_if_failure_thresh ); // Two of four actions in this test throw handled failures. - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().NotThrow(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().NotThrow(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); @@ -508,7 +508,7 @@ public void Should_open_circuit_with_the_last_raised_exception_if_failure_thresh // They are still placed within same timeslice SystemClock.UtcNow = () => time.AddSeconds(samplingDuration.Seconds / 2d + 1); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw() .WithMessage("The circuit is now open and is not allowing calls.") .WithInnerException(); @@ -534,22 +534,22 @@ public void Should_not_open_circuit_if_failure_threshold_exceeded_but_throughput ); // Four of four actions in this test throw handled failures; but only the first three within the timeslice. - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().NotThrow(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().NotThrow(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); // Adjust SystemClock so that timeslice (clearly) expires; fourth exception thrown in next-recorded timeslice. SystemClock.UtcNow = () => time.Add(samplingDuration).Add(samplingDuration); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); @@ -573,22 +573,22 @@ public void Should_not_open_circuit_if_failure_threshold_exceeded_but_throughput ); // Four of four actions in this test throw handled failures; but only the first three within the timeslice. - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().NotThrow(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().NotThrow(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); // Adjust SystemClock so that timeslice (just) expires; fourth exception thrown in following timeslice. SystemClock.UtcNow = () => time.Add(samplingDuration); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); @@ -614,25 +614,25 @@ public void Should_not_open_circuit_if_failure_threshold_exceeded_but_throughput // Four of four actions in this test throw handled failures; but only the first three within the original timeslice. // Two actions at the start of the original timeslice. - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().NotThrow(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().NotThrow(); breaker.CircuitState.Should().Be(CircuitState.Closed); // Creates a new window right at the end of the original timeslice. SystemClock.UtcNow = () => time.AddTicks(samplingDuration.Ticks - 1); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); // Adjust SystemClock so that timeslice (just) expires; fourth exception thrown in following timeslice. If timeslice/window rollover is precisely defined, this should cause first two actions to be forgotten from statistics (rolled out of the window of relevance), and thus the circuit not to break. SystemClock.UtcNow = () => time.Add(samplingDuration); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); @@ -656,26 +656,26 @@ public void Should_open_circuit_with_the_last_raised_exception_if_failure_thresh ); // Four of four actions in this test throw handled failures. - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); // Adjust SystemClock so that timeslice doesn't quite expire; fourth exception thrown in same timeslice. SystemClock.UtcNow = () => time.AddTicks(samplingDuration.Ticks - 1); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw() .WithMessage("The circuit is now open and is not allowing calls.") .WithInnerException(); @@ -699,15 +699,15 @@ public void Should_not_open_circuit_if_failure_threshold_not_met_and_throughput_ ); // One of three actions in this test throw handled failures. - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().NotThrow(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().NotThrow(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); // No adjustment to SystemClock.UtcNow, so all exceptions were raised within same timeslice @@ -729,19 +729,19 @@ public void Should_not_open_circuit_if_failure_threshold_not_met_but_throughput_ ); // One of four actions in this test throw handled failures. - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().NotThrow(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().NotThrow(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().NotThrow(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); // No adjustment to SystemClock.UtcNow, so all exceptions were raised within same timeslice @@ -766,7 +766,7 @@ public void Should_open_circuit_if_failures_at_end_of_last_timeslice_below_failu // Executing a single invocation to ensure timeslice is created // This invocation is not be counted against the threshold - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().NotThrow(); breaker.CircuitState.Should().Be(CircuitState.Closed); @@ -775,15 +775,15 @@ public void Should_open_circuit_if_failures_at_end_of_last_timeslice_below_failu SystemClock.UtcNow = () => time.AddTicks(samplingDuration.Ticks - 1); // Three of four actions in this test occur within the first timeslice. - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().NotThrow(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().NotThrow(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); @@ -793,7 +793,7 @@ public void Should_open_circuit_if_failures_at_end_of_last_timeslice_below_failu // This failure opens the circuit, because it is the second failure of four calls // equalling the failure threshold. The minimum threshold within the defined // sampling duration is met, when using rolling windows. - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); } @@ -817,7 +817,7 @@ public void Should_not_open_circuit_if_failures_at_end_of_last_timeslice_and_fai // Executing a single invocation to ensure timeslice is created // This invocation is not be counted against the threshold - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().NotThrow(); breaker.CircuitState.Should().Be(CircuitState.Closed); @@ -826,21 +826,21 @@ public void Should_not_open_circuit_if_failures_at_end_of_last_timeslice_and_fai SystemClock.UtcNow = () => time.AddTicks(samplingDuration.Ticks - 1); // Two of three actions in this test occur within the first timeslice. - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); // Setting the time to just barely into the new timeslice SystemClock.UtcNow = () => time.Add(samplingDuration); - // A third failure occurs just at the beginning of the new timeslice making - // the number of failures above the failure threshold. However, the throughput is + // A third failure occurs just at the beginning of the new timeslice making + // the number of failures above the failure threshold. However, the throughput is // below the minimum threshold as to open the circuit. - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); } @@ -865,7 +865,7 @@ public void Should_open_circuit_if_failures_in_second_window_of_last_timeslice_a // Executing a single invocation to ensure timeslice is created // This invocation is not be counted against the threshold - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().NotThrow(); breaker.CircuitState.Should().Be(CircuitState.Closed); @@ -873,22 +873,22 @@ public void Should_open_circuit_if_failures_in_second_window_of_last_timeslice_a SystemClock.UtcNow = () => time.AddSeconds(samplingDuration.Seconds / (double)numberOfWindowsDefinedInCircuitBreaker); // Three actions occur in the second window of the first timeslice - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().NotThrow(); breaker.CircuitState.Should().Be(CircuitState.Closed); // Setting the time to just barely into the new timeslice SystemClock.UtcNow = () => time.Add(samplingDuration); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); } @@ -897,8 +897,8 @@ public void Should_open_circuit_if_failures_in_second_window_of_last_timeslice_a #region With sample duration at 199 ms so that only a single window is used - // These tests on AdvancedCircuitBreakerAsync operation typically use a breaker: - // - with a failure threshold of >=50%, + // These tests on AdvancedCircuitBreakerAsync operation typically use a breaker: + // - with a failure threshold of >=50%, // - and a throughput threshold of 4 // - across a 199ms period. // These provide easy values for testing for failure and throughput thresholds each being met and non-met, in combination. @@ -919,25 +919,25 @@ public void Should_open_circuit_with_the_last_raised_exception_if_failure_thresh ); // Four of four actions in this test throw handled failures. - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); // No adjustment to SystemClock.UtcNow, so all exceptions raised within same timeslice - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw() .WithMessage("The circuit is now open and is not allowing calls.") .WithInnerException(); @@ -961,24 +961,24 @@ public void Should_open_circuit_with_the_last_raised_exception_if_failure_thresh ); // Three of four actions in this test throw handled failures. - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().NotThrow(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); // No adjustment to SystemClock.UtcNow, so all exceptions were raised within same timeslice - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw() .WithMessage("The circuit is now open and is not allowing calls.") .WithInnerException(); @@ -1002,24 +1002,24 @@ public void Should_open_circuit_with_the_last_raised_exception_if_failure_thresh ); // Two of four actions in this test throw handled failures. - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().NotThrow(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().NotThrow(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); // No adjustment to SystemClock.UtcNow, so all exceptions were raised within same timeslice - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw() .WithMessage("The circuit is now open and is not allowing calls.") .WithInnerException(); @@ -1045,22 +1045,22 @@ public void Should_not_open_circuit_if_failure_threshold_exceeded_but_throughput ); // Four of four actions in this test throw handled failures; but only the first within the timeslice. - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); // Adjust SystemClock so that timeslice (clearly) expires; fourth exception thrown in next-recorded timeslice. SystemClock.UtcNow = () => time.Add(samplingDuration).Add(samplingDuration); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); @@ -1084,22 +1084,22 @@ public void Should_not_open_circuit_if_failure_threshold_exceeded_but_throughput ); // Two of four actions in this test throw handled failures; but only the first within the timeslice. - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().NotThrow(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().NotThrow(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); // Adjust SystemClock so that timeslice (just) expires; fourth exception thrown in following timeslice. SystemClock.UtcNow = () => time.Add(samplingDuration); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); @@ -1123,26 +1123,26 @@ public void Should_open_circuit_with_the_last_raised_exception_if_failure_thresh ); // Four of four actions in this test throw handled failures. - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); // Adjust SystemClock so that timeslice doesn't quite expire; fourth exception thrown in same timeslice. SystemClock.UtcNow = () => time.AddTicks(samplingDuration.Ticks - 1); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw() .WithMessage("The circuit is now open and is not allowing calls.") .WithInnerException(); @@ -1166,15 +1166,15 @@ public void Should_not_open_circuit_if_failure_threshold_not_met_and_throughput_ ); // One of three actions in this test throw handled failures. - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().NotThrow(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().NotThrow(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); // No adjustment to SystemClock.UtcNow, so all exceptions were raised within same timeslice @@ -1196,19 +1196,19 @@ public void Should_not_open_circuit_if_failure_threshold_not_met_but_throughput_ ); // One of four actions in this test throw handled failures. - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().NotThrow(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().NotThrow(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().NotThrow(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); // No adjustment to SystemClock.UtcNow, so all exceptions were raised within same timeslice @@ -1233,7 +1233,7 @@ public void Should_not_open_circuit_if_failures_at_end_of_last_timeslice_below_f // Executing a single invocation to ensure timeslice is created // This invocation is not be counted against the threshold - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().NotThrow(); breaker.CircuitState.Should().Be(CircuitState.Closed); @@ -1242,24 +1242,24 @@ public void Should_not_open_circuit_if_failures_at_end_of_last_timeslice_below_f SystemClock.UtcNow = () => time.AddTicks(samplingDuration.Ticks - 1); // Three of four actions in this test occur within the first timeslice. - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().NotThrow(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().NotThrow(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); // Setting the time to just barely into the new timeslice SystemClock.UtcNow = () => time.Add(samplingDuration); - // This failure does not open the circuit, because a new duration should have + // This failure does not open the circuit, because a new duration should have // started and with such low sampling duration, windows should not be used. - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); } @@ -1288,19 +1288,19 @@ public void Should_halfopen_circuit_after_the_specified_duration_has_passed_with ); // Four of four actions in this test throw handled failures. - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); @@ -1309,7 +1309,7 @@ public void Should_halfopen_circuit_after_the_specified_duration_has_passed_with // duration has passed, circuit now half open breaker.CircuitState.Should().Be(CircuitState.HalfOpen); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); } @@ -1332,15 +1332,15 @@ public void Should_halfopen_circuit_after_the_specified_duration_has_passed_with ); // Four of four actions in this test throw handled failures. - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); @@ -1350,7 +1350,7 @@ public void Should_halfopen_circuit_after_the_specified_duration_has_passed_with var anotherWindowDuration = samplingDuration.Seconds / 2d + 1; SystemClock.UtcNow = () => time.AddSeconds(anotherWindowDuration); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); @@ -1364,7 +1364,7 @@ public void Should_halfopen_circuit_after_the_specified_duration_has_passed_with // duration has passed, circuit now half open breaker.CircuitState.Should().Be(CircuitState.HalfOpen); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); } @@ -1386,19 +1386,19 @@ public void Should_open_circuit_again_after_the_specified_duration_has_passed_if ); // Four of four actions in this test throw handled failures. - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); @@ -1409,10 +1409,10 @@ public void Should_open_circuit_again_after_the_specified_duration_has_passed_if breaker.CircuitState.Should().Be(CircuitState.HalfOpen); // first call after duration raises an exception, so circuit should open again - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); } @@ -1435,19 +1435,19 @@ public async Task Should_reset_circuit_after_the_specified_duration_has_passed_i ); // Four of four actions in this test throw handled failures. - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); @@ -1478,12 +1478,12 @@ public void Should_only_allow_single_execution_on_first_entering_halfopen_state_ durationOfBreak: durationOfBreak ); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); - // exception raised, circuit is now open. + // exception raised, circuit is now open. breaker.CircuitState.Should().Be(CircuitState.Open); // break duration passes, circuit now half open @@ -1516,12 +1516,12 @@ public void Should_allow_single_execution_per_break_duration_in_halfopen_state__ durationOfBreak: durationOfBreak ); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); - // exception raised, circuit is now open. + // exception raised, circuit is now open. breaker.CircuitState.Should().Be(CircuitState.Open); // break duration passes, circuit now half open @@ -1562,12 +1562,12 @@ public void Should_only_allow_single_execution_on_first_entering_halfopen_state_ durationOfBreak: durationOfBreak ); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); - // exceptions raised, circuit is now open. + // exceptions raised, circuit is now open. breaker.CircuitState.Should().Be(CircuitState.Open); // break duration passes, circuit now half open @@ -1580,7 +1580,7 @@ public void Should_only_allow_single_execution_on_first_entering_halfopen_state_ TimeSpan testTimeoutToExposeDeadlocks = TimeSpan.FromSeconds(5); using (ManualResetEvent permitSecondExecutionAttempt = new ManualResetEvent(false)) using (ManualResetEvent permitFirstExecutionEnd = new ManualResetEvent(false)) - { + { bool? firstDelegateExecutedInHalfOpenState = null; bool? secondDelegateExecutedInHalfOpenState = null; bool? secondDelegateRejectedInHalfOpenState = null; @@ -1642,7 +1642,7 @@ await breaker.ExecuteAsync(async () => firstExecution.Status.Should().Be(TaskStatus.RanToCompletion); secondExecution.Status.Should().Be(TaskStatus.RanToCompletion); - // Assert: + // Assert: // - First execution should have been permitted and executed under a HalfOpen state // - Second overlapping execution in halfopen state should not have been permitted. // - Second execution attempt should have been rejected with HalfOpen state as cause. @@ -1668,12 +1668,12 @@ public void Should_allow_single_execution_per_break_duration_in_halfopen_state__ durationOfBreak: durationOfBreak ); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); - // exception raised, circuit is now open. + // exception raised, circuit is now open. breaker.CircuitState.Should().Be(CircuitState.Open); // break duration passes, circuit now half open @@ -1687,7 +1687,7 @@ public void Should_allow_single_execution_per_break_duration_in_halfopen_state__ TimeSpan testTimeoutToExposeDeadlocks = TimeSpan.FromSeconds(5); using (ManualResetEvent permitSecondExecutionAttempt = new ManualResetEvent(false)) using (ManualResetEvent permitFirstExecutionEnd = new ManualResetEvent(false)) - { + { bool? firstDelegateExecutedInHalfOpenState = null; bool? secondDelegateExecutedInHalfOpenState = null; bool? secondDelegateRejectedInHalfOpenState = null; @@ -1751,7 +1751,7 @@ await breaker.ExecuteAsync(async () => firstExecution.Status.Should().Be(TaskStatus.RanToCompletion); secondExecution.Status.Should().Be(TaskStatus.RanToCompletion); - // Assert: + // Assert: // - First execution should have been permitted and executed under a HalfOpen state // - Second overlapping execution in halfopen state should have been permitted, one breakDuration later. firstDelegateExecutedInHalfOpenState.Should().BeTrue(); @@ -1778,7 +1778,7 @@ public void Should_open_circuit_and_block_calls_if_manual_override_open() // circuit manually broken: execution should be blocked; even non-exception-throwing executions should not reset circuit bool delegateExecutedWhenBroken = false; - breaker.Awaiting(async x => await x.ExecuteAsync(() => { delegateExecutedWhenBroken = true; return TaskHelper.EmptyTask; })) + breaker.Awaiting(x => x.ExecuteAsync(() => { delegateExecutedWhenBroken = true; return TaskHelper.EmptyTask; })) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Isolated); breaker.LastException.Should().BeOfType(); @@ -1804,7 +1804,7 @@ public void Should_hold_circuit_open_despite_elapsed_time_if_manual_override_ope breaker.CircuitState.Should().Be(CircuitState.Isolated); bool delegateExecutedWhenBroken = false; - breaker.Awaiting(async x => await x.ExecuteAsync(() => { delegateExecutedWhenBroken = true; return TaskHelper.EmptyTask; })) + breaker.Awaiting(x => x.ExecuteAsync(() => { delegateExecutedWhenBroken = true; return TaskHelper.EmptyTask; })) .Should().Throw(); delegateExecutedWhenBroken.Should().BeFalse(); } @@ -1823,12 +1823,12 @@ public void Should_close_circuit_again_on_reset_after_manual_override() breaker.Isolate(); breaker.CircuitState.Should().Be(CircuitState.Isolated); - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().Throw(); breaker.Reset(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().NotThrow(); } @@ -1850,19 +1850,19 @@ public void Should_be_able_to_reset_automatically_opened_circuit_without_specifi ); // Four of four actions in this test throw handled failures. - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); @@ -1870,7 +1870,7 @@ public void Should_be_able_to_reset_automatically_opened_circuit_without_specifi breaker.Reset(); SystemClock.UtcNow().Should().Be(time); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().NotThrow(); } @@ -1915,21 +1915,21 @@ public void Should_call_onbreak_when_breaking_circuit_automatically() ); // Four of four actions in this test throw handled failures. - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); // No adjustment to SystemClock.UtcNow, so all exceptions raised within same timeslice - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); @@ -1977,31 +1977,31 @@ public void Should_call_onbreak_when_breaking_circuit_first_time_but_not_for_sub ); // Four of four actions in this test throw handled failures. - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); onBreakCalled.Should().Be(0); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); onBreakCalled.Should().Be(0); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); onBreakCalled.Should().Be(0); // No adjustment to SystemClock.UtcNow, so all exceptions raised within same timeslice - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); onBreakCalled.Should().Be(1); - // call through circuit when already broken - should not retrigger onBreak - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + // call through circuit when already broken - should not retrigger onBreak + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); @@ -2031,7 +2031,7 @@ public void Should_call_onbreak_when_breaking_circuit_first_time_but_not_for_sub using (ManualResetEvent permitLongRunningExecutionToReturnItsFailure = new ManualResetEvent(false)) using (ManualResetEvent permitMainThreadToOpenCircuit = new ManualResetEvent(false)) - { + { Task longRunningExecution = Task.Factory.StartNew(() => { breaker.CircuitState.Should().Be(CircuitState.Closed); @@ -2057,9 +2057,9 @@ public void Should_call_onbreak_when_breaking_circuit_first_time_but_not_for_sub // Break circuit in the normal manner: onBreak() should be called once. breaker.CircuitState.Should().Be(CircuitState.Closed); onBreakCalled.Should().Be(0); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); onBreakCalled.Should().Be(1); @@ -2103,21 +2103,21 @@ public async Task Should_call_onreset_when_automatically_closing_circuit_but_not ); // Four of four actions in this test throw handled failures. - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); // No adjustment to SystemClock.UtcNow, so all exceptions raised within same timeslice - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); @@ -2187,24 +2187,24 @@ public async Task Should_call_onhalfopen_when_automatically_transitioning_to_hal ); // Four of four actions in this test throw handled failures. - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); onBreakCalled.Should().Be(0); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); onBreakCalled.Should().Be(0); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); onBreakCalled.Should().Be(0); // No adjustment to SystemClock.UtcNow, so all exceptions raised within same timeslice - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); @@ -2249,24 +2249,24 @@ public void Should_call_onhalfopen_when_automatically_transitioning_to_halfopen_ ); // Four of four actions in this test throw handled failures. - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); onBreakCalled.Should().Be(0); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); onBreakCalled.Should().Be(0); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); onBreakCalled.Should().Be(0); // No adjustment to SystemClock.UtcNow, so all exceptions raised within same timeslice - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); @@ -2299,7 +2299,7 @@ public void Should_call_onreset_when_manually_resetting_circuit() onBreakCalled.Should().Be(1); breaker.CircuitState.Should().Be(CircuitState.Isolated); - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().Throw(); onResetCalled.Should().Be(0); @@ -2307,7 +2307,7 @@ public void Should_call_onreset_when_manually_resetting_circuit() onResetCalled.Should().Be(1); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().NotThrow(); } @@ -2333,21 +2333,21 @@ public void Should_call_onbreak_with_the_last_raised_exception() ); // Four of four actions in this test throw handled failures. - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); // No adjustment to SystemClock.UtcNow, so all exceptions raised within same timeslice - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); @@ -2375,21 +2375,21 @@ public void Should_call_onbreak_with_a_state_of_closed() onHalfOpen: onHalfOpen ); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); // No adjustment to SystemClock.UtcNow, so all exceptions raised within same timeslice - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); @@ -2423,19 +2423,19 @@ public void Should_call_onbreak_with_a_state_of_half_open() ); // Four of four actions in this test throw handled failures. - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); @@ -2446,10 +2446,10 @@ public void Should_call_onbreak_with_a_state_of_half_open() breaker.CircuitState.Should().Be(CircuitState.HalfOpen); // first call after duration raises an exception, so circuit should open again - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); transitionedStates[0].Should().Be(CircuitState.Closed); @@ -2478,21 +2478,21 @@ public void Should_call_onbreak_with_the_correct_timespan() ); // Four of four actions in this test throw handled failures. - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); // No adjustment to SystemClock.UtcNow, so all exceptions raised within same timeslice - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); @@ -2555,19 +2555,19 @@ public void Should_call_onbreak_with_the_passed_context() ); // Four of four actions in this test throw handled failures. - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync( + breaker.Awaiting(x => x.RaiseExceptionAsync( new { key1 = "value1", key2 = "value2" }.AsDictionary() )).Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); @@ -2602,19 +2602,19 @@ public async Task Should_call_onreset_with_the_passed_context() ); // Four of four actions in this test throw handled failures. - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); @@ -2655,19 +2655,19 @@ public void Context_should_be_empty_if_execute_not_called_with_any_context_data( ); // Four of four actions in this test throw handled failures. - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); @@ -2702,19 +2702,19 @@ public async Task Should_create_new_context_for_each_call_to_execute() ); // Four of four actions in this test throw handled failures. - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync(new { key = "original_value" }.AsDictionary())) + breaker.Awaiting(x => x.RaiseExceptionAsync(new { key = "original_value" }.AsDictionary())) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); contextValue.Should().Be("original_value"); @@ -2765,7 +2765,7 @@ public void Should_set_LastException_on_handling_exception_even_when_not_breakin durationOfBreak: TimeSpan.FromSeconds(30) ); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); @@ -2784,11 +2784,11 @@ public void Should_set_LastException_to_last_raised_exception_when_breaking() durationOfBreak: TimeSpan.FromSeconds(30) ); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); @@ -2807,11 +2807,11 @@ public void Should_set_LastException_to_null_on_circuit_reset() durationOfBreak: TimeSpan.FromSeconds(30) ); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); @@ -2845,7 +2845,7 @@ public void Should_execute_action_when_non_faulting_and_cancellationToken_not_ca AttemptDuringWhichToCancel = null, }; - breaker.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + breaker.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().NotThrow(); attemptsInvoked.Should().Be(1); @@ -2873,7 +2873,7 @@ public void Should_not_execute_action_when_cancellationToken_cancelled_before_ex cancellationTokenSource.Cancel(); - breaker.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + breaker.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); @@ -2901,7 +2901,7 @@ public void Should_report_cancellation_during_otherwise_non_faulting_action_exec ActionObservesCancellation = true }; - breaker.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + breaker.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); @@ -2929,7 +2929,7 @@ public void Should_report_cancellation_during_faulting_action_execution_when_use ActionObservesCancellation = true }; - breaker.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + breaker.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); @@ -2956,7 +2956,7 @@ public void Should_report_faulting_from_faulting_action_execution_when_user_dele ActionObservesCancellation = false }; - breaker.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + breaker.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw(); attemptsInvoked.Should().Be(1); @@ -2970,13 +2970,13 @@ public void Should_report_cancellation_when_both_open_circuit_and_cancellation() .Handle() .AdvancedCircuitBreakerAsync(0.5, TimeSpan.FromSeconds(10), 2, durationOfBreak); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw() .WithMessage("The circuit is now open and is not allowing calls.") .WithInnerException(); @@ -2997,7 +2997,7 @@ public void Should_report_cancellation_when_both_open_circuit_and_cancellation() ActionObservesCancellation = false }; - breaker.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + breaker.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); @@ -3024,7 +3024,7 @@ public void Should_honour_different_cancellationToken_captured_implicitly_by_act int attemptsInvoked = 0; - breaker.Awaiting(async x => await x.ExecuteAsync(async ct => + breaker.Awaiting(x => x.ExecuteAsync(async ct => { attemptsInvoked++; await TaskHelper.EmptyTask; @@ -3058,7 +3058,8 @@ public void Should_execute_func_returning_value_when_cancellationToken_not_cance AttemptDuringWhichToCancel = null, }; - breaker.Awaiting(async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true)) + Func action = async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true); + breaker.Awaiting(action) .Should().NotThrow(); result.Should().BeTrue(); @@ -3089,7 +3090,8 @@ public void Should_honour_and_report_cancellation_during_func_execution() ActionObservesCancellation = true }; - breaker.Awaiting(async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true)) + Func action = async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true); + breaker.Awaiting(action) .Should().Throw().And.CancellationToken.Should().Be(cancellationToken); result.Should().Be(null); diff --git a/src/Polly.Specs/CircuitBreaker/CircuitBreakerAsyncSpecs.cs b/src/Polly.Specs/CircuitBreaker/CircuitBreakerAsyncSpecs.cs index e0bd0b6953..94a941a3fb 100644 --- a/src/Polly.Specs/CircuitBreaker/CircuitBreakerAsyncSpecs.cs +++ b/src/Polly.Specs/CircuitBreaker/CircuitBreakerAsyncSpecs.cs @@ -25,7 +25,7 @@ public void Should_be_able_to_handle_a_duration_of_timespan_maxvalue() .Handle() .CircuitBreakerAsync(1, TimeSpan.MaxValue); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); } @@ -85,14 +85,14 @@ public void Should_not_open_circuit_if_specified_number_of_specified_exception_a .Handle() .CircuitBreakerAsync(2, TimeSpan.FromMinutes(1)); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async b => await b.ExecuteAsync(() => TaskHelper.EmptyTask)).Should().NotThrow(); + breaker.Awaiting(b => b.ExecuteAsync(() => TaskHelper.EmptyTask)).Should().NotThrow(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); } @@ -104,16 +104,16 @@ public void Should_open_circuit_blocking_executions_and_noting_the_last_raised_e .Handle() .CircuitBreakerAsync(2, TimeSpan.FromMinutes(1)); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); bool delegateExecutedWhenBroken = false; - breaker.Awaiting(async x => await x.ExecuteAsync(() => { delegateExecutedWhenBroken = true; return TaskHelper.EmptyTask; })) + breaker.Awaiting(x => x.ExecuteAsync(() => { delegateExecutedWhenBroken = true; return TaskHelper.EmptyTask; })) .Should().Throw() .WithMessage("The circuit is now open and is not allowing calls.") .WithInnerException(); @@ -130,17 +130,17 @@ public void Should_open_circuit_blocking_executions_and_noting_the_last_raised_e .Or() .CircuitBreakerAsync(2, TimeSpan.FromMinutes(1)); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); // 2 exception raised, circuit is now open bool delegateExecutedWhenBroken = false; - breaker.Awaiting(async x => await x.ExecuteAsync(() => { delegateExecutedWhenBroken = true; return TaskHelper.EmptyTask; })) + breaker.Awaiting(x => x.ExecuteAsync(() => { delegateExecutedWhenBroken = true; return TaskHelper.EmptyTask; })) .Should().Throw() .WithMessage("The circuit is now open and is not allowing calls.") .WithInnerException(); @@ -155,15 +155,15 @@ public void Should_not_open_circuit_if_exception_raised_is_not_the_specified_exc .Handle() .CircuitBreakerAsync(2, TimeSpan.FromMinutes(1)); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); } @@ -176,15 +176,15 @@ public void Should_not_open_circuit_if_exception_raised_is_not_one_of_the_specif .Or() .CircuitBreakerAsync(2, TimeSpan.FromMinutes(1)); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); } @@ -205,16 +205,16 @@ public void Should_halfopen_circuit_after_the_specified_duration_has_passed() .Handle() .CircuitBreakerAsync(2, durationOfBreak); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); // 2 exception raised, circuit is now open - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); @@ -222,7 +222,7 @@ public void Should_halfopen_circuit_after_the_specified_duration_has_passed() // duration has passed, circuit now half open breaker.CircuitState.Should().Be(CircuitState.HalfOpen); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); } @@ -238,16 +238,16 @@ public void Should_open_circuit_again_after_the_specified_duration_has_passed_if .Handle() .CircuitBreakerAsync(2, durationOfBreak); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); // 2 exception raised, circuit is now open - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); @@ -256,10 +256,10 @@ public void Should_open_circuit_again_after_the_specified_duration_has_passed_if // duration has passed, circuit now half open breaker.CircuitState.Should().Be(CircuitState.HalfOpen); // first call after duration raises an exception, so circuit should break again - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); } @@ -276,16 +276,16 @@ public async Task Should_reset_circuit_after_the_specified_duration_has_passed_i .Handle() .CircuitBreakerAsync(2, durationOfBreak); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); // 2 exception raised, circuit is now open - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); @@ -298,15 +298,15 @@ public async Task Should_reset_circuit_after_the_specified_duration_has_passed_i breaker.CircuitState.Should().Be(CircuitState.Closed); // circuit has been reset so should once again allow 2 exceptions to be raised before breaking - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); } @@ -322,7 +322,7 @@ public void Should_only_allow_single_execution_on_first_entering_halfopen_state_ .Handle() .CircuitBreakerAsync(1, durationOfBreak); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); // exception raised, circuit is now open. @@ -353,7 +353,7 @@ public void Should_allow_single_execution_per_break_duration_in_halfopen_state__ .Handle() .CircuitBreakerAsync(1, durationOfBreak); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); // exception raised, circuit is now open. @@ -392,7 +392,7 @@ public void Should_only_allow_single_execution_on_first_entering_halfopen_state_ .Handle() .CircuitBreakerAsync(1, durationOfBreak); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); // exception raised, circuit is now open. @@ -491,7 +491,7 @@ public void Should_allow_single_execution_per_break_duration_in_halfopen_state__ .Handle() .CircuitBreakerAsync(1, durationOfBreak); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); // exception raised, circuit is now open. @@ -604,7 +604,7 @@ public void Should_open_circuit_and_block_calls_if_manual_override_open() // circuit manually broken: execution should be blocked; even non-exception-throwing executions should not reset circuit bool delegateExecutedWhenBroken = false; - breaker.Awaiting(async x => await x.ExecuteAsync(() => { delegateExecutedWhenBroken = true; return TaskHelper.EmptyTask; })) + breaker.Awaiting(x => x.ExecuteAsync(() => { delegateExecutedWhenBroken = true; return TaskHelper.EmptyTask; })) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Isolated); breaker.LastException.Should().BeOfType(); @@ -631,7 +631,7 @@ public void Should_hold_circuit_open_despite_elapsed_time_if_manual_override_ope SystemClock.UtcNow = () => time.Add(durationOfBreak); breaker.CircuitState.Should().Be(CircuitState.Isolated); bool delegateExecutedWhenBroken = false; - breaker.Awaiting(async x => await x.ExecuteAsync(() => { delegateExecutedWhenBroken = true; return TaskHelper.EmptyTask; })) + breaker.Awaiting(x => x.ExecuteAsync(() => { delegateExecutedWhenBroken = true; return TaskHelper.EmptyTask; })) .Should().Throw(); delegateExecutedWhenBroken.Should().BeFalse(); } @@ -651,12 +651,12 @@ public void Should_close_circuit_again_on_reset_after_manual_override() breaker.Isolate(); breaker.CircuitState.Should().Be(CircuitState.Isolated); - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().Throw(); breaker.Reset(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)).Should().NotThrow(); + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)).Should().NotThrow(); } [Fact] @@ -671,16 +671,16 @@ public void Should_be_able_to_reset_automatically_opened_circuit_without_specifi .Handle() .CircuitBreakerAsync(2, durationOfBreak); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); // 2 exception raised, circuit is now open - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); @@ -688,7 +688,7 @@ public void Should_be_able_to_reset_automatically_opened_circuit_without_specifi breaker.Reset(); SystemClock.UtcNow().Should().Be(time); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)).Should().NotThrow(); + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)).Should().NotThrow(); } #endregion @@ -720,13 +720,13 @@ public void Should_call_onbreak_when_breaking_circuit_automatically() .Handle() .CircuitBreakerAsync(2, TimeSpan.FromMinutes(1), onBreak, onReset); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); onBreakCalled.Should().BeFalse(); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); @@ -761,20 +761,20 @@ public void Should_call_onbreak_when_breaking_circuit_first_time_but_not_for_sub .Handle() .CircuitBreakerAsync(2, TimeSpan.FromMinutes(1), onBreak, onReset); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); onBreakCalled.Should().Be(0); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); onBreakCalled.Should().Be(1); // call through circuit when already broken - should not retrigger onBreak - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); @@ -822,7 +822,7 @@ public void Should_call_onbreak_when_breaking_circuit_first_time_but_not_for_sub // Break circuit in the normal manner: onBreak() should be called once. breaker.CircuitState.Should().Be(CircuitState.Closed); onBreakCalled.Should().Be(0); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); onBreakCalled.Should().Be(1); @@ -860,16 +860,16 @@ public void Should_call_onreset_when_automatically_closing_circuit_but_not_when_ onBreakCalled.Should().Be(0); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); onBreakCalled.Should().Be(0); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); onBreakCalled.Should().Be(1); // 2 exception raised, circuit is now open - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); onBreakCalled.Should().Be(1); @@ -930,16 +930,16 @@ public void Should_call_onhalfopen_when_automatically_transitioning_to_halfopen_ onBreakCalled.Should().Be(0); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); onBreakCalled.Should().Be(0); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); onBreakCalled.Should().Be(1); // 2 exception raised, circuit is now open - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); onBreakCalled.Should().Be(1); @@ -976,16 +976,16 @@ public void Should_call_onhalfopen_when_automatically_transitioning_to_halfopen_ onBreakCalled.Should().Be(0); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); onBreakCalled.Should().Be(0); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); onBreakCalled.Should().Be(1); // 2 exception raised, circuit is now open - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); onBreakCalled.Should().Be(1); @@ -1018,7 +1018,7 @@ public void Should_call_onreset_when_manually_resetting_circuit() onBreakCalled.Should().Be(1); breaker.CircuitState.Should().Be(CircuitState.Isolated); - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)) + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)) .Should().Throw(); onResetCalled.Should().Be(0); @@ -1026,7 +1026,7 @@ public void Should_call_onreset_when_manually_resetting_circuit() onResetCalled.Should().Be(1); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.ExecuteAsync(() => TaskHelper.EmptyTask)).Should().NotThrow(); + breaker.Awaiting(x => x.ExecuteAsync(() => TaskHelper.EmptyTask)).Should().NotThrow(); } #region Tests of supplied parameters to onBreak delegate @@ -1043,11 +1043,11 @@ public void Should_call_onbreak_with_the_last_raised_exception() .Handle() .CircuitBreakerAsync(2, TimeSpan.FromMinutes(1), onBreak, onReset); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); @@ -1067,11 +1067,11 @@ public void Should_call_onbreak_with_a_state_of_closed() .Handle() .CircuitBreakerAsync(2, TimeSpan.FromMinutes(1), onBreak, onReset, onHalfOpen); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); @@ -1096,16 +1096,16 @@ public void Should_call_onbreak_with_a_state_of_half_open() .Handle() .CircuitBreakerAsync(2, TimeSpan.FromMinutes(1), onBreak, onReset, onHalfOpen); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); // 2 exception raised, circuit is now open - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); @@ -1114,10 +1114,10 @@ public void Should_call_onbreak_with_a_state_of_half_open() // duration has passed, circuit now half open breaker.CircuitState.Should().Be(CircuitState.HalfOpen); // first call after duration raises an exception, so circuit should break again - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); transitionedStates[0].Should().Be(CircuitState.Closed); @@ -1167,11 +1167,11 @@ public void Should_call_onbreak_with_the_correct_timespan() .Handle() .CircuitBreakerAsync(2, durationOfBreak, onBreak, onReset); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); @@ -1216,10 +1216,10 @@ public void Should_call_onbreak_with_the_passed_context() .Handle() .CircuitBreakerAsync(2, TimeSpan.FromMinutes(1), onBreak, onReset); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); - breaker.Awaiting(async x => await x.RaiseExceptionAsync( + breaker.Awaiting(x => x.RaiseExceptionAsync( new { key1 = "value1", key2 = "value2" }.AsDictionary() )).Should().Throw(); @@ -1247,9 +1247,9 @@ public async Task Should_call_onreset_with_the_passed_context() .Handle() .CircuitBreakerAsync(2, durationOfBreak, onBreak, onReset); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); @@ -1276,10 +1276,10 @@ public void Context_should_be_empty_if_execute_not_called_with_any_context_data( .Handle() .CircuitBreakerAsync(2, TimeSpan.FromMinutes(1), onBreak, onReset); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); @@ -1304,11 +1304,11 @@ public void Should_create_new_context_for_each_call_to_execute() var durationOfBreak = TimeSpan.FromMinutes(1); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); // 2 exception raised, circuit is now open - breaker.Awaiting(async x => await x.RaiseExceptionAsync(new { key = "original_value" }.AsDictionary())) + breaker.Awaiting(x => x.RaiseExceptionAsync(new { key = "original_value" }.AsDictionary())) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); contextValue.Should().Be("original_value"); @@ -1348,7 +1348,7 @@ public void Should_set_LastException_on_handling_exception_even_when_not_breakin .Handle() .CircuitBreakerAsync(2, TimeSpan.FromMinutes(1)); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); @@ -1381,10 +1381,10 @@ public void Should_set_LastException_to_last_raised_exception_when_breaking() .Handle() .CircuitBreakerAsync(2, TimeSpan.FromMinutes(1)); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); @@ -1399,10 +1399,10 @@ public void Should_set_LastException_to_null_on_circuit_reset() .Handle() .CircuitBreakerAsync(2, TimeSpan.FromMinutes(1)); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); @@ -1437,7 +1437,7 @@ public void Should_execute_action_when_non_faulting_and_cancellationToken_not_ca AttemptDuringWhichToCancel = null, }; - breaker.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + breaker.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().NotThrow(); attemptsInvoked.Should().Be(1); @@ -1465,7 +1465,7 @@ public void Should_not_execute_action_when_cancellationToken_cancelled_before_ex cancellationTokenSource.Cancel(); - breaker.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + breaker.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); @@ -1493,7 +1493,7 @@ public void Should_report_cancellation_during_otherwise_non_faulting_action_exec ActionObservesCancellation = true }; - breaker.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + breaker.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); @@ -1521,7 +1521,7 @@ public void Should_report_cancellation_during_faulting_action_execution_when_use ActionObservesCancellation = true }; - breaker.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + breaker.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); @@ -1548,7 +1548,7 @@ public void Should_report_faulting_from_faulting_action_execution_when_user_dele ActionObservesCancellation = false }; - breaker.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + breaker.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw(); attemptsInvoked.Should().Be(1); @@ -1561,10 +1561,10 @@ public void Should_report_cancellation_when_both_open_circuit_and_cancellation() .Handle() .CircuitBreakerAsync(1, TimeSpan.FromMinutes(1)); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); - breaker.Awaiting(async x => await x.RaiseExceptionAsync()) + breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw() .WithMessage("The circuit is now open and is not allowing calls.") .WithInnerException(); @@ -1585,7 +1585,7 @@ public void Should_report_cancellation_when_both_open_circuit_and_cancellation() ActionObservesCancellation = false }; - breaker.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + breaker.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); @@ -1612,7 +1612,7 @@ public void Should_honour_different_cancellationToken_captured_implicitly_by_act int attemptsInvoked = 0; - breaker.Awaiting(async x => await x.ExecuteAsync(async ct => + breaker.Awaiting(x => x.ExecuteAsync(async ct => { attemptsInvoked++; await TaskHelper.EmptyTask; @@ -1646,7 +1646,8 @@ public void Should_execute_func_returning_value_when_cancellationToken_not_cance AttemptDuringWhichToCancel = null, }; - breaker.Awaiting(async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true)) + Func action = async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true); + breaker.Awaiting(action) .Should().NotThrow(); result.Should().BeTrue(); @@ -1676,7 +1677,8 @@ public void Should_honour_and_report_cancellation_during_func_execution() ActionObservesCancellation = true }; - breaker.Awaiting(async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true)) + Func action = async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true); + breaker.Awaiting(action) .Should().Throw().And.CancellationToken.Should().Be(cancellationToken); result.Should().Be(null); diff --git a/src/Polly.Specs/CircuitBreaker/CircuitBreakerTResultAsyncSpecs.cs b/src/Polly.Specs/CircuitBreaker/CircuitBreakerTResultAsyncSpecs.cs index fba64897da..dac26522aa 100644 --- a/src/Polly.Specs/CircuitBreaker/CircuitBreakerTResultAsyncSpecs.cs +++ b/src/Polly.Specs/CircuitBreaker/CircuitBreakerTResultAsyncSpecs.cs @@ -113,7 +113,7 @@ public async Task Should_open_circuit_with_the_last_handled_result_after_specifi .Should().Be(ResultPrimitive.Fault); breaker.CircuitState.Should().Be(CircuitState.Open); - breaker.Awaiting(async b => await b.RaiseResultSequenceAsync(ResultPrimitive.Fault)) + breaker.Awaiting(b => b.RaiseResultSequenceAsync(ResultPrimitive.Fault)) .Should().Throw>() .WithMessage("The circuit is now open and is not allowing calls.") .Where(e => e.Result == ResultPrimitive.Fault); @@ -138,7 +138,7 @@ public async Task Should_open_circuit_with_the_last_handled_result_after_specifi breaker.CircuitState.Should().Be(CircuitState.Open); // 2 exception or fault raised, circuit is now open - breaker.Awaiting(async b => await b.RaiseResultSequenceAsync(ResultPrimitive.Fault)) + breaker.Awaiting(b => b.RaiseResultSequenceAsync(ResultPrimitive.Fault)) .Should().Throw>() .WithMessage("The circuit is now open and is not allowing calls.") .Where(e => e.Result == ResultPrimitive.FaultAgain); @@ -160,7 +160,7 @@ public async Task Should_open_circuit_with_the_last_handled_result_after_specifi .ResultCode.Should().Be(ResultPrimitive.Fault); breaker.CircuitState.Should().Be(CircuitState.Open); - breaker.Awaiting(async b => await b.RaiseResultSequenceAsync(new ResultClass(ResultPrimitive.Good))) + breaker.Awaiting(b => b.RaiseResultSequenceAsync(new ResultClass(ResultPrimitive.Good))) .Should().Throw>() .WithMessage("The circuit is now open and is not allowing calls.") .Where(e => e.Result.ResultCode == ResultPrimitive.Fault); @@ -275,7 +275,7 @@ public async Task Should_halfopen_circuit_after_the_specified_duration_has_passe breaker.CircuitState.Should().Be(CircuitState.Open); // 2 exception or fault raised, circuit is now open - breaker.Awaiting(async b => await b.RaiseResultSequenceAsync(ResultPrimitive.Fault)) + breaker.Awaiting(b => b.RaiseResultSequenceAsync(ResultPrimitive.Fault)) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); @@ -308,7 +308,7 @@ public async Task Should_open_circuit_again_after_the_specified_duration_has_pas breaker.CircuitState.Should().Be(CircuitState.Open); // 2 exception or fault raised, circuit is now open - breaker.Awaiting(async b => await b.RaiseResultSequenceAsync(ResultPrimitive.Fault)) + breaker.Awaiting(b => b.RaiseResultSequenceAsync(ResultPrimitive.Fault)) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); @@ -321,9 +321,8 @@ public async Task Should_open_circuit_again_after_the_specified_duration_has_pas (await breaker.RaiseResultSequenceAsync(ResultPrimitive.Fault)) .Should().Be(ResultPrimitive.Fault); breaker.CircuitState.Should().Be(CircuitState.Open); - breaker.Awaiting(async b => await b.RaiseResultSequenceAsync(ResultPrimitive.Fault)) + breaker.Awaiting(b => b.RaiseResultSequenceAsync(ResultPrimitive.Fault)) .Should().Throw(); - } [Fact] @@ -347,7 +346,7 @@ public async Task Should_reset_circuit_after_the_specified_duration_has_passed_i breaker.CircuitState.Should().Be(CircuitState.Open); // 2 exception or fault raised, circuit is now open - breaker.Awaiting(async b => await b.RaiseResultSequenceAsync(ResultPrimitive.Fault)) + breaker.Awaiting(b => b.RaiseResultSequenceAsync(ResultPrimitive.Fault)) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); @@ -369,7 +368,7 @@ public async Task Should_reset_circuit_after_the_specified_duration_has_passed_i .Should().Be(ResultPrimitive.Fault); breaker.CircuitState.Should().Be(CircuitState.Open); - breaker.Awaiting(async b => await b.RaiseResultSequenceAsync(ResultPrimitive.Fault)) + breaker.Awaiting(b => b.RaiseResultSequenceAsync(ResultPrimitive.Fault)) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); } @@ -388,7 +387,7 @@ public async Task Should_only_allow_single_execution_on_first_entering_halfopen_ (await breaker.RaiseResultSequenceAsync(ResultPrimitive.Fault)) .Should().Be(ResultPrimitive.Fault); - // exception raised, circuit is now open. + // exception raised, circuit is now open. breaker.CircuitState.Should().Be(CircuitState.Open); // break duration passes, circuit now half open @@ -419,7 +418,7 @@ public async Task Should_allow_single_execution_per_break_duration_in_halfopen_s (await breaker.RaiseResultSequenceAsync(ResultPrimitive.Fault)) .Should().Be(ResultPrimitive.Fault); - // exception raised, circuit is now open. + // exception raised, circuit is now open. breaker.CircuitState.Should().Be(CircuitState.Open); // break duration passes, circuit now half open @@ -458,7 +457,7 @@ public async Task Should_only_allow_single_execution_on_first_entering_halfopen_ (await breaker.RaiseResultSequenceAsync(ResultPrimitive.Fault)) .Should().Be(ResultPrimitive.Fault); - // exception raised, circuit is now open. + // exception raised, circuit is now open. breaker.CircuitState.Should().Be(CircuitState.Open); // break duration passes, circuit now half open @@ -537,7 +536,7 @@ await breaker.ExecuteAsync(async () => firstExecution.Status.Should().Be(TaskStatus.RanToCompletion); secondExecution.Status.Should().Be(TaskStatus.RanToCompletion); - // Assert: + // Assert: // - First execution should have been permitted and executed under a HalfOpen state // - Second overlapping execution in halfopen state should not have been permitted. // - Second execution attempt should have been rejected with HalfOpen state as cause. @@ -561,7 +560,7 @@ public async Task Should_allow_single_execution_per_break_duration_in_halfopen_s (await breaker.RaiseResultSequenceAsync(ResultPrimitive.Fault)) .Should().Be(ResultPrimitive.Fault); - // exception raised, circuit is now open. + // exception raised, circuit is now open. breaker.CircuitState.Should().Be(CircuitState.Open); // break duration passes, circuit now half open @@ -643,7 +642,7 @@ await breaker.ExecuteAsync(async () => firstExecution.Status.Should().Be(TaskStatus.RanToCompletion); secondExecution.Status.Should().Be(TaskStatus.RanToCompletion); - // Assert: + // Assert: // - First execution should have been permitted and executed under a HalfOpen state // - Second overlapping execution in halfopen state should have been permitted, one breakDuration later. firstDelegateExecutedInHalfOpenState.Should().BeTrue(); @@ -675,7 +674,7 @@ public void Should_open_circuit_and_block_calls_if_manual_override_open() // circuit manually broken: execution should be blocked; even non-fault-returning executions should not reset circuit bool delegateExecutedWhenBroken = false; - breaker.Awaiting(async b => await b.ExecuteAsync(() => { delegateExecutedWhenBroken = true; return Task.FromResult(ResultPrimitive.Good); })) + breaker.Awaiting(b => b.ExecuteAsync(() => { delegateExecutedWhenBroken = true; return Task.FromResult(ResultPrimitive.Good); })) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Isolated); breaker.LastException.Should().BeOfType(); @@ -703,7 +702,7 @@ public void Should_hold_circuit_open_despite_elapsed_time_if_manual_override_ope breaker.CircuitState.Should().Be(CircuitState.Isolated); bool delegateExecutedWhenBroken = false; - breaker.Awaiting(async x => await x.ExecuteAsync(() => { delegateExecutedWhenBroken = true; return Task.FromResult(ResultPrimitive.Good); })) + breaker.Awaiting(x => x.ExecuteAsync(() => { delegateExecutedWhenBroken = true; return Task.FromResult(ResultPrimitive.Good); })) .Should().Throw(); delegateExecutedWhenBroken.Should().BeFalse(); } @@ -723,12 +722,12 @@ public void Should_close_circuit_again_on_reset_after_manual_override() breaker.Isolate(); breaker.CircuitState.Should().Be(CircuitState.Isolated); - breaker.Awaiting(async x => await x.ExecuteAsync(() => Task.FromResult(ResultPrimitive.Good))) + breaker.Awaiting(x => x.ExecuteAsync(() => Task.FromResult(ResultPrimitive.Good))) .Should().Throw(); breaker.Reset(); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.ExecuteAsync(() => Task.FromResult(ResultPrimitive.Good))).Should().NotThrow(); + breaker.Awaiting(x => x.ExecuteAsync(() => Task.FromResult(ResultPrimitive.Good))).Should().NotThrow(); } [Fact] @@ -752,7 +751,7 @@ public async Task Should_be_able_to_reset_automatically_opened_circuit_without_s breaker.CircuitState.Should().Be(CircuitState.Open); // 2 exception or fault raised, circuit is now open - breaker.Awaiting(async x => await x.ExecuteAsync(() => Task.FromResult(ResultPrimitive.Good))) + breaker.Awaiting(x => x.ExecuteAsync(() => Task.FromResult(ResultPrimitive.Good))) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); @@ -760,7 +759,7 @@ public async Task Should_be_able_to_reset_automatically_opened_circuit_without_s breaker.Reset(); SystemClock.UtcNow().Should().Be(time); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.ExecuteAsync(() => Task.FromResult(ResultPrimitive.Good))).Should().NotThrow(); + breaker.Awaiting(x => x.ExecuteAsync(() => Task.FromResult(ResultPrimitive.Good))).Should().NotThrow(); } #endregion @@ -845,8 +844,8 @@ public async Task Should_call_onbreak_when_breaking_circuit_first_time_but_not_f breaker.CircuitState.Should().Be(CircuitState.Open); onBreakCalled.Should().Be(1); - // call through circuit when already broken - should not retrigger onBreak - breaker.Awaiting(async x => await x.ExecuteAsync(() => Task.FromResult(ResultPrimitive.Good))) + // call through circuit when already broken - should not retrigger onBreak + breaker.Awaiting(x => x.ExecuteAsync(() => Task.FromResult(ResultPrimitive.Good))) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); @@ -941,7 +940,7 @@ public async Task Should_call_onreset_when_automatically_closing_circuit_but_not onBreakCalled.Should().Be(1); // 2 exception or fault raised, circuit is now open - breaker.Awaiting(async x => await x.ExecuteAsync(() => Task.FromResult(ResultPrimitive.Good))) + breaker.Awaiting(x => x.ExecuteAsync(() => Task.FromResult(ResultPrimitive.Good))) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); onBreakCalled.Should().Be(1); @@ -954,7 +953,7 @@ public async Task Should_call_onreset_when_automatically_closing_circuit_but_not onResetCalled.Should().Be(0); // first call after duration is successful, so circuit should reset - breaker.Awaiting(async x => await x.ExecuteAsync(() => Task.FromResult(ResultPrimitive.Good))).Should().NotThrow(); + breaker.Awaiting(x => x.ExecuteAsync(() => Task.FromResult(ResultPrimitive.Good))).Should().NotThrow(); breaker.CircuitState.Should().Be(CircuitState.Closed); onResetCalled.Should().Be(1); } @@ -972,11 +971,11 @@ public void Should_not_call_onreset_on_successive_successful_calls() onResetCalled.Should().BeFalse(); - breaker.Awaiting(async x => await x.ExecuteAsync(() => Task.FromResult(ResultPrimitive.Good))).Should().NotThrow(); + breaker.Awaiting(x => x.ExecuteAsync(() => Task.FromResult(ResultPrimitive.Good))).Should().NotThrow(); breaker.CircuitState.Should().Be(CircuitState.Closed); onResetCalled.Should().BeFalse(); - breaker.Awaiting(async x => await x.ExecuteAsync(() => Task.FromResult(ResultPrimitive.Good))).Should().NotThrow(); + breaker.Awaiting(x => x.ExecuteAsync(() => Task.FromResult(ResultPrimitive.Good))).Should().NotThrow(); breaker.CircuitState.Should().Be(CircuitState.Closed); onResetCalled.Should().BeFalse(); } @@ -1011,7 +1010,7 @@ public async Task Should_call_onhalfopen_when_automatically_transitioning_to_hal onBreakCalled.Should().Be(1); // 2 exception or fault raised, circuit is now open - breaker.Awaiting(async x => await x.ExecuteAsync(() => Task.FromResult(ResultPrimitive.Good))) + breaker.Awaiting(x => x.ExecuteAsync(() => Task.FromResult(ResultPrimitive.Good))) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); onBreakCalled.Should().Be(1); @@ -1021,7 +1020,7 @@ public async Task Should_call_onhalfopen_when_automatically_transitioning_to_hal onHalfOpenCalled.Should().Be(0); // not yet transitioned to half-open, because we have not queried state // first call after duration is successful, so circuit should reset - breaker.Awaiting(async x => await x.ExecuteAsync(() => Task.FromResult(ResultPrimitive.Good))) + breaker.Awaiting(x => x.ExecuteAsync(() => Task.FromResult(ResultPrimitive.Good))) .Should().NotThrow(); onHalfOpenCalled.Should().Be(1); breaker.CircuitState.Should().Be(CircuitState.Closed); @@ -1058,7 +1057,7 @@ public async Task Should_call_onhalfopen_when_automatically_transitioning_to_hal onBreakCalled.Should().Be(1); // 2 exception or fault raised, circuit is now open - breaker.Awaiting(async x => await x.ExecuteAsync(() => Task.FromResult(ResultPrimitive.Good))) + breaker.Awaiting(x => x.ExecuteAsync(() => Task.FromResult(ResultPrimitive.Good))) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); onBreakCalled.Should().Be(1); @@ -1091,7 +1090,7 @@ public void Should_call_onreset_when_manually_resetting_circuit() onBreakCalled.Should().Be(1); breaker.CircuitState.Should().Be(CircuitState.Isolated); - breaker.Awaiting(async x => await x.ExecuteAsync(() => Task.FromResult(ResultPrimitive.Good))) + breaker.Awaiting(x => x.ExecuteAsync(() => Task.FromResult(ResultPrimitive.Good))) .Should().Throw(); onResetCalled.Should().Be(0); @@ -1099,7 +1098,7 @@ public void Should_call_onreset_when_manually_resetting_circuit() onResetCalled.Should().Be(1); breaker.CircuitState.Should().Be(CircuitState.Closed); - breaker.Awaiting(async x => await x.ExecuteAsync(() => Task.FromResult(ResultPrimitive.Good))) + breaker.Awaiting(x => x.ExecuteAsync(() => Task.FromResult(ResultPrimitive.Good))) .Should().NotThrow(); } @@ -1404,7 +1403,7 @@ public async Task Should_execute_action_when_non_faulting_and_cancellationToken_ AttemptDuringWhichToCancel = null, }; - (await breaker.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, + (await breaker.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Good)) .Should().Be(ResultPrimitive.Good); @@ -1432,7 +1431,7 @@ public void Should_not_execute_action_when_cancellationToken_cancelled_before_ex cancellationTokenSource.Cancel(); - breaker.Awaiting(async x => await x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, + breaker.Awaiting(x => x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Good)) @@ -1462,7 +1461,7 @@ public void Should_report_cancellation_during_otherwise_non_faulting_action_exec ActionObservesCancellation = true }; - breaker.Awaiting(async x => await x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, + breaker.Awaiting(x => x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Good, ResultPrimitive.Good)) .Should().Throw() @@ -1491,7 +1490,7 @@ public void Should_report_cancellation_during_faulting_action_execution_when_use ActionObservesCancellation = true }; - breaker.Awaiting(async x => await x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, + breaker.Awaiting(x => x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Fault, ResultPrimitive.Good)) .Should().Throw() @@ -1536,7 +1535,7 @@ public async Task Should_report_cancellation_when_both_open_circuit_and_cancella (await breaker.RaiseResultSequenceAsync(ResultPrimitive.Fault)) .Should().Be(ResultPrimitive.Fault); - breaker.Awaiting(async x => await x.RaiseResultSequenceAsync(ResultPrimitive.Fault)) + breaker.Awaiting(x => x.RaiseResultSequenceAsync(ResultPrimitive.Fault)) .Should().Throw() .WithMessage("The circuit is now open and is not allowing calls."); // Circuit is now broken. @@ -1555,7 +1554,7 @@ public async Task Should_report_cancellation_when_both_open_circuit_and_cancella ActionObservesCancellation = false }; - breaker.Awaiting(async x => await x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, + breaker.Awaiting(x => x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Fault, ResultPrimitive.Good)) .Should().Throw() @@ -1584,7 +1583,7 @@ public void Should_honour_different_cancellationToken_captured_implicitly_by_act int attemptsInvoked = 0; - breaker.Awaiting(async x => await x.ExecuteAsync(async ct => + breaker.Awaiting(x => x.ExecuteAsync(async ct => { attemptsInvoked++; await TaskHelper.EmptyTask; diff --git a/src/Polly.Specs/Fallback/FallbackAsyncSpecs.cs b/src/Polly.Specs/Fallback/FallbackAsyncSpecs.cs index 37a4ba1135..b286271b5d 100644 --- a/src/Polly.Specs/Fallback/FallbackAsyncSpecs.cs +++ b/src/Polly.Specs/Fallback/FallbackAsyncSpecs.cs @@ -113,7 +113,7 @@ public void Should_not_execute_fallback_when_executed_delegate_throws_exception_ .Handle() .FallbackAsync(fallbackActionAsync); - fallbackPolicy.Awaiting(async x => await x.RaiseExceptionAsync()).Should().Throw(); + fallbackPolicy.Awaiting(x => x.RaiseExceptionAsync()).Should().Throw(); fallbackActionExecuted.Should().BeFalse(); } @@ -128,7 +128,7 @@ public void Should_execute_fallback_when_executed_delegate_throws_exception_hand .Handle() .FallbackAsync(fallbackActionAsync); - fallbackPolicy.Awaiting(async x => await x.RaiseExceptionAsync()).Should().NotThrow(); + fallbackPolicy.Awaiting(x => x.RaiseExceptionAsync()).Should().NotThrow(); fallbackActionExecuted.Should().BeTrue(); } @@ -145,7 +145,7 @@ public void Should_execute_fallback_when_executed_delegate_throws_one_of_excepti .Or() .FallbackAsync(fallbackActionAsync); - fallbackPolicy.Awaiting(async x => await x.RaiseExceptionAsync()).Should().NotThrow(); + fallbackPolicy.Awaiting(x => x.RaiseExceptionAsync()).Should().NotThrow(); fallbackActionExecuted.Should().BeTrue(); } @@ -162,7 +162,7 @@ public void Should_not_execute_fallback_when_executed_delegate_throws_exception_ .Or() .FallbackAsync(fallbackActionAsync); - fallbackPolicy.Awaiting(async x => await x.RaiseExceptionAsync()).Should().Throw(); + fallbackPolicy.Awaiting(x => x.RaiseExceptionAsync()).Should().Throw(); fallbackActionExecuted.Should().BeFalse(); } @@ -177,7 +177,7 @@ public void Should_not_execute_fallback_when_exception_thrown_does_not_match_han .Handle(e => false) .FallbackAsync(fallbackActionAsync); - fallbackPolicy.Awaiting(async x => await x.RaiseExceptionAsync()).Should().Throw(); + fallbackPolicy.Awaiting(x => x.RaiseExceptionAsync()).Should().Throw(); fallbackActionExecuted.Should().BeFalse(); } @@ -193,7 +193,7 @@ public void Should_not_execute_fallback_when_exception_thrown_does_not_match_any .Or(e => false) .FallbackAsync(fallbackActionAsync); - fallbackPolicy.Awaiting(async x => await x.RaiseExceptionAsync()).Should().Throw(); + fallbackPolicy.Awaiting(x => x.RaiseExceptionAsync()).Should().Throw(); fallbackActionExecuted.Should().BeFalse(); } @@ -208,7 +208,7 @@ public void Should_execute_fallback_when_exception_thrown_matches_handling_predi .Handle(e => true) .FallbackAsync(fallbackActionAsync); - fallbackPolicy.Awaiting(async x => await x.RaiseExceptionAsync()).Should().NotThrow(); + fallbackPolicy.Awaiting(x => x.RaiseExceptionAsync()).Should().NotThrow(); fallbackActionExecuted.Should().BeTrue(); } @@ -225,7 +225,7 @@ public void Should_execute_fallback_when_exception_thrown_matches_one_of_handlin .Or() .FallbackAsync(fallbackActionAsync); - fallbackPolicy.Awaiting(async x => await x.RaiseExceptionAsync()).Should().NotThrow(); + fallbackPolicy.Awaiting(x => x.RaiseExceptionAsync()).Should().NotThrow(); fallbackActionExecuted.Should().BeTrue(); } @@ -244,7 +244,7 @@ public void Should_not_handle_exception_thrown_by_fallback_delegate_even_if_is_e .Handle() .FallbackAsync(fallbackActionAsync); - fallbackPolicy.Awaiting(async x => await x.RaiseExceptionAsync((e, i) => e.HelpLink = "FromExecuteDelegate")) + fallbackPolicy.Awaiting(x => x.RaiseExceptionAsync((e, i) => e.HelpLink = "FromExecuteDelegate")) .Should().Throw().And.HelpLink.Should().Be("FromFallbackAction"); fallbackActionExecuted.Should().BeTrue(); @@ -319,7 +319,7 @@ public void Should_call_onFallback_with_the_passed_context() .Handle() .FallbackAsync(fallbackActionAsync, onFallbackAsync); - fallbackPolicy.Awaiting(async p => await p.ExecuteAsync(ctx => throw new ArgumentNullException(), + fallbackPolicy.Awaiting(p => p.ExecuteAsync(ctx => throw new ArgumentNullException(), new { key1 = "value1", key2 = "value2" }.AsDictionary())) .Should().NotThrow(); @@ -341,7 +341,7 @@ public void Should_call_onFallback_with_the_passed_context_when_execute_and_capt .Handle() .FallbackAsync(fallbackActionAsync, onFallbackAsync); - fallbackPolicy.Awaiting(async p => await p.ExecuteAndCaptureAsync(ctx => throw new ArgumentNullException(), + fallbackPolicy.Awaiting(p => p.ExecuteAndCaptureAsync(ctx => throw new ArgumentNullException(), new { key1 = "value1", key2 = "value2" }.AsDictionary())) .Should().NotThrow(); @@ -364,12 +364,10 @@ public void Should_call_onFallback_with_independent_context_for_independent_call .Or() .FallbackAsync(fallbackActionAsync, onFallbackAsync); - fallbackPolicy.Awaiting(async - p => await p.ExecuteAsync(ctx => throw new ArgumentNullException(), new { key = "value1" }.AsDictionary())) + fallbackPolicy.Awaiting(p => p.ExecuteAsync(ctx => throw new ArgumentNullException(), new { key = "value1" }.AsDictionary())) .Should().NotThrow(); - fallbackPolicy.Awaiting(async - p => await p.ExecuteAsync(ctx => throw new DivideByZeroException(), new { key = "value2" }.AsDictionary())) + fallbackPolicy.Awaiting(p => p.ExecuteAsync(ctx => throw new DivideByZeroException(), new { key = "value2" }.AsDictionary())) .Should().NotThrow(); contextData.Count.Should().Be(2); @@ -413,7 +411,7 @@ public void Should_call_fallbackAction_with_the_passed_context() .Handle() .FallbackAsync(fallbackActionAsync, onFallbackAsync); - fallbackPolicy.Awaiting(async p => await p.ExecuteAsync(ctx => throw new ArgumentNullException(), + fallbackPolicy.Awaiting(p => p.ExecuteAsync(ctx => throw new ArgumentNullException(), new { key1 = "value1", key2 = "value2" }.AsDictionary())) .Should().NotThrow(); @@ -435,7 +433,7 @@ public void Should_call_fallbackAction_with_the_passed_context_when_execute_and_ .Handle() .FallbackAsync(fallbackActionAsync, onFallbackAsync); - fallbackPolicy.Awaiting(async p => await p.ExecuteAndCaptureAsync(ctx => throw new ArgumentNullException(), + fallbackPolicy.Awaiting(p => p.ExecuteAndCaptureAsync(ctx => throw new ArgumentNullException(), new { key1 = "value1", key2 = "value2" }.AsDictionary())) .Should().NotThrow(); @@ -502,7 +500,7 @@ public void Should_call_fallbackAction_with_the_exception_when_execute_and_captu .Handle() .FallbackAsync(fallbackFunc, onFallback); - fallbackPolicy.Awaiting(async p => await p.ExecuteAndCaptureAsync(() => throw new ArgumentNullException())) + fallbackPolicy.Awaiting(p => p.ExecuteAndCaptureAsync(() => throw new ArgumentNullException())) .Should().NotThrow(); fallbackException.Should().NotBeNull() @@ -564,7 +562,7 @@ public void Should_not_call_fallbackAction_with_the_exception_if_exception_unhan .Handle() .FallbackAsync(fallbackFunc, onFallback); - fallbackPolicy.Awaiting(async p => await p.ExecuteAsync(() => throw new ArgumentNullException())) + fallbackPolicy.Awaiting(p => p.ExecuteAsync(() => throw new ArgumentNullException())) .Should().Throw(); fallbackException.Should().BeNull(); @@ -596,7 +594,7 @@ public void Should_execute_action_when_non_faulting_and_cancellationToken_not_ca AttemptDuringWhichToCancel = null, }; - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().NotThrow(); attemptsInvoked.Should().Be(1); @@ -625,7 +623,7 @@ public void Should_execute_fallback_when_faulting_and_cancellationToken_not_canc AttemptDuringWhichToCancel = null, }; - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().NotThrow(); attemptsInvoked.Should().Be(1); @@ -656,7 +654,7 @@ public void Should_not_execute_action_when_cancellationToken_cancelled_before_ex cancellationTokenSource.Cancel(); - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); attemptsInvoked.Should().Be(0); @@ -688,7 +686,7 @@ public void Should_report_cancellation_and_not_execute_fallback_during_otherwise ActionObservesCancellation = true }; - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); attemptsInvoked.Should().Be(1); @@ -720,7 +718,7 @@ public void Should_handle_cancellation_and_execute_fallback_during_otherwise_non ActionObservesCancellation = true }; - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().NotThrow(); attemptsInvoked.Should().Be(1); @@ -751,7 +749,7 @@ public void Should_not_report_cancellation_and_not_execute_fallback_if_non_fault ActionObservesCancellation = false }; - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().NotThrow(); attemptsInvoked.Should().Be(1); @@ -783,7 +781,7 @@ public void Should_report_unhandled_fault_and_not_execute_fallback_if_action_exe ActionObservesCancellation = false }; - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw(); attemptsInvoked.Should().Be(1); @@ -815,7 +813,7 @@ public void Should_handle_handled_fault_and_execute_fallback_following_faulting_ ActionObservesCancellation = false }; - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().NotThrow(); attemptsInvoked.Should().Be(1); diff --git a/src/Polly.Specs/Fallback/FallbackTResultAsyncSpecs.cs b/src/Polly.Specs/Fallback/FallbackTResultAsyncSpecs.cs index 4625a1af92..17a0035bee 100644 --- a/src/Polly.Specs/Fallback/FallbackTResultAsyncSpecs.cs +++ b/src/Polly.Specs/Fallback/FallbackTResultAsyncSpecs.cs @@ -472,7 +472,7 @@ public void Should_call_fallbackAction_with_the_passed_context_when_execute_and_ .HandleResult(ResultPrimitive.Fault) .FallbackAsync(fallbackActionAsync, onFallbackAsync); - fallbackPolicy.Awaiting(async p => await p.ExecuteAndCaptureAsync(ctx => Task.FromResult(ResultPrimitive.Fault), + fallbackPolicy.Awaiting(p => p.ExecuteAndCaptureAsync(ctx => Task.FromResult(ResultPrimitive.Fault), new { key1 = "value1", key2 = "value2" }.AsDictionary())) .Should().NotThrow(); diff --git a/src/Polly.Specs/NoOp/NoOpAsyncSpecs.cs b/src/Polly.Specs/NoOp/NoOpAsyncSpecs.cs index 089b429b56..6cdc13a246 100644 --- a/src/Polly.Specs/NoOp/NoOpAsyncSpecs.cs +++ b/src/Polly.Specs/NoOp/NoOpAsyncSpecs.cs @@ -13,7 +13,7 @@ public void Should_execute_user_delegate() var policy = Policy.NoOpAsync(); bool executed = false; - policy.Awaiting(async p => await p.ExecuteAsync(() => { executed = true; return TaskHelper.EmptyTask; })) + policy.Awaiting(p => p.ExecuteAsync(() => { executed = true; return TaskHelper.EmptyTask; })) .Should().NotThrow(); executed.Should().BeTrue(); @@ -30,7 +30,7 @@ public void Should_execute_user_delegate_without_adding_extra_cancellation_behav { cts.Cancel(); - policy.Awaiting(async p => await p.ExecuteAsync( + policy.Awaiting(p => p.ExecuteAsync( ct => { executed = true; return TaskHelper.EmptyTask; }, cts.Token)) .Should().NotThrow(); } diff --git a/src/Polly.Specs/NoOp/NoOpTResultAsyncSpecs.cs b/src/Polly.Specs/NoOp/NoOpTResultAsyncSpecs.cs index 9a4a5bb43c..e5f1185f2b 100644 --- a/src/Polly.Specs/NoOp/NoOpTResultAsyncSpecs.cs +++ b/src/Polly.Specs/NoOp/NoOpTResultAsyncSpecs.cs @@ -1,6 +1,8 @@ -using System.Threading; +using System; +using System.Threading; using System.Threading.Tasks; using FluentAssertions; +using Polly.NoOp; using Xunit; namespace Polly.Specs.NoOp @@ -13,7 +15,8 @@ public void Should_execute_user_delegate() var policy = Policy.NoOpAsync(); int? result = null; - policy.Awaiting(async p => result = await p.ExecuteAsync(() => Task.FromResult((int?)10))) + Func, Task> action = async p => result = await p.ExecuteAsync(() => Task.FromResult((int?)10)); + policy.Awaiting(action) .Should().NotThrow(); result.HasValue.Should().BeTrue(); @@ -30,7 +33,8 @@ public void Should_execute_user_delegate_without_adding_extra_cancellation_behav { cts.Cancel(); - policy.Awaiting(async p => result = await p.ExecuteAsync(ct => Task.FromResult((int?)10), cts.Token)) + Func, Task> action = async p => result = await p.ExecuteAsync(ct => Task.FromResult((int?)10), cts.Token); + policy.Awaiting(action) .Should().NotThrow(); } diff --git a/src/Polly.Specs/PolicyAsyncSpecs.cs b/src/Polly.Specs/PolicyAsyncSpecs.cs index cea4ca852b..8a9d621677 100644 --- a/src/Polly.Specs/PolicyAsyncSpecs.cs +++ b/src/Polly.Specs/PolicyAsyncSpecs.cs @@ -171,7 +171,7 @@ public void Executing_the_policy_action_should_throw_when_context_data_is_null() .Handle() .RetryAsync((_, __, ___) => { }); - policy.Awaiting(async p => await p.ExecuteAsync(ctx => TaskHelper.EmptyTask, (IDictionary)null)) + policy.Awaiting(p => p.ExecuteAsync(ctx => TaskHelper.EmptyTask, (IDictionary)null)) .Should().Throw(); } @@ -182,7 +182,7 @@ public void Executing_the_policy_action_should_throw_when_context_is_null() .Handle() .RetryAsync((_, __, ___) => { }); - policy.Awaiting(async p => await p.ExecuteAsync(ctx => TaskHelper.EmptyTask, (Context)null)) + policy.Awaiting(p => p.ExecuteAsync(ctx => TaskHelper.EmptyTask, (Context)null)) .Should().Throw().And .ParamName.Should().Be("context"); } @@ -194,7 +194,7 @@ public void Executing_the_policy_function_should_throw_when_context_data_is_null .Handle() .RetryAsync((_, __, ___) => { }); - policy.Awaiting(async p => await p.ExecuteAsync(ctx => Task.FromResult(2), (IDictionary)null)) + policy.Awaiting(p => p.ExecuteAsync(ctx => Task.FromResult(2), (IDictionary)null)) .Should().Throw(); } @@ -205,7 +205,7 @@ public void Executing_the_policy_function_should_throw_when_context_is_null() .Handle() .RetryAsync((_, __, ___) => { }); - policy.Awaiting(async p => await p.ExecuteAsync(ctx => Task.FromResult(2), (Context)null)) + policy.Awaiting(p => p.ExecuteAsync(ctx => Task.FromResult(2), (Context)null)) .Should().Throw().And .ParamName.Should().Be("context"); } @@ -231,7 +231,7 @@ public void Execute_and_capturing_the_policy_action_should_throw_when_context_da .Handle() .RetryAsync((_, __, ___) => { }); - policy.Awaiting(async p => await p.ExecuteAndCaptureAsync(ctx => TaskHelper.EmptyTask, (IDictionary)null)) + policy.Awaiting(p => p.ExecuteAndCaptureAsync(ctx => TaskHelper.EmptyTask, (IDictionary)null)) .Should().Throw(); } @@ -242,7 +242,7 @@ public void Execute_and_capturing_the_policy_action_should_throw_when_context_is .Handle() .RetryAsync((_, __, ___) => { }); - policy.Awaiting(async p => await p.ExecuteAndCaptureAsync(ctx => TaskHelper.EmptyTask, (Context)null)) + policy.Awaiting(p => p.ExecuteAndCaptureAsync(ctx => TaskHelper.EmptyTask, (Context)null)) .Should().Throw().And .ParamName.Should().Be("context"); } @@ -254,7 +254,7 @@ public void Execute_and_capturing_the_policy_function_should_throw_when_context_ .Handle() .RetryAsync((_, __, ___) => { }); - policy.Awaiting(async p => await p.ExecuteAndCaptureAsync(ctx => Task.FromResult(2), (IDictionary)null)) + policy.Awaiting(p => p.ExecuteAndCaptureAsync(ctx => Task.FromResult(2), (IDictionary)null)) .Should().Throw(); } @@ -265,7 +265,7 @@ public void Execute_and_capturing_the_policy_function_should_throw_when_context_ .Handle() .RetryAsync((_, __, ___) => { }); - policy.Awaiting(async p => await p.ExecuteAndCaptureAsync(ctx => Task.FromResult(2), (Context)null)) + policy.Awaiting(p => p.ExecuteAndCaptureAsync(ctx => Task.FromResult(2), (Context)null)) .Should().Throw().And .ParamName.Should().Be("context"); } diff --git a/src/Polly.Specs/PolicyTResultAsyncSpecs.cs b/src/Polly.Specs/PolicyTResultAsyncSpecs.cs index 813ea39451..edfa3ea2f3 100644 --- a/src/Polly.Specs/PolicyTResultAsyncSpecs.cs +++ b/src/Polly.Specs/PolicyTResultAsyncSpecs.cs @@ -102,7 +102,7 @@ public void Executing_the_policy_function_should_throw_when_context_data_is_null .HandleResult(ResultPrimitive.Fault) .RetryAsync((_, __, ___) => { }); - policy.Awaiting(async p => await p.ExecuteAsync(ctx => Task.FromResult(ResultPrimitive.Good), (IDictionary)null)) + policy.Awaiting(p => p.ExecuteAsync(ctx => Task.FromResult(ResultPrimitive.Good), (IDictionary)null)) .Should().Throw(); } @@ -113,7 +113,7 @@ public void Executing_the_policy_function_should_throw_when_context_is_null() .HandleResult(ResultPrimitive.Fault) .RetryAsync((_, __, ___) => { }); - policy.Awaiting(async p => await p.ExecuteAsync(ctx => Task.FromResult(ResultPrimitive.Good), (Context)null)) + policy.Awaiting(p => p.ExecuteAsync(ctx => Task.FromResult(ResultPrimitive.Good), (Context)null)) .Should().Throw().And .ParamName.Should().Be("context"); } @@ -125,7 +125,7 @@ public void Execute_and_capturing_the_policy_function_should_throw_when_context_ .HandleResult(ResultPrimitive.Fault) .RetryAsync((_, __, ___) => { }); - policy.Awaiting(async p => await p.ExecuteAndCaptureAsync(ctx => Task.FromResult(ResultPrimitive.Good), (Context)null)) + policy.Awaiting(p => p.ExecuteAndCaptureAsync(ctx => Task.FromResult(ResultPrimitive.Good), (Context)null)) .Should().Throw().And .ParamName.Should().Be("context"); } @@ -151,7 +151,7 @@ public void Execute_and_capturing_the_policy_function_should_throw_when_context_ .HandleResult(ResultPrimitive.Fault) .RetryAsync((_, __, ___) => { }); - policy.Awaiting(async p => await p.ExecuteAndCaptureAsync(ctx => Task.FromResult(ResultPrimitive.Good), (Context)null)) + policy.Awaiting(p => p.ExecuteAndCaptureAsync(ctx => Task.FromResult(ResultPrimitive.Good), (Context)null)) .Should().Throw().And .ParamName.Should().Be("context"); } diff --git a/src/Polly.Specs/Polly.Specs.csproj b/src/Polly.Specs/Polly.Specs.csproj index 198edd2862..2d6ba13a09 100644 --- a/src/Polly.Specs/Polly.Specs.csproj +++ b/src/Polly.Specs/Polly.Specs.csproj @@ -12,7 +12,7 @@ - + diff --git a/src/Polly.Specs/Retry/RetryAsyncSpecs.cs b/src/Polly.Specs/Retry/RetryAsyncSpecs.cs index 07e5ee7798..927d721cda 100644 --- a/src/Polly.Specs/Retry/RetryAsyncSpecs.cs +++ b/src/Polly.Specs/Retry/RetryAsyncSpecs.cs @@ -4,6 +4,7 @@ using System.Threading; using System.Threading.Tasks; using FluentAssertions; +using Polly.Retry; using Polly.Specs.Helpers; using Polly.Utilities; using Xunit; @@ -22,7 +23,7 @@ public void Should_throw_when_retry_count_is_less_than_zero_without_context() Action policy = () => Policy .Handle() .RetryAsync(-1, onRetry); - + policy.Should().Throw().And .ParamName.Should().Be("retryCount"); } @@ -34,7 +35,7 @@ public void Should_not_throw_when_specified_exception_thrown_same_number_of_time .Handle() .RetryAsync(3); - policy.Awaiting(async x => await x.RaiseExceptionAsync(3)) + policy.Awaiting(x => x.RaiseExceptionAsync(3)) .Should().NotThrow(); } @@ -46,7 +47,7 @@ public void Should_not_throw_when_one_of_the_specified_exceptions_thrown_same_nu .Or() .RetryAsync(3); - policy.Awaiting(async x => await x.RaiseExceptionAsync(3)) + policy.Awaiting(x => x.RaiseExceptionAsync(3)) .Should().NotThrow(); } @@ -57,7 +58,7 @@ public void Should_not_throw_when_specified_exception_thrown_less_number_of_time .Handle() .RetryAsync(3); - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().NotThrow(); } @@ -69,7 +70,7 @@ public void Should_not_throw_when_one_of_the_specified_exceptions_thrown_less_nu .Or() .RetryAsync(3); - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().NotThrow(); } @@ -80,7 +81,7 @@ public void Should_throw_when_specified_exception_thrown_more_times_then_retry_c .Handle() .RetryAsync(3); - policy.Awaiting(async x => await x.RaiseExceptionAsync(3 + 1)) + policy.Awaiting(x => x.RaiseExceptionAsync(3 + 1)) .Should().Throw(); } @@ -92,7 +93,7 @@ public void Should_throw_when_one_of_the_specified_exceptions_are_thrown_more_ti .Or() .RetryAsync(3); - policy.Awaiting(async x => await x.RaiseExceptionAsync(3 + 1)) + policy.Awaiting(x => x.RaiseExceptionAsync(3 + 1)) .Should().Throw(); } @@ -103,7 +104,7 @@ public void Should_throw_when_exception_thrown_is_not_the_specified_exception_ty .Handle() .RetryAsync(); - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); } @@ -115,7 +116,7 @@ public void Should_throw_when_exception_thrown_is_not_one_of_the_specified_excep .Or() .RetryAsync(); - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); } @@ -126,7 +127,7 @@ public void Should_throw_when_specified_exception_predicate_is_not_satisfied() .Handle(e => false) .RetryAsync(); - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); } @@ -138,7 +139,7 @@ public void Should_throw_when_none_of_the_specified_exception_predicates_are_sat .Or(e => false) .RetryAsync(); - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); } @@ -149,7 +150,7 @@ public void Should_not_throw_when_specified_exception_predicate_is_satisfied() .Handle(e => true) .RetryAsync(); - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().NotThrow(); } @@ -161,7 +162,7 @@ public void Should_not_throw_when_one_of_the_specified_exception_predicates_are_ .Or(e => true) .RetryAsync(); - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().NotThrow(); } @@ -225,7 +226,7 @@ public void Should_not_call_onretry_when_no_retries_are_performed() .Handle() .RetryAsync((_, retryCount) => retryCounts.Add(retryCount)); - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); retryCounts.Should() @@ -239,10 +240,10 @@ public void Should_create_new_state_for_each_call_to_policy() .Handle() .RetryAsync(); - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().NotThrow(); - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().NotThrow(); } @@ -273,7 +274,7 @@ public void Should_call_onretry_with_the_passed_context_when_execute_and_capture .Handle() .RetryAsync((_, __, context) => contextData = context); - policy.Awaiting(async p => await p.ExecuteAndCaptureAsync(ctx => { throw new DivideByZeroException(); }, + policy.Awaiting(p => p.ExecuteAndCaptureAsync(ctx => { throw new DivideByZeroException(); }, new { key1 = "value1", key2 = "value2" }.AsDictionary())) .Should().NotThrow(); @@ -291,7 +292,7 @@ public void Context_should_be_empty_if_execute_not_called_with_any_data() .Handle() .RetryAsync((_, __, context) => capturedContext = context); - policy.Awaiting(async x => await x.RaiseExceptionAsync()).Should().NotThrow(); + policy.Awaiting(x => x.RaiseExceptionAsync()).Should().NotThrow(); capturedContext.Should() .BeEmpty(); @@ -328,13 +329,13 @@ public void Should_create_new_context_for_each_call_to_execute_and_capture() .Handle() .RetryAsync((_, __, context) => contextValue = context["key"].ToString()); - policy.Awaiting(async p => await p.ExecuteAndCaptureAsync(ctx => throw new DivideByZeroException(), + policy.Awaiting(p => p.ExecuteAndCaptureAsync(ctx => throw new DivideByZeroException(), new { key = "original_value" }.AsDictionary())) .Should().NotThrow(); contextValue.Should().Be("original_value"); - policy.Awaiting(async p => await p.ExecuteAndCaptureAsync(ctx => throw new DivideByZeroException(), + policy.Awaiting(p => p.ExecuteAndCaptureAsync(ctx => throw new DivideByZeroException(), new { key = "new_value" }.AsDictionary())) .Should().NotThrow(); @@ -352,7 +353,7 @@ public void Should_not_call_onretry_when_retry_count_is_zero() .Handle() .RetryAsync(0, onRetry); - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); retryInvoked.Should().BeFalse(); @@ -363,7 +364,7 @@ public void Should_not_call_onretry_when_retry_count_is_zero() [Fact] public void Should_wait_asynchronously_for_async_onretry_delegate() { - // This test relates to https://github.com/App-vNext/Polly/issues/107. + // This test relates to https://github.com/App-vNext/Polly/issues/107. // An async (...) => { ... } anonymous delegate with no return type may compile to either an async void or an async Task method; which assign to an Action<...> or Func<..., Task> respectively. However, if it compiles to async void (assigning to Action<...>), then the delegate, when run, will return at the first await, and execution continues without waiting for the Action to complete, as described by Stephen Toub: http://blogs.msdn.com/b/pfxteam/archive/2012/02/08/10265476.aspx // If Polly were to declare only an Action<...> delegate for onRetry - but users declared async () => { } onRetry delegates - the compiler would happily assign them to the Action<...>, but the next 'try' of the retry policy would/could occur before onRetry execution had completed. // This test ensures the relevant retry policy does have a Func<..., Task> form for onRetry, and that it is awaited before the next try commences. @@ -381,16 +382,16 @@ public void Should_wait_asynchronously_for_async_onretry_delegate() executeDelegateInvocationsWhenOnRetryExits = executeDelegateInvocations; }); - policy.Awaiting(async p => await p.ExecuteAsync(async () => + policy.Awaiting(p => p.ExecuteAsync(async () => { executeDelegateInvocations++; await TaskHelper.EmptyTask; throw new DivideByZeroException(); })).Should().Throw(); - + while (executeDelegateInvocationsWhenOnRetryExits == 0) { } // Wait for the onRetry delegate to complete. - - executeDelegateInvocationsWhenOnRetryExits.Should().Be(1); // If the async onRetry delegate is genuinely awaited, only one execution of the .Execute delegate should have occurred by the time onRetry completes. If the async onRetry delegate were instead assigned to an Action<...>, then onRetry will return, and the second action execution will commence, before await Task.Delay() completes, leaving executeDelegateInvocationsWhenOnRetryExits == 2. + + executeDelegateInvocationsWhenOnRetryExits.Should().Be(1); // If the async onRetry delegate is genuinely awaited, only one execution of the .Execute delegate should have occurred by the time onRetry completes. If the async onRetry delegate were instead assigned to an Action<...>, then onRetry will return, and the second action execution will commence, before await Task.Delay() completes, leaving executeDelegateInvocationsWhenOnRetryExits == 2. executeDelegateInvocations.Should().Be(2); } @@ -413,7 +414,7 @@ public void Should_execute_action_when_non_faulting_and_cancellationToken_not_ca AttemptDuringWhichToCancel = null, }; - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().NotThrow(); attemptsInvoked.Should().Be(1); @@ -438,7 +439,7 @@ public void Should_execute_all_tries_when_faulting_and_cancellationToken_not_can AttemptDuringWhichToCancel = null, }; - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw(); attemptsInvoked.Should().Be(1 + 3); @@ -465,7 +466,7 @@ public void Should_not_execute_action_when_cancellationToken_cancelled_before_ex cancellationTokenSource.Cancel(); - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); @@ -492,7 +493,7 @@ public void Should_report_cancellation_during_otherwise_non_faulting_action_exec ActionObservesCancellation = true }; - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); @@ -519,7 +520,7 @@ public void Should_report_cancellation_during_faulting_initial_action_execution_ ActionObservesCancellation = true }; - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); @@ -546,7 +547,7 @@ public void Should_report_cancellation_during_faulting_initial_action_execution_ ActionObservesCancellation = false }; - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); @@ -573,7 +574,7 @@ public void Should_report_cancellation_during_faulting_retried_action_execution_ ActionObservesCancellation = true }; - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); @@ -600,7 +601,7 @@ public void Should_report_cancellation_during_faulting_retried_action_execution_ ActionObservesCancellation = false }; - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); @@ -627,7 +628,7 @@ public void Should_report_cancellation_during_faulting_last_retry_execution_when ActionObservesCancellation = true }; - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); @@ -654,7 +655,7 @@ public void Should_report_faulting_from_faulting_last_retry_execution_when_user_ ActionObservesCancellation = false }; - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw(); attemptsInvoked.Should().Be(1 + 3); @@ -683,7 +684,7 @@ public void Should_report_cancellation_after_faulting_action_execution_and_cance ActionObservesCancellation = false }; - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); @@ -711,7 +712,8 @@ public void Should_execute_func_returning_value_when_cancellationToken_not_cance AttemptDuringWhichToCancel = null, }; - policy.Awaiting(async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true)) + Func action = async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true); + policy.Awaiting(action) .Should().NotThrow(); result.Should().BeTrue(); @@ -741,7 +743,8 @@ public void Should_honour_and_report_cancellation_during_func_execution() ActionObservesCancellation = true }; - policy.Awaiting(async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true)) + Func action = async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true); + policy.Awaiting(action) .Should().Throw().And.CancellationToken.Should().Be(cancellationToken); result.Should().Be(null); diff --git a/src/Polly.Specs/Retry/RetryForeverAsyncSpecs.cs b/src/Polly.Specs/Retry/RetryForeverAsyncSpecs.cs index 85d3d9f196..9bb29024ad 100644 --- a/src/Polly.Specs/Retry/RetryForeverAsyncSpecs.cs +++ b/src/Polly.Specs/Retry/RetryForeverAsyncSpecs.cs @@ -4,6 +4,7 @@ using System.Threading; using System.Threading.Tasks; using FluentAssertions; +using Polly.Retry; using Polly.Specs.Helpers; using Polly.Utilities; using Xunit; @@ -21,7 +22,7 @@ public void Should_not_throw_regardless_of_how_many_times_the_specified_exceptio .Handle() .RetryForeverAsync(); - policy.Awaiting(async x => await x.RaiseExceptionAsync(3)) + policy.Awaiting(x => x.RaiseExceptionAsync(3)) .Should().NotThrow(); } @@ -33,7 +34,7 @@ public void Should_not_throw_regardless_of_how_many_times_one_of_the_specified_e .Or() .RetryForeverAsync(); - policy.Awaiting(async x => await x.RaiseExceptionAsync(3)) + policy.Awaiting(x => x.RaiseExceptionAsync(3)) .Should().NotThrow(); } @@ -44,7 +45,7 @@ public void Should_throw_when_exception_thrown_is_not_the_specified_exception_ty .Handle() .RetryForeverAsync(); - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); } @@ -56,7 +57,7 @@ public void Should_throw_when_exception_thrown_is_not_one_of_the_specified_excep .Or() .RetryForeverAsync(); - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); } @@ -67,7 +68,7 @@ public void Should_throw_when_specified_exception_predicate_is_not_satisfied() .Handle(e => false) .RetryForeverAsync(); - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); } @@ -79,7 +80,7 @@ public void Should_throw_when_none_of_the_specified_exception_predicates_are_sat .Or(e => false) .RetryForeverAsync(); - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); } @@ -90,7 +91,7 @@ public void Should_not_throw_when_specified_exception_predicate_is_satisfied() .Handle(e => true) .RetryForeverAsync(); - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().NotThrow(); } @@ -102,7 +103,7 @@ public void Should_not_throw_when_one_of_the_specified_exception_predicates_are_ .Or(e => true) .RetryForeverAsync(); - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().NotThrow(); } @@ -167,7 +168,7 @@ public void Context_should_be_empty_if_execute_not_called_with_any_data() .Handle() .RetryForeverAsync((_, context) => capturedContext = context); - policy.Awaiting(async x => await x.RaiseExceptionAsync()).Should().NotThrow(); + policy.Awaiting(x => x.RaiseExceptionAsync()).Should().NotThrow(); capturedContext.Should() .BeEmpty(); @@ -204,7 +205,7 @@ public void Should_not_call_onretry_when_no_retries_are_performed() .Handle() .RetryForeverAsync(exception => retryExceptions.Add(exception)); - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); retryExceptions.Should() @@ -232,7 +233,7 @@ public void Should_wait_asynchronously_for_async_onretry_delegate() executeDelegateInvocationsWhenOnRetryExits = executeDelegateInvocations; }); - policy.Awaiting(async p => await p.ExecuteAsync(async () => + policy.Awaiting(p => p.ExecuteAsync(async () => { executeDelegateInvocations++; await TaskHelper.EmptyTask; @@ -264,7 +265,7 @@ public void Should_execute_action_when_non_faulting_and_cancellationToken_not_ca AttemptDuringWhichToCancel = null, }; - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().NotThrow(); attemptsInvoked.Should().Be(1); @@ -291,7 +292,7 @@ public void Should_not_execute_action_when_cancellationToken_cancelled_before_ex cancellationTokenSource.Cancel(); - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); @@ -318,7 +319,7 @@ public void Should_report_cancellation_during_otherwise_non_faulting_action_exec ActionObservesCancellation = true }; - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); @@ -345,7 +346,7 @@ public void Should_report_cancellation_during_faulting_initial_action_execution_ ActionObservesCancellation = true }; - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); @@ -372,7 +373,7 @@ public void Should_report_cancellation_during_faulting_initial_action_execution_ ActionObservesCancellation = false }; - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); @@ -399,7 +400,7 @@ public void Should_report_cancellation_during_faulting_retried_action_execution_ ActionObservesCancellation = true }; - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); @@ -426,7 +427,7 @@ public void Should_report_cancellation_during_faulting_retried_action_execution_ ActionObservesCancellation = false }; - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); @@ -457,7 +458,7 @@ public void Should_report_cancellation_after_faulting_action_execution_and_cance ActionObservesCancellation = false }; - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); @@ -485,7 +486,8 @@ public void Should_execute_func_returning_value_when_cancellationToken_not_cance AttemptDuringWhichToCancel = null, }; - policy.Awaiting(async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true)) + Func action = async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true); + policy.Awaiting(action) .Should().NotThrow(); result.Should().BeTrue(); @@ -515,7 +517,8 @@ public void Should_honour_and_report_cancellation_during_func_execution() ActionObservesCancellation = true }; - policy.Awaiting(async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true)) + Func action = async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true); + policy.Awaiting(action) .Should().Throw().And.CancellationToken.Should().Be(cancellationToken); result.Should().Be(null); diff --git a/src/Polly.Specs/Retry/RetryForeverSpecs.cs b/src/Polly.Specs/Retry/RetryForeverSpecs.cs index 3ebf7d9848..4cc043c93b 100644 --- a/src/Polly.Specs/Retry/RetryForeverSpecs.cs +++ b/src/Polly.Specs/Retry/RetryForeverSpecs.cs @@ -76,7 +76,7 @@ public void Should_throw_when_exception_thrown_is_not_the_specified_exception_ty .Handle() .RetryForeverAsync(); - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); } @@ -133,7 +133,7 @@ public void Should_not_throw_when_specified_exception_predicate_is_satisfied_asy .Handle(e => true) .RetryForeverAsync(); - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().NotThrow(); } @@ -157,7 +157,7 @@ public void Should_not_throw_when_one_of_the_specified_exception_predicates_are_ .Or(e => true) .RetryForeverAsync(); - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().NotThrow(); } diff --git a/src/Polly.Specs/Retry/RetryTResultSpecsAsync.cs b/src/Polly.Specs/Retry/RetryTResultSpecsAsync.cs index 4011a49f78..3b35c3dcc8 100644 --- a/src/Polly.Specs/Retry/RetryTResultSpecsAsync.cs +++ b/src/Polly.Specs/Retry/RetryTResultSpecsAsync.cs @@ -501,7 +501,7 @@ public void Should_not_execute_action_when_cancellationToken_cancelled_before_ex cancellationTokenSource.Cancel(); - policy.Awaiting(async x => await x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, + policy.Awaiting(x => x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Fault, @@ -531,7 +531,7 @@ public void Should_report_cancellation_during_otherwise_non_faulting_action_exec ActionObservesCancellation = true }; - policy.Awaiting(async x => await x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, + policy.Awaiting(x => x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Good, ResultPrimitive.Good, ResultPrimitive.Good, @@ -561,7 +561,7 @@ public void Should_report_cancellation_during_faulting_initial_action_execution_ ActionObservesCancellation = true }; - policy.Awaiting(async x => await x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, + policy.Awaiting(x => x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Fault, @@ -591,7 +591,7 @@ public void Should_report_cancellation_during_faulting_initial_action_execution_ ActionObservesCancellation = false }; - policy.Awaiting(async x => await x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, + policy.Awaiting(x => x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Fault, @@ -621,7 +621,7 @@ public void Should_report_cancellation_during_faulting_retried_action_execution_ ActionObservesCancellation = true }; - policy.Awaiting(async x => await x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, + policy.Awaiting(x => x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Fault, @@ -651,7 +651,7 @@ public void Should_report_cancellation_during_faulting_retried_action_execution_ ActionObservesCancellation = false }; - policy.Awaiting(async x => await x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, + policy.Awaiting(x => x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Fault, @@ -681,7 +681,7 @@ public void Should_report_cancellation_during_faulting_last_retry_execution_when ActionObservesCancellation = true }; - policy.Awaiting(async x => await x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, + policy.Awaiting(x => x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Fault, @@ -745,7 +745,7 @@ public void Should_report_cancellation_after_faulting_action_execution_and_cance ActionObservesCancellation = false }; - policy.Awaiting(async x => await x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, + policy.Awaiting(x => x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Fault, diff --git a/src/Polly.Specs/Retry/WaitAndRetryAsyncSpecs.cs b/src/Polly.Specs/Retry/WaitAndRetryAsyncSpecs.cs index 754ee1de1f..0598220381 100644 --- a/src/Polly.Specs/Retry/WaitAndRetryAsyncSpecs.cs +++ b/src/Polly.Specs/Retry/WaitAndRetryAsyncSpecs.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; using FluentAssertions; using FluentAssertions.Extensions; +using Polly.Retry; using Polly.Specs.Helpers; using Polly.Utilities; using Xunit; @@ -48,7 +49,7 @@ public void Should_throw_when_onretry_action_is_null_without_context() policy.Should().Throw().And .ParamName.Should().Be("onRetry"); } - + [Fact] public void Should_not_throw_when_specified_exception_thrown_same_number_of_times_as_there_are_sleep_durations() { @@ -61,7 +62,7 @@ public void Should_not_throw_when_specified_exception_thrown_same_number_of_time 3.Seconds() }); - policy.Awaiting(async x => await x.RaiseExceptionAsync(3)) + policy.Awaiting(x => x.RaiseExceptionAsync(3)) .Should().NotThrow(); } @@ -78,7 +79,7 @@ public void Should_not_throw_when_one_of_the_specified_exceptions_thrown_same_nu 3.Seconds() }); - policy.Awaiting(async x => await x.RaiseExceptionAsync(3)) + policy.Awaiting(x => x.RaiseExceptionAsync(3)) .Should().NotThrow(); } @@ -94,7 +95,7 @@ public void Should_not_throw_when_specified_exception_thrown_less_number_of_time 3.Seconds() }); - policy.Awaiting(async x => await x.RaiseExceptionAsync(2)) + policy.Awaiting(x => x.RaiseExceptionAsync(2)) .Should().NotThrow(); } @@ -111,7 +112,7 @@ public void Should_not_throw_when_one_of_the_specified_exceptions_thrown_less_nu 3.Seconds() }); - policy.Awaiting(async x => await x.RaiseExceptionAsync(2)) + policy.Awaiting(x => x.RaiseExceptionAsync(2)) .Should().NotThrow(); } @@ -127,7 +128,7 @@ public void Should_throw_when_specified_exception_thrown_more_times_than_there_a 3.Seconds() }); - policy.Awaiting(async x => await x.RaiseExceptionAsync(3 + 1)) + policy.Awaiting(x => x.RaiseExceptionAsync(3 + 1)) .Should().Throw(); } @@ -144,7 +145,7 @@ public void Should_throw_when_one_of_the_specified_exceptions_are_thrown_more_ti 3.Seconds() }); - policy.Awaiting(async x => await x.RaiseExceptionAsync(3 + 1)) + policy.Awaiting(x => x.RaiseExceptionAsync(3 + 1)) .Should().Throw(); } @@ -155,7 +156,7 @@ public void Should_throw_when_exception_thrown_is_not_the_specified_exception_ty .Handle() .WaitAndRetryAsync(Enumerable.Empty()); - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); } @@ -167,7 +168,7 @@ public void Should_throw_when_exception_thrown_is_not_one_of_the_specified_excep .Or() .WaitAndRetryAsync(Enumerable.Empty()); - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); } @@ -178,7 +179,7 @@ public void Should_throw_when_specified_exception_predicate_is_not_satisfied() .Handle(e => false) .WaitAndRetryAsync(Enumerable.Empty()); - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); } @@ -190,7 +191,7 @@ public void Should_throw_when_none_of_the_specified_exception_predicates_are_sat .Or(e => false) .WaitAndRetryAsync(Enumerable.Empty()); - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); } @@ -204,7 +205,7 @@ public void Should_not_throw_when_specified_exception_predicate_is_satisfied() 1.Seconds() }); - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().NotThrow(); } @@ -219,7 +220,7 @@ public void Should_not_throw_when_one_of_the_specified_exception_predicates_are_ 1.Seconds() }); - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().NotThrow(); } @@ -269,7 +270,7 @@ public void Should_sleep_for_the_specified_duration_each_retry_when_specified_ex return TaskHelper.EmptyTask; }; - policy.Awaiting(async x => await x.RaiseExceptionAsync(3 + 1)) + policy.Awaiting(x => x.RaiseExceptionAsync(3 + 1)) .Should().Throw(); totalTimeSlept.Should() @@ -317,7 +318,7 @@ public void Should_not_sleep_if_no_retries() return TaskHelper.EmptyTask; }; - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); totalTimeSlept.Should() @@ -329,8 +330,8 @@ public async Task Should_call_onretry_on_each_retry_with_the_current_timespan() { var expectedRetryWaits = new [] { - 1.Seconds(), - 2.Seconds(), + 1.Seconds(), + 2.Seconds(), 3.Seconds() }; @@ -404,7 +405,7 @@ public void Should_not_call_onretry_when_no_retries_are_performed() .Handle() .WaitAndRetryAsync(Enumerable.Empty(), (exception, _) => retryExceptions.Add(exception)); - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); retryExceptions.Should().BeEmpty(); @@ -420,10 +421,10 @@ public void Should_create_new_state_for_each_call_to_policy() 1.Seconds() }); - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().NotThrow(); - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().NotThrow(); } @@ -463,7 +464,7 @@ public void Context_should_be_empty_if_execute_not_called_with_any_data() 2.Seconds(), 3.Seconds() }, (_, __, context) => capturedContext = context); - policy.Awaiting(async x => await x.RaiseExceptionAsync()).Should().NotThrow(); + policy.Awaiting(x => x.RaiseExceptionAsync()).Should().NotThrow(); capturedContext.Should() .BeEmpty(); @@ -503,8 +504,8 @@ public void Should_throw_when_retry_count_is_less_than_zero_without_context() Action policy = () => Policy .Handle() .WaitAndRetryAsync(-1, _ => TimeSpan.Zero, onRetry); - - policy.Should().Throw().And + + policy.Should().Throw().And .ParamName.Should().Be("retryCount"); } @@ -539,19 +540,19 @@ public async Task Should_calculate_retry_timespans_from_current_retry_attempt_an { var expectedRetryWaits = new[] { - 2.Seconds(), - 4.Seconds(), - 8.Seconds(), - 16.Seconds(), - 32.Seconds() + 2.Seconds(), + 4.Seconds(), + 8.Seconds(), + 16.Seconds(), + 32.Seconds() }; var actualRetryWaits = new List(); var policy = Policy .Handle() - .WaitAndRetryAsync(5, - retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), + .WaitAndRetryAsync(5, + retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (_, timeSpan) => actualRetryWaits.Add(timeSpan) ); @@ -625,7 +626,7 @@ public async Task Should_be_able_to_pass_retry_duration_from_execution_to_sleepD var policy = Policy .Handle() - .WaitAndRetryAsync(1, + .WaitAndRetryAsync(1, sleepDurationProvider: (retryAttempt, context) => context.ContainsKey("RetryAfter") ? (TimeSpan)context["RetryAfter"] : defaultRetryAfter, // Set sleep duration from Context, when available. onRetry: (_, timeSpan, __) => actualRetryDuration = timeSpan // Capture the actual sleep duration that was used, for test verification purposes. ); @@ -660,7 +661,7 @@ public void Should_not_call_onretry_when_retry_count_is_zero() .Handle() .WaitAndRetryAsync(0, retryAttempt => TimeSpan.FromSeconds(1), onRetry); - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); retryInvoked.Should().BeFalse(); @@ -669,7 +670,7 @@ public void Should_not_call_onretry_when_retry_count_is_zero() [Fact] public void Should_wait_asynchronously_for_async_onretry_delegate() { - // This test relates to https://github.com/App-vNext/Polly/issues/107. + // This test relates to https://github.com/App-vNext/Polly/issues/107. // An async (...) => { ... } anonymous delegate with no return type may compile to either an async void or an async Task method; which assign to an Action<...> or Func<..., Task> respectively. However, if it compiles to async void (assigning tp Action<...>), then the delegate, when run, will return at the first await, and execution continues without waiting for the Action to complete, as described by Stephen Toub: http://blogs.msdn.com/b/pfxteam/archive/2012/02/08/10265476.aspx // If Polly were to declare only an Action<...> delegate for onRetry - but users declared async () => { } onRetry delegates - the compiler would happily assign them to the Action<...>, but the next 'try' would/could occur before onRetry execution had completed. // This test ensures the relevant retry policy does have a Func<..., Task> form for onRetry, and that it is awaited before the next try commences. @@ -681,7 +682,7 @@ public void Should_wait_asynchronously_for_async_onretry_delegate() var policy = Policy .Handle() - .WaitAndRetryAsync(1, + .WaitAndRetryAsync(1, _ => TimeSpan.Zero, async (ex, timespan) => { @@ -689,16 +690,16 @@ public void Should_wait_asynchronously_for_async_onretry_delegate() executeDelegateInvocationsWhenOnRetryExits = executeDelegateInvocations; }); - policy.Awaiting(async p => await p.ExecuteAsync(async () => + policy.Awaiting(p => p.ExecuteAsync(async () => { executeDelegateInvocations++; await TaskHelper.EmptyTask; - throw new DivideByZeroException(); + throw new DivideByZeroException(); })).Should().Throw(); while (executeDelegateInvocationsWhenOnRetryExits == 0) { } // Wait for the onRetry delegate to complete. - executeDelegateInvocationsWhenOnRetryExits.Should().Be(1); // If the async onRetry delegate is genuinely awaited, only one execution of the .Execute delegate should have occurred by the time onRetry completes. If the async onRetry delegate were instead assigned to an Action<...>, then onRetry will return, and the second action execution will commence, before await Task.Delay() completes, leaving executeDelegateInvocationsWhenOnRetryExits == 2. + executeDelegateInvocationsWhenOnRetryExits.Should().Be(1); // If the async onRetry delegate is genuinely awaited, only one execution of the .Execute delegate should have occurred by the time onRetry completes. If the async onRetry delegate were instead assigned to an Action<...>, then onRetry will return, and the second action execution will commence, before await Task.Delay() completes, leaving executeDelegateInvocationsWhenOnRetryExits == 2. executeDelegateInvocations.Should().Be(2); } @@ -721,7 +722,7 @@ public void Should_execute_action_when_non_faulting_and_cancellationToken_not_ca AttemptDuringWhichToCancel = null, }; - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().NotThrow(); attemptsInvoked.Should().Be(1); @@ -746,7 +747,7 @@ public void Should_execute_all_tries_when_faulting_and_cancellationToken_not_can AttemptDuringWhichToCancel = null, }; - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw(); attemptsInvoked.Should().Be(1 + 3); @@ -773,7 +774,7 @@ public void Should_not_execute_action_when_cancellationToken_cancelled_before_ex cancellationTokenSource.Cancel(); - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); @@ -800,7 +801,7 @@ public void Should_report_cancellation_during_otherwise_non_faulting_action_exec ActionObservesCancellation = true }; - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); @@ -827,11 +828,11 @@ public void Should_report_cancellation_during_faulting_initial_action_execution_ ActionObservesCancellation = true }; - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); - attemptsInvoked.Should().Be(1); + attemptsInvoked.Should().Be(1); } [Fact] @@ -854,7 +855,7 @@ public void Should_report_cancellation_during_faulting_initial_action_execution_ ActionObservesCancellation = false }; - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); @@ -881,7 +882,7 @@ public void Should_report_cancellation_during_faulting_retried_action_execution_ ActionObservesCancellation = true }; - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); @@ -908,7 +909,7 @@ public void Should_report_cancellation_during_faulting_retried_action_execution_ ActionObservesCancellation = false }; - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); @@ -935,7 +936,7 @@ public void Should_report_cancellation_during_faulting_last_retry_execution_when ActionObservesCancellation = true }; - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); @@ -962,7 +963,7 @@ public void Should_report_faulting_from_faulting_last_retry_execution_when_user_ ActionObservesCancellation = false }; - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw(); attemptsInvoked.Should().Be(1 + 3); @@ -998,7 +999,7 @@ public void Should_honour_cancellation_immediately_during_wait_phase_of_waitandr cancellationTokenSource.CancelAfter(shimTimeSpan); - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); watch.Stop(); @@ -1033,7 +1034,7 @@ public void Should_report_cancellation_after_faulting_action_execution_and_cance ActionObservesCancellation = false }; - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); @@ -1061,7 +1062,8 @@ public void Should_execute_func_returning_value_when_cancellationToken_not_cance AttemptDuringWhichToCancel = null }; - policy.Awaiting(async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true)) + Func action = async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true); + policy.Awaiting(action) .Should().NotThrow(); result.Should().BeTrue(); @@ -1091,14 +1093,15 @@ public void Should_honour_and_report_cancellation_during_func_execution() ActionObservesCancellation = true }; - policy.Awaiting(async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true)) + Func action = async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true); + policy.Awaiting(action) .Should().Throw().And.CancellationToken.Should().Be(cancellationToken); result.Should().Be(null); attemptsInvoked.Should().Be(1); } - + public void Dispose() { SystemClock.Reset(); diff --git a/src/Polly.Specs/Retry/WaitAndRetryForeverAsyncSpecs.cs b/src/Polly.Specs/Retry/WaitAndRetryForeverAsyncSpecs.cs index 33add2f26c..abf1e3efcf 100644 --- a/src/Polly.Specs/Retry/WaitAndRetryForeverAsyncSpecs.cs +++ b/src/Polly.Specs/Retry/WaitAndRetryForeverAsyncSpecs.cs @@ -5,6 +5,7 @@ using System.Threading.Tasks; using FluentAssertions; using FluentAssertions.Extensions; +using Polly.Retry; using Polly.Specs.Helpers; using Polly.Utilities; using Xunit; @@ -83,7 +84,7 @@ public void Should_not_throw_regardless_of_how_many_times_the_specified_exceptio .Handle() .WaitAndRetryForeverAsync(_ => TimeSpan.Zero); - policy.Awaiting(async x => await x.RaiseExceptionAsync(3)) + policy.Awaiting(x => x.RaiseExceptionAsync(3)) .Should().NotThrow(); } @@ -95,7 +96,7 @@ public void Should_not_throw_regardless_of_how_many_times_one_of_the_specified_e .Or() .WaitAndRetryForeverAsync(_ => TimeSpan.Zero); - policy.Awaiting(async x => await x.RaiseExceptionAsync(3)) + policy.Awaiting(x => x.RaiseExceptionAsync(3)) .Should().NotThrow(); } @@ -108,7 +109,7 @@ public void Should_throw_when_exception_thrown_is_not_the_specified_exception_ty .Handle() .WaitAndRetryForeverAsync(provider); - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); } @@ -122,7 +123,7 @@ public void Should_throw_when_exception_thrown_is_not_one_of_the_specified_excep .Or() .WaitAndRetryForeverAsync(provider); - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); } @@ -135,7 +136,7 @@ public void Should_throw_when_specified_exception_predicate_is_not_satisfied() .Handle(e => false) .WaitAndRetryForeverAsync(provider); - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); } @@ -149,7 +150,7 @@ public void Should_throw_when_none_of_the_specified_exception_predicates_are_sat .Or(e => false) .WaitAndRetryForeverAsync(provider); - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); } @@ -162,7 +163,7 @@ public void Should_not_throw_when_specified_exception_predicate_is_satisfied() .Handle(e => true) .WaitAndRetryForeverAsync(provider); - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().NotThrow(); } @@ -176,7 +177,7 @@ public void Should_not_throw_when_one_of_the_specified_exception_predicates_are_ .Or(e => true) .WaitAndRetryForeverAsync(provider); - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().NotThrow(); } @@ -193,7 +194,7 @@ public void Should_not_sleep_if_no_retries() SystemClock.Sleep = (span, ct) => totalTimeSlept += span.Seconds; - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); totalTimeSlept.Should() @@ -246,7 +247,7 @@ public void Should_not_call_onretry_when_no_retries_are_performed() .Handle() .WaitAndRetryForeverAsync(provider, (excpetion, _) => retryExceptions.Add(excpetion)); - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); retryExceptions.Should().BeEmpty(); @@ -375,7 +376,7 @@ await policy.ExecuteAsync(async (context, ct) => [Fact] public void Should_wait_asynchronously_for_async_onretry_delegate() { - // This test relates to https://github.com/App-vNext/Polly/issues/107. + // This test relates to https://github.com/App-vNext/Polly/issues/107. // An async (...) => { ... } anonymous delegate with no return type may compile to either an async void or an async Task method; which assign to an Action<...> or Func<..., Task> respectively. However, if it compiles to async void (assigning tp Action<...>), then the delegate, when run, will return at the first await, and execution continues without waiting for the Action to complete, as described by Stephen Toub: http://blogs.msdn.com/b/pfxteam/archive/2012/02/08/10265476.aspx // If Polly were to declare only an Action<...> delegate for onRetry - but users declared async () => { } onRetry delegates - the compiler would happily assign them to the Action<...>, but the next 'try' would/could occur before onRetry execution had completed. // This test ensures the relevant retry policy does have a Func<..., Task> form for onRetry, and that it is awaited before the next try commences. @@ -388,14 +389,14 @@ public void Should_wait_asynchronously_for_async_onretry_delegate() var policy = Policy .Handle() .WaitAndRetryForeverAsync( - _ => TimeSpan.Zero, + _ => TimeSpan.Zero, async (ex, timespan) => { await Task.Delay(shimTimeSpan); executeDelegateInvocationsWhenOnRetryExits = executeDelegateInvocations; }); - policy.Awaiting(async p => await p.ExecuteAsync(async () => + policy.Awaiting(p => p.ExecuteAsync(async () => { executeDelegateInvocations++; await TaskHelper.EmptyTask; @@ -404,7 +405,7 @@ public void Should_wait_asynchronously_for_async_onretry_delegate() while (executeDelegateInvocationsWhenOnRetryExits == 0) { } // Wait for the onRetry delegate to complete. - executeDelegateInvocationsWhenOnRetryExits.Should().Be(1); // If the async onRetry delegate is genuinely awaited, only one execution of the .Execute delegate should have occurred by the time onRetry completes. If the async onRetry delegate were instead assigned to an Action<...>, then onRetry will return, and the second action execution will commence, before await Task.Delay() completes, leaving executeDelegateInvocationsWhenOnRetryExits == 2. + executeDelegateInvocationsWhenOnRetryExits.Should().Be(1); // If the async onRetry delegate is genuinely awaited, only one execution of the .Execute delegate should have occurred by the time onRetry completes. If the async onRetry delegate were instead assigned to an Action<...>, then onRetry will return, and the second action execution will commence, before await Task.Delay() completes, leaving executeDelegateInvocationsWhenOnRetryExits == 2. executeDelegateInvocations.Should().Be(2); } @@ -428,7 +429,7 @@ public void Should_execute_action_when_non_faulting_and_cancellationToken_not_ca AttemptDuringWhichToCancel = null, }; - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().NotThrow(); attemptsInvoked.Should().Be(1); @@ -457,7 +458,7 @@ public void Should_not_execute_action_when_cancellationToken_cancelled_before_ex cancellationTokenSource.Cancel(); - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); @@ -486,7 +487,7 @@ public void Should_report_cancellation_during_otherwise_non_faulting_action_exec ActionObservesCancellation = true }; - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); @@ -515,7 +516,7 @@ public void Should_report_cancellation_during_faulting_initial_action_execution_ ActionObservesCancellation = true }; - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); @@ -544,7 +545,7 @@ public void Should_report_cancellation_during_faulting_initial_action_execution_ ActionObservesCancellation = false }; - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); @@ -573,7 +574,7 @@ public void Should_report_cancellation_during_faulting_retried_action_execution_ ActionObservesCancellation = true }; - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); @@ -602,7 +603,7 @@ public void Should_report_cancellation_during_faulting_retried_action_execution_ ActionObservesCancellation = false }; - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); @@ -635,7 +636,7 @@ public void Should_report_cancellation_after_faulting_action_execution_and_cance ActionObservesCancellation = false }; - policy.Awaiting(async x => await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); @@ -664,8 +665,8 @@ public void Should_execute_func_returning_value_when_cancellationToken_not_cance AttemptDuringWhichToCancel = null, }; - policy.Awaiting(async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true)) - .Should().NotThrow(); + Func action = async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true); + policy.Awaiting(action).Should().NotThrow(); result.Should().BeTrue(); @@ -696,7 +697,8 @@ public void Should_honour_and_report_cancellation_during_func_execution() ActionObservesCancellation = true }; - policy.Awaiting(async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true)) + Func action = async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true); + policy.Awaiting(action) .Should().Throw().And.CancellationToken.Should().Be(cancellationToken); result.Should().Be(null); diff --git a/src/Polly.Specs/Retry/WaitAndRetrySpecs.cs b/src/Polly.Specs/Retry/WaitAndRetrySpecs.cs index de35aad435..65787cdaec 100644 --- a/src/Polly.Specs/Retry/WaitAndRetrySpecs.cs +++ b/src/Polly.Specs/Retry/WaitAndRetrySpecs.cs @@ -216,7 +216,7 @@ public void Should_throw_when_exception_thrown_is_not_the_specified_exception_ty .Handle() .WaitAndRetryAsync(Enumerable.Empty()); - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); } @@ -240,7 +240,7 @@ public void Should_throw_when_exception_thrown_is_not_one_of_the_specified_excep .Or() .WaitAndRetryAsync(Enumerable.Empty()); - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().Throw(); } @@ -291,7 +291,7 @@ public void Should_not_throw_when_specified_exception_predicate_is_satisfied_asy 1.Seconds() }); - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().NotThrow(); } @@ -321,7 +321,7 @@ public void Should_not_throw_when_one_of_the_specified_exception_predicates_are_ 1.Seconds() }); - policy.Awaiting(async x => await x.RaiseExceptionAsync()) + policy.Awaiting(x => x.RaiseExceptionAsync()) .Should().NotThrow(); } diff --git a/src/Polly.Specs/Timeout/TimeoutAsyncSpecs.cs b/src/Polly.Specs/Timeout/TimeoutAsyncSpecs.cs index 484d16e12c..632297a00c 100644 --- a/src/Polly.Specs/Timeout/TimeoutAsyncSpecs.cs +++ b/src/Polly.Specs/Timeout/TimeoutAsyncSpecs.cs @@ -205,7 +205,7 @@ public void Should_throw_when_timeout_is_less_than_execution_duration__pessimist var policy = Policy.TimeoutAsync(timeout, TimeoutStrategy.Pessimistic); - policy.Awaiting(async p => await p.ExecuteAsync(async () => + policy.Awaiting(p => p.ExecuteAsync(async () => { await SystemClock.SleepAsync(TimeSpan.FromSeconds(3), CancellationToken.None); @@ -239,7 +239,7 @@ public void Should_throw_timeout_after_correct_duration__pessimistic() TimeSpan tolerance = TimeSpan.FromSeconds(3); // Consider increasing tolerance, if test fails transiently in different test/build environments. watch.Start(); - policy.Awaiting(async p => await p.ExecuteAsync(async () => + policy.Awaiting(p => p.ExecuteAsync(async () => { await SystemClock.SleepAsync(TimeSpan.FromSeconds(10), CancellationToken.None); @@ -271,7 +271,7 @@ public void Should_throw_when_timeout_is_less_than_execution_duration__optimisti var policy = Policy.TimeoutAsync(timeout, TimeoutStrategy.Optimistic); var userCancellationToken = CancellationToken.None; - policy.Awaiting(async p => await p.ExecuteAsync(async ct => + policy.Awaiting(p => p.ExecuteAsync(async ct => { await SystemClock.SleepAsync(TimeSpan.FromSeconds(3), ct); @@ -310,7 +310,7 @@ public void Should_throw_timeout_after_correct_duration__optimistic() TimeSpan tolerance = TimeSpan.FromSeconds(3); // Consider increasing tolerance, if test fails transiently in different test/build environments. watch.Start(); - policy.Awaiting(async p => await p.ExecuteAsync(async ct => + policy.Awaiting(p => p.ExecuteAsync(async ct => { await SystemClock.SleepAsync(TimeSpan.FromSeconds(10), ct); @@ -341,7 +341,7 @@ public void Should_not_be_able_to_cancel_with_unobserved_user_cancellation_token using (CancellationTokenSource userTokenSource = new CancellationTokenSource()) { - policy.Awaiting(async p => await p.ExecuteAsync(async + policy.Awaiting(p => p.ExecuteAsync(async _ => { userTokenSource.Cancel(); // User token cancels in the middle of execution ... await SystemClock.SleepAsync(TimeSpan.FromSeconds(timeout * 2), @@ -363,7 +363,7 @@ public void Should_not_execute_user_delegate_if_user_cancellationToken_cancelled { cts.Cancel(); - policy.Awaiting(async p => await p.ExecuteAsync(ct => + policy.Awaiting(p => p.ExecuteAsync(ct => { executed = true; return TaskHelper.EmptyTask; @@ -386,7 +386,7 @@ public void Should_be_able_to_cancel_with_user_cancellation_token_before_timeout using (CancellationTokenSource userTokenSource = new CancellationTokenSource()) { - policy.Awaiting(async p => await p.ExecuteAsync( + policy.Awaiting(p => p.ExecuteAsync( ct => { userTokenSource.Cancel(); ct.ThrowIfCancellationRequested(); // Simulate cancel in the middle of execution return TaskHelper.EmptyTask; @@ -406,7 +406,7 @@ public void Should_not_execute_user_delegate_if_user_cancellationToken_cancelled { cts.Cancel(); - policy.Awaiting(async p => await p.ExecuteAsync(ct => + policy.Awaiting(p => p.ExecuteAsync(ct => { executed = true; return TaskHelper.EmptyTask; @@ -470,7 +470,7 @@ public void Should_call_ontimeout_with_configured_timeout__pessimistic() var policy = Policy.TimeoutAsync(timeoutPassedToConfiguration, TimeoutStrategy.Pessimistic, onTimeoutAsync); - policy.Awaiting(async p => await p.ExecuteAsync(async () => + policy.Awaiting(p => p.ExecuteAsync(async () => { await SystemClock.SleepAsync(TimeSpan.FromSeconds(3), CancellationToken.None); @@ -496,7 +496,7 @@ public void Should_call_ontimeout_with_passed_context__pessimistic() TimeSpan timeout = TimeSpan.FromMilliseconds(250); var policy = Policy.TimeoutAsync(timeout, TimeoutStrategy.Pessimistic, onTimeoutAsync); - policy.Awaiting(async p => await p.ExecuteAsync(async ctx => + policy.Awaiting(p => p.ExecuteAsync(async ctx => { await SystemClock.SleepAsync(TimeSpan.FromSeconds(3), CancellationToken.None); @@ -525,7 +525,7 @@ public void Should_call_ontimeout_with_timeout_supplied_different_for_each_execu var policy = Policy.TimeoutAsync(timeoutFunc, TimeoutStrategy.Pessimistic, onTimeoutAsync); - policy.Awaiting(async p => await p.ExecuteAsync(async () => + policy.Awaiting(p => p.ExecuteAsync(async () => { await SystemClock.SleepAsync(TimeSpan.FromSeconds(3), CancellationToken.None); })) @@ -554,7 +554,7 @@ public void Should_call_ontimeout_with_timeout_supplied_different_for_each_execu // Supply a programatically-controlled timeout, via the execution context. Context context = new Context("SomeOperationKey") { ["timeout"] = TimeSpan.FromMilliseconds(25 * programaticallyControlledDelay) }; - policy.Awaiting(async p => await p.ExecuteAsync(async ctx => + policy.Awaiting(p => p.ExecuteAsync(async ctx => { await SystemClock.SleepAsync(TimeSpan.FromSeconds(3), CancellationToken.None); }, context)) @@ -576,7 +576,7 @@ public void Should_call_ontimeout_with_task_wrapping_abandoned_action__pessimist TimeSpan timeout = TimeSpan.FromMilliseconds(250); var policy = Policy.TimeoutAsync(timeout, TimeoutStrategy.Pessimistic, onTimeoutAsync); - policy.Awaiting(async p => await p.ExecuteAsync(async () => + policy.Awaiting(p => p.ExecuteAsync(async () => { await SystemClock.SleepAsync(TimeSpan.FromSeconds(3), CancellationToken.None); })) @@ -605,7 +605,7 @@ public async Task Should_call_ontimeout_with_task_wrapping_abandoned_action_allo TimeSpan thriceShimTimeSpan = shimTimespan + shimTimespan + shimTimespan; var policy = Policy.TimeoutAsync(shimTimespan, TimeoutStrategy.Pessimistic, onTimeoutAsync); - policy.Awaiting(async p => await p.ExecuteAsync(async () => + policy.Awaiting(p => p.ExecuteAsync(async () => { await SystemClock.SleepAsync(thriceShimTimeSpan, CancellationToken.None); throw exceptionToThrow; @@ -632,7 +632,7 @@ public void Should_call_ontimeout_with_timing_out_exception__pessimistic() var policy = Policy.TimeoutAsync(timeoutPassedToConfiguration, TimeoutStrategy.Pessimistic, onTimeoutAsync); - policy.Awaiting(async p => await p.ExecuteAsync(async () => + policy.Awaiting(p => p.ExecuteAsync(async () => { await SystemClock.SleepAsync(TimeSpan.FromSeconds(3), CancellationToken.None); @@ -662,7 +662,7 @@ public void Should_call_ontimeout_with_configured_timeout__optimistic() var policy = Policy.TimeoutAsync(timeoutPassedToConfiguration, TimeoutStrategy.Optimistic, onTimeoutAsync); var userCancellationToken = CancellationToken.None; - policy.Awaiting(async p => await p.ExecuteAsync(async ct => + policy.Awaiting(p => p.ExecuteAsync(async ct => { await SystemClock.SleepAsync(TimeSpan.FromSeconds(3), ct); @@ -689,7 +689,7 @@ public void Should_call_ontimeout_with_passed_context__optimistic() var policy = Policy.TimeoutAsync(timeout, TimeoutStrategy.Optimistic, onTimeoutAsync); var userCancellationToken = CancellationToken.None; - policy.Awaiting(async p => await p.ExecuteAsync(async (ctx, ct) => + policy.Awaiting(p => p.ExecuteAsync(async (ctx, ct) => { await SystemClock.SleepAsync(TimeSpan.FromSeconds(3), ct); @@ -719,7 +719,7 @@ public void Should_call_ontimeout_with_timeout_supplied_different_for_each_execu var policy = Policy.TimeoutAsync(timeoutFunc, TimeoutStrategy.Optimistic, onTimeoutAsync); var userCancellationToken = CancellationToken.None; - policy.Awaiting(async p => await p.ExecuteAsync(async ct => + policy.Awaiting(p => p.ExecuteAsync(async ct => { await SystemClock.SleepAsync(TimeSpan.FromSeconds(3), ct); @@ -753,7 +753,7 @@ public void Should_call_ontimeout_with_timeout_supplied_different_for_each_execu ["timeout"] = TimeSpan.FromMilliseconds(25 * programaticallyControlledDelay) }; - policy.Awaiting(async p => await p.ExecuteAsync(async (ctx, ct) => + policy.Awaiting(p => p.ExecuteAsync(async (ctx, ct) => { await SystemClock.SleepAsync(TimeSpan.FromSeconds(3), ct); @@ -777,7 +777,7 @@ public void Should_call_ontimeout_but_not_with_task_wrapping_abandoned_action__o var policy = Policy.TimeoutAsync(timeout, TimeoutStrategy.Optimistic, onTimeoutAsync); var userCancellationToken = CancellationToken.None; - policy.Awaiting(async p => await p.ExecuteAsync(async ct => + policy.Awaiting(p => p.ExecuteAsync(async ct => { await SystemClock.SleepAsync(TimeSpan.FromSeconds(3), ct); @@ -802,7 +802,7 @@ public void Should_call_ontimeout_with_timing_out_exception__optimistic() var policy = Policy.TimeoutAsync(timeoutPassedToConfiguration, TimeoutStrategy.Optimistic, onTimeoutAsync); var userCancellationToken = CancellationToken.None; - policy.Awaiting(async p => await p.ExecuteAsync(async ct => + policy.Awaiting(p => p.ExecuteAsync(async ct => { await SystemClock.SleepAsync(TimeSpan.FromSeconds(3), ct); diff --git a/src/Polly.Specs/Timeout/TimeoutTResultAsyncSpecs.cs b/src/Polly.Specs/Timeout/TimeoutTResultAsyncSpecs.cs index 4905c42285..0647a96f1b 100644 --- a/src/Polly.Specs/Timeout/TimeoutTResultAsyncSpecs.cs +++ b/src/Polly.Specs/Timeout/TimeoutTResultAsyncSpecs.cs @@ -206,7 +206,7 @@ public void Should_throw_when_timeout_is_less_than_execution_duration__pessimist var policy = Policy.TimeoutAsync(timeout, TimeoutStrategy.Pessimistic); - policy.Awaiting(async p => await p.ExecuteAsync(async () => + policy.Awaiting(p => p.ExecuteAsync(async () => { await SystemClock.SleepAsync(TimeSpan.FromSeconds(3), CancellationToken.None); return ResultPrimitive.WhateverButTooLate; @@ -237,7 +237,7 @@ public void Should_throw_timeout_after_correct_duration__pessimistic() TimeSpan tolerance = TimeSpan.FromSeconds(3); // Consider increasing tolerance, if test fails transiently in different test/build environments. watch.Start(); - policy.Awaiting(async p => await p.ExecuteAsync(async () => + policy.Awaiting(p => p.ExecuteAsync(async () => { await SystemClock.SleepAsync(TimeSpan.FromSeconds(10), CancellationToken.None); return ResultPrimitive.WhateverButTooLate; @@ -266,7 +266,7 @@ public void Should_throw_when_timeout_is_less_than_execution_duration__optimisti var policy = Policy.TimeoutAsync(TimeSpan.FromMilliseconds(50), TimeoutStrategy.Optimistic); var userCancellationToken = CancellationToken.None; - policy.Awaiting(async p => await p.ExecuteAsync(async ct => + policy.Awaiting(p => p.ExecuteAsync(async ct => { await SystemClock.SleepAsync(TimeSpan.FromSeconds(3), ct); return ResultPrimitive.WhateverButTooLate; @@ -299,7 +299,7 @@ public void Should_throw_timeout_after_correct_duration__optimistic() TimeSpan tolerance = TimeSpan.FromSeconds(3); // Consider increasing tolerance, if test fails transiently in different test/build environments. watch.Start(); - policy.Awaiting(async p => await p.ExecuteAsync(async ct => + policy.Awaiting(p => p.ExecuteAsync(async ct => { await SystemClock.SleepAsync(TimeSpan.FromSeconds(10), ct); return ResultPrimitive.WhateverButTooLate; @@ -330,7 +330,7 @@ public void Should_not_be_able_to_cancel_with_unobserved_user_cancellation_token using (CancellationTokenSource userTokenSource = new CancellationTokenSource()) { - policy.Awaiting(async p => await p.ExecuteAsync(async + policy.Awaiting(p => p.ExecuteAsync(async _ => { userTokenSource.Cancel(); // User token cancels in the middle of execution ... await SystemClock.SleepAsync(TimeSpan.FromSeconds(timeout * 2), @@ -353,7 +353,7 @@ public void Should_not_execute_user_delegate_if_user_cancellationToken_cancelled { cts.Cancel(); - policy.Awaiting(async p => await p.ExecuteAsync(async ct => + policy.Awaiting(p => p.ExecuteAsync(async ct => { executed = true; await TaskHelper.EmptyTask; @@ -376,7 +376,7 @@ public void Should_be_able_to_cancel_with_user_cancellation_token_before_timeout var policy = Policy.TimeoutAsync(timeout, TimeoutStrategy.Optimistic); using (CancellationTokenSource userTokenSource = new CancellationTokenSource()) { - policy.Awaiting(async p => await p.ExecuteAsync( + policy.Awaiting(p => p.ExecuteAsync( ct => { userTokenSource.Cancel(); ct.ThrowIfCancellationRequested(); // Simulate cancel in the middle of execution return Task.FromResult(ResultPrimitive.WhateverButTooLate); @@ -396,7 +396,7 @@ public void Should_not_execute_user_delegate_if_user_cancellationToken_cancelled { cts.Cancel(); - policy.Awaiting(async p => await p.ExecuteAsync(async ct => + policy.Awaiting(p => p.ExecuteAsync(async ct => { executed = true; await TaskHelper.EmptyTask; @@ -426,7 +426,7 @@ public void Should_call_ontimeout_with_configured_timeout__pessimistic() var policy = Policy.TimeoutAsync(timeoutPassedToConfiguration, TimeoutStrategy.Pessimistic, onTimeoutAsync); - policy.Awaiting(async p => await p.ExecuteAsync(async () => + policy.Awaiting(p => p.ExecuteAsync(async () => { await SystemClock.SleepAsync(TimeSpan.FromSeconds(3), CancellationToken.None); return ResultPrimitive.WhateverButTooLate; @@ -452,7 +452,7 @@ public void Should_call_ontimeout_with_passed_context__pessimistic() TimeSpan timeout = TimeSpan.FromMilliseconds(250); var policy = Policy.TimeoutAsync(timeout, TimeoutStrategy.Pessimistic, onTimeoutAsync); - policy.Awaiting(async p => await p.ExecuteAsync(async ctx => + policy.Awaiting(p => p.ExecuteAsync(async ctx => { await SystemClock.SleepAsync(TimeSpan.FromSeconds(3), CancellationToken.None); return ResultPrimitive.WhateverButTooLate; @@ -481,7 +481,7 @@ public void Should_call_ontimeout_with_timeout_supplied_different_for_each_execu var policy = Policy.TimeoutAsync(timeoutFunc, TimeoutStrategy.Pessimistic, onTimeoutAsync); - policy.Awaiting(async p => await p.ExecuteAsync(async () => + policy.Awaiting(p => p.ExecuteAsync(async () => { await SystemClock.SleepAsync(TimeSpan.FromSeconds(3), CancellationToken.None); return ResultPrimitive.WhateverButTooLate; @@ -511,7 +511,7 @@ public void Should_call_ontimeout_with_timeout_supplied_different_for_each_execu // Supply a programatically-controlled timeout, via the execution context. Context context = new Context("SomeOperationKey") { ["timeout"] = TimeSpan.FromMilliseconds(25 * programaticallyControlledDelay) }; - policy.Awaiting(async p => await p.ExecuteAsync(async ctx => + policy.Awaiting(p => p.ExecuteAsync(async ctx => { await SystemClock.SleepAsync(TimeSpan.FromSeconds(3), CancellationToken.None); return ResultPrimitive.WhateverButTooLate; @@ -534,7 +534,7 @@ public void Should_call_ontimeout_with_task_wrapping_abandoned_action__pessimist TimeSpan timeout = TimeSpan.FromMilliseconds(250); var policy = Policy.TimeoutAsync(timeout, TimeoutStrategy.Pessimistic, onTimeoutAsync); - policy.Awaiting(async p => await p.ExecuteAsync(async () => + policy.Awaiting(p => p.ExecuteAsync(async () => { await SystemClock.SleepAsync(TimeSpan.FromSeconds(3), CancellationToken.None); return ResultPrimitive.WhateverButTooLate; @@ -564,7 +564,7 @@ public async Task Should_call_ontimeout_with_task_wrapping_abandoned_action_allo TimeSpan thriceShimTimeSpan = shimTimespan + shimTimespan + shimTimespan; var policy = Policy.TimeoutAsync(shimTimespan, TimeoutStrategy.Pessimistic, onTimeoutAsync); - policy.Awaiting(async p => await p.ExecuteAsync(async () => + policy.Awaiting(p => p.ExecuteAsync(async () => { await SystemClock.SleepAsync(thriceShimTimeSpan, CancellationToken.None); throw exceptionToThrow; @@ -591,7 +591,7 @@ public void Should_call_ontimeout_with_timing_out_exception__pessimistic() var policy = Policy.TimeoutAsync(timeoutPassedToConfiguration, TimeoutStrategy.Pessimistic, onTimeoutAsync); - policy.Awaiting(async p => await p.ExecuteAsync(async () => + policy.Awaiting(p => p.ExecuteAsync(async () => { await SystemClock.SleepAsync(TimeSpan.FromSeconds(3), CancellationToken.None); return ResultPrimitive.WhateverButTooLate; @@ -621,7 +621,7 @@ public void Should_call_ontimeout_with_configured_timeout__optimistic() var policy = Policy.TimeoutAsync(timeoutPassedToConfiguration, TimeoutStrategy.Optimistic, onTimeoutAsync); var userCancellationToken = CancellationToken.None; - policy.Awaiting(async p => await p.ExecuteAsync(async ct => + policy.Awaiting(p => p.ExecuteAsync(async ct => { await SystemClock.SleepAsync(TimeSpan.FromSeconds(3), ct); return ResultPrimitive.WhateverButTooLate; @@ -648,7 +648,7 @@ public void Should_call_ontimeout_with_passed_context__optimistic() var policy = Policy.TimeoutAsync(timeout, TimeoutStrategy.Optimistic, onTimeoutAsync); var userCancellationToken = CancellationToken.None; - policy.Awaiting(async p => await p.ExecuteAsync(async (ctx, ct) => + policy.Awaiting(p => p.ExecuteAsync(async (ctx, ct) => { await SystemClock.SleepAsync(TimeSpan.FromSeconds(3), ct); return ResultPrimitive.WhateverButTooLate; @@ -679,7 +679,7 @@ public void Should_call_ontimeout_with_timeout_supplied_different_for_each_execu var policy = Policy.TimeoutAsync(timeoutFunc, TimeoutStrategy.Optimistic, onTimeoutAsync); var userCancellationToken = CancellationToken.None; - policy.Awaiting(async p => await p.ExecuteAsync(async ct => + policy.Awaiting(p => p.ExecuteAsync(async ct => { await SystemClock.SleepAsync(TimeSpan.FromSeconds(3), ct); return ResultPrimitive.WhateverButTooLate; @@ -713,7 +713,7 @@ public void Should_call_ontimeout_with_timeout_supplied_different_for_each_execu ["timeout"] = TimeSpan.FromMilliseconds(25 * programaticallyControlledDelay) }; - policy.Awaiting(async p => await p.ExecuteAsync(async (ctx, ct) => + policy.Awaiting(p => p.ExecuteAsync(async (ctx, ct) => { await SystemClock.SleepAsync(TimeSpan.FromSeconds(3), ct); return ResultPrimitive.WhateverButTooLate; @@ -737,7 +737,7 @@ public void Should_call_ontimeout_but_not_with_task_wrapping_abandoned_action__o var policy = Policy.TimeoutAsync(timeout, TimeoutStrategy.Optimistic, onTimeoutAsync); var userCancellationToken = CancellationToken.None; - policy.Awaiting(async p => await p.ExecuteAsync(async ct => + policy.Awaiting(p => p.ExecuteAsync(async ct => { await SystemClock.SleepAsync(TimeSpan.FromSeconds(3), ct); return ResultPrimitive.WhateverButTooLate; @@ -762,7 +762,7 @@ public void Should_call_ontimeout_with_timing_out_exception__optimistic() var policy = Policy.TimeoutAsync(timeoutPassedToConfiguration, TimeoutStrategy.Optimistic, onTimeoutAsync); var userCancellationToken = CancellationToken.None; - policy.Awaiting(async p => await p.ExecuteAsync(async ct => + policy.Awaiting(p => p.ExecuteAsync(async ct => { await SystemClock.SleepAsync(TimeSpan.FromSeconds(3), ct); return ResultPrimitive.WhateverButTooLate; diff --git a/src/Polly.Specs/Wrap/PolicyWrapSpecsAsync.cs b/src/Polly.Specs/Wrap/PolicyWrapSpecsAsync.cs index 137c0ec827..5b0f9e85e9 100644 --- a/src/Polly.Specs/Wrap/PolicyWrapSpecsAsync.cs +++ b/src/Polly.Specs/Wrap/PolicyWrapSpecsAsync.cs @@ -370,13 +370,13 @@ public void Wrapping_two_policies_by_instance_syntax_and_executing_should_wrap_o // When the retry wraps the breaker, the retry (being outer) should cause the call to be put through the breaker twice - causing the breaker to break. breaker.Reset(); - retryWrappingBreaker.Awaiting(async x => await x.RaiseExceptionAsync(2)) + retryWrappingBreaker.Awaiting(x => x.RaiseExceptionAsync(2)) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); // When the breaker wraps the retry, the retry (being inner) should retry twice before throwing the exception back on the breaker - the exception only hits the breaker once - so the breaker should not break. breaker.Reset(); - breakerWrappingRetry.Awaiting(async x => await x.RaiseExceptionAsync(2)) + breakerWrappingRetry.Awaiting(x => x.RaiseExceptionAsync(2)) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); } @@ -418,13 +418,13 @@ public void Wrapping_two_policies_by_static_syntax_and_executing_should_wrap_out // When the retry wraps the breaker, the retry (being outer) should cause the call to be put through the breaker twice - causing the breaker to break. breaker.Reset(); - retryWrappingBreaker.Awaiting(async x => await x.RaiseExceptionAsync(2)) + retryWrappingBreaker.Awaiting(x => x.RaiseExceptionAsync(2)) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Open); // When the breaker wraps the retry, the retry (being inner) should retry twice before throwing the exception back on the breaker - the exception only hits the breaker once - so the breaker should not break. breaker.Reset(); - breakerWrappingRetry.Awaiting(async x => await x.RaiseExceptionAsync(2)) + breakerWrappingRetry.Awaiting(x => x.RaiseExceptionAsync(2)) .Should().Throw(); breaker.CircuitState.Should().Be(CircuitState.Closed); }