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

Simmy API review Part 1 #1909

Merged
merged 13 commits into from
Jan 22, 2024
Merged
2 changes: 1 addition & 1 deletion docs/chaos/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ All the strategies' options implement the [`MonkeyStrategyOptions`](xref:Polly.S
| `InjectionRate` | 0.001 ms | 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. |
| `EnabledGenerator` | `null` | The generator that indicates whether the chaos strategy is enabled for a given execution. |
| `EnabledGenerator` | `null` | The generator that indicates whether the chaos strategy is enabled for a given execution. |

[simmy]: https://github.com/Polly-Contrib/Simmy
16 changes: 8 additions & 8 deletions docs/chaos/latency.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ var optionsWithLatencyGenerator = new LatencyStrategyOptions
};

// To get notifications when a delay is injected
var optionsOnBehaviorInjected = new LatencyStrategyOptions
var optionsOnLatencyInjected = new LatencyStrategyOptions
{
Latency = TimeSpan.FromSeconds(30),
Enabled = true,
InjectionRate = 0.1,
OnLatency = static args =>
OnLatencyInjected = static args =>
{
Console.WriteLine($"OnLatency, Latency: {args.Latency}, Operation: {args.Context.OperationKey}.");
Console.WriteLine($"OnLatencyInjected, Latency: {args.Latency}, Operation: {args.Context.OperationKey}.");
return default;
}
};
Expand Down Expand Up @@ -95,11 +95,11 @@ var pipeline = new ResiliencePipelineBuilder()

## Defaults

| Property | Default Value | Description |
|--------------------|---------------|--------------------------------------------------------|
| `Latency` | `30 seconds` | A `TimeSpan` indicating the delay to be injected. |
| `LatencyGenerator` | `null` | Generates the latency to inject for a given execution. |
| `OnLatency` | `null` | Action executed when latency is injected. |
| Property | Default Value | Description |
|---------------------|---------------|--------------------------------------------------------|
| `Latency` | `30 seconds` | A `TimeSpan` indicating the delay to be injected. |
| `LatencyGenerator` | `null` | Generates the latency to inject for a given execution. |
| `OnLatencyInjected` | `null` | Action executed when latency is injected. |

## Diagrams

Expand Down
14 changes: 7 additions & 7 deletions src/Polly.Core/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ Polly.Simmy.Latency.LatencyStrategyOptions.Latency.set -> void
Polly.Simmy.Latency.LatencyStrategyOptions.LatencyGenerator.get -> System.Func<Polly.Simmy.Latency.LatencyGeneratorArguments, System.Threading.Tasks.ValueTask<System.TimeSpan>>?
Polly.Simmy.Latency.LatencyStrategyOptions.LatencyGenerator.set -> void
Polly.Simmy.Latency.LatencyStrategyOptions.LatencyStrategyOptions() -> void
Polly.Simmy.Latency.LatencyStrategyOptions.OnLatency.get -> System.Func<Polly.Simmy.Latency.OnLatencyArguments, System.Threading.Tasks.ValueTask>?
Polly.Simmy.Latency.LatencyStrategyOptions.OnLatency.set -> void
Polly.Simmy.Latency.OnLatencyArguments
Polly.Simmy.Latency.OnLatencyArguments.Context.get -> Polly.ResilienceContext!
Polly.Simmy.Latency.OnLatencyArguments.Latency.get -> System.TimeSpan
Polly.Simmy.Latency.OnLatencyArguments.OnLatencyArguments() -> void
Polly.Simmy.Latency.OnLatencyArguments.OnLatencyArguments(Polly.ResilienceContext! context, System.TimeSpan latency) -> void
Polly.Simmy.Latency.LatencyStrategyOptions.OnLatencyInjected.get -> System.Func<Polly.Simmy.Latency.OnLatencyInjectedArguments, System.Threading.Tasks.ValueTask>?
Polly.Simmy.Latency.LatencyStrategyOptions.OnLatencyInjected.set -> void
Polly.Simmy.Latency.OnLatencyInjectedArguments
Polly.Simmy.Latency.OnLatencyInjectedArguments.Context.get -> Polly.ResilienceContext!
Polly.Simmy.Latency.OnLatencyInjectedArguments.Latency.get -> System.TimeSpan
Polly.Simmy.Latency.OnLatencyInjectedArguments.OnLatencyInjectedArguments() -> void
Polly.Simmy.Latency.OnLatencyInjectedArguments.OnLatencyInjectedArguments(Polly.ResilienceContext! context, System.TimeSpan latency) -> void
Polly.Simmy.LatencyPipelineBuilderExtensions
Polly.Simmy.MonkeyStrategy
Polly.Simmy.MonkeyStrategy.MonkeyStrategy(Polly.Simmy.MonkeyStrategyOptions! options) -> void
Expand Down
4 changes: 3 additions & 1 deletion src/Polly.Core/Simmy/Behavior/BehaviorConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@

