Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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: 0 additions & 3 deletions docs/chaos/behavior.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@ The behavior chaos strategy is designed to inject custom behaviors into system o
var optionsWithBehaviorGenerator = new ChaosBehaviorStrategyOptions
{
BehaviorAction = static args => RestartRedisAsync(args.Context.CancellationToken),
Enabled = true,
InjectionRate = 0.05
};

// To get notifications when a behavior is injected
var optionsOnBehaviorInjected = new ChaosBehaviorStrategyOptions
{
BehaviorAction = static args => RestartRedisAsync(args.Context.CancellationToken),
Enabled = true,
InjectionRate = 0.05,
OnBehaviorInjected = static args =>
{
Expand Down Expand Up @@ -63,7 +61,6 @@ var pipeline = new ResiliencePipelineBuilder()
.AddChaosBehavior(new ChaosBehaviorStrategyOptions // Chaos strategies are usually placed as the last ones in the pipeline
{
BehaviorAction = static args => RestartRedisAsync(args.Context.CancellationToken),
Enabled = true,
InjectionRate = 0.05
})
.Build();
Expand Down
4 changes: 0 additions & 4 deletions docs/chaos/fault.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ var optionsBasic = new ChaosFaultStrategyOptions
FaultGenerator = new FaultGenerator()
.AddException<InvalidOperationException>() // Uses default constructor
.AddException(() => new TimeoutException("Chaos timeout injected.")), // Custom exception generator
Enabled = true,
InjectionRate = 0.1
};

Expand All @@ -43,15 +42,13 @@ var optionsWithFaultGenerator = new ChaosFaultStrategyOptions

return new ValueTask<Exception?>(exception);
},
Enabled = true,
InjectionRate = 0.1
};

// To get notifications when a fault is injected
var optionsOnFaultInjected = new ChaosFaultStrategyOptions
{
FaultGenerator = new FaultGenerator().AddException<InvalidOperationException>(),
Enabled = true,
InjectionRate = 0.1,
OnFaultInjected = static args =>
{
Expand Down Expand Up @@ -85,7 +82,6 @@ var pipeline = new ResiliencePipelineBuilder()
.AddChaosFault(new ChaosFaultStrategyOptions // Chaos strategies are usually placed as the last ones in the pipeline
{
FaultGenerator = static args => new ValueTask<Exception?>(new InvalidOperationException("Dummy exception")),
Enabled = true,
InjectionRate = 0.1
})
.Build();
Expand Down
5 changes: 4 additions & 1 deletion docs/chaos/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ All the strategies' options implement the [`ChaosStrategyOptions`](xref:Polly.Si
|--------------------------|---------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `InjectionRate` | 0.001 | A decimal between 0 and 1 inclusive. The strategy will inject the chaos, randomly, that proportion of the time, e.g.: if 0.2, twenty percent of calls will be randomly affected; if 0.01, one percent of calls; if 1, all calls. |
| `InjectionRateGenerator` | `null` | Generates the injection rate for a given execution, which the value should be between [0, 1] (inclusive). |
| `Enabled` | `false` | Determines whether the strategy is enabled or not. |
| `Enabled` | `true` | Determines whether the strategy is enabled or not. |
| `EnabledGenerator` | `null` | The generator that indicates whether the chaos strategy is enabled for a given execution. |

> [!NOTE]
> If both `Enabled` and `EnabledGenerator` are specified then `Enabled` will be ignored.

[simmy]: https://github.com/Polly-Contrib/Simmy
7 changes: 3 additions & 4 deletions docs/chaos/latency.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ var optionsDefault = new ChaosLatencyStrategyOptions();
var basicOptions = new ChaosLatencyStrategyOptions
{
Latency = TimeSpan.FromSeconds(30),
Enabled = true,
InjectionRate = 0.1
};

Expand All @@ -45,15 +44,13 @@ var optionsWithLatencyGenerator = new ChaosLatencyStrategyOptions

return new ValueTask<TimeSpan>(latency);
},
Enabled = true,
InjectionRate = 0.1
};

