Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop the ResilienceStrategyBuilder.IsGenericBuilder property #1431

Merged
merged 2 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/Polly.Core/Fallback/FallbackHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ namespace Polly.Fallback;

internal sealed record class FallbackHandler<T>(
Func<OutcomeArguments<T, FallbackPredicateArguments>, ValueTask<bool>> ShouldHandle,
Func<OutcomeArguments<T, FallbackPredicateArguments>, ValueTask<Outcome<T>>> ActionGenerator,
bool IsGeneric)
Func<OutcomeArguments<T, FallbackPredicateArguments>, ValueTask<Outcome<T>>> ActionGenerator)
{
public async ValueTask<Outcome<TResult>> GetFallbackOutcomeAsync<TResult>(OutcomeArguments<TResult, FallbackPredicateArguments> args)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ internal static ResilienceStrategyBuilder AddFallback(this ResilienceStrategyBui
{
var handler = new FallbackHandler<TResult>(
options.ShouldHandle!,
options.FallbackAction!,
IsGeneric: context.IsGenericBuilder);
options.FallbackAction!);

return new FallbackResilienceStrategy<TResult>(
handler,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ internal static ResilienceStrategyBuilder AddHedging(this ResilienceStrategyBuil
var handler = new HedgingHandler<TResult>(
options.ShouldHandle!,
options.HedgingActionGenerator,
context.IsGenericBuilder);
IsGeneric: builder is not ResilienceStrategyBuilder);

return new HedgingResilienceStrategy<TResult>(
options.HedgingDelay,
Expand Down
2 changes: 0 additions & 2 deletions src/Polly.Core/ResilienceStrategyBuilder.TResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ internal ResilienceStrategyBuilder(ResilienceStrategyBuilderBase other)
{
}

internal override bool IsGenericBuilder => true;

/// <summary>
/// Builds the resilience strategy.
/// </summary>
Expand Down
2 changes: 0 additions & 2 deletions src/Polly.Core/ResilienceStrategyBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ namespace Polly;
/// </remarks>
public sealed class ResilienceStrategyBuilder : ResilienceStrategyBuilderBase
{
internal override bool IsGenericBuilder => false;

/// <summary>
/// Builds the resilience strategy.
/// </summary>
Expand Down
3 changes: 0 additions & 3 deletions src/Polly.Core/ResilienceStrategyBuilderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,6 @@ private protected ResilienceStrategyBuilderBase(ResilienceStrategyBuilderBase ot
[EditorBrowsable(EditorBrowsableState.Never)]
public Action<ResilienceValidationContext> Validator { get; private protected set; } = ValidationHelper.ValidateObject;

internal abstract bool IsGenericBuilder { get; }

[RequiresUnreferencedCode(Constants.OptionsValidation)]
internal void AddStrategyCore(Func<ResilienceStrategyBuilderContext, ResilienceStrategy> factory, ResilienceStrategyOptions options)
{
Expand Down Expand Up @@ -168,7 +166,6 @@ private ResilienceStrategy CreateResilienceStrategy(Entry entry)
builderProperties: Properties,
strategyName: entry.Options.Name,
timeProvider: TimeProvider,
isGenericBuilder: IsGenericBuilder,
diagnosticSource: DiagnosticSource,
randomizer: Randomizer);

Expand Down
4 changes: 0 additions & 4 deletions src/Polly.Core/ResilienceStrategyBuilderContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ internal ResilienceStrategyBuilderContext(
ResilienceProperties builderProperties,
string? strategyName,
TimeProvider timeProvider,
bool isGenericBuilder,
DiagnosticSource? diagnosticSource,
Func<double> randomizer)
{
Expand All @@ -24,7 +23,6 @@ internal ResilienceStrategyBuilderContext(
BuilderProperties = builderProperties;
StrategyName = strategyName;
TimeProvider = timeProvider;
IsGenericBuilder = isGenericBuilder;
Telemetry = TelemetryUtil.CreateTelemetry(diagnosticSource, builderName, builderInstanceName, builderProperties, strategyName);
Randomizer = randomizer;
}
Expand Down Expand Up @@ -60,6 +58,4 @@ internal ResilienceStrategyBuilderContext(
internal TimeProvider TimeProvider { get; }

internal Func<double> Randomizer { get; }

internal bool IsGenericBuilder { get; }
}
4 changes: 2 additions & 2 deletions test/Polly.Core.Tests/Fallback/FallbackHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, FallbackPredicateArguments>(context, Outcome.FromResult("primary"), default))!;

Expand All @@ -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<string, FallbackPredicateArguments>(context, Outcome.FromResult("primary"), default))!;

Expand Down
6 changes: 2 additions & 4 deletions test/Polly.Core.Tests/Fallback/FallbackHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ internal static class FallbackHelper
{
public static FallbackHandler<T> CreateHandler<T>(
Func<Outcome<T>, bool> shouldHandle,
Func<Outcome<T>> fallback,
bool isGeneric = true)
Func<Outcome<T>> fallback)
{
return new FallbackHandler<T>(
args => new ValueTask<bool>(shouldHandle(args.Outcome)),
_ => new ValueTask<Outcome<T>>(fallback()),
isGeneric);
_ => new ValueTask<Outcome<T>>(fallback()));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,9 @@ public void Handle_UnhandledResult_Ok()

private void SetHandler(
Func<Outcome<string>, bool> shouldHandle,
Func<Outcome<string>> fallback,
bool isGeneric = true)
Func<Outcome<string>> fallback)
{
_handler = FallbackHelper.CreateHandler(shouldHandle, fallback, isGeneric);
_handler = FallbackHelper.CreateHandler(shouldHandle, fallback);
}

private FallbackResilienceStrategy<string> Create() => new(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>(new ResilienceStrategyBuilder()).IsGenericBuilder.Should().BeTrue();
new ResilienceStrategyBuilder<string>(new ResilienceStrategyBuilder()).Should().NotBeNull();
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ public class HedgingResilienceStrategyBuilderExtensionsTests
public void AddHedging_Ok()
{
_builder.AddHedging(new HedgingStrategyOptions { ShouldHandle = _ => PredicateResult.True });
_builder.Build().Should().BeOfType<HedgingResilienceStrategy<object>>();
var strategy = _builder.Build().Should().BeOfType<HedgingResilienceStrategy<object>>()
.Subject
.HedgingHandler.IsGeneric.Should().BeFalse();
}

[Fact]
Expand All @@ -23,7 +25,9 @@ public void AddHedging_Generic_Ok()
HedgingActionGenerator = args => () => Outcome.FromResultAsTask("dummy"),
ShouldHandle = _ => PredicateResult.True
});
_genericBuilder.Build().Strategy.Should().BeOfType<HedgingResilienceStrategy<string>>();
_genericBuilder.Build().Strategy
.Should().BeOfType<HedgingResilienceStrategy<string>>()
.Subject.HedgingHandler.IsGeneric.Should().BeTrue();
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<DiagnosticSource>(), () => 1.0);
var context = new ResilienceStrategyBuilderContext("builder-name", "instance", properties, "strategy-name", timeProvider, Mock.Of<DiagnosticSource>(), () => 1.0);

context.IsGenericBuilder.Should().BeTrue();
context.BuilderName.Should().Be("builder-name");
context.BuilderInstanceName.Should().Be("instance");
context.BuilderProperties.Should().BeSameAs(properties);
Expand Down
4 changes: 0 additions & 4 deletions test/Polly.Core.Tests/ResilienceStrategyBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -460,7 +458,5 @@ private class InvalidResilienceStrategyBuilder : ResilienceStrategyBuilderBase
{
[Required]
public string? RequiredProperty { get; set; }

internal override bool IsGenericBuilder => false;
}
}