internal static class BehaviorConstants
{
public const string OnBehaviorInjectedEvent = "OnBehaviorInjected";
public const string DefaultName = "Chaos.Behavior";

public const string OnBehaviorInjectedEvent = "Chaos.OnBehavior";
}
5 changes: 5 additions & 0 deletions src/Polly.Core/Simmy/Behavior/BehaviorStrategyOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ namespace Polly.Simmy.Behavior;
/// </summary>
public class BehaviorStrategyOptions : MonkeyStrategyOptions
{
/// <summary>
/// Initializes a new instance of the <see cref="BehaviorStrategyOptions"/> class.
/// </summary>
public BehaviorStrategyOptions() => Name = BehaviorConstants.DefaultName;

/// <summary>
/// Gets or sets the delegate that's raised when the custom behavior is injected.
/// </summary>
Expand Down
4 changes: 3 additions & 1 deletion src/Polly.Core/Simmy/Fault/FaultConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@

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

public const string OnFaultInjectedEvent = "Chaos.OnFault";
}
8 changes: 6 additions & 2 deletions src/Polly.Core/Simmy/Fault/FaultStrategyOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ namespace Polly.Simmy.Fault;
/// </summary>
public class FaultStrategyOptions : MonkeyStrategyOptions
{
/// <summary>
/// Initializes a new instance of the <see cref="FaultStrategyOptions"/> class.
/// </summary>
public FaultStrategyOptions() => Name = FaultConstants.DefaultName;

/// <summary>
/// Gets or sets the delegate that's raised when the outcome is injected.
/// </summary>
Expand All @@ -19,8 +24,7 @@ public class FaultStrategyOptions : MonkeyStrategyOptions
/// Gets or sets the fault generator to be injected for a given execution.
/// </summary>
/// <remarks>
/// Defaults to <see langword="null"/>. Either <see cref="Fault"/> or this property is required.
/// When this property is <see langword="null"/> the <see cref="Fault"/> is used.
/// Defaults to <see langword="null"/>.
/// </remarks>
[Required]
public Func<FaultGeneratorArguments, ValueTask<Exception?>>? FaultGenerator { get; set; } = default!;
Expand Down
12 changes: 6 additions & 6 deletions src/Polly.Core/Simmy/Latency/LatencyChaosStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ public LatencyChaosStrategy(
{
Latency = options.Latency;
LatencyGenerator = options.LatencyGenerator is not null ? options.LatencyGenerator : (_) => new(options.Latency);
OnLatency = options.OnLatency;
OnLatencyInjected = options.OnLatencyInjected;

_telemetry = telemetry;
_timeProvider = timeProvider;
}

public Func<OnLatencyArguments, ValueTask>? OnLatency { get; }
public Func<OnLatencyInjectedArguments, ValueTask>? OnLatencyInjected { get; }

public Func<LatencyGeneratorArguments, ValueTask<TimeSpan>> LatencyGenerator { get; }

Expand All @@ -43,14 +43,14 @@ protected internal override async ValueTask<Outcome<TResult>> ExecuteCore<TResul
return await StrategyHelper.ExecuteCallbackSafeAsync(callback, context, state).ConfigureAwait(context.ContinueOnCapturedContext);
}

var args = new OnLatencyArguments(context, latency);
_telemetry.Report(new(ResilienceEventSeverity.Information, LatencyConstants.OnLatencyEvent), context, args);
var args = new OnLatencyInjectedArguments(context, latency);
_telemetry.Report(new(ResilienceEventSeverity.Information, LatencyConstants.OnLatencyInjectedEvent), context, args);

await _timeProvider.DelayAsync(latency, context).ConfigureAwait(context.ContinueOnCapturedContext);

if (OnLatency is not null)
if (OnLatencyInjected is not null)
{
await OnLatency(args).ConfigureAwait(context.ContinueOnCapturedContext);
await OnLatencyInjected(args).ConfigureAwait(context.ContinueOnCapturedContext);
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/Polly.Core/Simmy/Latency/LatencyConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

internal static class LatencyConstants
{
public const string OnLatencyEvent = "OnLatency";
public const string DefaultName = "Chaos.Latency";

public const string OnLatencyInjectedEvent = "Chaos.OnLatency";

public static readonly TimeSpan DefaultLatency = TimeSpan.FromSeconds(30);
}
7 changes: 6 additions & 1 deletion src/Polly.Core/Simmy/Latency/LatencyStrategyOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@
/// </summary>
public class LatencyStrategyOptions : MonkeyStrategyOptions
martincostello marked this conversation as resolved.
Show resolved Hide resolved
{
/// <summary>
/// Initializes a new instance of the <see cref="LatencyStrategyOptions"/> class.
/// </summary>
public LatencyStrategyOptions() => Name = LatencyConstants.DefaultName;

/// <summary>
/// Gets or sets the delegate that's raised when a delay occurs.
/// </summary>
/// <remarks>
martintmk marked this conversation as resolved.
Show resolved Hide resolved
/// Defaults to <see langword="null"/>.
/// </remarks>
public Func<OnLatencyArguments, ValueTask>? OnLatency { get; set; }
public Func<OnLatencyInjectedArguments, ValueTask>? OnLatencyInjected { get; set; }

/// <summary>
/// Gets or sets the latency generator that generates the delay for a given execution.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
/// <summary>
/// Arguments used by the latency chaos strategy to notify that a delayed occurred.
/// </summary>
public readonly struct OnLatencyArguments
public readonly struct OnLatencyInjectedArguments
{
/// <summary>
/// Initializes a new instance of the <see cref="OnLatencyArguments"/> struct.
/// Initializes a new instance of the <see cref="OnLatencyInjectedArguments"/> struct.
/// </summary>
/// <param name="context">The context associated with the execution of a user-provided callback.</param>
/// <param name="latency">The latency that was injected.</param>
public OnLatencyArguments(ResilienceContext context, TimeSpan latency)
public OnLatencyInjectedArguments(ResilienceContext context, TimeSpan latency)
{
Context = context;
Latency = latency;
Expand Down
4 changes: 3 additions & 1 deletion src/Polly.Core/Simmy/Outcomes/OutcomeConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@

internal static class OutcomeConstants
{
public const string OnOutcomeInjectedEvent = "OnOutcomeInjected";
public const string DefaultName = "Chaos.Outcome";

public const string OnOutcomeInjectedEvent = "Chaos.OnOutcome";
}
5 changes: 5 additions & 0 deletions src/Polly.Core/Simmy/Outcomes/OutcomeStrategyOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ namespace Polly.Simmy.Outcomes;
/// <typeparam name="TResult">The type of the outcome that was injected.</typeparam>
public class OutcomeStrategyOptions<TResult> : MonkeyStrategyOptions
{
/// <summary>
/// Initializes a new instance of the <see cref="OutcomeStrategyOptions{TResult}"/> class.
/// </summary>
public OutcomeStrategyOptions() => Name = OutcomeConstants.DefaultName;

/// <summary>
/// Gets or sets the delegate that's invoked when the outcome is injected.
/// </summary>
Expand Down
6 changes: 3 additions & 3 deletions src/Snippets/Docs/Chaos.Latency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ public static void LatencyUsage()
};

// To get notifications when a delay is injected
var optionsOnBehaviorInjected = new LatencyStrategyOptions
var optionsOnLatencyInjected = new LatencyStrategyOptions
{
Latency = TimeSpan.FromSeconds(30),
Enabled = true,
InjectionRate = 0.1,
OnLatency = static args =>
OnLatencyInjected = static args =>
{
Console.WriteLine($"OnLatency, Latency: {args.Latency}, Operation: {args.Context.OperationKey}.");
Console.WriteLine($"OnLatencyInjected, Latency: {args.Latency}, Operation: {args.Context.OperationKey}.");
return default;
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class BehaviorConstantsTests
[Fact]
public void EnsureDefaults()
{
BehaviorConstants.OnBehaviorInjectedEvent.Should().Be("OnBehaviorInjected");
BehaviorConstants.DefaultName.Should().Be("Chaos.Behavior");
BehaviorConstants.OnBehaviorInjectedEvent.Should().Be("Chaos.OnBehavior");
}
}
3 changes: 2 additions & 1 deletion test/Polly.Core.Tests/Simmy/Fault/FaultConstantsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class FaultConstantsTests
[Fact]
public void EnsureDefaults()
{
FaultConstants.OnFaultInjectedEvent.Should().Be("OnFaultInjectedEvent");
FaultConstants.DefaultName.Should().Be("Chaos.Fault");
FaultConstants.OnFaultInjectedEvent.Should().Be("Chaos.OnFault");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public async Task Given_enabled_and_randomly_within_threshold_should_inject_late
_options.Enabled = true;
_options.Latency = _delay;
_options.Randomizer = () => 0.5;
_options.OnLatency = args =>
_options.OnLatencyInjected = args =>
{
args.Context.Should().NotBeNull();
args.Context.CancellationToken.IsCancellationRequested.Should().BeFalse();
Expand All @@ -51,7 +51,7 @@ public async Task Given_enabled_and_randomly_within_threshold_should_inject_late
(after - before).Should().Be(_delay);

_args.Should().HaveCount(1);
_args[0].Arguments.Should().BeOfType<OnLatencyArguments>();
_args[0].Arguments.Should().BeOfType<OnLatencyInjectedArguments>();
onLatencyExecuted.Should().BeTrue();
}

Expand Down Expand Up @@ -110,7 +110,7 @@ public async Task Given_latency_is_negative_should_not_inject_latency(double lat
_options.Latency = TimeSpan.FromSeconds(latency);
_options.Randomizer = () => 0.5;

_options.OnLatency = args =>
_options.OnLatencyInjected = args =>
{
args.Context.Should().NotBeNull();
args.Context.CancellationToken.IsCancellationRequested.Should().BeFalse();
Expand Down
3 changes: 2 additions & 1 deletion test/Polly.Core.Tests/Simmy/Latency/LatencyConstantsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ public class LatencyConstantsTests
[Fact]
public void EnsureDefaults()
{
LatencyConstants.OnLatencyEvent.Should().Be("OnLatency");
LatencyConstants.DefaultName.Should().Be("Chaos.Latency");
LatencyConstants.OnLatencyInjectedEvent.Should().Be("Chaos.OnLatency");
LatencyConstants.DefaultLatency.Should().Be(TimeSpan.FromSeconds(30));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public void Ctor_Ok()
sut.InjectionRateGenerator.Should().BeNull();
sut.Latency.Should().Be(LatencyConstants.DefaultLatency);
sut.LatencyGenerator.Should().BeNull();
sut.OnLatency.Should().BeNull();
sut.OnLatencyInjected.Should().BeNull();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace Polly.Core.Tests.Simmy.Latency;

public class OnLatencyArgumentsTests
public class OnLatencyInjectedArgumentsTests
{
[Fact]
public void Ctor_Ok()
{
var args = new OnLatencyArguments(ResilienceContextPool.Shared.Get(), TimeSpan.FromSeconds(10));
var args = new OnLatencyInjectedArguments(ResilienceContextPool.Shared.Get(), TimeSpan.FromSeconds(10));
args.Context.Should().NotBeNull();
args.Latency.Should().Be(TimeSpan.FromSeconds(10));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class OutcomeConstantsTests
[Fact]
public void EnsureDefaults()
{
OutcomeConstants.OnOutcomeInjectedEvent.Should().Be("OnOutcomeInjected");
OutcomeConstants.DefaultName.Should().Be("Chaos.Outcome");
OutcomeConstants.OnOutcomeInjectedEvent.Should().Be("Chaos.OnOutcome");
}
}