// To get notifications when a delay is injected
var optionsOnLatencyInjected = new ChaosLatencyStrategyOptions
{
Latency = TimeSpan.FromSeconds(30),
Enabled = true,
InjectionRate = 0.1,
OnLatencyInjected = static args =>
{
Expand Down Expand Up @@ -88,7 +85,6 @@ var pipeline = new ResiliencePipelineBuilder()
.AddChaosLatency(new ChaosLatencyStrategyOptions // Chaos strategies are usually placed as the last ones in the pipeline
{
Latency = TimeSpan.FromSeconds(10),
Enabled = true,
InjectionRate = 0.1
})
.Build();
Expand All @@ -103,6 +99,9 @@ var pipeline = new ResiliencePipelineBuilder()
| `LatencyGenerator` | `null` | Generates the latency to inject for a given execution. |
| `OnLatencyInjected` | `null` | Action executed when latency is injected. |

> [!NOTE]
> If both `Latency` and `LatencyGenerator` are specified then `Latency` will be ignored.

## Diagrams

### Normal 🐵 sequence diagram
Expand Down
3 changes: 0 additions & 3 deletions docs/chaos/outcome.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ var optionsWithResultGenerator = new ChaosOutcomeStrategyOptions<HttpResponseMes
.AddResult(() => new HttpResponseMessage(HttpStatusCode.TooManyRequests))
.AddResult(() => new HttpResponseMessage(HttpStatusCode.InternalServerError))
.AddException(() => new HttpRequestException("Chaos request exception.")),
Enabled = true,
InjectionRate = 0.1
};

Expand All @@ -34,7 +33,6 @@ var optionsOnBehaviorInjected = new ChaosOutcomeStrategyOptions<HttpResponseMess
{
OutcomeGenerator = new OutcomeGenerator<HttpResponseMessage>()
.AddResult(() => new HttpResponseMessage(HttpStatusCode.InternalServerError)),
Enabled = true,
InjectionRate = 0.1,
OnOutcomeInjected = static args =>
{
Expand Down Expand Up @@ -76,7 +74,6 @@ var pipeline = new ResiliencePipelineBuilder<HttpResponseMessage>()
var response = new HttpResponseMessage(HttpStatusCode.InternalServerError);
return new ValueTask<Outcome<HttpResponseMessage>?>(Outcome.FromResult(response));
},
Enabled = true,
InjectionRate = 0.1
})
.Build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public static TBuilder AddChaosBehavior<TBuilder>(this TBuilder builder, double

return builder.AddChaosBehavior(new ChaosBehaviorStrategyOptions
{
Enabled = true,
InjectionRate = injectionRate,
BehaviorAction = args => behavior(args.Context.CancellationToken)
});
Expand Down
2 changes: 2 additions & 0 deletions src/Polly.Core/Simmy/ChaosStrategyConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ internal static class ChaosStrategyConstants
public const double MaxInjectionThreshold = 1;

public const double DefaultInjectionRate = 0.001;

public const bool DefaultEnabled = true;
}
6 changes: 3 additions & 3 deletions src/Polly.Core/Simmy/ChaosStrategyOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ public abstract class ChaosStrategyOptions : ResilienceStrategyOptions
/// </summary>
/// <remarks>
/// Defaults to <see langword="null"/>. Either <see cref="Enabled"/> or this property is required.
/// When this property is <see langword="null"/> the <see cref="Enabled"/> is used.
/// When this property is <see langword="null"/> then the <see cref="Enabled"/> property is used.
/// </remarks>
public Func<EnabledGeneratorArguments, ValueTask<bool>>? EnabledGenerator { get; set; }

/// <summary>
/// Gets or sets a value indicating whether or not the chaos strategy is enabled for a given execution.
/// </summary>
/// <remarks>
/// Defaults to <see langword="false"/>. Either <see cref="EnabledGenerator"/> or this property is required.
/// Defaults to <see langword="true"/>. Either <see cref="EnabledGenerator"/> or this property is required.
/// </remarks>
public bool Enabled { get; set; }
public bool Enabled { get; set; } = ChaosStrategyConstants.DefaultEnabled;

