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

Do not encourage returning the same instance from chaos strategies #1899

Merged
merged 2 commits into from
Jan 15, 2024
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
2 changes: 0 additions & 2 deletions src/Polly.Core/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,9 @@ Polly.Simmy.Outcomes.OutcomeStrategyOptions<TResult>.OutcomeGenerator.set -> voi
Polly.Simmy.Outcomes.OutcomeStrategyOptions<TResult>.OutcomeStrategyOptions() -> void
static Polly.Simmy.BehaviorPipelineBuilderExtensions.AddChaosBehavior<TBuilder>(this TBuilder! builder, double injectionRate, System.Func<System.Threading.Tasks.ValueTask>! behavior) -> TBuilder!
static Polly.Simmy.BehaviorPipelineBuilderExtensions.AddChaosBehavior<TBuilder>(this TBuilder! builder, Polly.Simmy.Behavior.BehaviorStrategyOptions! options) -> TBuilder!
static Polly.Simmy.FaultPipelineBuilderExtensions.AddChaosFault<TBuilder>(this TBuilder! builder, double injectionRate, System.Exception! fault) -> TBuilder!
static Polly.Simmy.FaultPipelineBuilderExtensions.AddChaosFault<TBuilder>(this TBuilder! builder, double injectionRate, System.Func<System.Exception?>! faultGenerator) -> TBuilder!
static Polly.Simmy.FaultPipelineBuilderExtensions.AddChaosFault<TBuilder>(this TBuilder! builder, Polly.Simmy.Fault.FaultStrategyOptions! options) -> TBuilder!
static Polly.Simmy.LatencyPipelineBuilderExtensions.AddChaosLatency<TBuilder>(this TBuilder! builder, double injectionRate, System.TimeSpan latency) -> TBuilder!
static Polly.Simmy.LatencyPipelineBuilderExtensions.AddChaosLatency<TBuilder>(this TBuilder! builder, Polly.Simmy.Latency.LatencyStrategyOptions! options) -> TBuilder!
static Polly.Simmy.OutcomePipelineBuilderExtensions.AddChaosResult<TResult>(this Polly.ResiliencePipelineBuilder<TResult>! builder, double injectionRate, System.Func<TResult?>! resultGenerator) -> Polly.ResiliencePipelineBuilder<TResult>!
static Polly.Simmy.OutcomePipelineBuilderExtensions.AddChaosResult<TResult>(this Polly.ResiliencePipelineBuilder<TResult>! builder, double injectionRate, TResult result) -> Polly.ResiliencePipelineBuilder<TResult>!
static Polly.Simmy.OutcomePipelineBuilderExtensions.AddChaosResult<TResult>(this Polly.ResiliencePipelineBuilder<TResult>! builder, Polly.Simmy.Outcomes.OutcomeStrategyOptions<TResult>! options) -> Polly.ResiliencePipelineBuilder<TResult>!
22 changes: 1 addition & 21 deletions src/Polly.Core/Simmy/Fault/FaultPipelineBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,6 @@ namespace Polly.Simmy;
/// </summary>
public static class FaultPipelineBuilderExtensions
{
/// <summary>
/// Adds a fault chaos strategy to the builder.
/// </summary>
/// <typeparam name="TBuilder">The builder type.</typeparam>
/// <param name="builder">The builder instance.</param>
/// <param name="injectionRate">The injection rate for a given execution, which the value should be between [0, 1] (inclusive).</param>
/// <param name="fault">The exception to inject.</param>
/// <returns>The builder instance with the retry strategy added.</returns>
public static TBuilder AddChaosFault<TBuilder>(this TBuilder builder, double injectionRate, Exception fault)
where TBuilder : ResiliencePipelineBuilderBase
{
builder.AddChaosFault(new FaultStrategyOptions
{
Enabled = true,
InjectionRate = injectionRate,
Fault = fault
});
return builder;
}

/// <summary>
/// Adds a fault chaos strategy to the builder.
/// </summary>
Expand All @@ -43,7 +23,7 @@ public static TBuilder AddChaosFault<TBuilder>(this TBuilder builder, double inj
{
Enabled = true,
InjectionRate = injectionRate,
FaultGenerator = (_) => new ValueTask<Exception?>(Task.FromResult(faultGenerator()))
FaultGenerator = (_) => new ValueTask<Exception?>(faultGenerator())
});
return builder;
}
Expand Down
29 changes: 4 additions & 25 deletions src/Polly.Core/Simmy/Outcomes/OutcomePipelineBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,6 @@ namespace Polly.Simmy;
/// </summary>
public static class OutcomePipelineBuilderExtensions
{
/// <summary>
/// Adds an outcome chaos strategy to the builder.
/// </summary>
/// <typeparam name="TResult">The type of result the retry strategy handles.</typeparam>
/// <param name="builder">The builder instance.</param>
/// <param name="injectionRate">The injection rate for a given execution, which the value should be between [0, 1] (inclusive).</param>
/// <param name="result">The outcome to inject. For disposable outcomes use either the generator or the options overload.</param>
/// <returns>The builder instance with the retry strategy added.</returns>
public static ResiliencePipelineBuilder<TResult> AddChaosResult<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TResult>(
this ResiliencePipelineBuilder<TResult> builder,
double injectionRate,
TResult result)
{
Guard.NotNull(builder);

builder.AddChaosResult(new OutcomeStrategyOptions<TResult>
{
Enabled = true,
InjectionRate = injectionRate,
OutcomeGenerator = (_) => new ValueTask<Outcome<TResult>?>(Task.FromResult<Outcome<TResult>?>(Outcome.FromResult(result)))
});
return builder;
}

/// <summary>
/// Adds an outcome chaos strategy to the builder.
/// </summary>
Expand All @@ -51,7 +27,10 @@ public static class OutcomePipelineBuilderExtensions
{
Enabled = true,
InjectionRate = injectionRate,
OutcomeGenerator = (_) => new ValueTask<Outcome<TResult>?>(Task.FromResult<Outcome<TResult>?>(Outcome.FromResult(resultGenerator())))
OutcomeGenerator = (_) =>
{
return new ValueTask<Outcome<TResult>?>(Outcome.FromResult(resultGenerator()));
}
});
return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,6 @@ internal void AddFault_Options_Ok(Action<ResiliencePipelineBuilder> configure)
builder.Invoking(b => configure(b)).Should().NotThrow();
}

