diff --git a/.editorconfig b/.editorconfig index 252cf527042..a801866b9ac 100644 --- a/.editorconfig +++ b/.editorconfig @@ -32,4 +32,14 @@ csharp_place_expr_method_on_single_line = false # Prefer property-like constructs to have an expression-body csharp_style_expression_bodied_properties = true:error csharp_style_expression_bodied_indexers = true:error -csharp_style_expression_bodied_accessors = true:error \ No newline at end of file +csharp_style_expression_bodied_accessors = true:error + +# static fields in PascalCase +dotnet_naming_rule.static_fields_should_have_prefix.severity = warning +dotnet_naming_rule.static_fields_should_have_prefix.symbols = static_fields +dotnet_naming_rule.static_fields_should_have_prefix.style = static_prefix_style +dotnet_naming_symbols.static_fields.applicable_kinds = field +dotnet_naming_symbols.static_fields.required_modifiers = static +dotnet_naming_symbols.static_fields.applicable_accessibilities = private, internal, private_protected +dotnet_naming_style.static_prefix_style.required_prefix = +dotnet_naming_style.static_prefix_style.capitalization = pascal_case \ No newline at end of file diff --git a/src/Polly.Core.Tests/Hedging/Controller/HedgingExecutionContextTests.cs b/src/Polly.Core.Tests/Hedging/Controller/HedgingExecutionContextTests.cs index 51491c7a58a..7778d146314 100644 --- a/src/Polly.Core.Tests/Hedging/Controller/HedgingExecutionContextTests.cs +++ b/src/Polly.Core.Tests/Hedging/Controller/HedgingExecutionContextTests.cs @@ -10,7 +10,7 @@ namespace Polly.Core.Tests.Hedging.Controller; public class HedgingExecutionContextTests : IDisposable { private const string Handled = "Handled"; - private static readonly TimeSpan _assertTimeout = TimeSpan.FromSeconds(5); + private static readonly TimeSpan AssertTimeout = TimeSpan.FromSeconds(5); private readonly ResiliencePropertyKey _myKey = new("my-key"); private readonly CancellationTokenSource _cts; private readonly HedgingTimeProvider _timeProvider; @@ -390,8 +390,8 @@ public async Task Complete_EnsurePendingTasksCleaned() await pending; - assertPrimary.WaitOne(_assertTimeout).Should().BeTrue(); - assertSecondary.WaitOne(_assertTimeout).Should().BeTrue(); + assertPrimary.WaitOne(AssertTimeout).Should().BeTrue(); + assertSecondary.WaitOne(AssertTimeout).Should().BeTrue(); } [Fact] @@ -408,7 +408,7 @@ public async Task Complete_EnsureCleaned() context.LoadedTasks.Should().Be(0); context.Snapshot.Context.Should().BeNull(); - _onReset.WaitOne(_assertTimeout); + _onReset.WaitOne(AssertTimeout); _resets.Count.Should().Be(1); _returnedExecutions.Count.Should().Be(2); } diff --git a/src/Polly.Core.Tests/Hedging/HedgingResilienceStrategyTests.cs b/src/Polly.Core.Tests/Hedging/HedgingResilienceStrategyTests.cs index 2d775c54b6d..6afea31d832 100644 --- a/src/Polly.Core.Tests/Hedging/HedgingResilienceStrategyTests.cs +++ b/src/Polly.Core.Tests/Hedging/HedgingResilienceStrategyTests.cs @@ -13,8 +13,8 @@ public class HedgingResilienceStrategyTests : IDisposable private const string Failure = "Failure"; - private static readonly TimeSpan _longDelay = TimeSpan.FromDays(1); - private static readonly TimeSpan _assertTimeout = TimeSpan.FromSeconds(15); + private static readonly TimeSpan LongDelay = TimeSpan.FromDays(1); + private static readonly TimeSpan AssertTimeout = TimeSpan.FromSeconds(15); private readonly HedgingStrategyOptions _options = new(); private readonly List _events = new(); @@ -134,7 +134,7 @@ public async void ExecuteAsync_EnsureHedgedTasksCancelled_Ok() try { _testOutput.WriteLine("Hedged task executing..."); - await Task.Delay(_longDelay, context.CancellationToken); + await Task.Delay(LongDelay, context.CancellationToken); _testOutput.WriteLine("Hedged task executing...done (not-cancelled)"); } catch (OperationCanceledException) @@ -168,7 +168,7 @@ public async void ExecuteAsync_EnsureHedgedTasksCancelled_Ok() _timeProvider.Advance(TimeSpan.FromHours(1)); (await result).Should().Be(Success); - cancelled.WaitOne(_assertTimeout).Should().BeTrue(); + cancelled.WaitOne(AssertTimeout).Should().BeTrue(); } [Fact] @@ -226,13 +226,13 @@ public async Task ExecuteAsync_EnsureDiscardedResultDisposed() var result = await strategy.ExecuteAsync(async token => { #pragma warning disable CA2016 // Forward the 'CancellationToken' parameter to methods - await _timeProvider.Delay(_longDelay); + await _timeProvider.Delay(LongDelay); #pragma warning restore CA2016 // Forward the 'CancellationToken' parameter to methods return primaryResult; }); // assert - _timeProvider.Advance(_longDelay); + _timeProvider.Advance(LongDelay); await primaryResult.WaitForDisposalAsync(); primaryResult.IsDisposed.Should().BeTrue(); @@ -277,7 +277,7 @@ public async Task ExecuteAsync_EveryHedgedTaskShouldHaveDifferentContexts() context.Properties.GetValue(beforeKey, "wrong").Should().Be("before"); context.Should().Be(primaryContext); contexts.Add(context); - await _timeProvider.Delay(_longDelay, context.CancellationToken); + await _timeProvider.Delay(LongDelay, context.CancellationToken); return "primary"; }, primaryContext, @@ -286,7 +286,7 @@ public async Task ExecuteAsync_EveryHedgedTaskShouldHaveDifferentContexts() // assert contexts.Should().HaveCountGreaterThan(1); contexts.Count.Should().Be(contexts.Distinct().Count()); - _timeProvider.Advance(_longDelay); + _timeProvider.Advance(LongDelay); tokenHashCodes.Distinct().Should().HaveCountGreaterThan(1); } @@ -501,8 +501,8 @@ public async Task ExecuteAsync_CancellationLinking_Ok() _timeProvider.Advance(TimeSpan.FromHours(1)); await task.Invoking(async t => await t).Should().ThrowAsync(); - primaryCancelled.WaitOne(_assertTimeout).Should().BeTrue(); - secondaryCancelled.WaitOne(_assertTimeout).Should().BeTrue(); + primaryCancelled.WaitOne(AssertTimeout).Should().BeTrue(); + secondaryCancelled.WaitOne(AssertTimeout).Should().BeTrue(); } [Fact] @@ -556,8 +556,8 @@ public async void ExecuteAsync_ZeroHedgingDelay_EnsureAllTasksSpawnedAtOnce() var task = Create().ExecuteAsync(async c => (await Execute(c)).Result, default); // assert - Assert.True(allExecutionsReached.WaitOne(_assertTimeout)); - _timeProvider.Advance(_longDelay); + Assert.True(allExecutionsReached.WaitOne(AssertTimeout)); + _timeProvider.Advance(LongDelay); await task; async ValueTask> Execute(CancellationToken token) @@ -567,7 +567,7 @@ async ValueTask> Execute(CancellationToken token) allExecutionsReached.Set(); } - await _timeProvider.Delay(_longDelay, token); + await _timeProvider.Delay(LongDelay, token); return Success.AsOutcome(); } } @@ -585,7 +585,7 @@ public void ExecuteAsync_InfiniteHedgingDelay_EnsureNoConcurrentExecutions() var pending = Create().ExecuteAsync(Execute, _cts.Token); // assert - Assert.True(allExecutions.WaitOne(_assertTimeout)); + Assert.True(allExecutions.WaitOne(AssertTimeout)); async ValueTask> Execute(CancellationToken token) { @@ -602,7 +602,7 @@ async ValueTask> Execute(CancellationToken token) allExecutions.Set(); } - await _timeProvider.Delay(_longDelay, token); + await _timeProvider.Delay(LongDelay, token); return "dummy".AsOutcome(); } diff --git a/src/Polly.Core/CircuitBreaker/Health/HealthMetrics.cs b/src/Polly.Core/CircuitBreaker/Health/HealthMetrics.cs index 4585661ce9a..0d9f03d97c9 100644 --- a/src/Polly.Core/CircuitBreaker/Health/HealthMetrics.cs +++ b/src/Polly.Core/CircuitBreaker/Health/HealthMetrics.cs @@ -7,13 +7,13 @@ namespace Polly.CircuitBreaker.Health; internal abstract class HealthMetrics { private const short NumberOfWindows = 10; - private static readonly TimeSpan _resolutionOfCircuitTimer = TimeSpan.FromMilliseconds(20); + private static readonly TimeSpan ResolutionOfCircuitTimer = TimeSpan.FromMilliseconds(20); protected HealthMetrics(TimeProvider timeProvider) => TimeProvider = timeProvider; public static HealthMetrics Create(TimeSpan samplingDuration, TimeProvider timeProvider) { - return samplingDuration < TimeSpan.FromTicks(_resolutionOfCircuitTimer.Ticks * NumberOfWindows) + return samplingDuration < TimeSpan.FromTicks(ResolutionOfCircuitTimer.Ticks * NumberOfWindows) ? new SingleHealthMetrics(samplingDuration, timeProvider) : new RollingHealthMetrics(samplingDuration, NumberOfWindows, timeProvider); } diff --git a/src/Polly.Core/ResilienceContext.cs b/src/Polly.Core/ResilienceContext.cs index 332200b6158..4cdde32d1e9 100644 --- a/src/Polly.Core/ResilienceContext.cs +++ b/src/Polly.Core/ResilienceContext.cs @@ -15,7 +15,7 @@ public sealed class ResilienceContext { private const bool ContinueOnCapturedContextDefault = false; - private static readonly ObjectPool _pool = new(static () => new ResilienceContext(), static c => c.Reset()); + private static readonly ObjectPool Pool = new(static () => new ResilienceContext(), static c => c.Reset()); private readonly List _resilienceEvents = new(); @@ -74,7 +74,7 @@ private ResilienceContext() /// After the execution is finished you should return the back to the pool /// by calling method. /// - public static ResilienceContext Get() => _pool.Get(); + public static ResilienceContext Get() => Pool.Get(); internal void InitializeFrom(ResilienceContext context) { @@ -95,7 +95,7 @@ public static void Return(ResilienceContext context) { Guard.NotNull(context); - _pool.Return(context); + Pool.Return(context); } [ExcludeFromCodeCoverage] diff --git a/src/Polly.Extensions.Tests/ReloadableResilienceStrategyTests.cs b/src/Polly.Extensions.Tests/ReloadableResilienceStrategyTests.cs index 2abea23dbd3..21f8cb0f1e4 100644 --- a/src/Polly.Extensions.Tests/ReloadableResilienceStrategyTests.cs +++ b/src/Polly.Extensions.Tests/ReloadableResilienceStrategyTests.cs @@ -9,7 +9,7 @@ namespace Polly.Extensions.Tests; public class ReloadableResilienceStrategyTests { - private static readonly ResiliencePropertyKey _tagKey = new("tests.tag"); + private static readonly ResiliencePropertyKey TagKey = new("tests.tag"); [InlineData(null)] [InlineData("custom-name")] @@ -47,14 +47,14 @@ public void AddResilienceStrategy_EnsureReloadable(string? name) // initial strategy.Execute(_ => "dummy", context); - context.Properties.GetValue(_tagKey, string.Empty).Should().Be("initial-tag"); + context.Properties.GetValue(TagKey, string.Empty).Should().Be("initial-tag"); // reloads for (int i = 0; i < 10; i++) { reloadableConfig.Reload(new() { { "tag", $"reload-{i}" } }); strategy.Execute(_ => "dummy", context); - context.Properties.GetValue(_tagKey, string.Empty).Should().Be($"reload-{i}"); + context.Properties.GetValue(TagKey, string.Empty).Should().Be($"reload-{i}"); } registry.Count.Should().Be(1); @@ -101,7 +101,7 @@ protected override ValueTask> ExecuteCoreAsync ResilienceContext context, TState state) { - context.Properties.Set(_tagKey, Tag); + context.Properties.Set(TagKey, Tag); return callback(context, state); } } diff --git a/src/Polly.Extensions/Telemetry/EnrichmentContext.Pool.cs b/src/Polly.Extensions/Telemetry/EnrichmentContext.Pool.cs index 1125ce8fd13..60224e48c8a 100644 --- a/src/Polly.Extensions/Telemetry/EnrichmentContext.Pool.cs +++ b/src/Polly.Extensions/Telemetry/EnrichmentContext.Pool.cs @@ -4,7 +4,7 @@ namespace Polly.Extensions.Telemetry; public partial class EnrichmentContext { - private static readonly ObjectPool _contextPool = new( + private static readonly ObjectPool ContextPool = new( static () => new EnrichmentContext(), static context => { @@ -16,7 +16,7 @@ public partial class EnrichmentContext internal static EnrichmentContext Get(ResilienceContext resilienceContext, object? arguments, Outcome? outcome) { - var context = _contextPool.Get(); + var context = ContextPool.Get(); context.Context = resilienceContext; context.Arguments = arguments; context.Outcome = outcome; @@ -27,6 +27,6 @@ internal static EnrichmentContext Get(ResilienceContext resilienceContext, objec internal static void Return(EnrichmentContext context) { context.Tags.Clear(); - _contextPool.Return(context); + ContextPool.Return(context); } } diff --git a/src/Polly.Extensions/Telemetry/Log.cs b/src/Polly.Extensions/Telemetry/Log.cs index 5c0edf25929..d6281868f47 100644 --- a/src/Polly.Extensions/Telemetry/Log.cs +++ b/src/Polly.Extensions/Telemetry/Log.cs @@ -44,7 +44,7 @@ public static partial void ResilienceEvent( object? result, Exception? exception); #else - private static readonly Action _resilienceEventAction = + private static readonly Action ResilienceEventAction = LoggerMessage.Define(LogLevel.Warning, new EventId(0, "ResilienceEvent"), ResilienceEventMessage); public static void ResilienceEvent( @@ -57,7 +57,7 @@ public static void ResilienceEvent( object? result, Exception? exception) { - _resilienceEventAction(logger, eventName, builderName, strategyName, strategyType, strategyKey, result, exception); + ResilienceEventAction(logger, eventName, builderName, strategyName, strategyType, strategyKey, result, exception); } #endif @@ -69,7 +69,7 @@ public static partial void ExecutingStrategy( string? strategyKey, string resultType); #else - private static readonly Action _executingStrategyAction = + private static readonly Action ExecutingStrategyAction = LoggerMessage.Define(LogLevel.Debug, new EventId(1, "StrategyExecuting"), StrategyExecutingMessage); public static void ExecutingStrategy( @@ -78,7 +78,7 @@ public static void ExecutingStrategy( string? strategyKey, string resultType) { - _executingStrategyAction(logger, builderName, strategyKey, resultType, null); + ExecutingStrategyAction(logger, builderName, strategyKey, resultType, null); } #endif @@ -94,7 +94,7 @@ public static partial void StrategyExecuted( double executionTime, Exception? exception); #else - private static readonly Action _strategyExecutedAction = + private static readonly Action StrategyExecutedAction = LoggerMessage.Define(LogLevel.Debug, new EventId(2, "StrategyExecuted"), StrategyExecutedMessage); public static void StrategyExecuted( @@ -107,7 +107,7 @@ public static void StrategyExecuted( double executionTime, Exception? exception) { - _strategyExecutedAction(logger, builderName, strategyKey, resultType, result, executionHealth, executionTime, exception); + StrategyExecutedAction(logger, builderName, strategyKey, resultType, result, executionHealth, executionTime, exception); } #endif }