/// <summary>
/// Gets or sets the Randomizer generator instance that is used to evaluate the injection rate.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public static TBuilder AddChaosFault<TBuilder>(this TBuilder builder, double inj
{
builder.AddChaosFault(new ChaosFaultStrategyOptions
{
Enabled = true,
InjectionRate = injectionRate,
FaultGenerator = (_) => new ValueTask<Exception?>(faultGenerator())
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public static TBuilder AddChaosLatency<TBuilder>(this TBuilder builder, double i

return builder.AddChaosLatency(new ChaosLatencyStrategyOptions
{
Enabled = true,
InjectionRate = injectionRate,
Latency = latency
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public static class ChaosOutcomePipelineBuilderExtensions

builder.AddChaosOutcome(new ChaosOutcomeStrategyOptions<TResult>
{
Enabled = true,
InjectionRate = injectionRate,
OutcomeGenerator = (_) =>
{
Expand Down
3 changes: 0 additions & 3 deletions src/Snippets/Docs/Chaos.Behavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@ public static void BehaviorUsage()
var optionsWithBehaviorGenerator = new ChaosBehaviorStrategyOptions
{
BehaviorAction = static args => RestartRedisAsync(args.Context.CancellationToken),
Enabled = true,
InjectionRate = 0.05
};

// To get notifications when a behavior is injected
var optionsOnBehaviorInjected = new ChaosBehaviorStrategyOptions
{
BehaviorAction = static args => RestartRedisAsync(args.Context.CancellationToken),
Enabled = true,
InjectionRate = 0.05,
OnBehaviorInjected = static args =>
{
Expand Down Expand Up @@ -52,7 +50,6 @@ public static void BehaviorUsage()
.AddChaosBehavior(new ChaosBehaviorStrategyOptions // Chaos strategies are usually placed as the last ones in the pipeline
{
BehaviorAction = static args => RestartRedisAsync(args.Context.CancellationToken),
Enabled = true,
InjectionRate = 0.05
})
.Build();
Expand Down
4 changes: 0 additions & 4 deletions src/Snippets/Docs/Chaos.Fault.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public static void FaultUsage()
FaultGenerator = new FaultGenerator()
.AddException<InvalidOperationException>() // Uses default constructor
.AddException(() => new TimeoutException("Chaos timeout injected.")), // Custom exception generator
Enabled = true,
InjectionRate = 0.1
};

Expand All @@ -38,15 +37,13 @@ public static void FaultUsage()

return new ValueTask<Exception?>(exception);
},
Enabled = true,
InjectionRate = 0.1
};

// To get notifications when a fault is injected
var optionsOnFaultInjected = new ChaosFaultStrategyOptions
{
FaultGenerator = new FaultGenerator().AddException<InvalidOperationException>(),
Enabled = true,
InjectionRate = 0.1,
OnFaultInjected = static args =>
{
Expand Down Expand Up @@ -76,7 +73,6 @@ public static void FaultUsage()
.AddChaosFault(new ChaosFaultStrategyOptions // Chaos strategies are usually placed as the last ones in the pipeline
{
FaultGenerator = static args => new ValueTask<Exception?>(new InvalidOperationException("Dummy exception")),
Enabled = true,
InjectionRate = 0.1
})
.Build();
Expand Down
4 changes: 0 additions & 4 deletions src/Snippets/Docs/Chaos.Latency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public static void LatencyUsage()
var basicOptions = new ChaosLatencyStrategyOptions
{
Latency = TimeSpan.FromSeconds(30),
Enabled = true,
InjectionRate = 0.1
};

Expand All @@ -39,15 +38,13 @@ public static void LatencyUsage()

return new ValueTask<TimeSpan>(latency);
},
Enabled = true,
InjectionRate = 0.1
};

// To get notifications when a delay is injected
var optionsOnLatencyInjected = new ChaosLatencyStrategyOptions
{
Latency = TimeSpan.FromSeconds(30),
Enabled = true,
InjectionRate = 0.1,
OnLatencyInjected = static args =>
{
Expand Down Expand Up @@ -78,7 +75,6 @@ public static void LatencyUsage()
.AddChaosLatency(new ChaosLatencyStrategyOptions // Chaos strategies are usually placed as the last ones in the pipeline
{
Latency = TimeSpan.FromSeconds(10),
Enabled = true,
InjectionRate = 0.1
})
.Build();
Expand Down
3 changes: 0 additions & 3 deletions src/Snippets/Docs/Chaos.Outcome.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public static void OutcomeUsage()
.AddResult(() => new HttpResponseMessage(HttpStatusCode.TooManyRequests))
.AddResult(() => new HttpResponseMessage(HttpStatusCode.InternalServerError))
.AddException(() => new HttpRequestException("Chaos request exception.")),
Enabled = true,
InjectionRate = 0.1
};

Expand All @@ -29,7 +28,6 @@ public static void OutcomeUsage()
{
OutcomeGenerator = new OutcomeGenerator<HttpResponseMessage>()
.AddResult(() => new HttpResponseMessage(HttpStatusCode.InternalServerError)),
Enabled = true,
InjectionRate = 0.1,
OnOutcomeInjected = static args =>
{
Expand Down Expand Up @@ -67,7 +65,6 @@ public static void OutcomeUsage()
var response = new HttpResponseMessage(HttpStatusCode.InternalServerError);
return new ValueTask<Outcome<HttpResponseMessage>?>(Outcome.FromResult(response));
},
Enabled = true,
InjectionRate = 0.1
})
.Build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public void AddBehavior_Options_Ok()
var sut = new ResiliencePipelineBuilder()
.AddChaosBehavior(new ChaosBehaviorStrategyOptions
{
Enabled = true,
InjectionRate = 1,
BehaviorAction = (_) => default
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public void Ctor_Ok()
{
var sut = new ChaosBehaviorStrategyOptions();
sut.Randomizer.Should().NotBeNull();
sut.Enabled.Should().BeFalse();
sut.Enabled.Should().BeTrue();
sut.EnabledGenerator.Should().BeNull();
sut.InjectionRate.Should().Be(ChaosStrategyConstants.DefaultInjectionRate);
sut.InjectionRateGenerator.Should().BeNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public void Given_not_enabled_should_not_inject_behavior()
public async Task Given_enabled_and_randomly_within_threshold_should_inject_behavior()
{
_options.InjectionRate = 0.6;
_options.Enabled = true;
_options.Randomizer = () => 0.5;
_options.BehaviorAction = (_) => { _behaviorActionExecuted = true; return default; };

Expand All @@ -56,7 +55,6 @@ public async Task Given_enabled_and_randomly_within_threshold_should_inject_beha
public async Task Given_enabled_and_randomly_within_threshold_ensure_on_behavior_injected_called()
{
_options.InjectionRate = 0.6;
_options.Enabled = true;
_options.Randomizer = () => 0.5;
_options.BehaviorAction = (_) => { _behaviorActionExecuted = true; return default; };
_options.OnBehaviorInjected = args =>
Expand Down Expand Up @@ -97,7 +95,6 @@ public async Task Given_enabled_and_randomly_not_within_threshold_should_not_inj
public async Task Should_inject_behavior_before_executing_user_delegate()
{
_options.InjectionRate = 0.6;
_options.Enabled = true;
_options.Randomizer = () => 0.5;
_options.BehaviorAction = (_) =>
{
Expand All @@ -119,7 +116,6 @@ public async Task Should_not_execute_user_delegate_when_it_was_cancelled_running
{
using var cts = new CancellationTokenSource();
_options.InjectionRate = 0.6;
_options.Enabled = true;
_options.Randomizer = () => 0.5;
_options.BehaviorAction = (_) =>
{
Expand Down
13 changes: 13 additions & 0 deletions test/Polly.Core.Tests/Simmy/ChaosStrategyConstantsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Polly.Simmy;

public class ChaosStrategyConstantsTests
{
[Fact]
public void EnsureDefaults()
{
ChaosStrategyConstants.MinInjectionThreshold.Should().Be(0d);
ChaosStrategyConstants.MaxInjectionThreshold.Should().Be(1d);
ChaosStrategyConstants.DefaultInjectionRate.Should().Be(0.001d);
ChaosStrategyConstants.DefaultEnabled.Should().BeTrue();
}
}
2 changes: 1 addition & 1 deletion test/Polly.Core.Tests/Simmy/ChaosStrategyOptionsTTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public void Ctor_Ok()
var sut = new TestChaosStrategyOptions<int>();

sut.Randomizer.Should().NotBeNull();
sut.Enabled.Should().BeFalse();
sut.Enabled.Should().BeTrue();
sut.EnabledGenerator.Should().BeNull();
sut.InjectionRate.Should().Be(ChaosStrategyConstants.DefaultInjectionRate);
sut.InjectionRateGenerator.Should().BeNull();
Expand Down
2 changes: 1 addition & 1 deletion test/Polly.Core.Tests/Simmy/ChaosStrategyOptionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public void Ctor_Ok()
var sut = new TestChaosStrategyOptions();

sut.Randomizer.Should().NotBeNull();
sut.Enabled.Should().BeFalse();
sut.Enabled.Should().BeTrue();
sut.EnabledGenerator.Should().BeNull();
sut.InjectionRate.Should().Be(ChaosStrategyConstants.DefaultInjectionRate);
sut.InjectionRateGenerator.Should().BeNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class ChaosFaultPipelineBuilderExtensionsTests
builder.AddChaosFault(new ChaosFaultStrategyOptions
{
InjectionRate = 0.6,
Enabled = true,
Randomizer = () => 0.5,
FaultGenerator = _=> new ValueTask<Exception?>( new InvalidOperationException("Dummy exception."))
});
Expand Down
Loading