From d92e8e8d07cfcacd0928c0b09fb5e25a545afb36 Mon Sep 17 00:00:00 2001 From: martincostello Date: Fri, 27 Oct 2023 17:31:45 +0100 Subject: [PATCH] Use collection initializers - Use C#12 collection initializers where relevant. - Suppress false positives when using `TheoryData`. - Remove some redundant code analysis suppressions. --- .../CircuitBreakerManualControl.cs | 4 ++-- .../Controller/HedgingExecutionContext.cs | 4 ++-- src/Polly.Core/PredicateBuilder.TResult.cs | 2 +- .../Registry/ConfigureBuilderContext.cs | 4 ++-- .../ResiliencePipelineBuilderBase.cs | 3 +-- .../Utils/Pipeline/ReloadableComponent.cs | 4 ++-- ...figureResiliencePipelineRegistryOptions.cs | 2 +- src/Polly/AsyncPolicy.ExecuteOverloads.cs | 24 +++++++++---------- .../AsyncPolicy.TResult.ExecuteOverloads.cs | 12 +++++----- src/Polly/Context.Dictionary.cs | 2 +- src/Polly/ExceptionPredicates.cs | 2 +- src/Polly/Policy.ExecuteOverloads.cs | 16 ++++++------- src/Polly/Policy.TResult.ExecuteOverloads.cs | 8 +++---- src/Polly/ResultPredicates.cs | 2 +- ...itBreakerResiliencePipelineBuilderTests.cs | 2 ++ ...esiliencePipelineBuilderExtensionsTests.cs | 2 ++ .../FallbackResilienceStrategyTests.cs | 2 +- .../HedgingExecutionContextTests.cs | 6 ++--- .../Hedging/Controller/TaskExecutionTests.cs | 2 +- .../Hedging/HedgingActions.cs | 6 ++--- .../Hedging/HedgingResilienceStrategyTests.cs | 2 +- .../ResiliencePipelineTTests.Async.cs | 6 ++--- .../ResiliencePipelineTTests.Sync.cs | 4 +++- .../ResiliencePipelineTests.cs | 10 ++++++-- ...esiliencePipelineBuilderExtensionsTests.cs | 8 +++---- .../Retry/RetryResilienceStrategyTests.cs | 2 +- .../Behavior/BehaviorChaosStrategyTests.cs | 3 ++- ...aultChaosPipelineBuilderExtensionsTests.cs | 4 +++- .../Simmy/Fault/FaultChaosStrategyTests.cs | 2 +- .../Latency/LatencyChaosStrategyTests.cs | 2 +- ...comeChaosPipelineBuilderExtensionsTests.cs | 4 +++- .../Outcomes/OutcomeChaosStrategyTests.cs | 2 +- .../ResilienceStrategyTelemetryTests.cs | 2 +- .../Timeout/TimeoutResilienceStrategyTests.cs | 2 +- .../Timeout/TimeoutTestUtils.cs | 2 ++ .../ReloadablePipelineComponentTests.cs | 8 +++---- .../Telemetry/TelemetryListenerImplTests.cs | 2 +- ...esiliencePipelineBuilderExtensionsTests.cs | 2 ++ .../AdvancedCircuitBreakerAsyncSpecs.cs | 2 +- .../AdvancedCircuitBreakerSpecs.cs | 10 +++++--- .../CircuitBreakerAsyncSpecs.cs | 2 +- .../CircuitBreaker/CircuitBreakerSpecs.cs | 2 +- test/Polly.Specs/ContextSpecs.cs | 2 +- .../Helpers/Caching/StubCacheProvider.cs | 2 +- .../RateLimitPolicyTResultSpecsBase.cs | 2 +- test/Polly.TestUtils/FakeLogger.cs | 2 +- 46 files changed, 112 insertions(+), 88 deletions(-) diff --git a/src/Polly.Core/CircuitBreaker/CircuitBreakerManualControl.cs b/src/Polly.Core/CircuitBreaker/CircuitBreakerManualControl.cs index bb4adb10a3f..1258c4f5ae4 100644 --- a/src/Polly.Core/CircuitBreaker/CircuitBreakerManualControl.cs +++ b/src/Polly.Core/CircuitBreaker/CircuitBreakerManualControl.cs @@ -9,8 +9,8 @@ namespace Polly.CircuitBreaker; public sealed class CircuitBreakerManualControl { private readonly object _lock = new(); - private readonly HashSet> _onIsolate = new(); - private readonly HashSet> _onReset = new(); + private readonly HashSet> _onIsolate = []; + private readonly HashSet> _onReset = []; private bool _isolated; /// diff --git a/src/Polly.Core/Hedging/Controller/HedgingExecutionContext.cs b/src/Polly.Core/Hedging/Controller/HedgingExecutionContext.cs index ad7487ddfe7..933cb5c26ad 100644 --- a/src/Polly.Core/Hedging/Controller/HedgingExecutionContext.cs +++ b/src/Polly.Core/Hedging/Controller/HedgingExecutionContext.cs @@ -10,8 +10,8 @@ internal sealed class HedgingExecutionContext : IAsyncDisposable { public readonly record struct ExecutionInfo(TaskExecution? Execution, bool Loaded, Outcome? Outcome); - private readonly List> _tasks = new(); - private readonly List> _executingTasks = new(); + private readonly List> _tasks = []; + private readonly List> _executingTasks = []; private readonly ObjectPool> _executionPool; private readonly TimeProvider _timeProvider; private readonly int _maxAttempts; diff --git a/src/Polly.Core/PredicateBuilder.TResult.cs b/src/Polly.Core/PredicateBuilder.TResult.cs index e8f94007911..a0f05e486a3 100644 --- a/src/Polly.Core/PredicateBuilder.TResult.cs +++ b/src/Polly.Core/PredicateBuilder.TResult.cs @@ -6,7 +6,7 @@ namespace Polly; /// The type of the result. public partial class PredicateBuilder { - private readonly List>> _predicates = new(); + private readonly List>> _predicates = []; /// /// Adds a predicate for handling exceptions of the specified type. diff --git a/src/Polly.Core/Registry/ConfigureBuilderContext.cs b/src/Polly.Core/Registry/ConfigureBuilderContext.cs index 1cf19ca6f56..56c8c483679 100644 --- a/src/Polly.Core/Registry/ConfigureBuilderContext.cs +++ b/src/Polly.Core/Registry/ConfigureBuilderContext.cs @@ -29,9 +29,9 @@ internal ConfigureBuilderContext(TKey strategyKey, string builderName, string? b /// internal string? BuilderInstanceName { get; } - internal List ReloadTokens { get; } = new(); + internal List ReloadTokens { get; } = []; - internal List DisposeCallbacks { get; } = new(); + internal List DisposeCallbacks { get; } = []; /// /// Reloads the pipeline when is canceled. diff --git a/src/Polly.Core/ResiliencePipelineBuilderBase.cs b/src/Polly.Core/ResiliencePipelineBuilderBase.cs index 2484ac72ac0..d7b49610d24 100644 --- a/src/Polly.Core/ResiliencePipelineBuilderBase.cs +++ b/src/Polly.Core/ResiliencePipelineBuilderBase.cs @@ -1,5 +1,4 @@ using System.ComponentModel; - using System.ComponentModel.DataAnnotations; using System.Diagnostics.CodeAnalysis; using Polly.Telemetry; @@ -19,7 +18,7 @@ namespace Polly; public abstract class ResiliencePipelineBuilderBase #pragma warning restore S1694 // An abstract class should have both abstract and concrete methods { - private readonly List _entries = new(); + private readonly List _entries = []; private bool _used; private protected ResiliencePipelineBuilderBase() diff --git a/src/Polly.Core/Utils/Pipeline/ReloadableComponent.cs b/src/Polly.Core/Utils/Pipeline/ReloadableComponent.cs index f8a216e37dc..82b3caac674 100644 --- a/src/Polly.Core/Utils/Pipeline/ReloadableComponent.cs +++ b/src/Polly.Core/Utils/Pipeline/ReloadableComponent.cs @@ -52,7 +52,7 @@ private void TryRegisterOnReload() return; } - _tokenSource = CancellationTokenSource.CreateLinkedTokenSource(_reloadTokens.ToArray()); + _tokenSource = CancellationTokenSource.CreateLinkedTokenSource([.. _reloadTokens]); _registration = _tokenSource.Token.Register(() => { var context = ResilienceContextPool.Shared.Get().Initialize(isSynchronous: true); @@ -65,7 +65,7 @@ private void TryRegisterOnReload() } catch (Exception e) { - _reloadTokens = new List(); + _reloadTokens = []; _telemetry.Report(new(ResilienceEventSeverity.Error, ReloadFailedEvent), context, Outcome.FromException(e), new ReloadFailedArguments(e)); ResilienceContextPool.Shared.Return(context); } diff --git a/src/Polly.Extensions/DependencyInjection/ConfigureResiliencePipelineRegistryOptions.cs b/src/Polly.Extensions/DependencyInjection/ConfigureResiliencePipelineRegistryOptions.cs index 83ecd3ffd0e..639946a6063 100644 --- a/src/Polly.Extensions/DependencyInjection/ConfigureResiliencePipelineRegistryOptions.cs +++ b/src/Polly.Extensions/DependencyInjection/ConfigureResiliencePipelineRegistryOptions.cs @@ -5,5 +5,5 @@ namespace Polly.DependencyInjection; internal sealed class ConfigureResiliencePipelineRegistryOptions where TKey : notnull { - public List>> Actions { get; } = new(); + public List>> Actions { get; } = []; } diff --git a/src/Polly/AsyncPolicy.ExecuteOverloads.cs b/src/Polly/AsyncPolicy.ExecuteOverloads.cs index 667b1d4cb4b..0d35960c437 100644 --- a/src/Polly/AsyncPolicy.ExecuteOverloads.cs +++ b/src/Polly/AsyncPolicy.ExecuteOverloads.cs @@ -10,7 +10,7 @@ public abstract partial class AsyncPolicy : PolicyBase, IAsyncPolicy /// The action to perform. [DebuggerStepThrough] public Task ExecuteAsync(Func action) => - ExecuteAsync((_, _) => action(), new Context(), DefaultCancellationToken, DefaultContinueOnCapturedContext); + ExecuteAsync((_, _) => action(), [], DefaultCancellationToken, DefaultContinueOnCapturedContext); /// /// Executes the specified asynchronous action within the policy. @@ -37,7 +37,7 @@ public Task ExecuteAsync(Func action, Context context) => /// A cancellation token which can be used to cancel the action. When a retry policy in use, also cancels any further retries. [DebuggerStepThrough] public Task ExecuteAsync(Func action, CancellationToken cancellationToken) => - ExecuteAsync((_, ct) => action(ct), new Context(), cancellationToken, DefaultContinueOnCapturedContext); + ExecuteAsync((_, ct) => action(ct), [], cancellationToken, DefaultContinueOnCapturedContext); /// /// Executes the specified asynchronous action within the policy. @@ -68,7 +68,7 @@ public Task ExecuteAsync(Func action, Context /// Please use asynchronous-defined policies when calling asynchronous ExecuteAsync (and similar) methods. [DebuggerStepThrough] public Task ExecuteAsync(Func action, CancellationToken cancellationToken, bool continueOnCapturedContext) => - ExecuteAsync((_, ct) => action(ct), new Context(), cancellationToken, continueOnCapturedContext); + ExecuteAsync((_, ct) => action(ct), [], cancellationToken, continueOnCapturedContext); /// /// Executes the specified asynchronous action within the policy. @@ -117,7 +117,7 @@ public async Task ExecuteAsync(Func action, Co /// The value returned by the action [DebuggerStepThrough] public Task ExecuteAsync(Func> action) => - ExecuteAsync((_, _) => action(), new Context(), DefaultCancellationToken, DefaultContinueOnCapturedContext); + ExecuteAsync((_, _) => action(), [], DefaultCancellationToken, DefaultContinueOnCapturedContext); /// /// Executes the specified asynchronous action within the policy and returns the result. @@ -149,7 +149,7 @@ public Task ExecuteAsync(Func> action, /// The value returned by the action [DebuggerStepThrough] public Task ExecuteAsync(Func> action, CancellationToken cancellationToken) => - ExecuteAsync((_, ct) => action(ct), new Context(), cancellationToken, DefaultContinueOnCapturedContext); + ExecuteAsync((_, ct) => action(ct), [], cancellationToken, DefaultContinueOnCapturedContext); /// /// Executes the specified asynchronous action within the policy and returns the result. @@ -185,7 +185,7 @@ public Task ExecuteAsync(FuncPlease use asynchronous-defined policies when calling asynchronous ExecuteAsync (and similar) methods. [DebuggerStepThrough] public Task ExecuteAsync(Func> action, CancellationToken cancellationToken, bool continueOnCapturedContext) => - ExecuteAsync((_, ct) => action(ct), new Context(), cancellationToken, continueOnCapturedContext); + ExecuteAsync((_, ct) => action(ct), [], cancellationToken, continueOnCapturedContext); /// /// Executes the specified asynchronous action within the policy and returns the result. @@ -240,7 +240,7 @@ public async Task ExecuteAsync(FuncThe captured result [DebuggerStepThrough] public Task ExecuteAndCaptureAsync(Func action) => - ExecuteAndCaptureAsync((_, _) => action(), new Context(), DefaultCancellationToken, DefaultContinueOnCapturedContext); + ExecuteAndCaptureAsync((_, _) => action(), [], DefaultCancellationToken, DefaultContinueOnCapturedContext); /// /// Executes the specified asynchronous action within the policy and returns the captured result. @@ -270,7 +270,7 @@ public Task ExecuteAndCaptureAsync(Func action, Con /// A cancellation token which can be used to cancel the action. When a retry policy in use, also cancels any further retries. [DebuggerStepThrough] public Task ExecuteAndCaptureAsync(Func action, CancellationToken cancellationToken) => - ExecuteAndCaptureAsync((_, ct) => action(ct), new Context(), cancellationToken, DefaultContinueOnCapturedContext); + ExecuteAndCaptureAsync((_, ct) => action(ct), [], cancellationToken, DefaultContinueOnCapturedContext); /// /// Executes the specified asynchronous action within the policy and returns the captured result. @@ -303,7 +303,7 @@ public Task ExecuteAndCaptureAsync(FuncPlease use asynchronous-defined policies when calling asynchronous ExecuteAsync (and similar) methods. [DebuggerStepThrough] public Task ExecuteAndCaptureAsync(Func action, CancellationToken cancellationToken, bool continueOnCapturedContext) => - ExecuteAndCaptureAsync((_, ct) => action(ct), new Context(), cancellationToken, continueOnCapturedContext); + ExecuteAndCaptureAsync((_, ct) => action(ct), [], cancellationToken, continueOnCapturedContext); /// /// Executes the specified asynchronous action within the policy and returns the captured result. @@ -352,7 +352,7 @@ public async Task ExecuteAndCaptureAsync(FuncThe captured result [DebuggerStepThrough] public Task> ExecuteAndCaptureAsync(Func> action) => - ExecuteAndCaptureAsync((_, _) => action(), new Context(), DefaultCancellationToken, DefaultContinueOnCapturedContext); + ExecuteAndCaptureAsync((_, _) => action(), [], DefaultCancellationToken, DefaultContinueOnCapturedContext); /// /// Executes the specified asynchronous action within the policy and returns the result. @@ -386,7 +386,7 @@ public Task> ExecuteAndCaptureAsync(FuncThe captured result [DebuggerStepThrough] public Task> ExecuteAndCaptureAsync(Func> action, CancellationToken cancellationToken) => - ExecuteAndCaptureAsync((_, ct) => action(ct), new Context(), cancellationToken, DefaultContinueOnCapturedContext); + ExecuteAndCaptureAsync((_, ct) => action(ct), [], cancellationToken, DefaultContinueOnCapturedContext); /// /// Executes the specified asynchronous action within the policy and returns the result. @@ -424,7 +424,7 @@ public Task> ExecuteAndCaptureAsync(FuncPlease use asynchronous-defined policies when calling asynchronous ExecuteAsync (and similar) methods. [DebuggerStepThrough] public Task> ExecuteAndCaptureAsync(Func> action, CancellationToken cancellationToken, bool continueOnCapturedContext) => - ExecuteAndCaptureAsync((_, ct) => action(ct), new Context(), cancellationToken, continueOnCapturedContext); + ExecuteAndCaptureAsync((_, ct) => action(ct), [], cancellationToken, continueOnCapturedContext); /// /// Executes the specified asynchronous action within the policy and returns the result. diff --git a/src/Polly/AsyncPolicy.TResult.ExecuteOverloads.cs b/src/Polly/AsyncPolicy.TResult.ExecuteOverloads.cs index e74d818070a..1daa607943e 100644 --- a/src/Polly/AsyncPolicy.TResult.ExecuteOverloads.cs +++ b/src/Polly/AsyncPolicy.TResult.ExecuteOverloads.cs @@ -11,7 +11,7 @@ public abstract partial class AsyncPolicy : IAsyncPolicy /// The value returned by the action [DebuggerStepThrough] public Task ExecuteAsync(Func> action) => - ExecuteAsync((_, _) => action(), new Context(), CancellationToken.None, DefaultContinueOnCapturedContext); + ExecuteAsync((_, _) => action(), [], CancellationToken.None, DefaultContinueOnCapturedContext); /// /// Executes the specified asynchronous action within the policy and returns the result. @@ -41,7 +41,7 @@ public Task ExecuteAsync(Func> action, Context c /// The value returned by the action [DebuggerStepThrough] public Task ExecuteAsync(Func> action, CancellationToken cancellationToken) => - ExecuteAsync((_, ct) => action(ct), new Context(), cancellationToken, DefaultContinueOnCapturedContext); + ExecuteAsync((_, ct) => action(ct), [], cancellationToken, DefaultContinueOnCapturedContext); /// /// Executes the specified asynchronous action within the policy and returns the result. @@ -53,7 +53,7 @@ public Task ExecuteAsync(Func> action, /// Please use asynchronous-defined policies when calling asynchronous ExecuteAsync (and similar) methods. [DebuggerStepThrough] public Task ExecuteAsync(Func> action, CancellationToken cancellationToken, bool continueOnCapturedContext) => - ExecuteAsync((_, ct) => action(ct), new Context(), cancellationToken, continueOnCapturedContext); + ExecuteAsync((_, ct) => action(ct), [], cancellationToken, continueOnCapturedContext); /// /// Executes the specified asynchronous action within the policy and returns the result. @@ -127,7 +127,7 @@ public async Task ExecuteAsync(FuncThe captured result [DebuggerStepThrough] public Task> ExecuteAndCaptureAsync(Func> action) => - ExecuteAndCaptureAsync((_, _) => action(), new Context(), CancellationToken.None, DefaultContinueOnCapturedContext); + ExecuteAndCaptureAsync((_, _) => action(), [], CancellationToken.None, DefaultContinueOnCapturedContext); /// /// Executes the specified asynchronous action within the policy and returns the result. @@ -158,7 +158,7 @@ public Task> ExecuteAndCaptureAsync(FuncThe captured result [DebuggerStepThrough] public Task> ExecuteAndCaptureAsync(Func> action, CancellationToken cancellationToken) => - ExecuteAndCaptureAsync((_, ct) => action(ct), new Context(), cancellationToken, DefaultContinueOnCapturedContext); + ExecuteAndCaptureAsync((_, ct) => action(ct), [], cancellationToken, DefaultContinueOnCapturedContext); /// /// Executes the specified asynchronous action within the policy and returns the result. @@ -170,7 +170,7 @@ public Task> ExecuteAndCaptureAsync(FuncPlease use asynchronous-defined policies when calling asynchronous ExecuteAsync (and similar) methods. [DebuggerStepThrough] public Task> ExecuteAndCaptureAsync(Func> action, CancellationToken cancellationToken, bool continueOnCapturedContext) => - ExecuteAndCaptureAsync((_, ct) => action(ct), new Context(), cancellationToken, continueOnCapturedContext); + ExecuteAndCaptureAsync((_, ct) => action(ct), [], cancellationToken, continueOnCapturedContext); /// /// Executes the specified asynchronous action within the policy and returns the result. diff --git a/src/Polly/Context.Dictionary.cs b/src/Polly/Context.Dictionary.cs index 8fd71070860..d0090ceba72 100644 --- a/src/Polly/Context.Dictionary.cs +++ b/src/Polly/Context.Dictionary.cs @@ -11,7 +11,7 @@ public partial class Context : IDictionary, IDictionary, IReadOn private Dictionary wrappedDictionary; - private Dictionary WrappedDictionary => wrappedDictionary ?? (wrappedDictionary = new Dictionary()); + private Dictionary WrappedDictionary => wrappedDictionary ?? (wrappedDictionary = []); /// /// Initializes a new instance of the class, with the specified and the supplied . diff --git a/src/Polly/ExceptionPredicates.cs b/src/Polly/ExceptionPredicates.cs index f8504e8e617..5bae4c9b15a 100644 --- a/src/Polly/ExceptionPredicates.cs +++ b/src/Polly/ExceptionPredicates.cs @@ -9,7 +9,7 @@ public class ExceptionPredicates internal void Add(ExceptionPredicate predicate) { - _predicates ??= new List(); // The ?? pattern here is sufficient; only a deliberately contrived example would lead to the same PolicyBuilder instance being used in a multi-threaded way to define policies simultaneously on multiple threads. + _predicates ??= []; // The ?? pattern here is sufficient; only a deliberately contrived example would lead to the same PolicyBuilder instance being used in a multi-threaded way to define policies simultaneously on multiple threads. _predicates.Add(predicate); } diff --git a/src/Polly/Policy.ExecuteOverloads.cs b/src/Polly/Policy.ExecuteOverloads.cs index 4d7e2e666aa..edfa5f41d4f 100644 --- a/src/Polly/Policy.ExecuteOverloads.cs +++ b/src/Polly/Policy.ExecuteOverloads.cs @@ -10,7 +10,7 @@ public abstract partial class Policy : ISyncPolicy /// The action to perform. [DebuggerStepThrough] public void Execute(Action action) => - Execute((_, _) => action(), new Context(), DefaultCancellationToken); + Execute((_, _) => action(), [], DefaultCancellationToken); /// /// Executes the specified action within the policy. @@ -37,7 +37,7 @@ public void Execute(Action action, Context context) => /// [DebuggerStepThrough] public void Execute(Action action, CancellationToken cancellationToken) => - Execute((_, ct) => action(ct), new Context(), cancellationToken); + Execute((_, ct) => action(ct), [], cancellationToken); /// /// Executes the specified action within the policy. @@ -83,7 +83,7 @@ public void Execute(Action action, Context context, /// The value returned by the action [DebuggerStepThrough] public TResult Execute(Func action) => - Execute((_, _) => action(), new Context(), DefaultCancellationToken); + Execute((_, _) => action(), [], DefaultCancellationToken); /// /// Executes the specified action within the policy and returns the result. @@ -124,7 +124,7 @@ public TResult Execute(Func action, Context context) /// The value returned by the action [DebuggerStepThrough] public TResult Execute(Func action, CancellationToken cancellationToken) => - Execute((_, ct) => action(ct), new Context(), cancellationToken); + Execute((_, ct) => action(ct), [], cancellationToken); /// /// Executes the specified action within the policy and returns the result. @@ -177,7 +177,7 @@ public TResult Execute(Func action /// The captured result [DebuggerStepThrough] public PolicyResult ExecuteAndCapture(Action action) => - ExecuteAndCapture((_, _) => action(), new Context(), DefaultCancellationToken); + ExecuteAndCapture((_, _) => action(), [], DefaultCancellationToken); /// /// Executes the specified action within the policy and returns the captured result. @@ -208,7 +208,7 @@ public PolicyResult ExecuteAndCapture(Action action, Context context) = /// The captured result [DebuggerStepThrough] public PolicyResult ExecuteAndCapture(Action action, CancellationToken cancellationToken) => - ExecuteAndCapture((_, ct) => action(ct), new Context(), cancellationToken); + ExecuteAndCapture((_, ct) => action(ct), [], cancellationToken); /// /// Executes the specified action within the policy and returns the captured result. @@ -254,7 +254,7 @@ public PolicyResult ExecuteAndCapture(Action action, /// The captured result [DebuggerStepThrough] public PolicyResult ExecuteAndCapture(Func action) => - ExecuteAndCapture((_, _) => action(), new Context(), DefaultCancellationToken); + ExecuteAndCapture((_, _) => action(), [], DefaultCancellationToken); /// /// Executes the specified action within the policy and returns the captured result. @@ -286,7 +286,7 @@ public PolicyResult ExecuteAndCapture(Func a /// The cancellation token. /// The captured result public PolicyResult ExecuteAndCapture(Func action, CancellationToken cancellationToken) => - ExecuteAndCapture((_, ct) => action(ct), new Context(), cancellationToken); + ExecuteAndCapture((_, ct) => action(ct), [], cancellationToken); /// /// Executes the specified action within the policy and returns the captured result. diff --git a/src/Polly/Policy.TResult.ExecuteOverloads.cs b/src/Polly/Policy.TResult.ExecuteOverloads.cs index a6f13261aa0..0ee2110329c 100644 --- a/src/Polly/Policy.TResult.ExecuteOverloads.cs +++ b/src/Polly/Policy.TResult.ExecuteOverloads.cs @@ -11,7 +11,7 @@ public abstract partial class Policy : ISyncPolicy /// The value returned by the action [DebuggerStepThrough] public TResult Execute(Func action) => - Execute((_, _) => action(), new Context(), DefaultCancellationToken); + Execute((_, _) => action(), [], DefaultCancellationToken); /// /// Executes the specified action within the policy and returns the result. @@ -49,7 +49,7 @@ public TResult Execute(Func action, Context context) => /// The value returned by the action [DebuggerStepThrough] public TResult Execute(Func action, CancellationToken cancellationToken) => - Execute((_, ct) => action(ct), new Context(), cancellationToken); + Execute((_, ct) => action(ct), [], cancellationToken); /// /// Executes the specified action within the policy and returns the result. @@ -98,7 +98,7 @@ public TResult Execute(Func action, Context /// The captured result [DebuggerStepThrough] public PolicyResult ExecuteAndCapture(Func action) => - ExecuteAndCapture((_, _) => action(), new Context(), DefaultCancellationToken); + ExecuteAndCapture((_, _) => action(), [], DefaultCancellationToken); /// /// Executes the specified action within the policy and returns the captured result. @@ -130,7 +130,7 @@ public PolicyResult ExecuteAndCapture(Func action, Co /// The captured result [DebuggerStepThrough] public PolicyResult ExecuteAndCapture(Func action, CancellationToken cancellationToken) => - ExecuteAndCapture((_, ct) => action(ct), new Context(), cancellationToken); + ExecuteAndCapture((_, ct) => action(ct), [], cancellationToken); /// /// Executes the specified action within the policy and returns the captured result. diff --git a/src/Polly/ResultPredicates.cs b/src/Polly/ResultPredicates.cs index 72b43db3a5c..6be1aa99fe9 100644 --- a/src/Polly/ResultPredicates.cs +++ b/src/Polly/ResultPredicates.cs @@ -9,7 +9,7 @@ public class ResultPredicates internal void Add(ResultPredicate predicate) { - _predicates ??= new List>(); // The ?? pattern here is sufficient; only a deliberately contrived example would lead to the same PolicyBuilder instance being used in a multi-threaded way to define policies simultaneously on multiple threads. + _predicates ??= []; // The ?? pattern here is sufficient; only a deliberately contrived example would lead to the same PolicyBuilder instance being used in a multi-threaded way to define policies simultaneously on multiple threads. _predicates.Add(predicate); } diff --git a/test/Polly.Core.Tests/CircuitBreaker/CircuitBreakerResiliencePipelineBuilderTests.cs b/test/Polly.Core.Tests/CircuitBreaker/CircuitBreakerResiliencePipelineBuilderTests.cs index a081a36655b..51c75f9e424 100644 --- a/test/Polly.Core.Tests/CircuitBreaker/CircuitBreakerResiliencePipelineBuilderTests.cs +++ b/test/Polly.Core.Tests/CircuitBreaker/CircuitBreakerResiliencePipelineBuilderTests.cs @@ -7,6 +7,7 @@ namespace Polly.Core.Tests.CircuitBreaker; public class CircuitBreakerResiliencePipelineBuilderTests { +#pragma warning disable IDE0028 public static TheoryData> ConfigureData = new() { builder => builder.AddCircuitBreaker(new CircuitBreakerStrategyOptions @@ -22,6 +23,7 @@ public class CircuitBreakerResiliencePipelineBuilderTests ShouldHandle = _ => PredicateResult.True() }), }; +#pragma warning restore IDE0028 [MemberData(nameof(ConfigureData))] [Theory] diff --git a/test/Polly.Core.Tests/Fallback/FallbackResiliencePipelineBuilderExtensionsTests.cs b/test/Polly.Core.Tests/Fallback/FallbackResiliencePipelineBuilderExtensionsTests.cs index 6dbe198712d..ccb10ef02b8 100644 --- a/test/Polly.Core.Tests/Fallback/FallbackResiliencePipelineBuilderExtensionsTests.cs +++ b/test/Polly.Core.Tests/Fallback/FallbackResiliencePipelineBuilderExtensionsTests.cs @@ -6,6 +6,7 @@ namespace Polly.Core.Tests.Fallback; public class FallbackResiliencePipelineBuilderExtensionsTests { +#pragma warning disable IDE0028 public static readonly TheoryData>> FallbackOverloadsGeneric = new() { builder => @@ -17,6 +18,7 @@ public class FallbackResiliencePipelineBuilderExtensionsTests }); } }; +#pragma warning restore IDE0028 [MemberData(nameof(FallbackOverloadsGeneric))] [Theory] diff --git a/test/Polly.Core.Tests/Fallback/FallbackResilienceStrategyTests.cs b/test/Polly.Core.Tests/Fallback/FallbackResilienceStrategyTests.cs index 8415d82e19d..131496df358 100644 --- a/test/Polly.Core.Tests/Fallback/FallbackResilienceStrategyTests.cs +++ b/test/Polly.Core.Tests/Fallback/FallbackResilienceStrategyTests.cs @@ -6,7 +6,7 @@ namespace Polly.Core.Tests.Fallback; public class FallbackResilienceStrategyTests { private readonly FallbackStrategyOptions _options = new(); - private readonly List> _args = new(); + private readonly List> _args = []; private readonly ResilienceStrategyTelemetry _telemetry; private FallbackHandler? _handler; diff --git a/test/Polly.Core.Tests/Hedging/Controller/HedgingExecutionContextTests.cs b/test/Polly.Core.Tests/Hedging/Controller/HedgingExecutionContextTests.cs index 8c0a2700e2d..6c91d4f210f 100644 --- a/test/Polly.Core.Tests/Hedging/Controller/HedgingExecutionContextTests.cs +++ b/test/Polly.Core.Tests/Hedging/Controller/HedgingExecutionContextTests.cs @@ -14,9 +14,9 @@ public class HedgingExecutionContextTests : IDisposable private readonly ResiliencePropertyKey _myKey = new("my-key"); private readonly CancellationTokenSource _cts; private readonly HedgingTimeProvider _timeProvider; - private readonly List> _createdExecutions = new(); - private readonly List> _returnedExecutions = new(); - private readonly List> _resets = new(); + private readonly List> _createdExecutions = []; + private readonly List> _returnedExecutions = []; + private readonly List> _resets = []; private readonly ResilienceContext _resilienceContext; private readonly AutoResetEvent _onReset = new(false); private int _maxAttempts = 2; diff --git a/test/Polly.Core.Tests/Hedging/Controller/TaskExecutionTests.cs b/test/Polly.Core.Tests/Hedging/Controller/TaskExecutionTests.cs index fa7ec8280c8..e32ba7d4fdc 100644 --- a/test/Polly.Core.Tests/Hedging/Controller/TaskExecutionTests.cs +++ b/test/Polly.Core.Tests/Hedging/Controller/TaskExecutionTests.cs @@ -14,7 +14,7 @@ public class TaskExecutionTests : IDisposable private readonly CancellationTokenSource _cts; private readonly HedgingTimeProvider _timeProvider; private readonly ResilienceStrategyTelemetry _telemetry; - private readonly List _args = new(); + private readonly List _args = []; private ContextSnapshot _snapshot; public TaskExecutionTests() diff --git a/test/Polly.Core.Tests/Hedging/HedgingActions.cs b/test/Polly.Core.Tests/Hedging/HedgingActions.cs index e2267d250ab..37911f2e9b2 100644 --- a/test/Polly.Core.Tests/Hedging/HedgingActions.cs +++ b/test/Polly.Core.Tests/Hedging/HedgingActions.cs @@ -10,12 +10,12 @@ public HedgingActions(TimeProvider timeProvider) { _timeProvider = timeProvider; - Functions = new() - { + Functions = + [ GetApples, GetOranges, GetPears - }; + ]; Generator = args => { diff --git a/test/Polly.Core.Tests/Hedging/HedgingResilienceStrategyTests.cs b/test/Polly.Core.Tests/Hedging/HedgingResilienceStrategyTests.cs index ea45fe7955f..985f77b19c9 100644 --- a/test/Polly.Core.Tests/Hedging/HedgingResilienceStrategyTests.cs +++ b/test/Polly.Core.Tests/Hedging/HedgingResilienceStrategyTests.cs @@ -623,7 +623,7 @@ public async Task ExecuteAsync_EnsureBackgroundWorkInSuccessfulCallNotCancelled( { // arrange using var cts = new CancellationTokenSource(); - List backgroundTasks = new List(); + List backgroundTasks = []; ConfigureHedging(BackgroundWork); var strategy = Create(); diff --git a/test/Polly.Core.Tests/ResiliencePipelineTTests.Async.cs b/test/Polly.Core.Tests/ResiliencePipelineTTests.Async.cs index 125548504d6..c22471a427d 100644 --- a/test/Polly.Core.Tests/ResiliencePipelineTTests.Async.cs +++ b/test/Polly.Core.Tests/ResiliencePipelineTTests.Async.cs @@ -3,10 +3,9 @@ namespace Polly.Core.Tests; -#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously - public partial class ResiliencePipelineTests { +#pragma warning disable IDE0028 public static TheoryData, ValueTask>> ExecuteAsyncGenericStrategyData = new() { async strategy => @@ -59,8 +58,9 @@ public partial class ResiliencePipelineTests return new ValueTask("res"); }, context)).Should().Be("res"); - } + }, }; +#pragma warning restore IDE0028 [MemberData(nameof(ExecuteAsyncGenericStrategyData))] [Theory] diff --git a/test/Polly.Core.Tests/ResiliencePipelineTTests.Sync.cs b/test/Polly.Core.Tests/ResiliencePipelineTTests.Sync.cs index 60dc613c269..d242e3aa62a 100644 --- a/test/Polly.Core.Tests/ResiliencePipelineTTests.Sync.cs +++ b/test/Polly.Core.Tests/ResiliencePipelineTTests.Sync.cs @@ -5,6 +5,7 @@ namespace Polly.Core.Tests; public partial class ResiliencePipelineTests { +#pragma warning disable IDE0028 public static TheoryData>> ExecuteGenericStrategyData = new() { strategy => @@ -73,8 +74,9 @@ public partial class ResiliencePipelineTests return "res"; }, context).Should().Be("res"); - } + }, }; +#pragma warning restore IDE0028 [MemberData(nameof(ExecuteGenericStrategyData))] [Theory] diff --git a/test/Polly.Core.Tests/ResiliencePipelineTests.cs b/test/Polly.Core.Tests/ResiliencePipelineTests.cs index 95d4cc939fe..f12ae724c32 100644 --- a/test/Polly.Core.Tests/ResiliencePipelineTests.cs +++ b/test/Polly.Core.Tests/ResiliencePipelineTests.cs @@ -4,12 +4,18 @@ namespace Polly.Core.Tests; -#pragma warning disable S3966 // Objects should not be disposed more than once - public partial class ResiliencePipelineTests { public static readonly CancellationToken CancellationToken = new CancellationTokenSource().Token; +#pragma warning disable IDE0028 + public static TheoryData ResilienceContextPools = new() + { + null, + ResilienceContextPool.Shared, + }; +#pragma warning restore IDE0028 + [Fact] public async Task DisposeAsync_NullPipeline_OK() { diff --git a/test/Polly.Core.Tests/Retry/RetryResiliencePipelineBuilderExtensionsTests.cs b/test/Polly.Core.Tests/Retry/RetryResiliencePipelineBuilderExtensionsTests.cs index f73886985ba..51801b11bf7 100644 --- a/test/Polly.Core.Tests/Retry/RetryResiliencePipelineBuilderExtensionsTests.cs +++ b/test/Polly.Core.Tests/Retry/RetryResiliencePipelineBuilderExtensionsTests.cs @@ -5,10 +5,9 @@ namespace Polly.Core.Tests.Retry; -#pragma warning disable CA2012 // Use ValueTasks correctly - public class RetryResiliencePipelineBuilderExtensionsTests { +#pragma warning disable IDE0028 public static readonly TheoryData> OverloadsData = new() { builder => @@ -22,7 +21,7 @@ public class RetryResiliencePipelineBuilderExtensionsTests }); AssertStrategy(builder, DelayBackoffType.Exponential, 3, TimeSpan.FromSeconds(2)); - } + }, }; public static readonly TheoryData>> OverloadsDataGeneric = new() @@ -38,8 +37,9 @@ public class RetryResiliencePipelineBuilderExtensionsTests }); AssertStrategy(builder, DelayBackoffType.Exponential, 3, TimeSpan.FromSeconds(2)); - } + }, }; +#pragma warning restore IDE0028 [MemberData(nameof(OverloadsData))] [Theory] diff --git a/test/Polly.Core.Tests/Retry/RetryResilienceStrategyTests.cs b/test/Polly.Core.Tests/Retry/RetryResilienceStrategyTests.cs index d0727bda922..0a94d396896 100644 --- a/test/Polly.Core.Tests/Retry/RetryResilienceStrategyTests.cs +++ b/test/Polly.Core.Tests/Retry/RetryResilienceStrategyTests.cs @@ -10,7 +10,7 @@ public class RetryResilienceStrategyTests { private readonly RetryStrategyOptions _options = new(); private readonly FakeTimeProvider _timeProvider = new(); - private readonly List> _args = new(); + private readonly List> _args = []; private ResilienceStrategyTelemetry _telemetry; public RetryResilienceStrategyTests() diff --git a/test/Polly.Core.Tests/Simmy/Behavior/BehaviorChaosStrategyTests.cs b/test/Polly.Core.Tests/Simmy/Behavior/BehaviorChaosStrategyTests.cs index 64947e22a85..aa58aaa7d0f 100644 --- a/test/Polly.Core.Tests/Simmy/Behavior/BehaviorChaosStrategyTests.cs +++ b/test/Polly.Core.Tests/Simmy/Behavior/BehaviorChaosStrategyTests.cs @@ -2,11 +2,12 @@ using Polly.Telemetry; namespace Polly.Core.Tests.Simmy.Behavior; + public class BehaviorChaosStrategyTests { private readonly ResilienceStrategyTelemetry _telemetry; private readonly BehaviorStrategyOptions _options; - private readonly List> _args = new(); + private readonly List> _args = []; public BehaviorChaosStrategyTests() { diff --git a/test/Polly.Core.Tests/Simmy/Fault/FaultChaosPipelineBuilderExtensionsTests.cs b/test/Polly.Core.Tests/Simmy/Fault/FaultChaosPipelineBuilderExtensionsTests.cs index 17fab140a8c..34f4408b7e2 100644 --- a/test/Polly.Core.Tests/Simmy/Fault/FaultChaosPipelineBuilderExtensionsTests.cs +++ b/test/Polly.Core.Tests/Simmy/Fault/FaultChaosPipelineBuilderExtensionsTests.cs @@ -7,6 +7,7 @@ namespace Polly.Core.Tests.Simmy.Fault; public class FaultChaosPipelineBuilderExtensionsTests { +#pragma warning disable IDE0028 public static readonly TheoryData> FaultStrategy = new() { builder => @@ -21,8 +22,9 @@ public class FaultChaosPipelineBuilderExtensionsTests AssertFaultStrategy(builder, true, 0.6) .Fault.Should().BeOfType(typeof(InvalidOperationException)); - } + }, }; +#pragma warning restore IDE0028 private static void AssertFaultStrategy(ResiliencePipelineBuilder builder, bool enabled, double injectionRate) where TException : Exception diff --git a/test/Polly.Core.Tests/Simmy/Fault/FaultChaosStrategyTests.cs b/test/Polly.Core.Tests/Simmy/Fault/FaultChaosStrategyTests.cs index c7014e1a5ce..1a076c6791e 100644 --- a/test/Polly.Core.Tests/Simmy/Fault/FaultChaosStrategyTests.cs +++ b/test/Polly.Core.Tests/Simmy/Fault/FaultChaosStrategyTests.cs @@ -7,7 +7,7 @@ namespace Polly.Core.Tests.Simmy.Fault; public class FaultChaosStrategyTests { private readonly ResilienceStrategyTelemetry _telemetry; - private readonly List> _args = new(); + private readonly List> _args = []; public FaultChaosStrategyTests() => _telemetry = TestUtilities.CreateResilienceTelemetry(arg => _args.Add(arg)); diff --git a/test/Polly.Core.Tests/Simmy/Latency/LatencyChaosStrategyTests.cs b/test/Polly.Core.Tests/Simmy/Latency/LatencyChaosStrategyTests.cs index 10b733b4f52..361ab2d75ba 100644 --- a/test/Polly.Core.Tests/Simmy/Latency/LatencyChaosStrategyTests.cs +++ b/test/Polly.Core.Tests/Simmy/Latency/LatencyChaosStrategyTests.cs @@ -11,7 +11,7 @@ public class LatencyChaosStrategyTests : IDisposable private readonly LatencyStrategyOptions _options; private readonly CancellationTokenSource _cancellationSource; private readonly TimeSpan _delay = TimeSpan.FromMilliseconds(500); - private readonly List> _args = new(); + private readonly List> _args = []; public LatencyChaosStrategyTests() { diff --git a/test/Polly.Core.Tests/Simmy/Outcomes/OutcomeChaosPipelineBuilderExtensionsTests.cs b/test/Polly.Core.Tests/Simmy/Outcomes/OutcomeChaosPipelineBuilderExtensionsTests.cs index 946b2a442f8..bfc56cfb9e2 100644 --- a/test/Polly.Core.Tests/Simmy/Outcomes/OutcomeChaosPipelineBuilderExtensionsTests.cs +++ b/test/Polly.Core.Tests/Simmy/Outcomes/OutcomeChaosPipelineBuilderExtensionsTests.cs @@ -7,6 +7,7 @@ namespace Polly.Core.Tests.Simmy.Outcomes; public class OutcomeChaosPipelineBuilderExtensionsTests { +#pragma warning disable IDE0028 public static readonly TheoryData>> ResultStrategy = new() { builder => @@ -20,8 +21,9 @@ public class OutcomeChaosPipelineBuilderExtensionsTests }); AssertResultStrategy(builder, true, 0.6, new(100)); - } + }, }; +#pragma warning restore IDE0028 private static void AssertResultStrategy(ResiliencePipelineBuilder builder, bool enabled, double injectionRate, Outcome outcome) { diff --git a/test/Polly.Core.Tests/Simmy/Outcomes/OutcomeChaosStrategyTests.cs b/test/Polly.Core.Tests/Simmy/Outcomes/OutcomeChaosStrategyTests.cs index 7a7e8fb631e..ada7f9e8a1a 100644 --- a/test/Polly.Core.Tests/Simmy/Outcomes/OutcomeChaosStrategyTests.cs +++ b/test/Polly.Core.Tests/Simmy/Outcomes/OutcomeChaosStrategyTests.cs @@ -6,7 +6,7 @@ namespace Polly.Core.Tests.Simmy.Outcomes; public class OutcomeChaosStrategyTests { private readonly ResilienceStrategyTelemetry _telemetry; - private readonly List> _args = new(); + private readonly List> _args = []; public OutcomeChaosStrategyTests() => _telemetry = TestUtilities.CreateResilienceTelemetry(arg => _args.Add(arg)); diff --git a/test/Polly.Core.Tests/Telemetry/ResilienceStrategyTelemetryTests.cs b/test/Polly.Core.Tests/Telemetry/ResilienceStrategyTelemetryTests.cs index fb2d56f1154..90b58338be1 100644 --- a/test/Polly.Core.Tests/Telemetry/ResilienceStrategyTelemetryTests.cs +++ b/test/Polly.Core.Tests/Telemetry/ResilienceStrategyTelemetryTests.cs @@ -4,7 +4,7 @@ namespace Polly.Core.Tests.Telemetry; public class ResilienceStrategyTelemetryTests { - private readonly List> _args = new(); + private readonly List> _args = []; private readonly ResilienceTelemetrySource _source; private readonly ResilienceStrategyTelemetry _sut; diff --git a/test/Polly.Core.Tests/Timeout/TimeoutResilienceStrategyTests.cs b/test/Polly.Core.Tests/Timeout/TimeoutResilienceStrategyTests.cs index 7bebeb49bcd..c625d0958dc 100644 --- a/test/Polly.Core.Tests/Timeout/TimeoutResilienceStrategyTests.cs +++ b/test/Polly.Core.Tests/Timeout/TimeoutResilienceStrategyTests.cs @@ -12,7 +12,7 @@ public class TimeoutResilienceStrategyTests : IDisposable private readonly TimeoutStrategyOptions _options; private readonly CancellationTokenSource _cancellationSource; private readonly TimeSpan _delay = TimeSpan.FromSeconds(12); - private readonly List> _args = new(); + private readonly List> _args = []; public TimeoutResilienceStrategyTests() { diff --git a/test/Polly.Core.Tests/Timeout/TimeoutTestUtils.cs b/test/Polly.Core.Tests/Timeout/TimeoutTestUtils.cs index 302cdc8be4a..ce037d8cf8c 100644 --- a/test/Polly.Core.Tests/Timeout/TimeoutTestUtils.cs +++ b/test/Polly.Core.Tests/Timeout/TimeoutTestUtils.cs @@ -8,6 +8,7 @@ public static class TimeoutTestUtils public static TimeoutGeneratorArguments TimeoutGeneratorArguments() => new(ResilienceContextPool.Shared.Get()); +#pragma warning disable IDE0028 public static readonly TheoryData InvalidTimeouts = new() { TimeSpan.MinValue, @@ -21,4 +22,5 @@ public static class TimeoutTestUtils TimeSpan.FromSeconds(1), TimeSpan.FromHours(1), }; +#pragma warning restore IDE0028 } diff --git a/test/Polly.Core.Tests/Utils/Pipeline/ReloadablePipelineComponentTests.cs b/test/Polly.Core.Tests/Utils/Pipeline/ReloadablePipelineComponentTests.cs index 371e4d88219..b60d1e17110 100644 --- a/test/Polly.Core.Tests/Utils/Pipeline/ReloadablePipelineComponentTests.cs +++ b/test/Polly.Core.Tests/Utils/Pipeline/ReloadablePipelineComponentTests.cs @@ -8,7 +8,7 @@ public class ReloadablePipelineComponentTests : IDisposable { private readonly FakeTelemetryListener _listener; private readonly ResilienceStrategyTelemetry _telemetry; - private readonly List _synchronousFlags = new(); + private readonly List _synchronousFlags = []; private CancellationTokenSource _cancellationTokenSource; public ReloadablePipelineComponentTests() @@ -56,7 +56,7 @@ public async Task ChangeTriggered_EnsureOldStrategyDisposed() { var telemetry = TestUtilities.CreateResilienceTelemetry(_listener); var component = Substitute.For(); - await using var sut = CreateSut(component, () => new(Substitute.For(), new List(), telemetry)); + await using var sut = CreateSut(component, () => new(Substitute.For(), [], telemetry)); for (var i = 0; i < 10; i++) { @@ -131,10 +131,10 @@ public async Task DisposeError_EnsureReported() private ReloadableComponent CreateSut(PipelineComponent? initial = null, Func? factory = null) { - factory ??= () => new ReloadableComponent.Entry(PipelineComponent.Empty, new List(), _telemetry); + factory ??= () => new ReloadableComponent.Entry(PipelineComponent.Empty, [], _telemetry); return (ReloadableComponent)PipelineComponentFactory.CreateReloadable( - new ReloadableComponent.Entry(initial ?? PipelineComponent.Empty, new List { _cancellationTokenSource.Token }, _telemetry), + new ReloadableComponent.Entry(initial ?? PipelineComponent.Empty, [_cancellationTokenSource.Token], _telemetry), factory); } diff --git a/test/Polly.Extensions.Tests/Telemetry/TelemetryListenerImplTests.cs b/test/Polly.Extensions.Tests/Telemetry/TelemetryListenerImplTests.cs index b6b5a387638..bc86c98e304 100644 --- a/test/Polly.Extensions.Tests/Telemetry/TelemetryListenerImplTests.cs +++ b/test/Polly.Extensions.Tests/Telemetry/TelemetryListenerImplTests.cs @@ -12,7 +12,7 @@ public class TelemetryListenerImplTests : IDisposable { private readonly FakeLogger _logger; private readonly ILoggerFactory _loggerFactory; - private readonly List _events = new(); + private readonly List _events = []; private Action>? _onEvent; public TelemetryListenerImplTests() => _loggerFactory = TestUtilities.CreateLoggerFactory(out _logger); diff --git a/test/Polly.RateLimiting.Tests/RateLimiterResiliencePipelineBuilderExtensionsTests.cs b/test/Polly.RateLimiting.Tests/RateLimiterResiliencePipelineBuilderExtensionsTests.cs index 9cf0f4f404a..60326769100 100644 --- a/test/Polly.RateLimiting.Tests/RateLimiterResiliencePipelineBuilderExtensionsTests.cs +++ b/test/Polly.RateLimiting.Tests/RateLimiterResiliencePipelineBuilderExtensionsTests.cs @@ -9,6 +9,7 @@ namespace Polly.RateLimiting.Tests; public class RateLimiterResiliencePipelineBuilderExtensionsTests { +#pragma warning disable IDE0028 public static readonly TheoryData> Data = new() { builder => @@ -41,6 +42,7 @@ public class RateLimiterResiliencePipelineBuilderExtensionsTests AssertRateLimiterStrategy(builder, strategy => strategy.Wrapper.Should().BeNull()); } }; +#pragma warning restore IDE0028 [MemberData(nameof(Data))] [Theory(Skip = "https://github.com/stryker-mutator/stryker-net/issues/2144")] diff --git a/test/Polly.Specs/CircuitBreaker/AdvancedCircuitBreakerAsyncSpecs.cs b/test/Polly.Specs/CircuitBreaker/AdvancedCircuitBreakerAsyncSpecs.cs index 73c234a9493..42911a81b9f 100644 --- a/test/Polly.Specs/CircuitBreaker/AdvancedCircuitBreakerAsyncSpecs.cs +++ b/test/Polly.Specs/CircuitBreaker/AdvancedCircuitBreakerAsyncSpecs.cs @@ -2358,7 +2358,7 @@ await breaker.Awaiting(x => x.RaiseExceptionAsync()) [Fact] public async Task Should_call_onbreak_with_a_state_of_half_open() { - List transitionedStates = new List(); + List transitionedStates = []; Action onBreak = (_, state, _, _) => { transitionedStates.Add(state); }; Action onReset = _ => { }; diff --git a/test/Polly.Specs/CircuitBreaker/AdvancedCircuitBreakerSpecs.cs b/test/Polly.Specs/CircuitBreaker/AdvancedCircuitBreakerSpecs.cs index d65c1c59412..d2140938f8b 100644 --- a/test/Polly.Specs/CircuitBreaker/AdvancedCircuitBreakerSpecs.cs +++ b/test/Polly.Specs/CircuitBreaker/AdvancedCircuitBreakerSpecs.cs @@ -1601,7 +1601,7 @@ public void Should_only_allow_single_execution_on_first_entering_halfopen_state_ permitFirstExecutionEnd.Set(); #pragma warning disable xUnit1031 // Do not use blocking task operations in test method - Task.WaitAll(new[] { firstExecution, secondExecution }, testTimeoutToExposeDeadlocks).Should().BeTrue(); + Task.WaitAll([firstExecution, secondExecution], testTimeoutToExposeDeadlocks).Should().BeTrue(); #pragma warning restore xUnit1031 // Do not use blocking task operations in test method if (firstExecution.IsFaulted) @@ -1713,7 +1713,7 @@ public void Should_allow_single_execution_per_break_duration_in_halfopen_state__ permitFirstExecutionEnd.Set(); #pragma warning disable xUnit1031 // Do not use blocking task operations in test method - Task.WaitAll(new[] { firstExecution, secondExecution }, testTimeoutToExposeDeadlocks).Should().BeTrue(); + Task.WaitAll([firstExecution, secondExecution], testTimeoutToExposeDeadlocks).Should().BeTrue(); #pragma warning restore xUnit1031 // Do not use blocking task operations in test method if (firstExecution.IsFaulted) @@ -2032,8 +2032,12 @@ public void Should_call_onbreak_when_breaking_circuit_first_time_but_not_for_sub #pragma warning disable xUnit1031 // Do not use blocking task operations in test method longRunningExecution.Wait(testTimeoutToExposeDeadlocks).Should().BeTrue(); #pragma warning restore xUnit1031 // Do not use blocking task operations in test method + if (longRunningExecution.IsFaulted) + { throw longRunningExecution!.Exception!; + } + longRunningExecution.Status.Should().Be(TaskStatus.RanToCompletion); // onBreak() should still only have been called once. @@ -2355,7 +2359,7 @@ public void Should_call_onbreak_with_a_state_of_closed() [Fact] public void Should_call_onbreak_with_a_state_of_half_open() { - List transitionedStates = new List(); + List transitionedStates = []; Action onBreak = (_, state, _, _) => { transitionedStates.Add(state); }; Action onReset = _ => { }; diff --git a/test/Polly.Specs/CircuitBreaker/CircuitBreakerAsyncSpecs.cs b/test/Polly.Specs/CircuitBreaker/CircuitBreakerAsyncSpecs.cs index 571c66005ee..0a46d9ee986 100644 --- a/test/Polly.Specs/CircuitBreaker/CircuitBreakerAsyncSpecs.cs +++ b/test/Polly.Specs/CircuitBreaker/CircuitBreakerAsyncSpecs.cs @@ -1074,7 +1074,7 @@ await breaker.Awaiting(x => x.RaiseExceptionAsync()) [Fact] public async Task Should_call_onbreak_with_a_state_of_half_open() { - List transitionedStates = new List(); + List transitionedStates = []; Action onBreak = (_, state, _, _) => { transitionedStates.Add(state); }; Action onReset = _ => { }; diff --git a/test/Polly.Specs/CircuitBreaker/CircuitBreakerSpecs.cs b/test/Polly.Specs/CircuitBreaker/CircuitBreakerSpecs.cs index 4aab6a90879..ba15f042984 100644 --- a/test/Polly.Specs/CircuitBreaker/CircuitBreakerSpecs.cs +++ b/test/Polly.Specs/CircuitBreaker/CircuitBreakerSpecs.cs @@ -1067,7 +1067,7 @@ public void Should_call_onbreak_with_a_state_of_closed() [Fact] public void Should_call_onbreak_with_a_state_of_half_open() { - List transitionedStates = new List(); + List transitionedStates = []; Action onBreak = (_, state, _, _) => { transitionedStates.Add(state); }; Action onReset = _ => { }; diff --git a/test/Polly.Specs/ContextSpecs.cs b/test/Polly.Specs/ContextSpecs.cs index f4709e8fc2b..4c3ec4f8093 100644 --- a/test/Polly.Specs/ContextSpecs.cs +++ b/test/Polly.Specs/ContextSpecs.cs @@ -25,7 +25,7 @@ public void Should_assign_OperationKey_and_context_data_from_constructor() [Fact] public void NoArgsCtor_should_assign_no_OperationKey() { - Context context = new Context(); + Context context = []; context.OperationKey.Should().BeNull(); } diff --git a/test/Polly.Specs/Helpers/Caching/StubCacheProvider.cs b/test/Polly.Specs/Helpers/Caching/StubCacheProvider.cs index 0c80c611f5f..dce2456be56 100644 --- a/test/Polly.Specs/Helpers/Caching/StubCacheProvider.cs +++ b/test/Polly.Specs/Helpers/Caching/StubCacheProvider.cs @@ -17,7 +17,7 @@ public CacheItem(object? value, Ttl ttl) public readonly object? Value; } - private readonly Dictionary cachedValues = new(); + private readonly Dictionary cachedValues = []; public (bool, object?) TryGet(string key) { diff --git a/test/Polly.Specs/RateLimit/RateLimitPolicyTResultSpecsBase.cs b/test/Polly.Specs/RateLimit/RateLimitPolicyTResultSpecsBase.cs index 4594098feee..44bd8acbbaa 100644 --- a/test/Polly.Specs/RateLimit/RateLimitPolicyTResultSpecsBase.cs +++ b/test/Polly.Specs/RateLimit/RateLimitPolicyTResultSpecsBase.cs @@ -35,7 +35,7 @@ public void Ratelimiter_specifies_correct_wait_until_next_execution_by_custom_fa // (do nothing - time not advanced) // Act - try another execution. - Context contextToPassIn = new Context(); + Context contextToPassIn = []; var resultExpectedBlocked = TryExecuteThroughPolicy(rateLimiter, contextToPassIn, new ResultClassWithRetryAfter(ResultPrimitive.Good)); // Assert - should be blocked - time not advanced. diff --git a/test/Polly.TestUtils/FakeLogger.cs b/test/Polly.TestUtils/FakeLogger.cs index 22bbc7e1e00..75e54555587 100644 --- a/test/Polly.TestUtils/FakeLogger.cs +++ b/test/Polly.TestUtils/FakeLogger.cs @@ -6,7 +6,7 @@ namespace Polly.TestUtils; public class FakeLogger : ILogger { - private readonly List _records = new(); + private readonly List _records = []; public bool Enabled { get; set; } = true;