diff --git a/bench/Polly.Core.Benchmarks/Utils/Helper.RateLimiting.cs b/bench/Polly.Core.Benchmarks/Utils/Helper.RateLimiting.cs index 00a68ac6c96..db9fbf19cd9 100644 --- a/bench/Polly.Core.Benchmarks/Utils/Helper.RateLimiting.cs +++ b/bench/Polly.Core.Benchmarks/Utils/Helper.RateLimiting.cs @@ -1,5 +1,3 @@ -#pragma warning disable S4225 // Extension methods should not extend "object" - using System.Threading.RateLimiting; namespace Polly.Core.Benchmarks.Utils; diff --git a/bench/Polly.Core.Benchmarks/Utils/Helper.Timeout.cs b/bench/Polly.Core.Benchmarks/Utils/Helper.Timeout.cs index cfe028f7cfe..d7b2eee8dd6 100644 --- a/bench/Polly.Core.Benchmarks/Utils/Helper.Timeout.cs +++ b/bench/Polly.Core.Benchmarks/Utils/Helper.Timeout.cs @@ -1,5 +1,3 @@ -#pragma warning disable S4225 // Extension methods should not extend "object" - namespace Polly.Core.Benchmarks.Utils; internal static partial class Helper diff --git a/src/Polly.Core/CircuitBreaker/CircuitBreakerResiliencePipelineBuilderExtensions.cs b/src/Polly.Core/CircuitBreaker/CircuitBreakerResiliencePipelineBuilderExtensions.cs index 37df92bb498..2951121cd79 100644 --- a/src/Polly.Core/CircuitBreaker/CircuitBreakerResiliencePipelineBuilderExtensions.cs +++ b/src/Polly.Core/CircuitBreaker/CircuitBreakerResiliencePipelineBuilderExtensions.cs @@ -77,7 +77,6 @@ internal static CircuitBreakerResilienceStrategy CreateStrategy( options.BreakDuration, options.OnOpened, @@ -87,7 +86,6 @@ internal static CircuitBreakerResilienceStrategy CreateStrategy( options.ShouldHandle!, diff --git a/src/Polly.Core/Registry/ResiliencePipelineProvider.cs b/src/Polly.Core/Registry/ResiliencePipelineProvider.cs index e6bdfb3679c..9215b19e440 100644 --- a/src/Polly.Core/Registry/ResiliencePipelineProvider.cs +++ b/src/Polly.Core/Registry/ResiliencePipelineProvider.cs @@ -2,8 +2,6 @@ namespace Polly.Registry; -#pragma warning disable CA1716 // Identifiers should not match keywords - /// /// Represents a provider for resilience pipelines that are accessible by . /// diff --git a/test/Polly.Core.Tests/Utils/Pipeline/CompositePipelineComponentTests.cs b/test/Polly.Core.Tests/Utils/Pipeline/CompositePipelineComponentTests.cs index 50688190d5e..f6a8ce30f59 100644 --- a/test/Polly.Core.Tests/Utils/Pipeline/CompositePipelineComponentTests.cs +++ b/test/Polly.Core.Tests/Utils/Pipeline/CompositePipelineComponentTests.cs @@ -6,8 +6,6 @@ namespace Polly.Core.Tests.Utils.Pipeline; -#pragma warning disable CA2000 // Dispose objects before losing scope - public class CompositePipelineComponentTests { private readonly ResilienceStrategyTelemetry _telemetry; diff --git a/test/Polly.Specs/Caching/CacheAsyncSpecs.cs b/test/Polly.Specs/Caching/CacheAsyncSpecs.cs index c8d352084ec..2b8c4eda5f2 100644 --- a/test/Polly.Specs/Caching/CacheAsyncSpecs.cs +++ b/test/Polly.Specs/Caching/CacheAsyncSpecs.cs @@ -450,24 +450,27 @@ public async Task Should_honour_cancellation_even_if_prior_execution_has_cached( var cache = Policy.CacheAsync(new StubCacheProvider(), TimeSpan.MaxValue); - CancellationTokenSource tokenSource = new CancellationTokenSource(); - int delegateInvocations = 0; - Func> func = async (_, _) => + + using (CancellationTokenSource tokenSource = new CancellationTokenSource()) { - // delegate does not observe cancellation token; test is whether CacheEngine does. - delegateInvocations++; - await TaskHelper.EmptyTask; - return ValueToReturn; - }; + Func> func = async (_, _) => + { + // delegate does not observe cancellation token; test is whether CacheEngine does. + delegateInvocations++; + await TaskHelper.EmptyTask; + return ValueToReturn; + }; - (await cache.ExecuteAsync(func, new Context(OperationKey), tokenSource.Token)).Should().Be(ValueToReturn); - delegateInvocations.Should().Be(1); + (await cache.ExecuteAsync(func, new Context(OperationKey), tokenSource.Token)).Should().Be(ValueToReturn); + delegateInvocations.Should().Be(1); - tokenSource.Cancel(); + tokenSource.Cancel(); + + await cache.Awaiting(policy => policy.ExecuteAsync(func, new Context(OperationKey), tokenSource.Token)) + .Should().ThrowAsync(); + } - await cache.Awaiting(policy => policy.ExecuteAsync(func, new Context(OperationKey), tokenSource.Token)) - .Should().ThrowAsync(); delegateInvocations.Should().Be(1); } @@ -480,18 +483,19 @@ public async Task Should_honour_cancellation_during_delegate_execution_and_not_p IAsyncCacheProvider stubCacheProvider = new StubCacheProvider(); var cache = Policy.CacheAsync(stubCacheProvider, TimeSpan.MaxValue); - CancellationTokenSource tokenSource = new CancellationTokenSource(); - - Func> func = async (_, ct) => + using (CancellationTokenSource tokenSource = new CancellationTokenSource()) { - tokenSource.Cancel(); // simulate cancellation raised during delegate execution - ct.ThrowIfCancellationRequested(); - await TaskHelper.EmptyTask; - return ValueToReturn; - }; - - await cache.Awaiting(policy => policy.ExecuteAsync(func, new Context(OperationKey), tokenSource.Token)) - .Should().ThrowAsync(); + Func> func = async (_, ct) => + { + tokenSource.Cancel(); // simulate cancellation raised during delegate execution + ct.ThrowIfCancellationRequested(); + await TaskHelper.EmptyTask; + return ValueToReturn; + }; + + await cache.Awaiting(policy => policy.ExecuteAsync(func, new Context(OperationKey), tokenSource.Token)) + .Should().ThrowAsync(); + } (bool cacheHit, object? fromCache) = await stubCacheProvider.TryGetAsync(OperationKey, CancellationToken.None, false); cacheHit.Should().BeFalse(); diff --git a/test/Polly.Specs/Caching/CacheSpecs.cs b/test/Polly.Specs/Caching/CacheSpecs.cs index cae6c4e5d9b..bd48940749a 100644 --- a/test/Polly.Specs/Caching/CacheSpecs.cs +++ b/test/Polly.Specs/Caching/CacheSpecs.cs @@ -442,23 +442,26 @@ public void Should_honour_cancellation_even_if_prior_execution_has_cached() CachePolicy cache = Policy.Cache(new StubCacheProvider(), TimeSpan.MaxValue); - CancellationTokenSource tokenSource = new CancellationTokenSource(); - int delegateInvocations = 0; - Func func = (_, _) => + + using (CancellationTokenSource tokenSource = new CancellationTokenSource()) { - // delegate does not observe cancellation token; test is whether CacheEngine does. - delegateInvocations++; - return ValueToReturn; - }; + Func func = (_, _) => + { + // delegate does not observe cancellation token; test is whether CacheEngine does. + delegateInvocations++; + return ValueToReturn; + }; - cache.Execute(func, new Context(OperationKey), tokenSource.Token).Should().Be(ValueToReturn); - delegateInvocations.Should().Be(1); + cache.Execute(func, new Context(OperationKey), tokenSource.Token).Should().Be(ValueToReturn); + delegateInvocations.Should().Be(1); - tokenSource.Cancel(); + tokenSource.Cancel(); + + cache.Invoking(policy => policy.Execute(func, new Context(OperationKey), tokenSource.Token)) + .Should().Throw(); + } - cache.Invoking(policy => policy.Execute(func, new Context(OperationKey), tokenSource.Token)) - .Should().Throw(); delegateInvocations.Should().Be(1); } @@ -471,17 +474,18 @@ public void Should_honour_cancellation_during_delegate_execution_and_not_put_to_ ISyncCacheProvider stubCacheProvider = new StubCacheProvider(); CachePolicy cache = Policy.Cache(stubCacheProvider, TimeSpan.MaxValue); - CancellationTokenSource tokenSource = new CancellationTokenSource(); - - Func func = (_, ct) => + using (CancellationTokenSource tokenSource = new CancellationTokenSource()) { - tokenSource.Cancel(); // simulate cancellation raised during delegate execution - ct.ThrowIfCancellationRequested(); - return ValueToReturn; - }; - - cache.Invoking(policy => policy.Execute(func, new Context(OperationKey), tokenSource.Token)) - .Should().Throw(); + Func func = (_, ct) => + { + tokenSource.Cancel(); // simulate cancellation raised during delegate execution + ct.ThrowIfCancellationRequested(); + return ValueToReturn; + }; + + cache.Invoking(policy => policy.Execute(func, new Context(OperationKey), tokenSource.Token)) + .Should().Throw(); + } (bool cacheHit, object? fromCache) = stubCacheProvider.TryGet(OperationKey); cacheHit.Should().BeFalse(); diff --git a/test/Polly.Specs/Caching/CacheTResultAsyncSpecs.cs b/test/Polly.Specs/Caching/CacheTResultAsyncSpecs.cs index ff4d038bc2b..81f665b230a 100644 --- a/test/Polly.Specs/Caching/CacheTResultAsyncSpecs.cs +++ b/test/Polly.Specs/Caching/CacheTResultAsyncSpecs.cs @@ -434,24 +434,27 @@ public async Task Should_honour_cancellation_even_if_prior_execution_has_cached( var cache = Policy.CacheAsync(new StubCacheProvider(), TimeSpan.MaxValue); - CancellationTokenSource tokenSource = new CancellationTokenSource(); - int delegateInvocations = 0; - Func> func = async (_, _) => + + using (CancellationTokenSource tokenSource = new CancellationTokenSource()) { - // delegate does not observe cancellation token; test is whether CacheEngine does. - delegateInvocations++; - await TaskHelper.EmptyTask; - return ValueToReturn; - }; + Func> func = async (_, _) => + { + // delegate does not observe cancellation token; test is whether CacheEngine does. + delegateInvocations++; + await TaskHelper.EmptyTask; + return ValueToReturn; + }; - (await cache.ExecuteAsync(func, new Context(OperationKey), tokenSource.Token)).Should().Be(ValueToReturn); - delegateInvocations.Should().Be(1); + (await cache.ExecuteAsync(func, new Context(OperationKey), tokenSource.Token)).Should().Be(ValueToReturn); + delegateInvocations.Should().Be(1); - tokenSource.Cancel(); + tokenSource.Cancel(); + + await cache.Awaiting(policy => policy.ExecuteAsync(func, new Context(OperationKey), tokenSource.Token)) + .Should().ThrowAsync(); + } - await cache.Awaiting(policy => policy.ExecuteAsync(func, new Context(OperationKey), tokenSource.Token)) - .Should().ThrowAsync(); delegateInvocations.Should().Be(1); } @@ -464,18 +467,19 @@ public async Task Should_honour_cancellation_during_delegate_execution_and_not_p IAsyncCacheProvider stubCacheProvider = new StubCacheProvider(); var cache = Policy.CacheAsync(stubCacheProvider, TimeSpan.MaxValue); - CancellationTokenSource tokenSource = new CancellationTokenSource(); - - Func> func = async (_, ct) => + using (CancellationTokenSource tokenSource = new CancellationTokenSource()) { - tokenSource.Cancel(); // simulate cancellation raised during delegate execution - ct.ThrowIfCancellationRequested(); - await TaskHelper.EmptyTask; - return ValueToReturn; - }; - - await cache.Awaiting(policy => policy.ExecuteAsync(func, new Context(OperationKey), tokenSource.Token)) - .Should().ThrowAsync(); + Func> func = async (_, ct) => + { + tokenSource.Cancel(); // simulate cancellation raised during delegate execution + ct.ThrowIfCancellationRequested(); + await TaskHelper.EmptyTask; + return ValueToReturn; + }; + + await cache.Awaiting(policy => policy.ExecuteAsync(func, new Context(OperationKey), tokenSource.Token)) + .Should().ThrowAsync(); + } (bool cacheHit, object? fromCache) = await stubCacheProvider.TryGetAsync(OperationKey, CancellationToken.None, false); cacheHit.Should().BeFalse(); diff --git a/test/Polly.Specs/Caching/CacheTResultSpecs.cs b/test/Polly.Specs/Caching/CacheTResultSpecs.cs index 9115491e1f3..fe492b6e690 100644 --- a/test/Polly.Specs/Caching/CacheTResultSpecs.cs +++ b/test/Polly.Specs/Caching/CacheTResultSpecs.cs @@ -425,23 +425,26 @@ public void Should_honour_cancellation_even_if_prior_execution_has_cached() CachePolicy cache = Policy.Cache(new StubCacheProvider(), TimeSpan.MaxValue); - CancellationTokenSource tokenSource = new CancellationTokenSource(); - int delegateInvocations = 0; - Func func = (_, _) => + + using (CancellationTokenSource tokenSource = new CancellationTokenSource()) { - // delegate does not observe cancellation token; test is whether CacheEngine does. - delegateInvocations++; - return ValueToReturn; - }; + Func func = (_, _) => + { + // delegate does not observe cancellation token; test is whether CacheEngine does. + delegateInvocations++; + return ValueToReturn; + }; - cache.Execute(func, new Context(OperationKey), tokenSource.Token).Should().Be(ValueToReturn); - delegateInvocations.Should().Be(1); + cache.Execute(func, new Context(OperationKey), tokenSource.Token).Should().Be(ValueToReturn); + delegateInvocations.Should().Be(1); - tokenSource.Cancel(); + tokenSource.Cancel(); + + cache.Invoking(policy => policy.Execute(func, new Context(OperationKey), tokenSource.Token)) + .Should().Throw(); + } - cache.Invoking(policy => policy.Execute(func, new Context(OperationKey), tokenSource.Token)) - .Should().Throw(); delegateInvocations.Should().Be(1); } @@ -454,17 +457,18 @@ public void Should_honour_cancellation_during_delegate_execution_and_not_put_to_ ISyncCacheProvider stubCacheProvider = new StubCacheProvider(); CachePolicy cache = Policy.Cache(stubCacheProvider, TimeSpan.MaxValue); - CancellationTokenSource tokenSource = new CancellationTokenSource(); - - Func func = (_, ct) => + using (CancellationTokenSource tokenSource = new CancellationTokenSource()) { - tokenSource.Cancel(); // simulate cancellation raised during delegate execution - ct.ThrowIfCancellationRequested(); - return ValueToReturn; - }; - - cache.Invoking(policy => policy.Execute(func, new Context(OperationKey), tokenSource.Token)) - .Should().Throw(); + Func func = (_, ct) => + { + tokenSource.Cancel(); // simulate cancellation raised during delegate execution + ct.ThrowIfCancellationRequested(); + return ValueToReturn; + }; + + cache.Invoking(policy => policy.Execute(func, new Context(OperationKey), tokenSource.Token)) + .Should().Throw(); + } (bool cacheHit, object? fromCache) = stubCacheProvider.TryGet(OperationKey); cacheHit.Should().BeFalse(); diff --git a/test/Polly.Specs/CircuitBreaker/AdvancedCircuitBreakerAsyncSpecs.cs b/test/Polly.Specs/CircuitBreaker/AdvancedCircuitBreakerAsyncSpecs.cs index fb73ee5b2ba..025c873ca96 100644 --- a/test/Polly.Specs/CircuitBreaker/AdvancedCircuitBreakerAsyncSpecs.cs +++ b/test/Polly.Specs/CircuitBreaker/AdvancedCircuitBreakerAsyncSpecs.cs @@ -2781,8 +2781,6 @@ public async Task Should_execute_action_when_non_faulting_and_cancellationToken_ .Handle() .AdvancedCircuitBreakerAsync(0.5, TimeSpan.FromSeconds(10), 4, durationOfBreak); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -2792,8 +2790,11 @@ public async Task Should_execute_action_when_non_faulting_and_cancellationToken_ AttemptDuringWhichToCancel = null, }; - await breaker.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) - .Should().NotThrowAsync(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + await breaker.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + .Should().NotThrowAsync(); + } attemptsInvoked.Should().Be(1); } @@ -2806,9 +2807,6 @@ public async Task Should_not_execute_action_when_cancellationToken_cancelled_bef .Handle() .AdvancedCircuitBreakerAsync(0.5, TimeSpan.FromSeconds(10), 4, durationOfBreak); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -2818,11 +2816,15 @@ public async Task Should_not_execute_action_when_cancellationToken_cancelled_bef AttemptDuringWhichToCancel = null, // Cancellation token cancelled manually below - before any scenario execution. }; - cancellationTokenSource.Cancel(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + cancellationTokenSource.Cancel(); - var ex = await breaker.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) - .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + var ex = await breaker.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + .Should().ThrowAsync(); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(0); } @@ -2835,9 +2837,6 @@ public async Task Should_report_cancellation_during_otherwise_non_faulting_actio .Handle() .AdvancedCircuitBreakerAsync(0.5, TimeSpan.FromSeconds(10), 4, durationOfBreak); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -2848,9 +2847,14 @@ public async Task Should_report_cancellation_during_otherwise_non_faulting_actio ActionObservesCancellation = true }; - var ex = await breaker.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) - .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + var ex = await breaker.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + .Should().ThrowAsync(); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -2863,9 +2867,6 @@ public async Task Should_report_cancellation_during_faulting_action_execution_wh .Handle() .AdvancedCircuitBreakerAsync(0.5, TimeSpan.FromSeconds(10), 4, durationOfBreak); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -2876,9 +2877,14 @@ public async Task Should_report_cancellation_during_faulting_action_execution_wh ActionObservesCancellation = true }; - var ex = await breaker.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) - .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + var ex = await breaker.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + .Should().ThrowAsync(); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -2891,8 +2897,6 @@ public async Task Should_report_faulting_from_faulting_action_execution_when_use .Handle() .AdvancedCircuitBreakerAsync(0.5, TimeSpan.FromSeconds(10), 4, durationOfBreak); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -2903,8 +2907,11 @@ public async Task Should_report_faulting_from_faulting_action_execution_when_use ActionObservesCancellation = false }; - await breaker.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) - .Should().ThrowAsync(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + await breaker.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + .Should().ThrowAsync(); + } attemptsInvoked.Should().Be(1); } @@ -2930,14 +2937,9 @@ await breaker.Awaiting(x => x.RaiseExceptionAsync()) // Circuit is now broken. - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; - cancellationTokenSource.Cancel(); - Scenario scenario = new Scenario { NumberOfTimesToRaiseException = 1, @@ -2945,9 +2947,15 @@ await breaker.Awaiting(x => x.RaiseExceptionAsync()) ActionObservesCancellation = false }; - var ex2 = await breaker.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) - .Should().ThrowAsync(); - ex2.And.CancellationToken.Should().Be(cancellationToken); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + cancellationTokenSource.Cancel(); + + var ex2 = await breaker.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + .Should().ThrowAsync(); + ex2.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(0); } @@ -2962,25 +2970,26 @@ public async Task Should_honour_different_cancellationToken_captured_implicitly_ .Handle() .AdvancedCircuitBreakerAsync(0.5, TimeSpan.FromSeconds(10), 4, durationOfBreak); - CancellationTokenSource policyCancellationTokenSource = new CancellationTokenSource(); - CancellationToken policyCancellationToken = policyCancellationTokenSource.Token; - - CancellationTokenSource implicitlyCapturedActionCancellationTokenSource = new CancellationTokenSource(); - CancellationToken implicitlyCapturedActionCancellationToken = implicitlyCapturedActionCancellationTokenSource.Token; - - implicitlyCapturedActionCancellationTokenSource.Cancel(); - int attemptsInvoked = 0; - var ex = await breaker.Awaiting(x => x.ExecuteAsync(async _ => + using (CancellationTokenSource policyCancellationTokenSource = new CancellationTokenSource()) + using (CancellationTokenSource implicitlyCapturedActionCancellationTokenSource = new CancellationTokenSource()) { - attemptsInvoked++; - await TaskHelper.EmptyTask; - implicitlyCapturedActionCancellationToken.ThrowIfCancellationRequested(); - }, policyCancellationToken)) - .Should().ThrowAsync(); + CancellationToken policyCancellationToken = policyCancellationTokenSource.Token; + CancellationToken implicitlyCapturedActionCancellationToken = implicitlyCapturedActionCancellationTokenSource.Token; + + implicitlyCapturedActionCancellationTokenSource.Cancel(); + + var ex = await breaker.Awaiting(x => x.ExecuteAsync(async _ => + { + attemptsInvoked++; + await TaskHelper.EmptyTask; + implicitlyCapturedActionCancellationToken.ThrowIfCancellationRequested(); + }, policyCancellationToken)) + .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(implicitlyCapturedActionCancellationToken); + ex.And.CancellationToken.Should().Be(implicitlyCapturedActionCancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -2993,9 +3002,6 @@ public async Task Should_execute_func_returning_value_when_cancellationToken_not .Handle() .AdvancedCircuitBreakerAsync(0.5, TimeSpan.FromSeconds(10), 4, durationOfBreak); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -3007,9 +3013,14 @@ public async Task Should_execute_func_returning_value_when_cancellationToken_not AttemptDuringWhichToCancel = null, }; - Func action = async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true); - await breaker.Awaiting(action) - .Should().NotThrowAsync(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + Func action = async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true); + await breaker.Awaiting(action) + .Should().NotThrowAsync(); + } result.Should().BeTrue(); @@ -3024,9 +3035,6 @@ public async Task Should_honour_and_report_cancellation_during_func_execution() .Handle() .AdvancedCircuitBreakerAsync(0.5, TimeSpan.FromSeconds(10), 4, durationOfBreak); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -3039,10 +3047,15 @@ public async Task Should_honour_and_report_cancellation_during_func_execution() ActionObservesCancellation = true }; - Func action = async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true); - var ex = await breaker.Awaiting(action) - .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + Func action = async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true); + var ex = await breaker.Awaiting(action) + .Should().ThrowAsync(); + ex.And.CancellationToken.Should().Be(cancellationToken); + } result.Should().Be(null); diff --git a/test/Polly.Specs/CircuitBreaker/AdvancedCircuitBreakerSpecs.cs b/test/Polly.Specs/CircuitBreaker/AdvancedCircuitBreakerSpecs.cs index 5b6fe1621e0..9642decb833 100644 --- a/test/Polly.Specs/CircuitBreaker/AdvancedCircuitBreakerSpecs.cs +++ b/test/Polly.Specs/CircuitBreaker/AdvancedCircuitBreakerSpecs.cs @@ -2776,8 +2776,6 @@ public void Should_execute_action_when_non_faulting_and_cancellationToken_not_ca .Handle() .AdvancedCircuitBreaker(0.5, TimeSpan.FromSeconds(10), 4, durationOfBreak); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -2787,8 +2785,11 @@ public void Should_execute_action_when_non_faulting_and_cancellationToken_not_ca AttemptDuringWhichToCancel = null, }; - breaker.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) - .Should().NotThrow(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + breaker.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) + .Should().NotThrow(); + } attemptsInvoked.Should().Be(1); } @@ -2801,9 +2802,6 @@ public void Should_not_execute_action_when_cancellationToken_cancelled_before_ex .Handle() .AdvancedCircuitBreaker(0.5, TimeSpan.FromSeconds(10), 4, durationOfBreak); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -2813,11 +2811,15 @@ public void Should_not_execute_action_when_cancellationToken_cancelled_before_ex AttemptDuringWhichToCancel = null, // Cancellation token cancelled manually below - before any scenario execution. }; - cancellationTokenSource.Cancel(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + cancellationTokenSource.Cancel(); - breaker.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) - .Should().Throw() - .And.CancellationToken.Should().Be(cancellationToken); + breaker.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) + .Should().Throw() + .And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(0); } @@ -2830,9 +2832,6 @@ public void Should_report_cancellation_during_otherwise_non_faulting_action_exec .Handle() .AdvancedCircuitBreaker(0.5, TimeSpan.FromSeconds(10), 4, durationOfBreak); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -2843,9 +2842,14 @@ public void Should_report_cancellation_during_otherwise_non_faulting_action_exec ActionObservesCancellation = true }; - breaker.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) - .Should().Throw() - .And.CancellationToken.Should().Be(cancellationToken); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + breaker.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) + .Should().Throw() + .And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -2858,9 +2862,6 @@ public void Should_report_cancellation_during_faulting_action_execution_when_use .Handle() .AdvancedCircuitBreaker(0.5, TimeSpan.FromSeconds(10), 4, durationOfBreak); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -2871,9 +2872,14 @@ public void Should_report_cancellation_during_faulting_action_execution_when_use ActionObservesCancellation = true }; - breaker.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) - .Should().Throw() - .And.CancellationToken.Should().Be(cancellationToken); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + breaker.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) + .Should().Throw() + .And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -2886,8 +2892,6 @@ public void Should_report_faulting_from_faulting_action_execution_when_user_dele .Handle() .AdvancedCircuitBreaker(0.5, TimeSpan.FromSeconds(10), 4, durationOfBreak); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -2898,8 +2902,11 @@ public void Should_report_faulting_from_faulting_action_execution_when_user_dele ActionObservesCancellation = false }; - breaker.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) - .Should().Throw(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + breaker.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) + .Should().Throw(); + } attemptsInvoked.Should().Be(1); } @@ -2925,14 +2932,9 @@ public void Should_report_cancellation_when_both_open_circuit_and_cancellation() // Circuit is now broken. - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; - cancellationTokenSource.Cancel(); - Scenario scenario = new Scenario { NumberOfTimesToRaiseException = 1, @@ -2940,9 +2942,15 @@ public void Should_report_cancellation_when_both_open_circuit_and_cancellation() ActionObservesCancellation = false }; - breaker.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) - .Should().Throw() - .And.CancellationToken.Should().Be(cancellationToken); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + cancellationTokenSource.Cancel(); + + breaker.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) + .Should().Throw() + .And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(0); } @@ -2957,23 +2965,24 @@ public void Should_honour_different_cancellationToken_captured_implicitly_by_act .Handle() .AdvancedCircuitBreaker(0.5, TimeSpan.FromSeconds(10), 4, durationOfBreak); - CancellationTokenSource policyCancellationTokenSource = new CancellationTokenSource(); - CancellationToken policyCancellationToken = policyCancellationTokenSource.Token; - - CancellationTokenSource implicitlyCapturedActionCancellationTokenSource = new CancellationTokenSource(); - CancellationToken implicitlyCapturedActionCancellationToken = implicitlyCapturedActionCancellationTokenSource.Token; - - implicitlyCapturedActionCancellationTokenSource.Cancel(); - int attemptsInvoked = 0; - breaker.Invoking(x => x.Execute(_ => + using (CancellationTokenSource policyCancellationTokenSource = new CancellationTokenSource()) + using (CancellationTokenSource implicitlyCapturedActionCancellationTokenSource = new CancellationTokenSource()) { - attemptsInvoked++; - implicitlyCapturedActionCancellationToken.ThrowIfCancellationRequested(); - }, policyCancellationToken)) - .Should().Throw() - .And.CancellationToken.Should().Be(implicitlyCapturedActionCancellationToken); + CancellationToken policyCancellationToken = policyCancellationTokenSource.Token; + CancellationToken implicitlyCapturedActionCancellationToken = implicitlyCapturedActionCancellationTokenSource.Token; + + implicitlyCapturedActionCancellationTokenSource.Cancel(); + + breaker.Invoking(x => x.Execute(_ => + { + attemptsInvoked++; + implicitlyCapturedActionCancellationToken.ThrowIfCancellationRequested(); + }, policyCancellationToken)) + .Should().Throw() + .And.CancellationToken.Should().Be(implicitlyCapturedActionCancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -2986,22 +2995,23 @@ public void Should_execute_func_returning_value_when_cancellationToken_not_cance .Handle() .AdvancedCircuitBreaker(0.5, TimeSpan.FromSeconds(10), 4, durationOfBreak); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; bool? result = null; - Scenario scenario = new Scenario { NumberOfTimesToRaiseException = 0, AttemptDuringWhichToCancel = null, }; - breaker.Invoking(x => result = x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute, true)) - .Should().NotThrow(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + breaker.Invoking(x => result = x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute, true)) + .Should().NotThrow(); + } result.Should().BeTrue(); @@ -3016,9 +3026,6 @@ public void Should_honour_and_report_cancellation_during_func_execution() .Handle() .AdvancedCircuitBreaker(0.5, TimeSpan.FromSeconds(10), 4, durationOfBreak); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -3031,8 +3038,13 @@ public void Should_honour_and_report_cancellation_during_func_execution() ActionObservesCancellation = true }; - breaker.Invoking(x => result = x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute, true)) - .Should().Throw().And.CancellationToken.Should().Be(cancellationToken); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + breaker.Invoking(x => result = x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute, true)) + .Should().Throw().And.CancellationToken.Should().Be(cancellationToken); + } result.Should().Be(null); diff --git a/test/Polly.Specs/CircuitBreaker/CircuitBreakerAsyncSpecs.cs b/test/Polly.Specs/CircuitBreaker/CircuitBreakerAsyncSpecs.cs index 0a46d9ee986..26685b667c2 100644 --- a/test/Polly.Specs/CircuitBreaker/CircuitBreakerAsyncSpecs.cs +++ b/test/Polly.Specs/CircuitBreaker/CircuitBreakerAsyncSpecs.cs @@ -1422,8 +1422,6 @@ public async Task Should_execute_action_when_non_faulting_and_cancellationToken_ .Handle() .CircuitBreakerAsync(2, durationOfBreak); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -1433,8 +1431,11 @@ public async Task Should_execute_action_when_non_faulting_and_cancellationToken_ AttemptDuringWhichToCancel = null, }; - await breaker.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) - .Should().NotThrowAsync(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + await breaker.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + .Should().NotThrowAsync(); + } attemptsInvoked.Should().Be(1); } @@ -1447,9 +1448,6 @@ public async Task Should_not_execute_action_when_cancellationToken_cancelled_bef .Handle() .CircuitBreakerAsync(2, durationOfBreak); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -1459,11 +1457,15 @@ public async Task Should_not_execute_action_when_cancellationToken_cancelled_bef AttemptDuringWhichToCancel = null, // Cancellation token cancelled manually below - before any scenario execution. }; - cancellationTokenSource.Cancel(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + cancellationTokenSource.Cancel(); - var ex = await breaker.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) - .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + var ex = await breaker.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + .Should().ThrowAsync(); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(0); } @@ -1476,9 +1478,6 @@ public async Task Should_report_cancellation_during_otherwise_non_faulting_actio .Handle() .CircuitBreakerAsync(2, durationOfBreak); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -1489,9 +1488,14 @@ public async Task Should_report_cancellation_during_otherwise_non_faulting_actio ActionObservesCancellation = true }; - var ex = await breaker.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) - .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + var ex = await breaker.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + .Should().ThrowAsync(); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -1504,9 +1508,6 @@ public async Task Should_report_cancellation_during_faulting_action_execution_wh .Handle() .CircuitBreakerAsync(2, durationOfBreak); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -1517,9 +1518,14 @@ public async Task Should_report_cancellation_during_faulting_action_execution_wh ActionObservesCancellation = true }; - var ex = await breaker.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) - .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + var ex = await breaker.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + .Should().ThrowAsync(); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -1532,8 +1538,6 @@ public async Task Should_report_faulting_from_faulting_action_execution_when_use .Handle() .CircuitBreakerAsync(2, durationOfBreak); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -1544,8 +1548,11 @@ public async Task Should_report_faulting_from_faulting_action_execution_when_use ActionObservesCancellation = false }; - await breaker.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) - .Should().ThrowAsync(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + await breaker.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + .Should().ThrowAsync(); + } attemptsInvoked.Should().Be(1); } @@ -1567,14 +1574,9 @@ await breaker.Awaiting(x => x.RaiseExceptionAsync()) // Circuit is now broken. - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; - cancellationTokenSource.Cancel(); - Scenario scenario = new Scenario { NumberOfTimesToRaiseException = 1, @@ -1582,9 +1584,15 @@ await breaker.Awaiting(x => x.RaiseExceptionAsync()) ActionObservesCancellation = false }; - var ex2 = await breaker.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) - .Should().ThrowAsync(); - ex2.And.CancellationToken.Should().Be(cancellationToken); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + cancellationTokenSource.Cancel(); + + var ex2 = await breaker.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + .Should().ThrowAsync(); + ex2.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(0); } @@ -1599,24 +1607,25 @@ public async Task Should_honour_different_cancellationToken_captured_implicitly_ .Handle() .CircuitBreakerAsync(2, durationOfBreak); - CancellationTokenSource policyCancellationTokenSource = new CancellationTokenSource(); - CancellationToken policyCancellationToken = policyCancellationTokenSource.Token; - - CancellationTokenSource implicitlyCapturedActionCancellationTokenSource = new CancellationTokenSource(); - CancellationToken implicitlyCapturedActionCancellationToken = implicitlyCapturedActionCancellationTokenSource.Token; - - implicitlyCapturedActionCancellationTokenSource.Cancel(); - int attemptsInvoked = 0; - var ex = await breaker.Awaiting(x => x.ExecuteAsync(async _ => + using (CancellationTokenSource policyCancellationTokenSource = new CancellationTokenSource()) + using (CancellationTokenSource implicitlyCapturedActionCancellationTokenSource = new CancellationTokenSource()) { - attemptsInvoked++; - await TaskHelper.EmptyTask; - implicitlyCapturedActionCancellationToken.ThrowIfCancellationRequested(); - }, policyCancellationToken)) - .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(implicitlyCapturedActionCancellationToken); + CancellationToken policyCancellationToken = policyCancellationTokenSource.Token; + CancellationToken implicitlyCapturedActionCancellationToken = implicitlyCapturedActionCancellationTokenSource.Token; + + implicitlyCapturedActionCancellationTokenSource.Cancel(); + + var ex = await breaker.Awaiting(x => x.ExecuteAsync(async _ => + { + attemptsInvoked++; + await TaskHelper.EmptyTask; + implicitlyCapturedActionCancellationToken.ThrowIfCancellationRequested(); + }, policyCancellationToken)) + .Should().ThrowAsync(); + ex.And.CancellationToken.Should().Be(implicitlyCapturedActionCancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -1629,9 +1638,6 @@ public async Task Should_execute_func_returning_value_when_cancellationToken_not .Handle() .CircuitBreakerAsync(2, durationOfBreak); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -1643,9 +1649,13 @@ public async Task Should_execute_func_returning_value_when_cancellationToken_not AttemptDuringWhichToCancel = null, }; - Func action = async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true); - await breaker.Awaiting(action) - .Should().NotThrowAsync(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + Func action = async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true); + await breaker.Awaiting(action) + .Should().NotThrowAsync(); + } result.Should().BeTrue(); @@ -1659,9 +1669,6 @@ public async Task Should_honour_and_report_cancellation_during_func_execution() .Handle() .CircuitBreakerAsync(2, TimeSpan.FromMinutes(1)); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -1674,12 +1681,17 @@ public async Task Should_honour_and_report_cancellation_during_func_execution() ActionObservesCancellation = true }; - Func action = async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true); - var ex = await breaker.Awaiting(action) - .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + Func action = async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true); + var ex = await breaker.Awaiting(action) + .Should().ThrowAsync(); + ex.And.CancellationToken.Should().Be(cancellationToken); - result.Should().Be(null); + result.Should().Be(null); + } attemptsInvoked.Should().Be(1); } diff --git a/test/Polly.Specs/CircuitBreaker/CircuitBreakerSpecs.cs b/test/Polly.Specs/CircuitBreaker/CircuitBreakerSpecs.cs index ba15f042984..47a4aafaff3 100644 --- a/test/Polly.Specs/CircuitBreaker/CircuitBreakerSpecs.cs +++ b/test/Polly.Specs/CircuitBreaker/CircuitBreakerSpecs.cs @@ -1435,8 +1435,6 @@ public void Should_execute_action_when_non_faulting_and_cancellationToken_not_ca .Handle() .CircuitBreaker(2, durationOfBreak); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -1446,8 +1444,11 @@ public void Should_execute_action_when_non_faulting_and_cancellationToken_not_ca AttemptDuringWhichToCancel = null, }; - breaker.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) - .Should().NotThrow(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + breaker.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) + .Should().NotThrow(); + } attemptsInvoked.Should().Be(1); } @@ -1460,9 +1461,6 @@ public void Should_not_execute_action_when_cancellationToken_cancelled_before_ex .Handle() .CircuitBreaker(2, durationOfBreak); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -1472,11 +1470,15 @@ public void Should_not_execute_action_when_cancellationToken_cancelled_before_ex AttemptDuringWhichToCancel = null, // Cancellation token cancelled manually below - before any scenario execution. }; - cancellationTokenSource.Cancel(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + cancellationTokenSource.Cancel(); - breaker.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) - .Should().Throw() - .And.CancellationToken.Should().Be(cancellationToken); + breaker.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) + .Should().Throw() + .And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(0); } @@ -1489,9 +1491,6 @@ public void Should_report_cancellation_during_otherwise_non_faulting_action_exec .Handle() .CircuitBreaker(2, durationOfBreak); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -1502,9 +1501,14 @@ public void Should_report_cancellation_during_otherwise_non_faulting_action_exec ActionObservesCancellation = true }; - breaker.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) - .Should().Throw() - .And.CancellationToken.Should().Be(cancellationToken); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + breaker.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) + .Should().Throw() + .And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -1517,9 +1521,6 @@ public void Should_report_cancellation_during_faulting_action_execution_when_use .Handle() .CircuitBreaker(2, durationOfBreak); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -1530,9 +1531,14 @@ public void Should_report_cancellation_during_faulting_action_execution_when_use ActionObservesCancellation = true }; - breaker.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) - .Should().Throw() - .And.CancellationToken.Should().Be(cancellationToken); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + breaker.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) + .Should().Throw() + .And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -1545,8 +1551,6 @@ public void Should_report_faulting_from_faulting_action_execution_when_user_dele .Handle() .CircuitBreaker(2, durationOfBreak); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -1557,8 +1561,11 @@ public void Should_report_faulting_from_faulting_action_execution_when_user_dele ActionObservesCancellation = false }; - breaker.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) - .Should().Throw(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + breaker.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) + .Should().Throw(); + } attemptsInvoked.Should().Be(1); } @@ -1580,14 +1587,9 @@ public void Should_report_cancellation_when_both_open_circuit_and_cancellation() // Circuit is now broken. - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; - cancellationTokenSource.Cancel(); - PolicyExtensions.ExceptionAndOrCancellationScenario scenario = new PolicyExtensions.ExceptionAndOrCancellationScenario { NumberOfTimesToRaiseException = 1, @@ -1595,9 +1597,15 @@ public void Should_report_cancellation_when_both_open_circuit_and_cancellation() ActionObservesCancellation = false }; - breaker.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) - .Should().Throw() - .And.CancellationToken.Should().Be(cancellationToken); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + cancellationTokenSource.Cancel(); + + breaker.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) + .Should().Throw() + .And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(0); } @@ -1612,23 +1620,24 @@ public void Should_honour_different_cancellationToken_captured_implicitly_by_act .Handle() .CircuitBreaker(2, durationOfBreak); - CancellationTokenSource policyCancellationTokenSource = new CancellationTokenSource(); - CancellationToken policyCancellationToken = policyCancellationTokenSource.Token; - - CancellationTokenSource implicitlyCapturedActionCancellationTokenSource = new CancellationTokenSource(); - CancellationToken implicitlyCapturedActionCancellationToken = implicitlyCapturedActionCancellationTokenSource.Token; - - implicitlyCapturedActionCancellationTokenSource.Cancel(); - int attemptsInvoked = 0; - breaker.Invoking(x => x.Execute(_ => + using (CancellationTokenSource policyCancellationTokenSource = new CancellationTokenSource()) + using (CancellationTokenSource implicitlyCapturedActionCancellationTokenSource = new CancellationTokenSource()) { - attemptsInvoked++; - implicitlyCapturedActionCancellationToken.ThrowIfCancellationRequested(); - }, policyCancellationToken)) - .Should().Throw() - .And.CancellationToken.Should().Be(implicitlyCapturedActionCancellationToken); + CancellationToken policyCancellationToken = policyCancellationTokenSource.Token; + CancellationToken implicitlyCapturedActionCancellationToken = implicitlyCapturedActionCancellationTokenSource.Token; + + implicitlyCapturedActionCancellationTokenSource.Cancel(); + + breaker.Invoking(x => x.Execute(_ => + { + attemptsInvoked++; + implicitlyCapturedActionCancellationToken.ThrowIfCancellationRequested(); + }, policyCancellationToken)) + .Should().Throw() + .And.CancellationToken.Should().Be(implicitlyCapturedActionCancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -1641,9 +1650,6 @@ public void Should_execute_func_returning_value_when_cancellationToken_not_cance .Handle() .CircuitBreaker(2, durationOfBreak); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -1655,8 +1661,13 @@ public void Should_execute_func_returning_value_when_cancellationToken_not_cance AttemptDuringWhichToCancel = null, }; - breaker.Invoking(x => result = x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute, true)) - .Should().NotThrow(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + breaker.Invoking(x => result = x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute, true)) + .Should().NotThrow(); + } result.Should().BeTrue(); @@ -1670,9 +1681,6 @@ public void Should_honour_and_report_cancellation_during_func_execution() .Handle() .CircuitBreaker(2, TimeSpan.FromMinutes(1)); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -1685,8 +1693,12 @@ public void Should_honour_and_report_cancellation_during_func_execution() ActionObservesCancellation = true }; - breaker.Invoking(x => result = x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute, true)) - .Should().Throw().And.CancellationToken.Should().Be(cancellationToken); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + breaker.Invoking(x => result = x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute, true)) + .Should().Throw().And.CancellationToken.Should().Be(cancellationToken); + } result.Should().Be(null); diff --git a/test/Polly.Specs/CircuitBreaker/CircuitBreakerTResultAsyncSpecs.cs b/test/Polly.Specs/CircuitBreaker/CircuitBreakerTResultAsyncSpecs.cs index d914f86b260..0d6e61a6a7c 100644 --- a/test/Polly.Specs/CircuitBreaker/CircuitBreakerTResultAsyncSpecs.cs +++ b/test/Polly.Specs/CircuitBreaker/CircuitBreakerTResultAsyncSpecs.cs @@ -1385,9 +1385,6 @@ public async Task Should_execute_action_when_non_faulting_and_cancellationToken_ .HandleResult(ResultPrimitive.Fault) .CircuitBreakerAsync(2, durationOfBreak); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -1396,9 +1393,13 @@ public async Task Should_execute_action_when_non_faulting_and_cancellationToken_ AttemptDuringWhichToCancel = null, }; - (await breaker.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, - ResultPrimitive.Good)) - .Should().Be(ResultPrimitive.Good); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + (await breaker.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, + ResultPrimitive.Good)) + .Should().Be(ResultPrimitive.Good); + } attemptsInvoked.Should().Be(1); } @@ -1411,9 +1412,6 @@ public async Task Should_not_execute_action_when_cancellationToken_cancelled_bef .HandleResult(ResultPrimitive.Fault) .CircuitBreakerAsync(2, durationOfBreak); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -1422,14 +1420,18 @@ public async Task Should_not_execute_action_when_cancellationToken_cancelled_bef AttemptDuringWhichToCancel = null, // Cancellation token cancelled manually below - before any scenario execution. }; - cancellationTokenSource.Cancel(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + cancellationTokenSource.Cancel(); - var ex = await breaker.Awaiting(x => x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, - ResultPrimitive.Fault, - ResultPrimitive.Fault, - ResultPrimitive.Good)) - .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + var ex = await breaker.Awaiting(x => x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, + ResultPrimitive.Fault, + ResultPrimitive.Fault, + ResultPrimitive.Good)) + .Should().ThrowAsync(); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(0); } @@ -1442,9 +1444,6 @@ public async Task Should_report_cancellation_during_otherwise_non_faulting_actio .HandleResult(ResultPrimitive.Fault) .CircuitBreakerAsync(2, durationOfBreak); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -1454,11 +1453,16 @@ public async Task Should_report_cancellation_during_otherwise_non_faulting_actio ActionObservesCancellation = true }; - var ex = await breaker.Awaiting(x => x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, - ResultPrimitive.Good, - ResultPrimitive.Good)) - .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + var ex = await breaker.Awaiting(x => x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, + ResultPrimitive.Good, + ResultPrimitive.Good)) + .Should().ThrowAsync(); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -1471,9 +1475,6 @@ public async Task Should_report_cancellation_during_faulting_action_execution_wh .HandleResult(ResultPrimitive.Fault) .CircuitBreakerAsync(2, durationOfBreak); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -1483,11 +1484,16 @@ public async Task Should_report_cancellation_during_faulting_action_execution_wh ActionObservesCancellation = true }; - var ex = await breaker.Awaiting(x => x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, - ResultPrimitive.Fault, - ResultPrimitive.Good)) - .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + var ex = await breaker.Awaiting(x => x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, + ResultPrimitive.Fault, + ResultPrimitive.Good)) + .Should().ThrowAsync(); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -1500,9 +1506,6 @@ public async Task Should_report_faulting_from_faulting_action_execution_when_use .HandleResult(ResultPrimitive.Fault) .CircuitBreakerAsync(2, durationOfBreak); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -1512,8 +1515,13 @@ public async Task Should_report_faulting_from_faulting_action_execution_when_use ActionObservesCancellation = false }; - (await breaker.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Fault)) - .Should().Be(ResultPrimitive.Fault); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + (await breaker.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Fault)) + .Should().Be(ResultPrimitive.Fault); + } attemptsInvoked.Should().Be(1); } @@ -1534,25 +1542,26 @@ public async Task Should_report_cancellation_when_both_open_circuit_and_cancella // Circuit is now broken. - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; - cancellationTokenSource.Cancel(); - Scenario scenario = new Scenario { AttemptDuringWhichToCancel = null, // Cancelled manually instead - see above. ActionObservesCancellation = false }; - var ex2 = await breaker.Awaiting(x => x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, - ResultPrimitive.Fault, - ResultPrimitive.Good)) - .Should().ThrowAsync(); - ex2.And.CancellationToken.Should().Be(cancellationToken); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + cancellationTokenSource.Cancel(); + + var ex2 = await breaker.Awaiting(x => x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, + ResultPrimitive.Fault, + ResultPrimitive.Good)) + .Should().ThrowAsync(); + ex2.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(0); } @@ -1567,25 +1576,26 @@ public async Task Should_honour_different_cancellationToken_captured_implicitly_ .HandleResult(ResultPrimitive.Fault) .CircuitBreakerAsync(2, durationOfBreak); - CancellationTokenSource policyCancellationTokenSource = new CancellationTokenSource(); - CancellationToken policyCancellationToken = policyCancellationTokenSource.Token; - - CancellationTokenSource implicitlyCapturedActionCancellationTokenSource = new CancellationTokenSource(); - CancellationToken implicitlyCapturedActionCancellationToken = implicitlyCapturedActionCancellationTokenSource.Token; - - implicitlyCapturedActionCancellationTokenSource.Cancel(); - int attemptsInvoked = 0; - var ex = await breaker.Awaiting(x => x.ExecuteAsync(async _ => + using (CancellationTokenSource policyCancellationTokenSource = new CancellationTokenSource()) + using (CancellationTokenSource implicitlyCapturedActionCancellationTokenSource = new CancellationTokenSource()) { - attemptsInvoked++; - await TaskHelper.EmptyTask; - implicitlyCapturedActionCancellationToken.ThrowIfCancellationRequested(); - return ResultPrimitive.Good; - }, policyCancellationToken)) - .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(implicitlyCapturedActionCancellationToken); + CancellationToken policyCancellationToken = policyCancellationTokenSource.Token; + CancellationToken implicitlyCapturedActionCancellationToken = implicitlyCapturedActionCancellationTokenSource.Token; + + implicitlyCapturedActionCancellationTokenSource.Cancel(); + + var ex = await breaker.Awaiting(x => x.ExecuteAsync(async _ => + { + attemptsInvoked++; + await TaskHelper.EmptyTask; + implicitlyCapturedActionCancellationToken.ThrowIfCancellationRequested(); + return ResultPrimitive.Good; + }, policyCancellationToken)) + .Should().ThrowAsync(); + ex.And.CancellationToken.Should().Be(implicitlyCapturedActionCancellationToken); + } attemptsInvoked.Should().Be(1); } diff --git a/test/Polly.Specs/CircuitBreaker/CircuitBreakerTResultSpecs.cs b/test/Polly.Specs/CircuitBreaker/CircuitBreakerTResultSpecs.cs index 47245d765ca..7000b6e498a 100644 --- a/test/Polly.Specs/CircuitBreaker/CircuitBreakerTResultSpecs.cs +++ b/test/Polly.Specs/CircuitBreaker/CircuitBreakerTResultSpecs.cs @@ -1375,8 +1375,6 @@ public void Should_execute_action_when_non_faulting_and_cancellationToken_not_ca .HandleResult(ResultPrimitive.Fault) .CircuitBreaker(2, durationOfBreak); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -1385,9 +1383,12 @@ public void Should_execute_action_when_non_faulting_and_cancellationToken_not_ca AttemptDuringWhichToCancel = null, }; - breaker.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, - ResultPrimitive.Good) - .Should().Be(ResultPrimitive.Good); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + breaker.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, + ResultPrimitive.Good) + .Should().Be(ResultPrimitive.Good); + } attemptsInvoked.Should().Be(1); } @@ -1400,9 +1401,6 @@ public void Should_not_execute_action_when_cancellationToken_cancelled_before_ex .HandleResult(ResultPrimitive.Fault) .CircuitBreaker(2, durationOfBreak); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -1411,14 +1409,18 @@ public void Should_not_execute_action_when_cancellationToken_cancelled_before_ex AttemptDuringWhichToCancel = null, // Cancellation token cancelled manually below - before any scenario execution. }; - cancellationTokenSource.Cancel(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + cancellationTokenSource.Cancel(); - breaker.Invoking(x => x.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, - ResultPrimitive.Fault, - ResultPrimitive.Fault, - ResultPrimitive.Good)) - .Should().Throw() - .And.CancellationToken.Should().Be(cancellationToken); + breaker.Invoking(x => x.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, + ResultPrimitive.Fault, + ResultPrimitive.Fault, + ResultPrimitive.Good)) + .Should().Throw() + .And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(0); } @@ -1431,9 +1433,6 @@ public void Should_report_cancellation_during_otherwise_non_faulting_action_exec .HandleResult(ResultPrimitive.Fault) .CircuitBreaker(2, durationOfBreak); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -1443,11 +1442,15 @@ public void Should_report_cancellation_during_otherwise_non_faulting_action_exec ActionObservesCancellation = true }; - breaker.Invoking(x => x.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, - ResultPrimitive.Good, - ResultPrimitive.Good)) - .Should().Throw() - .And.CancellationToken.Should().Be(cancellationToken); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + breaker.Invoking(x => x.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, + ResultPrimitive.Good, + ResultPrimitive.Good)) + .Should().Throw() + .And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -1460,9 +1463,6 @@ public void Should_report_cancellation_during_faulting_action_execution_when_use .HandleResult(ResultPrimitive.Fault) .CircuitBreaker(2, durationOfBreak); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -1472,11 +1472,15 @@ public void Should_report_cancellation_during_faulting_action_execution_when_use ActionObservesCancellation = true }; - breaker.Invoking(x => x.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, - ResultPrimitive.Fault, - ResultPrimitive.Good)) - .Should().Throw() - .And.CancellationToken.Should().Be(cancellationToken); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + breaker.Invoking(x => x.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, + ResultPrimitive.Fault, + ResultPrimitive.Good)) + .Should().Throw() + .And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -1489,8 +1493,6 @@ public void Should_report_faulting_from_faulting_action_execution_when_user_dele .HandleResult(ResultPrimitive.Fault) .CircuitBreaker(2, durationOfBreak); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -1500,8 +1502,11 @@ public void Should_report_faulting_from_faulting_action_execution_when_user_dele ActionObservesCancellation = false }; - breaker.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Fault) - .Should().Be(ResultPrimitive.Fault); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + breaker.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Fault) + .Should().Be(ResultPrimitive.Fault); + } attemptsInvoked.Should().Be(1); } @@ -1521,26 +1526,26 @@ public void Should_report_cancellation_when_both_open_circuit_and_cancellation() .WithMessage("The circuit is now open and is not allowing calls."); // Circuit is now broken. - - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; - cancellationTokenSource.Cancel(); - Scenario scenario = new Scenario { AttemptDuringWhichToCancel = null, // Cancelled manually instead - see above. ActionObservesCancellation = false }; - breaker.Invoking(x => x.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, - ResultPrimitive.Fault, - ResultPrimitive.Good)) - .Should().Throw() - .And.CancellationToken.Should().Be(cancellationToken); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + cancellationTokenSource.Cancel(); + + breaker.Invoking(x => x.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, + ResultPrimitive.Fault, + ResultPrimitive.Good)) + .Should().Throw() + .And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(0); } @@ -1555,24 +1560,25 @@ public void Should_honour_different_cancellationToken_captured_implicitly_by_act .HandleResult(ResultPrimitive.Fault) .CircuitBreaker(2, durationOfBreak); - CancellationTokenSource policyCancellationTokenSource = new CancellationTokenSource(); - CancellationToken policyCancellationToken = policyCancellationTokenSource.Token; - - CancellationTokenSource implicitlyCapturedActionCancellationTokenSource = new CancellationTokenSource(); - CancellationToken implicitlyCapturedActionCancellationToken = implicitlyCapturedActionCancellationTokenSource.Token; - - implicitlyCapturedActionCancellationTokenSource.Cancel(); - int attemptsInvoked = 0; - breaker.Invoking(x => x.Execute(_ => + using (CancellationTokenSource policyCancellationTokenSource = new CancellationTokenSource()) + using (CancellationTokenSource implicitlyCapturedActionCancellationTokenSource = new CancellationTokenSource()) { - attemptsInvoked++; - implicitlyCapturedActionCancellationToken.ThrowIfCancellationRequested(); - return ResultPrimitive.Good; - }, policyCancellationToken)) - .Should().Throw() - .And.CancellationToken.Should().Be(implicitlyCapturedActionCancellationToken); + CancellationToken policyCancellationToken = policyCancellationTokenSource.Token; + CancellationToken implicitlyCapturedActionCancellationToken = implicitlyCapturedActionCancellationTokenSource.Token; + + implicitlyCapturedActionCancellationTokenSource.Cancel(); + + breaker.Invoking(x => x.Execute(_ => + { + attemptsInvoked++; + implicitlyCapturedActionCancellationToken.ThrowIfCancellationRequested(); + return ResultPrimitive.Good; + }, policyCancellationToken)) + .Should().Throw() + .And.CancellationToken.Should().Be(implicitlyCapturedActionCancellationToken); + } attemptsInvoked.Should().Be(1); } diff --git a/test/Polly.Specs/Fallback/FallbackAsyncSpecs.cs b/test/Polly.Specs/Fallback/FallbackAsyncSpecs.cs index 36fa5ce85f2..bd4695d6600 100644 --- a/test/Polly.Specs/Fallback/FallbackAsyncSpecs.cs +++ b/test/Polly.Specs/Fallback/FallbackAsyncSpecs.cs @@ -571,9 +571,6 @@ public async Task Should_execute_action_when_non_faulting_and_cancellationToken_ .Handle() .FallbackAsync(fallbackActionAsync); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -583,8 +580,13 @@ public async Task Should_execute_action_when_non_faulting_and_cancellationToken_ AttemptDuringWhichToCancel = null, }; - await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) - .Should().NotThrowAsync(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + .Should().NotThrowAsync(); + } + attemptsInvoked.Should().Be(1); fallbackActionExecuted.Should().BeFalse(); @@ -600,9 +602,6 @@ public async Task Should_execute_fallback_when_faulting_and_cancellationToken_no .Handle() .FallbackAsync(fallbackActionAsync); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -612,8 +611,13 @@ public async Task Should_execute_fallback_when_faulting_and_cancellationToken_no AttemptDuringWhichToCancel = null, }; - await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) - .Should().NotThrowAsync(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + .Should().NotThrowAsync(); + } + attemptsInvoked.Should().Be(1); fallbackActionExecuted.Should().BeTrue(); @@ -629,9 +633,6 @@ public async Task Should_not_execute_action_when_cancellationToken_cancelled_bef .Handle() .FallbackAsync(fallbackActionAsync); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -641,15 +642,19 @@ public async Task Should_not_execute_action_when_cancellationToken_cancelled_bef AttemptDuringWhichToCancel = null, // Cancellation token cancelled manually below - before any scenario execution. }; - cancellationTokenSource.Cancel(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + cancellationTokenSource.Cancel(); + + var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + .Should().ThrowAsync(); + ex.And.CancellationToken.Should().Be(cancellationToken); + } - var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) - .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); attemptsInvoked.Should().Be(0); fallbackActionExecuted.Should().BeFalse(); - } [Fact] @@ -662,9 +667,6 @@ public async Task Should_report_cancellation_and_not_execute_fallback_during_oth .Handle() .FallbackAsync(fallbackActionAsync); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -675,9 +677,15 @@ public async Task Should_report_cancellation_and_not_execute_fallback_during_oth ActionObservesCancellation = true }; - var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) - .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + .Should().ThrowAsync(); + ex.And.CancellationToken.Should().Be(cancellationToken); + } + attemptsInvoked.Should().Be(1); fallbackActionExecuted.Should().BeFalse(); @@ -694,9 +702,6 @@ public async Task Should_handle_cancellation_and_execute_fallback_during_otherwi .Or() .FallbackAsync(fallbackActionAsync); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -707,8 +712,14 @@ public async Task Should_handle_cancellation_and_execute_fallback_during_otherwi ActionObservesCancellation = true }; - await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) - .Should().NotThrowAsync(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + .Should().NotThrowAsync(); + } + attemptsInvoked.Should().Be(1); fallbackActionExecuted.Should().BeTrue(); @@ -724,9 +735,6 @@ public async Task Should_not_report_cancellation_and_not_execute_fallback_if_non .Handle() .FallbackAsync(fallbackActionAsync); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -737,8 +745,14 @@ public async Task Should_not_report_cancellation_and_not_execute_fallback_if_non ActionObservesCancellation = false }; - await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) - .Should().NotThrowAsync(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + .Should().NotThrowAsync(); + } + attemptsInvoked.Should().Be(1); fallbackActionExecuted.Should().BeFalse(); @@ -750,9 +764,6 @@ public async Task Should_report_unhandled_fault_and_not_execute_fallback_if_acti bool fallbackActionExecuted = false; Func fallbackActionAsync = _ => { fallbackActionExecuted = true; return TaskHelper.EmptyTask; }; - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - var policy = Policy .Handle() .FallbackAsync(fallbackActionAsync); @@ -767,8 +778,14 @@ public async Task Should_report_unhandled_fault_and_not_execute_fallback_if_acti ActionObservesCancellation = false }; - await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) - .Should().ThrowAsync(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + .Should().ThrowAsync(); + } + attemptsInvoked.Should().Be(1); fallbackActionExecuted.Should().BeFalse(); @@ -780,9 +797,6 @@ public async Task Should_handle_handled_fault_and_execute_fallback_following_fau bool fallbackActionExecuted = false; Func fallbackActionAsync = _ => { fallbackActionExecuted = true; return TaskHelper.EmptyTask; }; - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - var policy = Policy .Handle() .FallbackAsync(fallbackActionAsync); @@ -797,8 +811,14 @@ public async Task Should_handle_handled_fault_and_execute_fallback_following_fau ActionObservesCancellation = false }; - await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) - .Should().NotThrowAsync(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + .Should().NotThrowAsync(); + } + attemptsInvoked.Should().Be(1); fallbackActionExecuted.Should().BeTrue(); diff --git a/test/Polly.Specs/Fallback/FallbackSpecs.cs b/test/Polly.Specs/Fallback/FallbackSpecs.cs index 45cd162f7e3..be9077871e8 100644 --- a/test/Polly.Specs/Fallback/FallbackSpecs.cs +++ b/test/Polly.Specs/Fallback/FallbackSpecs.cs @@ -1030,8 +1030,6 @@ public void Should_execute_action_when_non_faulting_and_cancellationToken_not_ca .Handle() .Fallback(fallbackAction); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -1041,8 +1039,12 @@ public void Should_execute_action_when_non_faulting_and_cancellationToken_not_ca AttemptDuringWhichToCancel = null, }; - policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) - .Should().NotThrow(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) + .Should().NotThrow(); + } + attemptsInvoked.Should().Be(1); fallbackActionExecuted.Should().BeFalse(); @@ -1058,8 +1060,6 @@ public void Should_execute_fallback_when_faulting_and_cancellationToken_not_canc .Handle() .Fallback(fallbackAction); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -1069,8 +1069,12 @@ public void Should_execute_fallback_when_faulting_and_cancellationToken_not_canc AttemptDuringWhichToCancel = null, }; - policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) - .Should().NotThrow(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) + .Should().NotThrow(); + } + attemptsInvoked.Should().Be(1); fallbackActionExecuted.Should().BeTrue(); @@ -1086,9 +1090,6 @@ public void Should_not_execute_action_when_cancellationToken_cancelled_before_ex .Handle() .Fallback(fallbackAction); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -1098,11 +1099,17 @@ public void Should_not_execute_action_when_cancellationToken_cancelled_before_ex AttemptDuringWhichToCancel = null, // Cancellation token cancelled manually below - before any scenario execution. }; - cancellationTokenSource.Cancel(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + cancellationTokenSource.Cancel(); + + policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) + .Should().Throw() + .And.CancellationToken.Should().Be(cancellationToken); + } - policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) - .Should().Throw() - .And.CancellationToken.Should().Be(cancellationToken); attemptsInvoked.Should().Be(0); fallbackActionExecuted.Should().BeFalse(); @@ -1119,9 +1126,6 @@ public void Should_report_cancellation_and_not_execute_fallback_during_otherwise .Handle() .Fallback(fallbackAction); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -1132,9 +1136,14 @@ public void Should_report_cancellation_and_not_execute_fallback_during_otherwise ActionObservesCancellation = true }; - policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) - .Should().Throw() - .And.CancellationToken.Should().Be(cancellationToken); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) + .Should().Throw() + .And.CancellationToken.Should().Be(cancellationToken); + } + attemptsInvoked.Should().Be(1); fallbackActionExecuted.Should().BeFalse(); @@ -1151,8 +1160,6 @@ public void Should_handle_cancellation_and_execute_fallback_during_otherwise_non .Or() .Fallback(fallbackAction); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -1163,8 +1170,12 @@ public void Should_handle_cancellation_and_execute_fallback_during_otherwise_non ActionObservesCancellation = true }; - policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) - .Should().NotThrow(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) + .Should().NotThrow(); + } + attemptsInvoked.Should().Be(1); fallbackActionExecuted.Should().BeTrue(); @@ -1180,8 +1191,6 @@ public void Should_not_report_cancellation_and_not_execute_fallback_if_non_fault .Handle() .Fallback(fallbackAction); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -1192,10 +1201,13 @@ public void Should_not_report_cancellation_and_not_execute_fallback_if_non_fault ActionObservesCancellation = false }; - policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) - .Should().NotThrow(); - attemptsInvoked.Should().Be(1); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) + .Should().NotThrow(); + } + attemptsInvoked.Should().Be(1); fallbackActionExecuted.Should().BeFalse(); } @@ -1209,8 +1221,6 @@ public void Should_report_unhandled_fault_and_not_execute_fallback_if_action_exe .Handle() .Fallback(fallbackAction); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -1221,8 +1231,12 @@ public void Should_report_unhandled_fault_and_not_execute_fallback_if_action_exe ActionObservesCancellation = false }; - policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) - .Should().Throw(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) + .Should().Throw(); + } + attemptsInvoked.Should().Be(1); fallbackActionExecuted.Should().BeFalse(); @@ -1234,8 +1248,6 @@ public void Should_handle_handled_fault_and_execute_fallback_following_faulting_ bool fallbackActionExecuted = false; Action fallbackAction = () => { fallbackActionExecuted = true; }; - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - FallbackPolicy policy = Policy .Handle() .Fallback(fallbackAction); @@ -1250,8 +1262,12 @@ public void Should_handle_handled_fault_and_execute_fallback_following_faulting_ ActionObservesCancellation = false }; - policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) - .Should().NotThrow(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) + .Should().NotThrow(); + } + attemptsInvoked.Should().Be(1); fallbackActionExecuted.Should().BeTrue(); diff --git a/test/Polly.Specs/Fallback/FallbackTResultAsyncSpecs.cs b/test/Polly.Specs/Fallback/FallbackTResultAsyncSpecs.cs index 45350dde783..8cd19d88bf4 100644 --- a/test/Polly.Specs/Fallback/FallbackTResultAsyncSpecs.cs +++ b/test/Polly.Specs/Fallback/FallbackTResultAsyncSpecs.cs @@ -552,9 +552,6 @@ public async Task Should_execute_action_when_non_faulting_and_cancellationToken_ .OrResult(ResultPrimitive.FaultAgain) .FallbackAsync(fallbackAction); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -563,8 +560,14 @@ public async Task Should_execute_action_when_non_faulting_and_cancellationToken_ AttemptDuringWhichToCancel = null, }; - (await policy.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Good)) - .Should().Be(ResultPrimitive.Good); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + (await policy.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Good)) + .Should().Be(ResultPrimitive.Good); + } + attemptsInvoked.Should().Be(1); fallbackActionExecuted.Should().BeFalse(); @@ -581,9 +584,6 @@ public async Task Should_execute_fallback_when_faulting_and_cancellationToken_no .OrResult(ResultPrimitive.FaultAgain) .FallbackAsync(fallbackAction); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -592,8 +592,14 @@ public async Task Should_execute_fallback_when_faulting_and_cancellationToken_no AttemptDuringWhichToCancel = null, }; - (await policy.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Fault)) - .Should().Be(ResultPrimitive.Substitute); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + (await policy.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Fault)) + .Should().Be(ResultPrimitive.Substitute); + } + attemptsInvoked.Should().Be(1); fallbackActionExecuted.Should().BeTrue(); @@ -610,9 +616,6 @@ public async Task Should_not_execute_action_when_cancellationToken_cancelled_bef .OrResult(ResultPrimitive.FaultAgain) .FallbackAsync(fallbackAction); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -621,11 +624,17 @@ public async Task Should_not_execute_action_when_cancellationToken_cancelled_bef AttemptDuringWhichToCancel = null, // Cancellation token cancelled manually below - before any scenario execution. }; - cancellationTokenSource.Cancel(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + cancellationTokenSource.Cancel(); + + var ex = await policy.Awaiting(x => x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Fault)) + .Should().ThrowAsync(); + ex.And.CancellationToken.Should().Be(cancellationToken); + + } - var ex = await policy.Awaiting(x => x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Fault)) - .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); attemptsInvoked.Should().Be(0); fallbackActionExecuted.Should().BeFalse(); @@ -643,9 +652,6 @@ public async Task Should_report_cancellation_and_not_execute_fallback_during_oth .OrResult(ResultPrimitive.FaultAgain) .FallbackAsync(fallbackAction); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -655,9 +661,15 @@ public async Task Should_report_cancellation_and_not_execute_fallback_during_oth ActionObservesCancellation = true }; - var ex = await policy.Awaiting(x => x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Good)) - .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + var ex = await policy.Awaiting(x => x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Good)) + .Should().ThrowAsync(); + ex.And.CancellationToken.Should().Be(cancellationToken); + } + attemptsInvoked.Should().Be(1); fallbackActionExecuted.Should().BeFalse(); @@ -675,9 +687,6 @@ public async Task Should_handle_cancellation_and_execute_fallback_during_otherwi .Or() .FallbackAsync(fallbackAction); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -687,8 +696,14 @@ public async Task Should_handle_cancellation_and_execute_fallback_during_otherwi ActionObservesCancellation = true }; - (await policy.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Good)) - .Should().Be(ResultPrimitive.Substitute); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + (await policy.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Good)) + .Should().Be(ResultPrimitive.Substitute); + } + attemptsInvoked.Should().Be(1); fallbackActionExecuted.Should().BeTrue(); @@ -705,9 +720,6 @@ public async Task Should_not_report_cancellation_and_not_execute_fallback_if_non .OrResult(ResultPrimitive.FaultAgain) .FallbackAsync(fallbackAction); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -717,8 +729,14 @@ public async Task Should_not_report_cancellation_and_not_execute_fallback_if_non ActionObservesCancellation = false }; - (await policy.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Good)) - .Should().Be(ResultPrimitive.Good); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + (await policy.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Good)) + .Should().Be(ResultPrimitive.Good); + } + attemptsInvoked.Should().Be(1); fallbackActionExecuted.Should().BeFalse(); @@ -735,9 +753,6 @@ public async Task Should_report_unhandled_fault_and_not_execute_fallback_if_acti .OrResult(ResultPrimitive.FaultAgain) .FallbackAsync(fallbackAction); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -747,8 +762,14 @@ public async Task Should_report_unhandled_fault_and_not_execute_fallback_if_acti ActionObservesCancellation = false }; - (await policy.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, ResultPrimitive.FaultYetAgain)) - .Should().Be(ResultPrimitive.FaultYetAgain); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + (await policy.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, ResultPrimitive.FaultYetAgain)) + .Should().Be(ResultPrimitive.FaultYetAgain); + } + attemptsInvoked.Should().Be(1); fallbackActionExecuted.Should().BeFalse(); @@ -760,9 +781,6 @@ public async Task Should_handle_handled_fault_and_execute_fallback_following_fau bool fallbackActionExecuted = false; Func> fallbackAction = _ => { fallbackActionExecuted = true; return Task.FromResult(ResultPrimitive.Substitute); }; - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - var policy = Policy .HandleResult(ResultPrimitive.Fault) .OrResult(ResultPrimitive.FaultAgain) @@ -777,8 +795,14 @@ public async Task Should_handle_handled_fault_and_execute_fallback_following_fau ActionObservesCancellation = false }; - (await policy.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Fault)) - .Should().Be(ResultPrimitive.Substitute); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + (await policy.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Fault)) + .Should().Be(ResultPrimitive.Substitute); + } + attemptsInvoked.Should().Be(1); fallbackActionExecuted.Should().BeTrue(); diff --git a/test/Polly.Specs/Fallback/FallbackTResultSpecs.cs b/test/Polly.Specs/Fallback/FallbackTResultSpecs.cs index ce5f04f6bd3..70686eab2ea 100644 --- a/test/Polly.Specs/Fallback/FallbackTResultSpecs.cs +++ b/test/Polly.Specs/Fallback/FallbackTResultSpecs.cs @@ -602,8 +602,6 @@ public void Should_execute_action_when_non_faulting_and_cancellationToken_not_ca .OrResult(ResultPrimitive.FaultAgain) .Fallback(fallbackAction); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -612,8 +610,12 @@ public void Should_execute_action_when_non_faulting_and_cancellationToken_not_ca AttemptDuringWhichToCancel = null, }; - policy.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Good) - .Should().Be(ResultPrimitive.Good); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + policy.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Good) + .Should().Be(ResultPrimitive.Good); + } + attemptsInvoked.Should().Be(1); fallbackActionExecuted.Should().BeFalse(); @@ -630,8 +632,6 @@ public void Should_execute_fallback_when_faulting_and_cancellationToken_not_canc .OrResult(ResultPrimitive.FaultAgain) .Fallback(fallbackAction); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -640,8 +640,12 @@ public void Should_execute_fallback_when_faulting_and_cancellationToken_not_canc AttemptDuringWhichToCancel = null, }; - policy.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Fault) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + policy.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Fault) .Should().Be(ResultPrimitive.Substitute); + } + attemptsInvoked.Should().Be(1); fallbackActionExecuted.Should().BeTrue(); @@ -658,9 +662,6 @@ public void Should_not_execute_action_when_cancellationToken_cancelled_before_ex .OrResult(ResultPrimitive.FaultAgain) .Fallback(fallbackAction); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -669,11 +670,16 @@ public void Should_not_execute_action_when_cancellationToken_cancelled_before_ex AttemptDuringWhichToCancel = null, // Cancellation token cancelled manually below - before any scenario execution. }; - cancellationTokenSource.Cancel(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + cancellationTokenSource.Cancel(); + + policy.Invoking(x => x.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Fault)) + .Should().Throw() + .And.CancellationToken.Should().Be(cancellationToken); + } - policy.Invoking(x => x.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Fault)) - .Should().Throw() - .And.CancellationToken.Should().Be(cancellationToken); attemptsInvoked.Should().Be(0); fallbackActionExecuted.Should().BeFalse(); @@ -691,9 +697,6 @@ public void Should_report_cancellation_and_not_execute_fallback_during_otherwise .OrResult(ResultPrimitive.FaultAgain) .Fallback(fallbackAction); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -703,9 +706,15 @@ public void Should_report_cancellation_and_not_execute_fallback_during_otherwise ActionObservesCancellation = true }; - policy.Invoking(x => x.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Good)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + policy.Invoking(x => x.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Good)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); + } + attemptsInvoked.Should().Be(1); fallbackActionExecuted.Should().BeFalse(); @@ -723,8 +732,6 @@ public void Should_handle_cancellation_and_execute_fallback_during_otherwise_non .Or() .Fallback(fallbackAction); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -734,8 +741,13 @@ public void Should_handle_cancellation_and_execute_fallback_during_otherwise_non ActionObservesCancellation = true }; - policy.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Good) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + policy.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Good) .Should().Be(ResultPrimitive.Substitute); + } + attemptsInvoked.Should().Be(1); fallbackActionExecuted.Should().BeTrue(); @@ -752,8 +764,6 @@ public void Should_not_report_cancellation_and_not_execute_fallback_if_non_fault .OrResult(ResultPrimitive.FaultAgain) .Fallback(fallbackAction); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -763,8 +773,14 @@ public void Should_not_report_cancellation_and_not_execute_fallback_if_non_fault ActionObservesCancellation = false }; - policy.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Good) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + policy.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Good) .Should().Be(ResultPrimitive.Good); + } + attemptsInvoked.Should().Be(1); fallbackActionExecuted.Should().BeFalse(); @@ -781,8 +797,6 @@ public void Should_report_unhandled_fault_and_not_execute_fallback_if_action_exe .OrResult(ResultPrimitive.FaultAgain) .Fallback(fallbackAction); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -792,8 +806,13 @@ public void Should_report_unhandled_fault_and_not_execute_fallback_if_action_exe ActionObservesCancellation = false }; - policy.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, ResultPrimitive.FaultYetAgain) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + policy.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, ResultPrimitive.FaultYetAgain) .Should().Be(ResultPrimitive.FaultYetAgain); + } + attemptsInvoked.Should().Be(1); fallbackActionExecuted.Should().BeFalse(); @@ -805,9 +824,6 @@ public void Should_handle_handled_fault_and_execute_fallback_following_faulting_ bool fallbackActionExecuted = false; Func fallbackAction = () => { fallbackActionExecuted = true; return ResultPrimitive.Substitute; }; - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - FallbackPolicy policy = Policy .HandleResult(ResultPrimitive.Fault) .OrResult(ResultPrimitive.FaultAgain) @@ -822,8 +838,14 @@ public void Should_handle_handled_fault_and_execute_fallback_following_faulting_ ActionObservesCancellation = false }; - policy.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Fault) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + policy.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Fault) .Should().Be(ResultPrimitive.Substitute); + } + attemptsInvoked.Should().Be(1); fallbackActionExecuted.Should().BeTrue(); diff --git a/test/Polly.Specs/Helpers/PolicyExtensions.cs b/test/Polly.Specs/Helpers/PolicyExtensions.cs index 0667622c53c..3ab856364b7 100644 --- a/test/Polly.Specs/Helpers/PolicyExtensions.cs +++ b/test/Polly.Specs/Helpers/PolicyExtensions.cs @@ -19,7 +19,8 @@ public static void RaiseException(this Policy policy, TException ins NumberOfTimesToRaiseException = 1 }; - policy.RaiseExceptionAndOrCancellation(scenario, new CancellationTokenSource(), () => { }, _ => instance); + using CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); + policy.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, () => { }, _ => instance); } public static void RaiseException(this Policy policy, Action? configureException = null) @@ -44,7 +45,8 @@ public static void RaiseException(this Policy policy, int numberOfTi return exception; }; - policy.RaiseExceptionAndOrCancellation(scenario, new CancellationTokenSource(), () => { }, exceptionFactory); + using CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); + policy.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, () => { }, exceptionFactory); } public static void RaiseExceptionAndOrCancellation(this Policy policy, ExceptionAndOrCancellationScenario scenario, CancellationTokenSource cancellationTokenSource, Action onExecute) diff --git a/test/Polly.Specs/Helpers/PolicyExtensionsAsync.cs b/test/Polly.Specs/Helpers/PolicyExtensionsAsync.cs index 684eab49aba..e5e71935c9a 100644 --- a/test/Polly.Specs/Helpers/PolicyExtensionsAsync.cs +++ b/test/Polly.Specs/Helpers/PolicyExtensionsAsync.cs @@ -21,7 +21,8 @@ public static Task RaiseExceptionAsync(this AsyncPolicy policy, TExc NumberOfTimesToRaiseException = 1 }; - return policy.RaiseExceptionAndOrCancellationAsync(scenario, new CancellationTokenSource(), () => { }, _ => instance); + using CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); + return policy.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, () => { }, _ => instance); } public static Task RaiseExceptionAsync(this AsyncPolicy policy, Action? configureException = null) @@ -46,7 +47,8 @@ public static Task RaiseExceptionAsync(this AsyncPolicy policy, int return exception; }; - return policy.RaiseExceptionAndOrCancellationAsync(scenario, new CancellationTokenSource(), () => { }, exceptionFactory); + using CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); + return policy.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, () => { }, exceptionFactory); } public static Task RaiseExceptionAndOrCancellationAsync(this AsyncPolicy policy, ExceptionAndOrCancellationScenario scenario, CancellationTokenSource cancellationTokenSource, Action onExecute) diff --git a/test/Polly.Specs/Polly.Specs.csproj b/test/Polly.Specs/Polly.Specs.csproj index b10f1158c4b..e5ff54a71c1 100644 --- a/test/Polly.Specs/Polly.Specs.csproj +++ b/test/Polly.Specs/Polly.Specs.csproj @@ -1,4 +1,4 @@ - + net6.0;net7.0;net8.0 @@ -8,7 +8,7 @@ 75,60,70 [Polly]* true - $(NoWarn);S103;S104;CA2000;IDE0011;SA1600;SA1204;CA2008;CA1806;CA2201; + $(NoWarn);S103;S104;IDE0011;SA1600;SA1204;CA2008;CA1806;CA2201; $(NoWarn);S3878;CA1030;S3717;SA1129;S1402;SA1649;SA1402;S4056;CA1031 $(NoWarn);S2184; diff --git a/test/Polly.Specs/RateLimit/RateLimitPolicySpecsBase.cs b/test/Polly.Specs/RateLimit/RateLimitPolicySpecsBase.cs index e215e497441..98653ae95d5 100644 --- a/test/Polly.Specs/RateLimit/RateLimitPolicySpecsBase.cs +++ b/test/Polly.Specs/RateLimit/RateLimitPolicySpecsBase.cs @@ -302,5 +302,7 @@ public void Given_immediate_parallel_contention_ratelimiter_still_only_permits_o var results = tasks.Select(t => t.Result).ToList(); results.Count(r => r.PermitExecution).Should().Be(1); results.Count(r => !r.PermitExecution).Should().Be(parallelContention - 1); + + gate.Dispose(); } } diff --git a/test/Polly.Specs/RateLimit/TokenBucketRateLimiterTestsBase.cs b/test/Polly.Specs/RateLimit/TokenBucketRateLimiterTestsBase.cs index cdf067bd52b..15e62267f36 100644 --- a/test/Polly.Specs/RateLimit/TokenBucketRateLimiterTestsBase.cs +++ b/test/Polly.Specs/RateLimit/TokenBucketRateLimiterTestsBase.cs @@ -180,7 +180,7 @@ public void Given_immediate_parallel_contention_ratelimiter_still_only_permits_o var rateLimiter = GetRateLimiter(onePer, 1); // Arrange - parallel tasks all waiting on a manual reset event. - ManualResetEventSlim gate = new ManualResetEventSlim(); + ManualResetEventSlim gate = new(); Task<(bool PermitExecution, TimeSpan RetryAfter)>[] tasks = new Task<(bool, TimeSpan)>[parallelContention]; for (int i = 0; i < parallelContention; i++) { @@ -201,5 +201,7 @@ public void Given_immediate_parallel_contention_ratelimiter_still_only_permits_o var results = tasks.Select(t => t.Result).ToList(); results.Count(r => r.PermitExecution).Should().Be(1); results.Count(r => !r.PermitExecution).Should().Be(parallelContention - 1); + + gate.Dispose(); } } diff --git a/test/Polly.Specs/Retry/RetryAsyncSpecs.cs b/test/Polly.Specs/Retry/RetryAsyncSpecs.cs index c202019c109..8d6138783d1 100644 --- a/test/Polly.Specs/Retry/RetryAsyncSpecs.cs +++ b/test/Polly.Specs/Retry/RetryAsyncSpecs.cs @@ -391,9 +391,6 @@ public async Task Should_execute_action_when_non_faulting_and_cancellationToken_ .Handle() .RetryAsync(3); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -403,8 +400,13 @@ public async Task Should_execute_action_when_non_faulting_and_cancellationToken_ AttemptDuringWhichToCancel = null, }; - await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) - .Should().NotThrowAsync(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + .Should().NotThrowAsync(); + } attemptsInvoked.Should().Be(1); } @@ -416,9 +418,6 @@ public async Task Should_execute_all_tries_when_faulting_and_cancellationToken_n .Handle() .RetryAsync(3); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -428,8 +427,13 @@ public async Task Should_execute_all_tries_when_faulting_and_cancellationToken_n AttemptDuringWhichToCancel = null, }; - await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().ThrowAsync(); + } attemptsInvoked.Should().Be(1 + 3); } @@ -441,9 +445,6 @@ public async Task Should_not_execute_action_when_cancellationToken_cancelled_bef .Handle() .RetryAsync(3); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -453,11 +454,15 @@ public async Task Should_not_execute_action_when_cancellationToken_cancelled_bef AttemptDuringWhichToCancel = null, // Cancellation token cancelled manually below - before any scenario execution. }; - cancellationTokenSource.Cancel(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + cancellationTokenSource.Cancel(); - var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) - .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + .Should().ThrowAsync(); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(0); } @@ -469,9 +474,6 @@ public async Task Should_report_cancellation_during_otherwise_non_faulting_actio .Handle() .RetryAsync(3); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -482,9 +484,14 @@ public async Task Should_report_cancellation_during_otherwise_non_faulting_actio ActionObservesCancellation = true }; - var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -496,9 +503,6 @@ public async Task Should_report_cancellation_during_faulting_initial_action_exec .Handle() .RetryAsync(3); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -509,9 +513,14 @@ public async Task Should_report_cancellation_during_faulting_initial_action_exec ActionObservesCancellation = true }; - var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -523,9 +532,6 @@ public async Task Should_report_cancellation_during_faulting_initial_action_exec .Handle() .RetryAsync(3); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -536,9 +542,13 @@ public async Task Should_report_cancellation_during_faulting_initial_action_exec ActionObservesCancellation = false }; - var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -550,9 +560,6 @@ public async Task Should_report_cancellation_during_faulting_retried_action_exec .Handle() .RetryAsync(3); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -563,9 +570,14 @@ public async Task Should_report_cancellation_during_faulting_retried_action_exec ActionObservesCancellation = true }; - var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(2); } @@ -577,9 +589,6 @@ public async Task Should_report_cancellation_during_faulting_retried_action_exec .Handle() .RetryAsync(3); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -590,9 +599,14 @@ public async Task Should_report_cancellation_during_faulting_retried_action_exec ActionObservesCancellation = false }; - var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(2); } @@ -604,9 +618,6 @@ public async Task Should_report_cancellation_during_faulting_last_retry_executio .Handle() .RetryAsync(3); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -617,9 +628,14 @@ public async Task Should_report_cancellation_during_faulting_last_retry_executio ActionObservesCancellation = true }; - var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1 + 3); } @@ -631,9 +647,6 @@ public async Task Should_report_faulting_from_faulting_last_retry_execution_when .Handle() .RetryAsync(3); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -644,8 +657,13 @@ public async Task Should_report_faulting_from_faulting_last_retry_execution_when ActionObservesCancellation = false }; - await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().ThrowAsync(); + } attemptsInvoked.Should().Be(1 + 3); } @@ -653,29 +671,31 @@ await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync() - .RetryAsync(3, (_, _) => - { - cancellationTokenSource.Cancel(); - }); - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; Scenario scenario = new Scenario { NumberOfTimesToRaiseException = 1 + 3, - AttemptDuringWhichToCancel = null, // Cancellation during onRetry instead - see above. + AttemptDuringWhichToCancel = null, // Cancellation during onRetry instead - see below. ActionObservesCancellation = false }; - var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) - .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + var policy = Policy + .Handle() + .RetryAsync(3, (_, _) => + { + cancellationTokenSource.Cancel(); + }); + + var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + .Should().ThrowAsync(); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -687,9 +707,6 @@ public async Task Should_execute_func_returning_value_when_cancellationToken_not .Handle() .RetryAsync(3); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -701,9 +718,14 @@ public async Task Should_execute_func_returning_value_when_cancellationToken_not AttemptDuringWhichToCancel = null, }; - Func action = async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true); - await policy.Awaiting(action) - .Should().NotThrowAsync(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + Func action = async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true); + await policy.Awaiting(action) + .Should().NotThrowAsync(); + } result.Should().BeTrue(); @@ -717,9 +739,6 @@ public async Task Should_honour_and_report_cancellation_during_func_execution() .Handle() .RetryAsync(3); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -732,9 +751,14 @@ public async Task Should_honour_and_report_cancellation_during_func_execution() ActionObservesCancellation = true }; - Func action = async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true); - var ex = await policy.Awaiting(action).Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + Func action = async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true); + var ex = await policy.Awaiting(action).Should().ThrowAsync(); + ex.And.CancellationToken.Should().Be(cancellationToken); + } result.Should().Be(null); diff --git a/test/Polly.Specs/Retry/RetryForeverAsyncSpecs.cs b/test/Polly.Specs/Retry/RetryForeverAsyncSpecs.cs index bf54904df26..58c8b472a9b 100644 --- a/test/Polly.Specs/Retry/RetryForeverAsyncSpecs.cs +++ b/test/Polly.Specs/Retry/RetryForeverAsyncSpecs.cs @@ -245,9 +245,6 @@ public async Task Should_execute_action_when_non_faulting_and_cancellationToken_ .Handle() .RetryForeverAsync(); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -257,8 +254,13 @@ public async Task Should_execute_action_when_non_faulting_and_cancellationToken_ AttemptDuringWhichToCancel = null, }; - await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().NotThrowAsync(); + } attemptsInvoked.Should().Be(1); } @@ -270,9 +272,6 @@ public async Task Should_not_execute_action_when_cancellationToken_cancelled_bef .Handle() .RetryForeverAsync(); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -282,11 +281,15 @@ public async Task Should_not_execute_action_when_cancellationToken_cancelled_bef AttemptDuringWhichToCancel = null, // Cancellation token cancelled manually below - before any scenario execution. }; - cancellationTokenSource.Cancel(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + cancellationTokenSource.Cancel(); - var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) - .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + .Should().ThrowAsync(); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(0); } @@ -298,9 +301,6 @@ public async Task Should_report_cancellation_during_otherwise_non_faulting_actio .Handle() .RetryForeverAsync(); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -311,9 +311,14 @@ public async Task Should_report_cancellation_during_otherwise_non_faulting_actio ActionObservesCancellation = true }; - var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -325,9 +330,6 @@ public async Task Should_report_cancellation_during_faulting_initial_action_exec .Handle() .RetryForeverAsync(); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -338,9 +340,14 @@ public async Task Should_report_cancellation_during_faulting_initial_action_exec ActionObservesCancellation = true }; - var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -352,9 +359,6 @@ public async Task Should_report_cancellation_during_faulting_initial_action_exec .Handle() .RetryForeverAsync(); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -365,9 +369,14 @@ public async Task Should_report_cancellation_during_faulting_initial_action_exec ActionObservesCancellation = false }; - var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -379,9 +388,6 @@ public async Task Should_report_cancellation_during_faulting_retried_action_exec .Handle() .RetryForeverAsync(); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -392,9 +398,14 @@ public async Task Should_report_cancellation_during_faulting_retried_action_exec ActionObservesCancellation = true }; - var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(2); } @@ -406,9 +417,6 @@ public async Task Should_report_cancellation_during_faulting_retried_action_exec .Handle() .RetryForeverAsync(); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -419,9 +427,14 @@ public async Task Should_report_cancellation_during_faulting_retried_action_exec ActionObservesCancellation = false }; - var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(2); } @@ -429,30 +442,32 @@ public async Task Should_report_cancellation_during_faulting_retried_action_exec [Fact] public async Task Should_report_cancellation_after_faulting_action_execution_and_cancel_further_retries_if_onRetry_invokes_cancellation() { - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - - var policy = Policy - .Handle() - .RetryForeverAsync( - _ => - { - cancellationTokenSource.Cancel(); - }); - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; Scenario scenario = new Scenario { NumberOfTimesToRaiseException = 1 + 3, - AttemptDuringWhichToCancel = null, // Cancellation during onRetry instead - see above. + AttemptDuringWhichToCancel = null, // Cancellation during onRetry instead - see below. ActionObservesCancellation = false }; - var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) - .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + var policy = Policy + .Handle() + .RetryForeverAsync( + _ => + { + cancellationTokenSource.Cancel(); + }); + + var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + .Should().ThrowAsync(); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -464,9 +479,6 @@ public async Task Should_execute_func_returning_value_when_cancellationToken_not .Handle() .RetryForeverAsync(); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -478,9 +490,14 @@ public async Task Should_execute_func_returning_value_when_cancellationToken_not AttemptDuringWhichToCancel = null, }; - Func action = async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true); - await policy.Awaiting(action) - .Should().NotThrowAsync(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + Func action = async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true); + await policy.Awaiting(action) + .Should().NotThrowAsync(); + } result.Should().BeTrue(); @@ -494,9 +511,6 @@ public async Task Should_honour_and_report_cancellation_during_func_execution() .Handle() .RetryForeverAsync(); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -509,9 +523,14 @@ public async Task Should_honour_and_report_cancellation_during_func_execution() ActionObservesCancellation = true }; - Func action = async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true); - var ex = await policy.Awaiting(action).Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + Func action = async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true); + var ex = await policy.Awaiting(action).Should().ThrowAsync(); + ex.And.CancellationToken.Should().Be(cancellationToken); + } result.Should().Be(null); diff --git a/test/Polly.Specs/Retry/RetrySpecs.cs b/test/Polly.Specs/Retry/RetrySpecs.cs index 420f72920e0..5124abd6f17 100644 --- a/test/Polly.Specs/Retry/RetrySpecs.cs +++ b/test/Polly.Specs/Retry/RetrySpecs.cs @@ -496,9 +496,6 @@ public void Should_execute_action_when_non_faulting_and_cancellationToken_not_ca .Handle() .Retry(3); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -508,8 +505,13 @@ public void Should_execute_action_when_non_faulting_and_cancellationToken_not_ca AttemptDuringWhichToCancel = null, }; - policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) .Should().NotThrow(); + } attemptsInvoked.Should().Be(1); } @@ -521,9 +523,6 @@ public void Should_execute_all_tries_when_faulting_and_cancellationToken_not_can .Handle() .Retry(3); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -533,8 +532,13 @@ public void Should_execute_all_tries_when_faulting_and_cancellationToken_not_can AttemptDuringWhichToCancel = null, }; - policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) .Should().Throw(); + } attemptsInvoked.Should().Be(1 + 3); } @@ -546,9 +550,6 @@ public void Should_not_execute_action_when_cancellationToken_cancelled_before_ex .Handle() .Retry(3); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -558,11 +559,15 @@ public void Should_not_execute_action_when_cancellationToken_cancelled_before_ex AttemptDuringWhichToCancel = null, // Cancellation token cancelled manually below - before any scenario execution. }; - cancellationTokenSource.Cancel(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + cancellationTokenSource.Cancel(); - policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) - .Should().Throw() - .And.CancellationToken.Should().Be(cancellationToken); + policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) + .Should().Throw() + .And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(0); } @@ -574,9 +579,6 @@ public void Should_report_cancellation_during_otherwise_non_faulting_action_exec .Handle() .Retry(3); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -587,9 +589,14 @@ public void Should_report_cancellation_during_otherwise_non_faulting_action_exec ActionObservesCancellation = true }; - policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -601,9 +608,6 @@ public void Should_report_cancellation_during_faulting_initial_action_execution_ .Handle() .Retry(3); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -614,9 +618,14 @@ public void Should_report_cancellation_during_faulting_initial_action_execution_ ActionObservesCancellation = true }; - policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -628,9 +637,6 @@ public void Should_report_cancellation_during_faulting_initial_action_execution_ .Handle() .Retry(3); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -641,9 +647,14 @@ public void Should_report_cancellation_during_faulting_initial_action_execution_ ActionObservesCancellation = false }; - policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -655,9 +666,6 @@ public void Should_report_cancellation_during_faulting_retried_action_execution_ .Handle() .Retry(3); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -668,9 +676,14 @@ public void Should_report_cancellation_during_faulting_retried_action_execution_ ActionObservesCancellation = true }; - policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(2); } @@ -682,9 +695,6 @@ public void Should_report_cancellation_during_faulting_retried_action_execution_ .Handle() .Retry(3); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -695,9 +705,14 @@ public void Should_report_cancellation_during_faulting_retried_action_execution_ ActionObservesCancellation = false }; - policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(2); } @@ -709,9 +724,6 @@ public void Should_report_cancellation_during_faulting_last_retry_execution_when .Handle() .Retry(3); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -722,9 +734,14 @@ public void Should_report_cancellation_during_faulting_last_retry_execution_when ActionObservesCancellation = true }; - policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1 + 3); } @@ -736,9 +753,6 @@ public void Should_report_faulting_from_faulting_last_retry_execution_when_user_ .Handle() .Retry(3); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -749,8 +763,13 @@ public void Should_report_faulting_from_faulting_last_retry_execution_when_user_ ActionObservesCancellation = false }; - policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) .Should().Throw(); + } attemptsInvoked.Should().Be(1 + 3); } @@ -758,29 +777,31 @@ public void Should_report_faulting_from_faulting_last_retry_execution_when_user_ [Fact] public void Should_report_cancellation_after_faulting_action_execution_and_cancel_further_retries_if_onRetry_invokes_cancellation() { - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - - var policy = Policy - .Handle() - .Retry(3, (_, _) => - { - cancellationTokenSource.Cancel(); - }); - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; PolicyExtensions.ExceptionAndOrCancellationScenario scenario = new PolicyExtensions.ExceptionAndOrCancellationScenario { NumberOfTimesToRaiseException = 1 + 3, - AttemptDuringWhichToCancel = null, // Cancellation during onRetry instead - see above. + AttemptDuringWhichToCancel = null, // Cancellation during onRetry instead - see below. ActionObservesCancellation = false }; - policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) - .Should().Throw() - .And.CancellationToken.Should().Be(cancellationToken); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + var policy = Policy + .Handle() + .Retry(3, (_, _) => + { + cancellationTokenSource.Cancel(); + }); + + policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) + .Should().Throw() + .And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -792,9 +813,6 @@ public void Should_execute_func_returning_value_when_cancellationToken_not_cance .Handle() .Retry(3); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -806,8 +824,13 @@ public void Should_execute_func_returning_value_when_cancellationToken_not_cance AttemptDuringWhichToCancel = null, }; - policy.Invoking(x => result = x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute, true)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + policy.Invoking(x => result = x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute, true)) .Should().NotThrow(); + } result.Should().BeTrue(); @@ -821,9 +844,6 @@ public void Should_honour_and_report_cancellation_during_func_execution() .Handle() .Retry(3); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -836,8 +856,13 @@ public void Should_honour_and_report_cancellation_during_func_execution() ActionObservesCancellation = true }; - policy.Invoking(x => result = x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute, true)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + policy.Invoking(x => result = x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute, true)) .Should().Throw().And.CancellationToken.Should().Be(cancellationToken); + } result.Should().Be(null); diff --git a/test/Polly.Specs/Retry/RetryTResultSpecs.cs b/test/Polly.Specs/Retry/RetryTResultSpecs.cs index 36b0a1328bc..63bc45b1f65 100644 --- a/test/Polly.Specs/Retry/RetryTResultSpecs.cs +++ b/test/Polly.Specs/Retry/RetryTResultSpecs.cs @@ -407,9 +407,6 @@ public void Should_execute_action_when_non_faulting_and_cancellationToken_not_ca .HandleResult(ResultPrimitive.Fault) .Retry(3); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -418,8 +415,13 @@ public void Should_execute_action_when_non_faulting_and_cancellationToken_not_ca AttemptDuringWhichToCancel = null, }; - policy.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Good) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + policy.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Good) .Should().Be(ResultPrimitive.Good); + } attemptsInvoked.Should().Be(1); } @@ -431,9 +433,6 @@ public void Should_execute_all_tries_when_faulting_and_cancellationToken_not_can .HandleResult(ResultPrimitive.Fault) .Retry(3); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -442,12 +441,17 @@ public void Should_execute_all_tries_when_faulting_and_cancellationToken_not_can AttemptDuringWhichToCancel = null, }; - policy.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + policy.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Good) .Should().Be(ResultPrimitive.Good); + } attemptsInvoked.Should().Be(1 + 3); } @@ -459,9 +463,6 @@ public void Should_not_execute_action_when_cancellationToken_cancelled_before_ex .HandleResult(ResultPrimitive.Fault) .Retry(3); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -470,15 +471,19 @@ public void Should_not_execute_action_when_cancellationToken_cancelled_before_ex AttemptDuringWhichToCancel = null, // Cancellation token cancelled manually below - before any scenario execution. }; - cancellationTokenSource.Cancel(); - - policy.Invoking(x => x.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, - ResultPrimitive.Fault, - ResultPrimitive.Fault, - ResultPrimitive.Fault, - ResultPrimitive.Good)) - .Should().Throw() - .And.CancellationToken.Should().Be(cancellationToken); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + cancellationTokenSource.Cancel(); + + policy.Invoking(x => x.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, + ResultPrimitive.Fault, + ResultPrimitive.Fault, + ResultPrimitive.Fault, + ResultPrimitive.Good)) + .Should().Throw() + .And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(0); } @@ -490,9 +495,6 @@ public void Should_report_cancellation_during_otherwise_non_faulting_action_exec .HandleResult(ResultPrimitive.Fault) .Retry(3); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -502,13 +504,18 @@ public void Should_report_cancellation_during_otherwise_non_faulting_action_exec ActionObservesCancellation = true }; - policy.Invoking(x => x.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + policy.Invoking(x => x.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Good, ResultPrimitive.Good, ResultPrimitive.Good, ResultPrimitive.Good)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -520,9 +527,6 @@ public void Should_report_cancellation_during_faulting_initial_action_execution_ .HandleResult(ResultPrimitive.Fault) .Retry(3); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -532,13 +536,18 @@ public void Should_report_cancellation_during_faulting_initial_action_execution_ ActionObservesCancellation = true }; - policy.Invoking(x => x.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + policy.Invoking(x => x.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Good)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -550,9 +559,6 @@ public void Should_report_cancellation_during_faulting_initial_action_execution_ .HandleResult(ResultPrimitive.Fault) .Retry(3); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -562,13 +568,18 @@ public void Should_report_cancellation_during_faulting_initial_action_execution_ ActionObservesCancellation = false }; - policy.Invoking(x => x.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + policy.Invoking(x => x.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Good)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -580,9 +591,6 @@ public void Should_report_cancellation_during_faulting_retried_action_execution_ .HandleResult(ResultPrimitive.Fault) .Retry(3); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -592,13 +600,18 @@ public void Should_report_cancellation_during_faulting_retried_action_execution_ ActionObservesCancellation = true }; - policy.Invoking(x => x.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + policy.Invoking(x => x.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Good)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(2); } @@ -610,9 +623,6 @@ public void Should_report_cancellation_during_faulting_retried_action_execution_ .HandleResult(ResultPrimitive.Fault) .Retry(3); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -622,13 +632,18 @@ public void Should_report_cancellation_during_faulting_retried_action_execution_ ActionObservesCancellation = false }; - policy.Invoking(x => x.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + policy.Invoking(x => x.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Good)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(2); } @@ -640,9 +655,6 @@ public void Should_report_cancellation_during_faulting_last_retry_execution_when .HandleResult(ResultPrimitive.Fault) .Retry(3); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -652,7 +664,11 @@ public void Should_report_cancellation_during_faulting_last_retry_execution_when ActionObservesCancellation = true }; - policy.Invoking(x => x.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + policy.Invoking(x => x.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Fault, @@ -660,6 +676,7 @@ public void Should_report_cancellation_during_faulting_last_retry_execution_when ResultPrimitive.Good)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1 + 3); } @@ -671,9 +688,6 @@ public void Should_report_faulting_from_faulting_last_retry_execution_when_user_ .HandleResult(ResultPrimitive.Fault) .Retry(3); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -683,13 +697,18 @@ public void Should_report_faulting_from_faulting_last_retry_execution_when_user_ ActionObservesCancellation = false }; - policy.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + policy.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Good) .Should().Be(ResultPrimitive.Fault); + } attemptsInvoked.Should().Be(1 + 3); } @@ -697,32 +716,34 @@ public void Should_report_faulting_from_faulting_last_retry_execution_when_user_ [Fact] public void Should_report_cancellation_after_faulting_action_execution_and_cancel_further_retries_if_onRetry_invokes_cancellation() { - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - - RetryPolicy policy = Policy - .HandleResult(ResultPrimitive.Fault) - .Retry(3, (_, _) => - { - cancellationTokenSource.Cancel(); - }); - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; Scenario scenario = new Scenario { - AttemptDuringWhichToCancel = null, // Cancellation during onRetry instead - see above. + AttemptDuringWhichToCancel = null, // Cancellation during onRetry instead - see below. ActionObservesCancellation = false }; - policy.Invoking(x => x.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, - ResultPrimitive.Fault, - ResultPrimitive.Fault, - ResultPrimitive.Fault, - ResultPrimitive.Good)) - .Should().Throw() - .And.CancellationToken.Should().Be(cancellationToken); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + RetryPolicy policy = Policy + .HandleResult(ResultPrimitive.Fault) + .Retry(3, (_, _) => + { + cancellationTokenSource.Cancel(); + }); + + policy.Invoking(x => x.RaiseResultSequenceAndOrCancellation(scenario, cancellationTokenSource, onExecute, + ResultPrimitive.Fault, + ResultPrimitive.Fault, + ResultPrimitive.Fault, + ResultPrimitive.Good)) + .Should().Throw() + .And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } diff --git a/test/Polly.Specs/Retry/RetryTResultSpecsAsync.cs b/test/Polly.Specs/Retry/RetryTResultSpecsAsync.cs index 4220a4555f7..5ba85c3f60f 100644 --- a/test/Polly.Specs/Retry/RetryTResultSpecsAsync.cs +++ b/test/Polly.Specs/Retry/RetryTResultSpecsAsync.cs @@ -447,9 +447,6 @@ public async Task Should_execute_all_tries_when_faulting_and_cancellationToken_n .HandleResult(ResultPrimitive.Fault) .RetryAsync(3); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -458,12 +455,17 @@ public async Task Should_execute_all_tries_when_faulting_and_cancellationToken_n AttemptDuringWhichToCancel = null, }; - (await policy.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + (await policy.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Good)) .Should().Be(ResultPrimitive.Good); + } attemptsInvoked.Should().Be(1 + 3); } @@ -475,9 +477,6 @@ public async Task Should_not_execute_action_when_cancellationToken_cancelled_bef .HandleResult(ResultPrimitive.Fault) .RetryAsync(3); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -486,15 +485,19 @@ public async Task Should_not_execute_action_when_cancellationToken_cancelled_bef AttemptDuringWhichToCancel = null, // Cancellation token cancelled manually below - before any scenario execution. }; - cancellationTokenSource.Cancel(); - - var ex = await policy.Awaiting(x => x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, - ResultPrimitive.Fault, - ResultPrimitive.Fault, - ResultPrimitive.Fault, - ResultPrimitive.Good)) - .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + cancellationTokenSource.Cancel(); + + var ex = await policy.Awaiting(x => x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, + ResultPrimitive.Fault, + ResultPrimitive.Fault, + ResultPrimitive.Fault, + ResultPrimitive.Good)) + .Should().ThrowAsync(); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(0); } @@ -506,9 +509,6 @@ public async Task Should_report_cancellation_during_otherwise_non_faulting_actio .HandleResult(ResultPrimitive.Fault) .RetryAsync(3); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -518,13 +518,18 @@ public async Task Should_report_cancellation_during_otherwise_non_faulting_actio ActionObservesCancellation = true }; - var ex = await policy.Awaiting(x => x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + var ex = await policy.Awaiting(x => x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Good, ResultPrimitive.Good, ResultPrimitive.Good, ResultPrimitive.Good)) .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -536,9 +541,6 @@ public async Task Should_report_cancellation_during_faulting_initial_action_exec .HandleResult(ResultPrimitive.Fault) .RetryAsync(3); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -548,13 +550,18 @@ public async Task Should_report_cancellation_during_faulting_initial_action_exec ActionObservesCancellation = true }; - var ex = await policy.Awaiting(x => x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + var ex = await policy.Awaiting(x => x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Good)) .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -566,9 +573,6 @@ public async Task Should_report_cancellation_during_faulting_initial_action_exec .HandleResult(ResultPrimitive.Fault) .RetryAsync(3); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -578,13 +582,18 @@ public async Task Should_report_cancellation_during_faulting_initial_action_exec ActionObservesCancellation = false }; - var ex = await policy.Awaiting(x => x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + var ex = await policy.Awaiting(x => x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Good)) .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -596,9 +605,6 @@ public async Task Should_report_cancellation_during_faulting_retried_action_exec .HandleResult(ResultPrimitive.Fault) .RetryAsync(3); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -608,13 +614,18 @@ public async Task Should_report_cancellation_during_faulting_retried_action_exec ActionObservesCancellation = true }; - var ex = await policy.Awaiting(x => x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + var ex = await policy.Awaiting(x => x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Good)) .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(2); } @@ -626,9 +637,6 @@ public async Task Should_report_cancellation_during_faulting_retried_action_exec .HandleResult(ResultPrimitive.Fault) .RetryAsync(3); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -638,13 +646,18 @@ public async Task Should_report_cancellation_during_faulting_retried_action_exec ActionObservesCancellation = false }; - var ex = await policy.Awaiting(x => x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + var ex = await policy.Awaiting(x => x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Good)) .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(2); } @@ -656,9 +669,6 @@ public async Task Should_report_cancellation_during_faulting_last_retry_executio .HandleResult(ResultPrimitive.Fault) .RetryAsync(3); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -668,14 +678,19 @@ public async Task Should_report_cancellation_during_faulting_last_retry_executio ActionObservesCancellation = true }; - var ex = await policy.Awaiting(x => x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + var ex = await policy.Awaiting(x => x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Good)) .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1 + 3); } @@ -687,9 +702,6 @@ public async Task Should_report_faulting_from_faulting_last_retry_execution_when .HandleResult(ResultPrimitive.Fault) .RetryAsync(3); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -699,13 +711,18 @@ public async Task Should_report_faulting_from_faulting_last_retry_execution_when ActionObservesCancellation = false }; - (await policy.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + (await policy.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Fault, ResultPrimitive.Good)) .Should().Be(ResultPrimitive.Fault); + } attemptsInvoked.Should().Be(1 + 3); } @@ -713,32 +730,34 @@ public async Task Should_report_faulting_from_faulting_last_retry_execution_when [Fact] public async Task Should_report_cancellation_after_faulting_action_execution_and_cancel_further_retries_if_onRetry_invokes_cancellation() { - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - - var policy = Policy - .HandleResult(ResultPrimitive.Fault) - .RetryAsync(3, (_, _) => - { - cancellationTokenSource.Cancel(); - }); - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; Scenario scenario = new Scenario { - AttemptDuringWhichToCancel = null, // Cancellation during onRetry instead - see above. + AttemptDuringWhichToCancel = null, // Cancellation during onRetry instead - see below. ActionObservesCancellation = false }; - var ex = await policy.Awaiting(x => x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, - ResultPrimitive.Fault, - ResultPrimitive.Fault, - ResultPrimitive.Fault, - ResultPrimitive.Good)) - .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + var policy = Policy + .HandleResult(ResultPrimitive.Fault) + .RetryAsync(3, (_, _) => + { + cancellationTokenSource.Cancel(); + }); + + var ex = await policy.Awaiting(x => x.RaiseResultSequenceAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, + ResultPrimitive.Fault, + ResultPrimitive.Fault, + ResultPrimitive.Fault, + ResultPrimitive.Good)) + .Should().ThrowAsync(); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } diff --git a/test/Polly.Specs/Retry/WaitAndRetryAsyncSpecs.cs b/test/Polly.Specs/Retry/WaitAndRetryAsyncSpecs.cs index a63d87b4a82..224b9dc997f 100644 --- a/test/Polly.Specs/Retry/WaitAndRetryAsyncSpecs.cs +++ b/test/Polly.Specs/Retry/WaitAndRetryAsyncSpecs.cs @@ -692,9 +692,6 @@ public async Task Should_execute_action_when_non_faulting_and_cancellationToken_ .Handle() .WaitAndRetryAsync(new[] { 1.Seconds(), 2.Seconds(), 3.Seconds() }); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -704,8 +701,13 @@ public async Task Should_execute_action_when_non_faulting_and_cancellationToken_ AttemptDuringWhichToCancel = null, }; - await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().NotThrowAsync(); + } attemptsInvoked.Should().Be(1); } @@ -717,9 +719,6 @@ public async Task Should_execute_all_tries_when_faulting_and_cancellationToken_n .Handle() .WaitAndRetryAsync(new[] { 1.Seconds(), 2.Seconds(), 3.Seconds() }); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -729,8 +728,13 @@ public async Task Should_execute_all_tries_when_faulting_and_cancellationToken_n AttemptDuringWhichToCancel = null, }; - await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().ThrowAsync(); + } attemptsInvoked.Should().Be(1 + 3); } @@ -742,9 +746,6 @@ public async Task Should_not_execute_action_when_cancellationToken_cancelled_bef .Handle() .WaitAndRetryAsync(new[] { 1.Seconds(), 2.Seconds(), 3.Seconds() }); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -754,11 +755,15 @@ public async Task Should_not_execute_action_when_cancellationToken_cancelled_bef AttemptDuringWhichToCancel = null, // Cancellation token cancelled manually below - before any scenario execution. }; - cancellationTokenSource.Cancel(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + cancellationTokenSource.Cancel(); - var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) - .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + .Should().ThrowAsync(); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(0); } @@ -770,9 +775,6 @@ public async Task Should_report_cancellation_during_otherwise_non_faulting_actio .Handle() .WaitAndRetryAsync(new[] { 1.Seconds(), 2.Seconds(), 3.Seconds() }); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -783,9 +785,14 @@ public async Task Should_report_cancellation_during_otherwise_non_faulting_actio ActionObservesCancellation = true }; - var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -797,9 +804,6 @@ public async Task Should_report_cancellation_during_faulting_initial_action_exec .Handle() .WaitAndRetryAsync(new[] { 1.Seconds(), 2.Seconds(), 3.Seconds() }); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -810,9 +814,14 @@ public async Task Should_report_cancellation_during_faulting_initial_action_exec ActionObservesCancellation = true }; - var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -824,9 +833,6 @@ public async Task Should_report_cancellation_during_faulting_initial_action_exec .Handle() .WaitAndRetryAsync(new[] { 1.Seconds(), 2.Seconds(), 3.Seconds() }); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -837,9 +843,14 @@ public async Task Should_report_cancellation_during_faulting_initial_action_exec ActionObservesCancellation = false }; - var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -851,9 +862,6 @@ public async Task Should_report_cancellation_during_faulting_retried_action_exec .Handle() .WaitAndRetryAsync(new[] { 1.Seconds(), 2.Seconds(), 3.Seconds() }); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -864,9 +872,14 @@ public async Task Should_report_cancellation_during_faulting_retried_action_exec ActionObservesCancellation = true }; - var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(2); } @@ -878,9 +891,6 @@ public async Task Should_report_cancellation_during_faulting_retried_action_exec .Handle() .WaitAndRetryAsync(new[] { 1.Seconds(), 2.Seconds(), 3.Seconds() }); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -891,9 +901,14 @@ public async Task Should_report_cancellation_during_faulting_retried_action_exec ActionObservesCancellation = false }; - var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(2); } @@ -905,9 +920,6 @@ public async Task Should_report_cancellation_during_faulting_last_retry_executio .Handle() .WaitAndRetryAsync(new[] { 1.Seconds(), 2.Seconds(), 3.Seconds() }); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -918,9 +930,14 @@ public async Task Should_report_cancellation_during_faulting_last_retry_executio ActionObservesCancellation = true }; - var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1 + 3); } @@ -932,9 +949,6 @@ public async Task Should_report_faulting_from_faulting_last_retry_execution_when .Handle() .WaitAndRetryAsync(new[] { 1.Seconds(), 2.Seconds(), 3.Seconds() }); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -945,8 +959,13 @@ public async Task Should_report_faulting_from_faulting_last_retry_execution_when ActionObservesCancellation = false }; - await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().ThrowAsync(); + } attemptsInvoked.Should().Be(1 + 3); } @@ -954,9 +973,6 @@ await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + .Should().ThrowAsync(); + ex.And.CancellationToken.Should().Be(cancellationToken); + } - var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) - .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); watch.Stop(); attemptsInvoked.Should().Be(1); @@ -995,30 +1017,32 @@ public async Task Should_honour_cancellation_immediately_during_wait_phase_of_wa [Fact] public async Task Should_report_cancellation_after_faulting_action_execution_and_cancel_further_retries_if_onRetry_invokes_cancellation() { - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - - var policy = Policy - .Handle() - .WaitAndRetryAsync(new[] { 1.Seconds(), 2.Seconds(), 3.Seconds() }, - (_, _) => - { - cancellationTokenSource.Cancel(); - }); - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; Scenario scenario = new Scenario { NumberOfTimesToRaiseException = 1 + 3, - AttemptDuringWhichToCancel = null, // Cancellation during onRetry instead - see above. + AttemptDuringWhichToCancel = null, // Cancellation during onRetry instead - see below. ActionObservesCancellation = false }; - var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) - .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + var policy = Policy + .Handle() + .WaitAndRetryAsync(new[] { 1.Seconds(), 2.Seconds(), 3.Seconds() }, + (_, _) => + { + cancellationTokenSource.Cancel(); + }); + + var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + .Should().ThrowAsync(); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -1030,9 +1054,6 @@ public async Task Should_execute_func_returning_value_when_cancellationToken_not .Handle() .WaitAndRetryAsync(new[] { 1.Seconds(), 2.Seconds(), 3.Seconds() }); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -1044,9 +1065,14 @@ public async Task Should_execute_func_returning_value_when_cancellationToken_not AttemptDuringWhichToCancel = null }; - Func action = async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true); - await policy.Awaiting(action) - .Should().NotThrowAsync(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + Func action = async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true); + await policy.Awaiting(action) + .Should().NotThrowAsync(); + } result.Should().BeTrue(); @@ -1060,9 +1086,6 @@ public async Task Should_honour_and_report_cancellation_during_func_execution() .Handle() .WaitAndRetryAsync(new[] { 1.Seconds(), 2.Seconds(), 3.Seconds() }); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -1075,9 +1098,14 @@ public async Task Should_honour_and_report_cancellation_during_func_execution() ActionObservesCancellation = true }; - Func action = async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true); - var ex = await policy.Awaiting(action).Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + Func action = async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true); + var ex = await policy.Awaiting(action).Should().ThrowAsync(); + ex.And.CancellationToken.Should().Be(cancellationToken); + } result.Should().Be(null); diff --git a/test/Polly.Specs/Retry/WaitAndRetryForeverAsyncSpecs.cs b/test/Polly.Specs/Retry/WaitAndRetryForeverAsyncSpecs.cs index 2d8bf8d9f48..0e8880e356e 100644 --- a/test/Polly.Specs/Retry/WaitAndRetryForeverAsyncSpecs.cs +++ b/test/Polly.Specs/Retry/WaitAndRetryForeverAsyncSpecs.cs @@ -404,8 +404,6 @@ public async Task Should_execute_action_when_non_faulting_and_cancellationToken_ .Handle() .WaitAndRetryForeverAsync(provider); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -415,8 +413,11 @@ public async Task Should_execute_action_when_non_faulting_and_cancellationToken_ AttemptDuringWhichToCancel = null, }; - await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().NotThrowAsync(); + } attemptsInvoked.Should().Be(1); } @@ -430,9 +431,6 @@ public async Task Should_not_execute_action_when_cancellationToken_cancelled_bef .Handle() .WaitAndRetryForeverAsync(provider); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -442,11 +440,15 @@ public async Task Should_not_execute_action_when_cancellationToken_cancelled_bef AttemptDuringWhichToCancel = null, // Cancellation token cancelled manually below - before any scenario execution. }; - cancellationTokenSource.Cancel(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + cancellationTokenSource.Cancel(); - var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) - .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + .Should().ThrowAsync(); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(0); } @@ -460,9 +462,6 @@ public async Task Should_report_cancellation_during_otherwise_non_faulting_actio .Handle() .WaitAndRetryForeverAsync(provider); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -473,9 +472,14 @@ public async Task Should_report_cancellation_during_otherwise_non_faulting_actio ActionObservesCancellation = true }; - var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -489,9 +493,6 @@ public async Task Should_report_cancellation_during_faulting_initial_action_exec .Handle() .WaitAndRetryForeverAsync(provider); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -502,9 +503,14 @@ public async Task Should_report_cancellation_during_faulting_initial_action_exec ActionObservesCancellation = true }; - var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -518,9 +524,6 @@ public async Task Should_report_cancellation_during_faulting_initial_action_exec .Handle() .WaitAndRetryForeverAsync(provider); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -531,9 +534,14 @@ public async Task Should_report_cancellation_during_faulting_initial_action_exec ActionObservesCancellation = false }; - var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -547,9 +555,6 @@ public async Task Should_report_cancellation_during_faulting_retried_action_exec .Handle() .WaitAndRetryForeverAsync(provider); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -560,9 +565,14 @@ public async Task Should_report_cancellation_during_faulting_retried_action_exec ActionObservesCancellation = true }; - var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(2); } @@ -576,9 +586,6 @@ public async Task Should_report_cancellation_during_faulting_retried_action_exec .Handle() .WaitAndRetryForeverAsync(provider); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -589,9 +596,14 @@ public async Task Should_report_cancellation_during_faulting_retried_action_exec ActionObservesCancellation = false }; - var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(2); } @@ -601,17 +613,6 @@ public async Task Should_report_cancellation_after_faulting_action_execution_and { Func provider = _ => TimeSpan.Zero; - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - - var policy = Policy - .Handle() - .WaitAndRetryForeverAsync(provider, - (_, _) => - { - cancellationTokenSource.Cancel(); - }); - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -622,9 +623,22 @@ public async Task Should_report_cancellation_after_faulting_action_execution_and ActionObservesCancellation = false }; - var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) - .Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + var policy = Policy + .Handle() + .WaitAndRetryForeverAsync(provider, + (_, _) => + { + cancellationTokenSource.Cancel(); + }); + + var ex = await policy.Awaiting(x => x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute)) + .Should().ThrowAsync(); + ex.And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -638,8 +652,6 @@ public async Task Should_execute_func_returning_value_when_cancellationToken_not .Handle() .WaitAndRetryForeverAsync(provider); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -651,8 +663,11 @@ public async Task Should_execute_func_returning_value_when_cancellationToken_not AttemptDuringWhichToCancel = null, }; - Func action = async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true); - await policy.Awaiting(action).Should().NotThrowAsync(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + Func action = async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true); + await policy.Awaiting(action).Should().NotThrowAsync(); + } result.Should().BeTrue(); @@ -668,9 +683,6 @@ public async Task Should_honour_and_report_cancellation_during_func_execution() .Handle() .WaitAndRetryForeverAsync(provider); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -683,9 +695,14 @@ public async Task Should_honour_and_report_cancellation_during_func_execution() ActionObservesCancellation = true }; - Func action = async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true); - var ex = await policy.Awaiting(action).Should().ThrowAsync(); - ex.And.CancellationToken.Should().Be(cancellationToken); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + Func action = async x => result = await x.RaiseExceptionAndOrCancellationAsync(scenario, cancellationTokenSource, onExecute, true); + var ex = await policy.Awaiting(action).Should().ThrowAsync(); + ex.And.CancellationToken.Should().Be(cancellationToken); + } result.Should().Be(null); diff --git a/test/Polly.Specs/Retry/WaitAndRetrySpecs.cs b/test/Polly.Specs/Retry/WaitAndRetrySpecs.cs index 82577c9c61b..1877aa215e6 100644 --- a/test/Polly.Specs/Retry/WaitAndRetrySpecs.cs +++ b/test/Polly.Specs/Retry/WaitAndRetrySpecs.cs @@ -831,9 +831,6 @@ public void Should_not_execute_action_when_cancellationToken_cancelled_before_ex .Handle() .WaitAndRetry(new[] { 1.Seconds(), 2.Seconds(), 3.Seconds() }); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -843,11 +840,15 @@ public void Should_not_execute_action_when_cancellationToken_cancelled_before_ex AttemptDuringWhichToCancel = null, // Cancellation token cancelled manually below - before any scenario execution. }; - cancellationTokenSource.Cancel(); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + cancellationTokenSource.Cancel(); - policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) - .Should().Throw() - .And.CancellationToken.Should().Be(cancellationToken); + policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) + .Should().Throw() + .And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(0); } @@ -859,9 +860,6 @@ public void Should_report_cancellation_during_otherwise_non_faulting_action_exec .Handle() .WaitAndRetry(new[] { 1.Seconds(), 2.Seconds(), 3.Seconds() }); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -872,9 +870,14 @@ public void Should_report_cancellation_during_otherwise_non_faulting_action_exec ActionObservesCancellation = true }; - policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -886,9 +889,6 @@ public void Should_report_cancellation_during_faulting_initial_action_execution_ .Handle() .WaitAndRetry(new[] { 1.Seconds(), 2.Seconds(), 3.Seconds() }); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -899,9 +899,14 @@ public void Should_report_cancellation_during_faulting_initial_action_execution_ ActionObservesCancellation = true }; - policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -913,9 +918,6 @@ public void Should_report_cancellation_during_faulting_initial_action_execution_ .Handle() .WaitAndRetry(new[] { 1.Seconds(), 2.Seconds(), 3.Seconds() }); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -926,9 +928,14 @@ public void Should_report_cancellation_during_faulting_initial_action_execution_ ActionObservesCancellation = false }; - policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -940,9 +947,6 @@ public void Should_report_cancellation_during_faulting_retried_action_execution_ .Handle() .WaitAndRetry(new[] { 1.Seconds(), 2.Seconds(), 3.Seconds() }); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -953,9 +957,14 @@ public void Should_report_cancellation_during_faulting_retried_action_execution_ ActionObservesCancellation = true }; - policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(2); } @@ -967,9 +976,6 @@ public void Should_report_cancellation_during_faulting_retried_action_execution_ .Handle() .WaitAndRetry(new[] { 1.Seconds(), 2.Seconds(), 3.Seconds() }); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -980,9 +986,14 @@ public void Should_report_cancellation_during_faulting_retried_action_execution_ ActionObservesCancellation = false }; - policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(2); } @@ -994,9 +1005,6 @@ public void Should_report_cancellation_during_faulting_last_retry_execution_when .Handle() .WaitAndRetry(new[] { 1.Seconds(), 2.Seconds(), 3.Seconds() }); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -1007,9 +1015,14 @@ public void Should_report_cancellation_during_faulting_last_retry_execution_when ActionObservesCancellation = true }; - policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) .Should().Throw() .And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1 + 3); } @@ -1021,9 +1034,6 @@ public void Should_report_faulting_from_faulting_last_retry_execution_when_user_ .Handle() .WaitAndRetry(new[] { 1.Seconds(), 2.Seconds(), 3.Seconds() }); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -1034,8 +1044,13 @@ public void Should_report_faulting_from_faulting_last_retry_execution_when_user_ ActionObservesCancellation = false }; - policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) .Should().Throw(); + } attemptsInvoked.Should().Be(1 + 3); } @@ -1043,9 +1058,6 @@ public void Should_report_faulting_from_faulting_last_retry_execution_when_user_ [Fact] public void Should_honour_cancellation_immediately_during_wait_phase_of_waitandretry() { - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - SystemClock.Sleep = (timeSpan, ct) => Task.Delay(timeSpan, ct).Wait(ct); TimeSpan shimTimeSpan = TimeSpan.FromSeconds(1); // Consider increasing shimTimeSpan if test fails transiently in different environments. @@ -1068,11 +1080,17 @@ public void Should_honour_cancellation_immediately_during_wait_phase_of_waitandr ActionObservesCancellation = false }; - cancellationTokenSource.CancelAfter(shimTimeSpan); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + cancellationTokenSource.CancelAfter(shimTimeSpan); + + policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) + .Should().Throw() + .And.CancellationToken.Should().Be(cancellationToken); + } - policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) - .Should().Throw() - .And.CancellationToken.Should().Be(cancellationToken); watch.Stop(); attemptsInvoked.Should().Be(1); @@ -1084,17 +1102,6 @@ public void Should_honour_cancellation_immediately_during_wait_phase_of_waitandr [Fact] public void Should_report_cancellation_after_faulting_action_execution_and_cancel_further_retries_if_onRetry_invokes_cancellation() { - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - - var policy = Policy - .Handle() - .WaitAndRetry(new[] { 1.Seconds(), 2.Seconds(), 3.Seconds() }, - (_, _) => - { - cancellationTokenSource.Cancel(); - }); - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -1105,9 +1112,22 @@ public void Should_report_cancellation_after_faulting_action_execution_and_cance ActionObservesCancellation = false }; - policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) - .Should().Throw() - .And.CancellationToken.Should().Be(cancellationToken); + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + var policy = Policy + .Handle() + .WaitAndRetry(new[] { 1.Seconds(), 2.Seconds(), 3.Seconds() }, + (_, _) => + { + cancellationTokenSource.Cancel(); + }); + + policy.Invoking(x => x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute)) + .Should().Throw() + .And.CancellationToken.Should().Be(cancellationToken); + } attemptsInvoked.Should().Be(1); } @@ -1119,9 +1139,6 @@ public void Should_execute_func_returning_value_when_cancellationToken_not_cance .Handle() .WaitAndRetry(new[] { 1.Seconds(), 2.Seconds(), 3.Seconds() }); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -1133,8 +1150,11 @@ public void Should_execute_func_returning_value_when_cancellationToken_not_cance AttemptDuringWhichToCancel = null }; - policy.Invoking(x => result = x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute, true)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + policy.Invoking(x => result = x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute, true)) .Should().NotThrow(); + } result.Should().BeTrue(); @@ -1148,9 +1168,6 @@ public void Should_honour_and_report_cancellation_during_func_execution() .Handle() .WaitAndRetry(new[] { 1.Seconds(), 2.Seconds(), 3.Seconds() }); - CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationTokenSource.Token; - int attemptsInvoked = 0; Action onExecute = () => attemptsInvoked++; @@ -1163,8 +1180,13 @@ public void Should_honour_and_report_cancellation_during_func_execution() ActionObservesCancellation = true }; - policy.Invoking(x => result = x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute, true)) + using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) + { + CancellationToken cancellationToken = cancellationTokenSource.Token; + + policy.Invoking(x => result = x.RaiseExceptionAndOrCancellation(scenario, cancellationTokenSource, onExecute, true)) .Should().Throw().And.CancellationToken.Should().Be(cancellationToken); + } result.Should().Be(null);