[Fact]
public void AddFault_Shortcut_Option_Ok()
{
var builder = new ResiliencePipelineBuilder();
builder
.AddChaosFault(0.5, new InvalidOperationException("Dummy exception"))
.Build();

AssertFaultStrategy<InvalidOperationException>(builder, true, 0.5);
}

[Fact]
public void AddFault_Generic_Shortcut_Generator_Option_Throws()
{
Expand Down Expand Up @@ -102,26 +91,6 @@ public void AddFault_Shortcut_Generator_Option_Ok()
AssertFaultStrategy<InvalidOperationException>(builder, true, 0.5);
}

[Fact]
public void AddFault_Generic_Shortcut_Option_Ok()
{
var builder = new ResiliencePipelineBuilder<string>();
builder
.AddChaosFault(0.5, new InvalidOperationException("Dummy exception"))
.Build();

AssertFaultStrategy<string, InvalidOperationException>(builder, true, 0.5);
}

[Fact]
public void AddFault_Generic_Shortcut_Option_Throws()
{
new ResiliencePipelineBuilder<int>()
.Invoking(b => b.AddChaosFault(-1, new InvalidOperationException()))
.Should()
.Throw<ValidationException>();
}

[Fact]
public void AddFault_Generic_Shortcut_Generator_Option_Ok()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,6 @@ internal void AddResult_Options_Ok(Action<ResiliencePipelineBuilder<int>> config
builder.Invoking(b => configure(b)).Should().NotThrow();
}

[Fact]
public void AddResult_Shortcut_Option_Ok()
{
var builder = new ResiliencePipelineBuilder<int>();
builder
.AddChaosResult(0.5, 120)
.Build();

AssertResultStrategy(builder, true, 0.5, new(120));
}

[Fact]
public void AddResult_Shortcut_Generator_Option_Ok()
{
Expand Down
Loading