From 7b7bf8fd9aea9f9a10dde477526474d8491eb8b0 Mon Sep 17 00:00:00 2001 From: Martin Tomka Date: Wed, 26 Jul 2023 09:33:42 +0200 Subject: [PATCH 1/2] Drop the ResilienceStrategyBuilder.IsGenericBuilder property --- src/Polly.Core/Fallback/FallbackHandler.cs | 3 +-- .../FallbackResilienceStrategyBuilderExtensions.cs | 3 +-- .../HedgingResilienceStrategyBuilderExtensions.cs | 9 +++++---- src/Polly.Core/ResilienceStrategyBuilder.TResult.cs | 2 -- src/Polly.Core/ResilienceStrategyBuilder.cs | 2 -- src/Polly.Core/ResilienceStrategyBuilderBase.cs | 3 --- src/Polly.Core/ResilienceStrategyBuilderContext.cs | 4 ---- test/Polly.Core.Tests/Fallback/FallbackHandlerTests.cs | 4 ++-- test/Polly.Core.Tests/Fallback/FallbackHelper.cs | 6 ++---- .../Fallback/FallbackResilienceStrategyTests.cs | 5 ++--- .../GenericResilienceStrategyBuilderTests.cs | 3 +-- .../ResilienceStrategyBuilderContextTests.cs | 3 +-- test/Polly.Core.Tests/ResilienceStrategyBuilderTests.cs | 4 ---- 13 files changed, 15 insertions(+), 36 deletions(-) diff --git a/src/Polly.Core/Fallback/FallbackHandler.cs b/src/Polly.Core/Fallback/FallbackHandler.cs index 3753cd7d191..9123514493f 100644 --- a/src/Polly.Core/Fallback/FallbackHandler.cs +++ b/src/Polly.Core/Fallback/FallbackHandler.cs @@ -2,8 +2,7 @@ namespace Polly.Fallback; internal sealed record class FallbackHandler( Func, ValueTask> ShouldHandle, - Func, ValueTask>> ActionGenerator, - bool IsGeneric) + Func, ValueTask>> ActionGenerator) { public async ValueTask> GetFallbackOutcomeAsync(OutcomeArguments args) { diff --git a/src/Polly.Core/Fallback/FallbackResilienceStrategyBuilderExtensions.cs b/src/Polly.Core/Fallback/FallbackResilienceStrategyBuilderExtensions.cs index b16aff94a8e..7a174fcca46 100644 --- a/src/Polly.Core/Fallback/FallbackResilienceStrategyBuilderExtensions.cs +++ b/src/Polly.Core/Fallback/FallbackResilienceStrategyBuilderExtensions.cs @@ -56,8 +56,7 @@ internal static ResilienceStrategyBuilder AddFallback(this ResilienceStrategyBui { var handler = new FallbackHandler( options.ShouldHandle!, - options.FallbackAction!, - IsGeneric: context.IsGenericBuilder); + options.FallbackAction!); return new FallbackResilienceStrategy( handler, diff --git a/src/Polly.Core/Hedging/HedgingResilienceStrategyBuilderExtensions.cs b/src/Polly.Core/Hedging/HedgingResilienceStrategyBuilderExtensions.cs index ddd80cf8066..ef924a648e3 100644 --- a/src/Polly.Core/Hedging/HedgingResilienceStrategyBuilderExtensions.cs +++ b/src/Polly.Core/Hedging/HedgingResilienceStrategyBuilderExtensions.cs @@ -24,7 +24,7 @@ public static ResilienceStrategyBuilder AddHedging(this Resili Guard.NotNull(builder); Guard.NotNull(options); - builder.AddHedgingCore>(options); + builder.AddHedgingCore>(options, isGeneric: true); return builder; } @@ -41,7 +41,7 @@ internal static ResilienceStrategyBuilder AddHedging(this ResilienceStrategyBuil Guard.NotNull(builder); Guard.NotNull(options); - builder.AddHedgingCore(options); + builder.AddHedgingCore(options, isGeneric: false); return builder; } @@ -51,14 +51,15 @@ internal static ResilienceStrategyBuilder AddHedging(this ResilienceStrategyBuil Justification = "All options members preserved.")] internal static void AddHedgingCore( this ResilienceStrategyBuilderBase builder, - HedgingStrategyOptions options) + HedgingStrategyOptions options, + bool isGeneric) { builder.AddStrategy(context => { var handler = new HedgingHandler( options.ShouldHandle!, options.HedgingActionGenerator, - context.IsGenericBuilder); + isGeneric); return new HedgingResilienceStrategy( options.HedgingDelay, diff --git a/src/Polly.Core/ResilienceStrategyBuilder.TResult.cs b/src/Polly.Core/ResilienceStrategyBuilder.TResult.cs index f5b65b884c7..ad35560ef84 100644 --- a/src/Polly.Core/ResilienceStrategyBuilder.TResult.cs +++ b/src/Polly.Core/ResilienceStrategyBuilder.TResult.cs @@ -25,8 +25,6 @@ internal ResilienceStrategyBuilder(ResilienceStrategyBuilderBase other) { } - internal override bool IsGenericBuilder => true; - /// /// Builds the resilience strategy. /// diff --git a/src/Polly.Core/ResilienceStrategyBuilder.cs b/src/Polly.Core/ResilienceStrategyBuilder.cs index 5129a31a4a3..e0007454e82 100644 --- a/src/Polly.Core/ResilienceStrategyBuilder.cs +++ b/src/Polly.Core/ResilienceStrategyBuilder.cs @@ -12,8 +12,6 @@ namespace Polly; /// public sealed class ResilienceStrategyBuilder : ResilienceStrategyBuilderBase { - internal override bool IsGenericBuilder => false; - /// /// Builds the resilience strategy. /// diff --git a/src/Polly.Core/ResilienceStrategyBuilderBase.cs b/src/Polly.Core/ResilienceStrategyBuilderBase.cs index c4a4d297d15..2e454f20a65 100644 --- a/src/Polly.Core/ResilienceStrategyBuilderBase.cs +++ b/src/Polly.Core/ResilienceStrategyBuilderBase.cs @@ -120,8 +120,6 @@ private protected ResilienceStrategyBuilderBase(ResilienceStrategyBuilderBase ot [EditorBrowsable(EditorBrowsableState.Never)] public Action Validator { get; private protected set; } = ValidationHelper.ValidateObject; - internal abstract bool IsGenericBuilder { get; } - [RequiresUnreferencedCode(Constants.OptionsValidation)] internal void AddStrategyCore(Func factory, ResilienceStrategyOptions options) { @@ -168,7 +166,6 @@ private ResilienceStrategy CreateResilienceStrategy(Entry entry) builderProperties: Properties, strategyName: entry.Options.Name, timeProvider: TimeProvider, - isGenericBuilder: IsGenericBuilder, diagnosticSource: DiagnosticSource, randomizer: Randomizer); diff --git a/src/Polly.Core/ResilienceStrategyBuilderContext.cs b/src/Polly.Core/ResilienceStrategyBuilderContext.cs index ef597d6c046..63a47762bf7 100644 --- a/src/Polly.Core/ResilienceStrategyBuilderContext.cs +++ b/src/Polly.Core/ResilienceStrategyBuilderContext.cs @@ -15,7 +15,6 @@ internal ResilienceStrategyBuilderContext( ResilienceProperties builderProperties, string? strategyName, TimeProvider timeProvider, - bool isGenericBuilder, DiagnosticSource? diagnosticSource, Func randomizer) { @@ -24,7 +23,6 @@ internal ResilienceStrategyBuilderContext( BuilderProperties = builderProperties; StrategyName = strategyName; TimeProvider = timeProvider; - IsGenericBuilder = isGenericBuilder; Telemetry = TelemetryUtil.CreateTelemetry(diagnosticSource, builderName, builderInstanceName, builderProperties, strategyName); Randomizer = randomizer; } @@ -60,6 +58,4 @@ internal ResilienceStrategyBuilderContext( internal TimeProvider TimeProvider { get; } internal Func Randomizer { get; } - - internal bool IsGenericBuilder { get; } } diff --git a/test/Polly.Core.Tests/Fallback/FallbackHandlerTests.cs b/test/Polly.Core.Tests/Fallback/FallbackHandlerTests.cs index fbf0451c37c..a2dc1f732d4 100644 --- a/test/Polly.Core.Tests/Fallback/FallbackHandlerTests.cs +++ b/test/Polly.Core.Tests/Fallback/FallbackHandlerTests.cs @@ -6,7 +6,7 @@ public class FallbackHandlerTests [Fact] public async Task GenerateAction_Generic_Ok() { - var handler = FallbackHelper.CreateHandler(_ => true, () => Outcome.FromResult("secondary"), true); + var handler = FallbackHelper.CreateHandler(_ => true, () => Outcome.FromResult("secondary")); var context = ResilienceContextPool.Shared.Get(); var outcome = await handler.GetFallbackOutcomeAsync(new OutcomeArguments(context, Outcome.FromResult("primary"), default))!; @@ -16,7 +16,7 @@ public async Task GenerateAction_Generic_Ok() [Fact] public async Task GenerateAction_NonGeneric_Ok() { - var handler = FallbackHelper.CreateHandler(_ => true, () => Outcome.FromResult((object)"secondary"), false); + var handler = FallbackHelper.CreateHandler(_ => true, () => Outcome.FromResult((object)"secondary")); var context = ResilienceContextPool.Shared.Get(); var outcome = await handler.GetFallbackOutcomeAsync(new OutcomeArguments(context, Outcome.FromResult("primary"), default))!; diff --git a/test/Polly.Core.Tests/Fallback/FallbackHelper.cs b/test/Polly.Core.Tests/Fallback/FallbackHelper.cs index 84c141a8722..41668105352 100644 --- a/test/Polly.Core.Tests/Fallback/FallbackHelper.cs +++ b/test/Polly.Core.Tests/Fallback/FallbackHelper.cs @@ -6,13 +6,11 @@ internal static class FallbackHelper { public static FallbackHandler CreateHandler( Func, bool> shouldHandle, - Func> fallback, - bool isGeneric = true) + Func> fallback) { return new FallbackHandler( args => new ValueTask(shouldHandle(args.Outcome)), - _ => new ValueTask>(fallback()), - isGeneric); + _ => new ValueTask>(fallback())); } } diff --git a/test/Polly.Core.Tests/Fallback/FallbackResilienceStrategyTests.cs b/test/Polly.Core.Tests/Fallback/FallbackResilienceStrategyTests.cs index 706f96bce91..ca17f5915c1 100644 --- a/test/Polly.Core.Tests/Fallback/FallbackResilienceStrategyTests.cs +++ b/test/Polly.Core.Tests/Fallback/FallbackResilienceStrategyTests.cs @@ -84,10 +84,9 @@ public void Handle_UnhandledResult_Ok() private void SetHandler( Func, bool> shouldHandle, - Func> fallback, - bool isGeneric = true) + Func> fallback) { - _handler = FallbackHelper.CreateHandler(shouldHandle, fallback, isGeneric); + _handler = FallbackHelper.CreateHandler(shouldHandle, fallback); } private FallbackResilienceStrategy Create() => new( diff --git a/test/Polly.Core.Tests/GenericResilienceStrategyBuilderTests.cs b/test/Polly.Core.Tests/GenericResilienceStrategyBuilderTests.cs index c0adcb7742d..099d7ca9d3f 100644 --- a/test/Polly.Core.Tests/GenericResilienceStrategyBuilderTests.cs +++ b/test/Polly.Core.Tests/GenericResilienceStrategyBuilderTests.cs @@ -14,13 +14,12 @@ public void Ctor_EnsureDefaults() _builder.Properties.Should().NotBeNull(); _builder.TimeProvider.Should().Be(TimeProvider.System); _builder.OnCreatingStrategy.Should().BeNull(); - _builder.IsGenericBuilder.Should().BeTrue(); } [Fact] public void CopyCtor_Ok() { - new ResilienceStrategyBuilder(new ResilienceStrategyBuilder()).IsGenericBuilder.Should().BeTrue(); + new ResilienceStrategyBuilder(new ResilienceStrategyBuilder()).Should().NotBeNull(); } [Fact] diff --git a/test/Polly.Core.Tests/ResilienceStrategyBuilderContextTests.cs b/test/Polly.Core.Tests/ResilienceStrategyBuilderContextTests.cs index 464227a012d..d3467cdd1ff 100644 --- a/test/Polly.Core.Tests/ResilienceStrategyBuilderContextTests.cs +++ b/test/Polly.Core.Tests/ResilienceStrategyBuilderContextTests.cs @@ -10,9 +10,8 @@ public void Ctor_EnsureDefaults() { var properties = new ResilienceProperties(); var timeProvider = new FakeTimeProvider(); - var context = new ResilienceStrategyBuilderContext("builder-name", "instance", properties, "strategy-name", timeProvider, true, Mock.Of(), () => 1.0); + var context = new ResilienceStrategyBuilderContext("builder-name", "instance", properties, "strategy-name", timeProvider, Mock.Of(), () => 1.0); - context.IsGenericBuilder.Should().BeTrue(); context.BuilderName.Should().Be("builder-name"); context.BuilderInstanceName.Should().Be("instance"); context.BuilderProperties.Should().BeSameAs(properties); diff --git a/test/Polly.Core.Tests/ResilienceStrategyBuilderTests.cs b/test/Polly.Core.Tests/ResilienceStrategyBuilderTests.cs index dbb38338dfb..9bd3c154607 100644 --- a/test/Polly.Core.Tests/ResilienceStrategyBuilderTests.cs +++ b/test/Polly.Core.Tests/ResilienceStrategyBuilderTests.cs @@ -16,7 +16,6 @@ public void Ctor_EnsureDefaults() builder.BuilderName.Should().BeNull(); builder.Properties.Should().NotBeNull(); builder.TimeProvider.Should().Be(TimeProvider.System); - builder.IsGenericBuilder.Should().BeFalse(); builder.Randomizer.Should().NotBeNull(); } @@ -296,7 +295,6 @@ public void BuildStrategy_EnsureCorrectContext() builder.AddStrategy( context => { - context.IsGenericBuilder.Should().BeFalse(); context.BuilderName.Should().Be("builder-name"); context.StrategyName.Should().Be("strategy-name"); context.BuilderProperties.Should().BeSameAs(builder.Properties); @@ -460,7 +458,5 @@ private class InvalidResilienceStrategyBuilder : ResilienceStrategyBuilderBase { [Required] public string? RequiredProperty { get; set; } - - internal override bool IsGenericBuilder => false; } } From 940a005c2bf0b47653c028a833f6944b813c7974 Mon Sep 17 00:00:00 2001 From: Martin Tomka Date: Wed, 26 Jul 2023 10:00:15 +0200 Subject: [PATCH 2/2] kill mutants --- .../HedgingResilienceStrategyBuilderExtensions.cs | 9 ++++----- .../HedgingResilienceStrategyBuilderExtensionsTests.cs | 8 ++++++-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/Polly.Core/Hedging/HedgingResilienceStrategyBuilderExtensions.cs b/src/Polly.Core/Hedging/HedgingResilienceStrategyBuilderExtensions.cs index ef924a648e3..632258557e3 100644 --- a/src/Polly.Core/Hedging/HedgingResilienceStrategyBuilderExtensions.cs +++ b/src/Polly.Core/Hedging/HedgingResilienceStrategyBuilderExtensions.cs @@ -24,7 +24,7 @@ public static ResilienceStrategyBuilder AddHedging(this Resili Guard.NotNull(builder); Guard.NotNull(options); - builder.AddHedgingCore>(options, isGeneric: true); + builder.AddHedgingCore>(options); return builder; } @@ -41,7 +41,7 @@ internal static ResilienceStrategyBuilder AddHedging(this ResilienceStrategyBuil Guard.NotNull(builder); Guard.NotNull(options); - builder.AddHedgingCore(options, isGeneric: false); + builder.AddHedgingCore(options); return builder; } @@ -51,15 +51,14 @@ internal static ResilienceStrategyBuilder AddHedging(this ResilienceStrategyBuil Justification = "All options members preserved.")] internal static void AddHedgingCore( this ResilienceStrategyBuilderBase builder, - HedgingStrategyOptions options, - bool isGeneric) + HedgingStrategyOptions options) { builder.AddStrategy(context => { var handler = new HedgingHandler( options.ShouldHandle!, options.HedgingActionGenerator, - isGeneric); + IsGeneric: builder is not ResilienceStrategyBuilder); return new HedgingResilienceStrategy( options.HedgingDelay, diff --git a/test/Polly.Core.Tests/Hedging/HedgingResilienceStrategyBuilderExtensionsTests.cs b/test/Polly.Core.Tests/Hedging/HedgingResilienceStrategyBuilderExtensionsTests.cs index 3aa5beb0ce1..8c5ffa6cb2b 100644 --- a/test/Polly.Core.Tests/Hedging/HedgingResilienceStrategyBuilderExtensionsTests.cs +++ b/test/Polly.Core.Tests/Hedging/HedgingResilienceStrategyBuilderExtensionsTests.cs @@ -12,7 +12,9 @@ public class HedgingResilienceStrategyBuilderExtensionsTests public void AddHedging_Ok() { _builder.AddHedging(new HedgingStrategyOptions { ShouldHandle = _ => PredicateResult.True }); - _builder.Build().Should().BeOfType>(); + var strategy = _builder.Build().Should().BeOfType>() + .Subject + .HedgingHandler.IsGeneric.Should().BeFalse(); } [Fact] @@ -23,7 +25,9 @@ public void AddHedging_Generic_Ok() HedgingActionGenerator = args => () => Outcome.FromResultAsTask("dummy"), ShouldHandle = _ => PredicateResult.True }); - _genericBuilder.Build().Strategy.Should().BeOfType>(); + _genericBuilder.Build().Strategy + .Should().BeOfType>() + .Subject.HedgingHandler.IsGeneric.Should().BeTrue(); } [Fact]