Skip to content

Commit

Permalink
PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
martintmk committed Jun 13, 2023
1 parent 43e760e commit fcf5307
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 38 deletions.
12 changes: 11 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> _myKey = new("my-key");
private readonly CancellationTokenSource _cts;
private readonly HedgingTimeProvider _timeProvider;
Expand Down Expand Up @@ -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]
Expand All @@ -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);
}
Expand Down
30 changes: 15 additions & 15 deletions src/Polly.Core.Tests/Hedging/HedgingResilienceStrategyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TelemetryEventArguments> _events = new();
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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,
Expand All @@ -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);
}

Expand Down Expand Up @@ -501,8 +501,8 @@ public async Task ExecuteAsync_CancellationLinking_Ok()
_timeProvider.Advance(TimeSpan.FromHours(1));
await task.Invoking(async t => await t).Should().ThrowAsync<OperationCanceledException>();

primaryCancelled.WaitOne(_assertTimeout).Should().BeTrue();
secondaryCancelled.WaitOne(_assertTimeout).Should().BeTrue();
primaryCancelled.WaitOne(AssertTimeout).Should().BeTrue();
secondaryCancelled.WaitOne(AssertTimeout).Should().BeTrue();
}

[Fact]
Expand Down Expand Up @@ -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<Outcome<string>> Execute(CancellationToken token)
Expand All @@ -567,7 +567,7 @@ async ValueTask<Outcome<string>> Execute(CancellationToken token)
allExecutionsReached.Set();
}

await _timeProvider.Delay(_longDelay, token);
await _timeProvider.Delay(LongDelay, token);
return Success.AsOutcome();
}
}
Expand All @@ -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<Outcome<string>> Execute(CancellationToken token)
{
Expand All @@ -602,7 +602,7 @@ async ValueTask<Outcome<string>> Execute(CancellationToken token)
allExecutions.Set();
}

await _timeProvider.Delay(_longDelay, token);
await _timeProvider.Delay(LongDelay, token);

return "dummy".AsOutcome();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Polly.Core/CircuitBreaker/Health/HealthMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Polly.Core/ResilienceContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public sealed class ResilienceContext
{
private const bool ContinueOnCapturedContextDefault = false;

private static readonly ObjectPool<ResilienceContext> _pool = new(static () => new ResilienceContext(), static c => c.Reset());
private static readonly ObjectPool<ResilienceContext> Pool = new(static () => new ResilienceContext(), static c => c.Reset());

private readonly List<ResilienceEvent> _resilienceEvents = new();

Expand Down Expand Up @@ -74,7 +74,7 @@ private ResilienceContext()
/// After the execution is finished you should return the <see cref="ResilienceContext"/> back to the pool
/// by calling <see cref="Return(ResilienceContext)"/> method.
/// </remarks>
public static ResilienceContext Get() => _pool.Get();
public static ResilienceContext Get() => Pool.Get();

internal void InitializeFrom(ResilienceContext context)
{
Expand All @@ -95,7 +95,7 @@ public static void Return(ResilienceContext context)
{
Guard.NotNull(context);

_pool.Return(context);
Pool.Return(context);
}

[ExcludeFromCodeCoverage]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Polly.Extensions.Tests;

public class ReloadableResilienceStrategyTests
{
private static readonly ResiliencePropertyKey<string> _tagKey = new("tests.tag");
private static readonly ResiliencePropertyKey<string> TagKey = new("tests.tag");

[InlineData(null)]
[InlineData("custom-name")]
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -101,7 +101,7 @@ protected override ValueTask<Outcome<TResult>> ExecuteCoreAsync<TResult, TState>
ResilienceContext context,
TState state)
{
context.Properties.Set(_tagKey, Tag);
context.Properties.Set(TagKey, Tag);
return callback(context, state);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Polly.Extensions/Telemetry/EnrichmentContext.Pool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Polly.Extensions.Telemetry;

public partial class EnrichmentContext
{
private static readonly ObjectPool<EnrichmentContext> _contextPool = new(
private static readonly ObjectPool<EnrichmentContext> ContextPool = new(
static () => new EnrichmentContext(),
static context =>
{
Expand All @@ -16,7 +16,7 @@ public partial class EnrichmentContext

internal static EnrichmentContext Get(ResilienceContext resilienceContext, object? arguments, Outcome<object>? outcome)
{
var context = _contextPool.Get();
var context = ContextPool.Get();
context.Context = resilienceContext;
context.Arguments = arguments;
context.Outcome = outcome;
Expand All @@ -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);
}
}
12 changes: 6 additions & 6 deletions src/Polly.Extensions/Telemetry/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static partial void ResilienceEvent(
object? result,
Exception? exception);
#else
private static readonly Action<ILogger, string, string?, string?, string, string?, object?, Exception?> _resilienceEventAction =
private static readonly Action<ILogger, string, string?, string?, string, string?, object?, Exception?> ResilienceEventAction =
LoggerMessage.Define<string, string?, string?, string, string?, object?>(LogLevel.Warning, new EventId(0, "ResilienceEvent"), ResilienceEventMessage);

public static void ResilienceEvent(
Expand All @@ -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

Expand All @@ -69,7 +69,7 @@ public static partial void ExecutingStrategy(
string? strategyKey,
string resultType);
#else
private static readonly Action<ILogger, string?, string?, string, Exception?> _executingStrategyAction =
private static readonly Action<ILogger, string?, string?, string, Exception?> ExecutingStrategyAction =
LoggerMessage.Define<string?, string?, string>(LogLevel.Debug, new EventId(1, "StrategyExecuting"), StrategyExecutingMessage);

public static void ExecutingStrategy(
Expand All @@ -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

Expand All @@ -94,7 +94,7 @@ public static partial void StrategyExecuted(
double executionTime,
Exception? exception);
#else
private static readonly Action<ILogger, string?, string?, string, object?, string, double, Exception?> _strategyExecutedAction =
private static readonly Action<ILogger, string?, string?, string, object?, string, double, Exception?> StrategyExecutedAction =
LoggerMessage.Define<string?, string?, string, object?, string, double>(LogLevel.Debug, new EventId(2, "StrategyExecuted"), StrategyExecutedMessage);

public static void StrategyExecuted(
Expand All @@ -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
}

0 comments on commit fcf5307

Please sign in to comment.