Skip to content
Merged
Prev Previous commit
Next Next commit
Perform renaming for fault
  • Loading branch information
peter-csala committed Jan 22, 2024
commit 64302f5a9537300c7c6fb0496490cd58478eb943
16 changes: 8 additions & 8 deletions docs/chaos/fault.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

## About

- **Options**: [`FaultStrategyOptions`](xref:Polly.Simmy.Fault.FaultStrategyOptions)
- **Options**: [`ChaosFaultStrategyOptions`](xref:Polly.Simmy.Fault.ChaosFaultStrategyOptions)
- **Extensions**: `AddChaosFault`
- **Strategy Type**: Proactive

Expand All @@ -18,7 +18,7 @@ The fault chaos strategy is designed to introduce faults (exceptions) into the s
<!-- snippet: chaos-fault-usage -->
```cs
// 10% of invocations will be randomly affected and one of the exceptions will be thrown (equal probability).
var optionsBasic = new FaultStrategyOptions
var optionsBasic = new ChaosFaultStrategyOptions
{
FaultGenerator = new FaultGenerator()
.AddException<InvalidOperationException>() // Uses default constructor
Expand All @@ -28,7 +28,7 @@ var optionsBasic = new FaultStrategyOptions
};

// To use a custom delegate to generate the fault to be injected
var optionsWithFaultGenerator = new FaultStrategyOptions
var optionsWithFaultGenerator = new ChaosFaultStrategyOptions
{
FaultGenerator = static args =>
{
Expand All @@ -48,7 +48,7 @@ var optionsWithFaultGenerator = new FaultStrategyOptions
};

// To get notifications when a fault is injected
var optionsOnFaultInjected = new FaultStrategyOptions
var optionsOnFaultInjected = new ChaosFaultStrategyOptions
{
FaultGenerator = new FaultGenerator().AddException<InvalidOperationException>(),
Enabled = true,
Expand All @@ -60,7 +60,7 @@ var optionsOnFaultInjected = new FaultStrategyOptions
}
};

// Add a fault strategy with a FaultStrategyOptions instance to the pipeline
// Add a fault strategy with a ChaosFaultStrategyOptions instance to the pipeline
new ResiliencePipelineBuilder().AddChaosFault(optionsBasic);
new ResiliencePipelineBuilder<HttpResponseMessage>().AddChaosFault(optionsWithFaultGenerator);

Expand All @@ -82,7 +82,7 @@ var pipeline = new ResiliencePipelineBuilder()
MaxRetryAttempts = 4,
Delay = TimeSpan.FromSeconds(3),
})
.AddChaosFault(new FaultStrategyOptions // Chaos strategies are usually placed as the last ones in the pipeline
.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,
Expand Down Expand Up @@ -152,7 +152,7 @@ The `FaultGenerator` is convenience API that allows you to specify what faults (
<!-- snippet: chaos-fault-generator-class -->
```cs
new ResiliencePipelineBuilder()
.AddChaosFault(new FaultStrategyOptions
.AddChaosFault(new ChaosFaultStrategyOptions
{
// Use FaultGenerator to register exceptions to be injected
FaultGenerator = new FaultGenerator()
Expand All @@ -171,7 +171,7 @@ Delegates give you the most flexibility at the expense of slightly more complica
<!-- snippet: chaos-fault-generator-delegate -->
```cs
new ResiliencePipelineBuilder()
.AddChaosFault(new FaultStrategyOptions
.AddChaosFault(new ChaosFaultStrategyOptions
{
// The same behavior can be achieved with delegates
FaultGenerator = args =>
Expand Down
18 changes: 9 additions & 9 deletions src/Polly.Core/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ Polly.Simmy.Fault.FaultGeneratorArguments
Polly.Simmy.Fault.FaultGeneratorArguments.Context.get -> Polly.ResilienceContext!
Polly.Simmy.Fault.FaultGeneratorArguments.FaultGeneratorArguments() -> void
Polly.Simmy.Fault.FaultGeneratorArguments.FaultGeneratorArguments(Polly.ResilienceContext! context) -> void
Polly.Simmy.Fault.FaultStrategyOptions
Polly.Simmy.Fault.FaultStrategyOptions.FaultGenerator.get -> System.Func<Polly.Simmy.Fault.FaultGeneratorArguments, System.Threading.Tasks.ValueTask<System.Exception?>>?
Polly.Simmy.Fault.FaultStrategyOptions.FaultGenerator.set -> void
Polly.Simmy.Fault.FaultStrategyOptions.FaultStrategyOptions() -> void
Polly.Simmy.Fault.FaultStrategyOptions.OnFaultInjected.get -> System.Func<Polly.Simmy.Fault.OnFaultInjectedArguments, System.Threading.Tasks.ValueTask>?
Polly.Simmy.Fault.FaultStrategyOptions.OnFaultInjected.set -> void
Polly.Simmy.Fault.ChaosFaultStrategyOptions
Polly.Simmy.Fault.ChaosFaultStrategyOptions.FaultGenerator.get -> System.Func<Polly.Simmy.Fault.FaultGeneratorArguments, System.Threading.Tasks.ValueTask<System.Exception?>>?
Polly.Simmy.Fault.ChaosFaultStrategyOptions.FaultGenerator.set -> void
Polly.Simmy.Fault.ChaosFaultStrategyOptions.ChaosFaultStrategyOptions() -> void
Polly.Simmy.Fault.ChaosFaultStrategyOptions.OnFaultInjected.get -> System.Func<Polly.Simmy.Fault.OnFaultInjectedArguments, System.Threading.Tasks.ValueTask>?
Polly.Simmy.Fault.ChaosFaultStrategyOptions.OnFaultInjected.set -> void
Polly.Simmy.Fault.OnFaultInjectedArguments
Polly.Simmy.Fault.OnFaultInjectedArguments.Context.get -> Polly.ResilienceContext!
Polly.Simmy.Fault.OnFaultInjectedArguments.Fault.get -> System.Exception!
Polly.Simmy.Fault.OnFaultInjectedArguments.OnFaultInjectedArguments() -> void
Polly.Simmy.Fault.OnFaultInjectedArguments.OnFaultInjectedArguments(Polly.ResilienceContext! context, System.Exception! fault) -> void
Polly.Simmy.FaultPipelineBuilderExtensions
Polly.Simmy.ChaosFaultPipelineBuilderExtensions
Polly.Simmy.InjectionRateGeneratorArguments
Polly.Simmy.InjectionRateGeneratorArguments.Context.get -> Polly.ResilienceContext!
Polly.Simmy.InjectionRateGeneratorArguments.InjectionRateGeneratorArguments() -> void
Expand Down Expand Up @@ -109,8 +109,8 @@ Polly.Simmy.Outcomes.OutcomeStrategyOptions<TResult>.OutcomeStrategyOptions() ->
static Polly.Simmy.ChaosBehaviorPipelineBuilderExtensions.AddChaosBehavior<TBuilder>(this TBuilder! builder, double injectionRate, System.Func<System.Threading.Tasks.ValueTask>! behavior) -> TBuilder!
static Polly.Simmy.ChaosBehaviorPipelineBuilderExtensions.AddChaosBehavior<TBuilder>(this TBuilder! builder, Polly.Simmy.Behavior.ChaosBehaviorStrategyOptions! options) -> TBuilder!
static Polly.Simmy.Fault.FaultGenerator.implicit operator System.Func<Polly.Simmy.Fault.FaultGeneratorArguments, System.Threading.Tasks.ValueTask<System.Exception?>>!(Polly.Simmy.Fault.FaultGenerator! generator) -> System.Func<Polly.Simmy.Fault.FaultGeneratorArguments, System.Threading.Tasks.ValueTask<System.Exception?>>!
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.ChaosFaultPipelineBuilderExtensions.AddChaosFault<TBuilder>(this TBuilder! builder, double injectionRate, System.Func<System.Exception?>! faultGenerator) -> TBuilder!
static Polly.Simmy.ChaosFaultPipelineBuilderExtensions.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>!
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Polly.Simmy.Fault;

internal static class FaultConstants
internal static class ChaosFaultConstants
{
public const string DefaultName = "Chaos.Fault";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Polly.Simmy;
/// <summary>
/// Extension methods for adding outcome to a <see cref="ResiliencePipelineBuilder"/>.
/// </summary>
public static class FaultPipelineBuilderExtensions
public static class ChaosFaultPipelineBuilderExtensions
{
/// <summary>
/// Adds a fault chaos strategy to the builder.
Expand All @@ -19,7 +19,7 @@ public static class FaultPipelineBuilderExtensions
public static TBuilder AddChaosFault<TBuilder>(this TBuilder builder, double injectionRate, Func<Exception?> faultGenerator)
where TBuilder : ResiliencePipelineBuilderBase
{
builder.AddChaosFault(new FaultStrategyOptions
builder.AddChaosFault(new ChaosFaultStrategyOptions
{
Enabled = true,
InjectionRate = injectionRate,
Expand All @@ -39,14 +39,14 @@ public static TBuilder AddChaosFault<TBuilder>(this TBuilder builder, double inj
"Trimming",
"IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code",
Justification = "All options members preserved.")]
public static TBuilder AddChaosFault<TBuilder>(this TBuilder builder, FaultStrategyOptions options)
public static TBuilder AddChaosFault<TBuilder>(this TBuilder builder, ChaosFaultStrategyOptions options)
where TBuilder : ResiliencePipelineBuilderBase
{
Guard.NotNull(builder);
Guard.NotNull(options);

builder.AddStrategy(
context => new FaultChaosStrategy(options, context.Telemetry),
context => new ChaosFaultStrategy(options, context.Telemetry),
options);

return builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Polly.Simmy.Fault;

internal class FaultChaosStrategy : ChaosStrategy
internal class ChaosFaultStrategy : ChaosStrategy
{
private readonly ResilienceStrategyTelemetry _telemetry;

public FaultChaosStrategy(FaultStrategyOptions options, ResilienceStrategyTelemetry telemetry)
public ChaosFaultStrategy(ChaosFaultStrategyOptions options, ResilienceStrategyTelemetry telemetry)
: base(options)
{
_telemetry = telemetry;
Expand All @@ -32,7 +32,7 @@ protected internal override async ValueTask<Outcome<TResult>> ExecuteCore<TResul
if (fault is not null)
{
var args = new OnFaultInjectedArguments(context, fault);
_telemetry.Report(new(ResilienceEventSeverity.Information, FaultConstants.OnFaultInjectedEvent), context, args);
_telemetry.Report(new(ResilienceEventSeverity.Information, ChaosFaultConstants.OnFaultInjectedEvent), context, args);

if (OnFaultInjected is not null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ namespace Polly.Simmy.Fault;
/// <summary>
/// Represents the options for the Fault chaos strategy.
/// </summary>
public class FaultStrategyOptions : ChaosStrategyOptions
public class ChaosFaultStrategyOptions : ChaosStrategyOptions
{
/// <summary>
/// Initializes a new instance of the <see cref="FaultStrategyOptions"/> class.
/// Initializes a new instance of the <see cref="ChaosFaultStrategyOptions"/> class.
/// </summary>
public FaultStrategyOptions() => Name = FaultConstants.DefaultName;
public ChaosFaultStrategyOptions() => Name = ChaosFaultConstants.DefaultName;

/// <summary>
/// Gets or sets the delegate that's raised when the outcome is injected.
Expand Down
14 changes: 7 additions & 7 deletions src/Snippets/Docs/Chaos.Fault.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static void FaultUsage()
{
#region chaos-fault-usage
// 10% of invocations will be randomly affected and one of the exceptions will be thrown (equal probability).
var optionsBasic = new FaultStrategyOptions
var optionsBasic = new ChaosFaultStrategyOptions
{
FaultGenerator = new FaultGenerator()
.AddException<InvalidOperationException>() // Uses default constructor
Expand All @@ -23,7 +23,7 @@ public static void FaultUsage()
};

// To use a custom delegate to generate the fault to be injected
var optionsWithFaultGenerator = new FaultStrategyOptions
var optionsWithFaultGenerator = new ChaosFaultStrategyOptions
{
FaultGenerator = static args =>
{
Expand All @@ -43,7 +43,7 @@ public static void FaultUsage()
};

// To get notifications when a fault is injected
var optionsOnFaultInjected = new FaultStrategyOptions
var optionsOnFaultInjected = new ChaosFaultStrategyOptions
{
FaultGenerator = new FaultGenerator().AddException<InvalidOperationException>(),
Enabled = true,
Expand All @@ -55,7 +55,7 @@ public static void FaultUsage()
}
};

// Add a fault strategy with a FaultStrategyOptions instance to the pipeline
// Add a fault strategy with a ChaosFaultStrategyOptions instance to the pipeline
new ResiliencePipelineBuilder().AddChaosFault(optionsBasic);
new ResiliencePipelineBuilder<HttpResponseMessage>().AddChaosFault(optionsWithFaultGenerator);

Expand All @@ -73,7 +73,7 @@ public static void FaultUsage()
MaxRetryAttempts = 4,
Delay = TimeSpan.FromSeconds(3),
})
.AddChaosFault(new FaultStrategyOptions // Chaos strategies are usually placed as the last ones in the pipeline
.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,
Expand All @@ -88,7 +88,7 @@ public static void FaultGenerator()
#region chaos-fault-generator-class

new ResiliencePipelineBuilder()
.AddChaosFault(new FaultStrategyOptions
.AddChaosFault(new ChaosFaultStrategyOptions
{
// Use FaultGenerator to register exceptions to be injected
FaultGenerator = new FaultGenerator()
Expand All @@ -106,7 +106,7 @@ public static void FaultGeneratorDelegates()
#region chaos-fault-generator-delegate

new ResiliencePipelineBuilder()
.AddChaosFault(new FaultStrategyOptions
.AddChaosFault(new ChaosFaultStrategyOptions
{
// The same behavior can be achieved with delegates
FaultGenerator = args =>
Expand Down
13 changes: 13 additions & 0 deletions test/Polly.Core.Tests/Simmy/Fault/ChaosFaultConstantsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Polly.Simmy.Fault;

namespace Polly.Core.Tests.Simmy.Fault;

public class ChaosFaultConstantsTests
{
[Fact]
public void EnsureDefaults()
{
ChaosFaultConstants.DefaultName.Should().Be("Chaos.Fault");
ChaosFaultConstants.OnFaultInjectedEvent.Should().Be("Chaos.OnFault");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

namespace Polly.Core.Tests.Simmy.Fault;

public class FaultChaosPipelineBuilderExtensionsTests
public class ChaosFaultPipelineBuilderExtensionsTests
{
#pragma warning disable IDE0028
public static readonly TheoryData<Action<ResiliencePipelineBuilder>> FaultStrategy = new()
{
builder =>
{
builder.AddChaosFault(new FaultStrategyOptions
builder.AddChaosFault(new ChaosFaultStrategyOptions
{
InjectionRate = 0.6,
Enabled = true,
Expand All @@ -29,18 +29,18 @@ private static void AssertFaultStrategy<T, TException>(ResiliencePipelineBuilder
where TException : Exception
{
var context = ResilienceContextPool.Shared.Get();
var strategy = builder.Build().GetPipelineDescriptor().FirstStrategy.StrategyInstance.Should().BeOfType<FaultChaosStrategy>().Subject;
var strategy = builder.Build().GetPipelineDescriptor().FirstStrategy.StrategyInstance.Should().BeOfType<ChaosFaultStrategy>().Subject;

strategy.EnabledGenerator.Invoke(new(context)).Preserve().GetAwaiter().GetResult().Should().Be(enabled);
strategy.InjectionRateGenerator.Invoke(new(context)).Preserve().GetAwaiter().GetResult().Should().Be(injectionRate);
strategy.FaultGenerator!.Invoke(new(context)).Preserve().GetAwaiter().GetResult().Should().BeOfType(typeof(TException));
}

private static FaultChaosStrategy AssertFaultStrategy<TException>(ResiliencePipelineBuilder builder, bool enabled, double injectionRate)
private static ChaosFaultStrategy AssertFaultStrategy<TException>(ResiliencePipelineBuilder builder, bool enabled, double injectionRate)
where TException : Exception
{
var context = ResilienceContextPool.Shared.Get();
var strategy = builder.Build().GetPipelineDescriptor().FirstStrategy.StrategyInstance.Should().BeOfType<FaultChaosStrategy>().Subject;
var strategy = builder.Build().GetPipelineDescriptor().FirstStrategy.StrategyInstance.Should().BeOfType<ChaosFaultStrategy>().Subject;

strategy.EnabledGenerator.Invoke(new(context)).Preserve().GetAwaiter().GetResult().Should().Be(enabled);
strategy.InjectionRateGenerator.Invoke(new(context)).Preserve().GetAwaiter().GetResult().Should().Be(injectionRate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

namespace Polly.Core.Tests.Simmy.Fault;

public class FaultStrategyOptionsTests
public class ChaosFaultStrategyOptionsTests
{
[Fact]
public void Ctor_Ok()
{
var sut = new FaultStrategyOptions();
var sut = new ChaosFaultStrategyOptions();
sut.Randomizer.Should().NotBeNull();
sut.Enabled.Should().BeFalse();
sut.EnabledGenerator.Should().BeNull();
Expand All @@ -23,7 +23,7 @@ public void Ctor_Ok()
[Fact]
public void InvalidOptions()
{
var options = new FaultStrategyOptions
var options = new ChaosFaultStrategyOptions
{
FaultGenerator = null!,
};
Expand Down
Loading