Skip to content

Commit 24a86c8

Browse files
authored
Simmy API review Part 1 (#1909)
1 parent 2e1a2a5 commit 24a86c8

21 files changed

+76
-45
lines changed

docs/chaos/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,6 @@ All the strategies' options implement the [`MonkeyStrategyOptions`](xref:Polly.S
5151
| `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. |
5252
| `InjectionRateGenerator` | `null` | Generates the injection rate for a given execution, which the value should be between [0, 1] (inclusive). |
5353
| `Enabled` | `false` | Determines whether the strategy is enabled or not. |
54-
| `EnabledGenerator` | `null` | The generator that indicates whether the chaos strategy is enabled for a given execution. |
54+
| `EnabledGenerator` | `null` | The generator that indicates whether the chaos strategy is enabled for a given execution. |
5555

5656
[simmy]: https://github.com/Polly-Contrib/Simmy

docs/chaos/latency.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ var optionsWithLatencyGenerator = new LatencyStrategyOptions
5050
};
5151

5252
// To get notifications when a delay is injected
53-
var optionsOnBehaviorInjected = new LatencyStrategyOptions
53+
var optionsOnLatencyInjected = new LatencyStrategyOptions
5454
{
5555
Latency = TimeSpan.FromSeconds(30),
5656
Enabled = true,
5757
InjectionRate = 0.1,
58-
OnLatency = static args =>
58+
OnLatencyInjected = static args =>
5959
{
60-
Console.WriteLine($"OnLatency, Latency: {args.Latency}, Operation: {args.Context.OperationKey}.");
60+
Console.WriteLine($"OnLatencyInjected, Latency: {args.Latency}, Operation: {args.Context.OperationKey}.");
6161
return default;
6262
}
6363
};
@@ -97,11 +97,11 @@ var pipeline = new ResiliencePipelineBuilder()
9797

9898
## Defaults
9999

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

106106
## Diagrams
107107

src/Polly.Core/PublicAPI.Unshipped.txt

+7-7
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ Polly.Simmy.Latency.LatencyStrategyOptions.Latency.set -> void
5555
Polly.Simmy.Latency.LatencyStrategyOptions.LatencyGenerator.get -> System.Func<Polly.Simmy.Latency.LatencyGeneratorArguments, System.Threading.Tasks.ValueTask<System.TimeSpan>>?
5656
Polly.Simmy.Latency.LatencyStrategyOptions.LatencyGenerator.set -> void
5757
Polly.Simmy.Latency.LatencyStrategyOptions.LatencyStrategyOptions() -> void
58-
Polly.Simmy.Latency.LatencyStrategyOptions.OnLatency.get -> System.Func<Polly.Simmy.Latency.OnLatencyArguments, System.Threading.Tasks.ValueTask>?
59-
Polly.Simmy.Latency.LatencyStrategyOptions.OnLatency.set -> void
60-
Polly.Simmy.Latency.OnLatencyArguments
61-
Polly.Simmy.Latency.OnLatencyArguments.Context.get -> Polly.ResilienceContext!
62-
Polly.Simmy.Latency.OnLatencyArguments.Latency.get -> System.TimeSpan
63-
Polly.Simmy.Latency.OnLatencyArguments.OnLatencyArguments() -> void
64-
Polly.Simmy.Latency.OnLatencyArguments.OnLatencyArguments(Polly.ResilienceContext! context, System.TimeSpan latency) -> void
58+
Polly.Simmy.Latency.LatencyStrategyOptions.OnLatencyInjected.get -> System.Func<Polly.Simmy.Latency.OnLatencyInjectedArguments, System.Threading.Tasks.ValueTask>?
59+
Polly.Simmy.Latency.LatencyStrategyOptions.OnLatencyInjected.set -> void
60+
Polly.Simmy.Latency.OnLatencyInjectedArguments
61+
Polly.Simmy.Latency.OnLatencyInjectedArguments.Context.get -> Polly.ResilienceContext!
62+
Polly.Simmy.Latency.OnLatencyInjectedArguments.Latency.get -> System.TimeSpan
63+
Polly.Simmy.Latency.OnLatencyInjectedArguments.OnLatencyInjectedArguments() -> void
64+
Polly.Simmy.Latency.OnLatencyInjectedArguments.OnLatencyInjectedArguments(Polly.ResilienceContext! context, System.TimeSpan latency) -> void
6565
Polly.Simmy.LatencyPipelineBuilderExtensions
6666
Polly.Simmy.MonkeyStrategy
6767
Polly.Simmy.MonkeyStrategy.MonkeyStrategy(Polly.Simmy.MonkeyStrategyOptions! options) -> void

src/Polly.Core/Simmy/Behavior/BehaviorConstants.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@
22

33
internal static class BehaviorConstants
44
{
5-
public const string OnBehaviorInjectedEvent = "OnBehaviorInjected";
5+
public const string DefaultName = "Chaos.Behavior";
6+
7+
public const string OnBehaviorInjectedEvent = "Chaos.OnBehavior";
68
}

src/Polly.Core/Simmy/Behavior/BehaviorStrategyOptions.cs

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ namespace Polly.Simmy.Behavior;
77
/// </summary>
88
public class BehaviorStrategyOptions : MonkeyStrategyOptions
99
{
10+
/// <summary>
11+
/// Initializes a new instance of the <see cref="BehaviorStrategyOptions"/> class.
12+
/// </summary>
13+
public BehaviorStrategyOptions() => Name = BehaviorConstants.DefaultName;
14+
1015
/// <summary>
1116
/// Gets or sets the delegate that's raised when the custom behavior is injected.
1217
/// </summary>

src/Polly.Core/Simmy/Fault/FaultConstants.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@
22

33
internal static class FaultConstants
44
{
5-
public const string OnFaultInjectedEvent = "OnFaultInjectedEvent";
5+
public const string DefaultName = "Chaos.Fault";
6+
7+
public const string OnFaultInjectedEvent = "Chaos.OnFault";
68
}

src/Polly.Core/Simmy/Fault/FaultStrategyOptions.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ namespace Polly.Simmy.Fault;
77
/// </summary>
88
public class FaultStrategyOptions : MonkeyStrategyOptions
99
{
10+
/// <summary>
11+
/// Initializes a new instance of the <see cref="FaultStrategyOptions"/> class.
12+
/// </summary>
13+
public FaultStrategyOptions() => Name = FaultConstants.DefaultName;
14+
1015
/// <summary>
1116
/// Gets or sets the delegate that's raised when the outcome is injected.
1217
/// </summary>
@@ -19,8 +24,7 @@ public class FaultStrategyOptions : MonkeyStrategyOptions
1924
/// Gets or sets the fault generator to be injected for a given execution.
2025
/// </summary>
2126
/// <remarks>
22-
/// Defaults to <see langword="null"/>. Either <see cref="Fault"/> or this property is required.
23-
/// When this property is <see langword="null"/> the <see cref="Fault"/> is used.
27+
/// Defaults to <see langword="null"/>.
2428
/// </remarks>
2529
[Required]
2630
public Func<FaultGeneratorArguments, ValueTask<Exception?>>? FaultGenerator { get; set; } = default!;

src/Polly.Core/Simmy/Latency/LatencyChaosStrategy.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ public LatencyChaosStrategy(
1515
{
1616
Latency = options.Latency;
1717
LatencyGenerator = options.LatencyGenerator is not null ? options.LatencyGenerator : (_) => new(options.Latency);
18-
OnLatency = options.OnLatency;
18+
OnLatencyInjected = options.OnLatencyInjected;
1919

2020
_telemetry = telemetry;
2121
_timeProvider = timeProvider;
2222
}
2323

24-
public Func<OnLatencyArguments, ValueTask>? OnLatency { get; }
24+
public Func<OnLatencyInjectedArguments, ValueTask>? OnLatencyInjected { get; }
2525

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

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

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

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

51-
if (OnLatency is not null)
51+
if (OnLatencyInjected is not null)
5252
{
53-
await OnLatency(args).ConfigureAwait(context.ContinueOnCapturedContext);
53+
await OnLatencyInjected(args).ConfigureAwait(context.ContinueOnCapturedContext);
5454
}
5555
}
5656

src/Polly.Core/Simmy/Latency/LatencyConstants.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
internal static class LatencyConstants
44
{
5-
public const string OnLatencyEvent = "OnLatency";
5+
public const string DefaultName = "Chaos.Latency";
6+
7+
public const string OnLatencyInjectedEvent = "Chaos.OnLatency";
68

79
public static readonly TimeSpan DefaultLatency = TimeSpan.FromSeconds(30);
810
}

src/Polly.Core/Simmy/Latency/LatencyStrategyOptions.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,18 @@
77
/// </summary>
88
public class LatencyStrategyOptions : MonkeyStrategyOptions
99
{
10+
/// <summary>
11+
/// Initializes a new instance of the <see cref="LatencyStrategyOptions"/> class.
12+
/// </summary>
13+
public LatencyStrategyOptions() => Name = LatencyConstants.DefaultName;
14+
1015
/// <summary>
1116
/// Gets or sets the delegate that's raised when a delay occurs.
1217
/// </summary>
1318
/// <remarks>
1419
/// Defaults to <see langword="null"/>.
1520
/// </remarks>
16-
public Func<OnLatencyArguments, ValueTask>? OnLatency { get; set; }
21+
public Func<OnLatencyInjectedArguments, ValueTask>? OnLatencyInjected { get; set; }
1722

1823
/// <summary>
1924
/// Gets or sets the latency generator that generates the delay for a given execution.

src/Polly.Core/Simmy/Latency/OnLatencyArguments.cs src/Polly.Core/Simmy/Latency/OnLatencyInjectedArguments.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
/// <summary>
66
/// Arguments used by the latency chaos strategy to notify that a delayed occurred.
77
/// </summary>
8-
public readonly struct OnLatencyArguments
8+
public readonly struct OnLatencyInjectedArguments
99
{
1010
/// <summary>
11-
/// Initializes a new instance of the <see cref="OnLatencyArguments"/> struct.
11+
/// Initializes a new instance of the <see cref="OnLatencyInjectedArguments"/> struct.
1212
/// </summary>
1313
/// <param name="context">The context associated with the execution of a user-provided callback.</param>
1414
/// <param name="latency">The latency that was injected.</param>
15-
public OnLatencyArguments(ResilienceContext context, TimeSpan latency)
15+
public OnLatencyInjectedArguments(ResilienceContext context, TimeSpan latency)
1616
{
1717
Context = context;
1818
Latency = latency;

src/Polly.Core/Simmy/Outcomes/OutcomeConstants.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@
22

33
internal static class OutcomeConstants
44
{
5-
public const string OnOutcomeInjectedEvent = "OnOutcomeInjected";
5+
public const string DefaultName = "Chaos.Outcome";
6+
7+
public const string OnOutcomeInjectedEvent = "Chaos.OnOutcome";
68
}

src/Polly.Core/Simmy/Outcomes/OutcomeStrategyOptions.cs

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ namespace Polly.Simmy.Outcomes;
88
/// <typeparam name="TResult">The type of the outcome that was injected.</typeparam>
99
public class OutcomeStrategyOptions<TResult> : MonkeyStrategyOptions
1010
{
11+
/// <summary>
12+
/// Initializes a new instance of the <see cref="OutcomeStrategyOptions{TResult}"/> class.
13+
/// </summary>
14+
public OutcomeStrategyOptions() => Name = OutcomeConstants.DefaultName;
15+
1116
/// <summary>
1217
/// Gets or sets the delegate that's invoked when the outcome is injected.
1318
/// </summary>

src/Snippets/Docs/Chaos.Latency.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ public static void LatencyUsage()
4444
};
4545

4646
// To get notifications when a delay is injected
47-
var optionsOnBehaviorInjected = new LatencyStrategyOptions
47+
var optionsOnLatencyInjected = new LatencyStrategyOptions
4848
{
4949
Latency = TimeSpan.FromSeconds(30),
5050
Enabled = true,
5151
InjectionRate = 0.1,
52-
OnLatency = static args =>
52+
OnLatencyInjected = static args =>
5353
{
54-
Console.WriteLine($"OnLatency, Latency: {args.Latency}, Operation: {args.Context.OperationKey}.");
54+
Console.WriteLine($"OnLatencyInjected, Latency: {args.Latency}, Operation: {args.Context.OperationKey}.");
5555
return default;
5656
}
5757
};

test/Polly.Core.Tests/Simmy/Behavior/BehaviorConstantsTests.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public class BehaviorConstantsTests
77
[Fact]
88
public void EnsureDefaults()
99
{
10-
BehaviorConstants.OnBehaviorInjectedEvent.Should().Be("OnBehaviorInjected");
10+
BehaviorConstants.DefaultName.Should().Be("Chaos.Behavior");
11+
BehaviorConstants.OnBehaviorInjectedEvent.Should().Be("Chaos.OnBehavior");
1112
}
1213
}

test/Polly.Core.Tests/Simmy/Fault/FaultConstantsTests.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public class FaultConstantsTests
77
[Fact]
88
public void EnsureDefaults()
99
{
10-
FaultConstants.OnFaultInjectedEvent.Should().Be("OnFaultInjectedEvent");
10+
FaultConstants.DefaultName.Should().Be("Chaos.Fault");
11+
FaultConstants.OnFaultInjectedEvent.Should().Be("Chaos.OnFault");
1112
}
1213
}

test/Polly.Core.Tests/Simmy/Latency/LatencyChaosStrategyTests.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public async Task Given_enabled_and_randomly_within_threshold_should_inject_late
3232
_options.Enabled = true;
3333
_options.Latency = _delay;
3434
_options.Randomizer = () => 0.5;
35-
_options.OnLatency = args =>
35+
_options.OnLatencyInjected = args =>
3636
{
3737
args.Context.Should().NotBeNull();
3838
args.Context.CancellationToken.IsCancellationRequested.Should().BeFalse();
@@ -51,7 +51,7 @@ public async Task Given_enabled_and_randomly_within_threshold_should_inject_late
5151
(after - before).Should().Be(_delay);
5252

5353
_args.Should().HaveCount(1);
54-
_args[0].Arguments.Should().BeOfType<OnLatencyArguments>();
54+
_args[0].Arguments.Should().BeOfType<OnLatencyInjectedArguments>();
5555
onLatencyExecuted.Should().BeTrue();
5656
}
5757

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

113-
_options.OnLatency = args =>
113+
_options.OnLatencyInjected = args =>
114114
{
115115
args.Context.Should().NotBeNull();
116116
args.Context.CancellationToken.IsCancellationRequested.Should().BeFalse();

test/Polly.Core.Tests/Simmy/Latency/LatencyConstantsTests.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ public class LatencyConstantsTests
77
[Fact]
88
public void EnsureDefaults()
99
{
10-
LatencyConstants.OnLatencyEvent.Should().Be("OnLatency");
10+
LatencyConstants.DefaultName.Should().Be("Chaos.Latency");
11+
LatencyConstants.OnLatencyInjectedEvent.Should().Be("Chaos.OnLatency");
1112
LatencyConstants.DefaultLatency.Should().Be(TimeSpan.FromSeconds(30));
1213
}
1314
}

test/Polly.Core.Tests/Simmy/Latency/LatencyStrategyOptionsTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ public void Ctor_Ok()
1616
sut.InjectionRateGenerator.Should().BeNull();
1717
sut.Latency.Should().Be(LatencyConstants.DefaultLatency);
1818
sut.LatencyGenerator.Should().BeNull();
19-
sut.OnLatency.Should().BeNull();
19+
sut.OnLatencyInjected.Should().BeNull();
2020
}
2121
}

test/Polly.Core.Tests/Simmy/Latency/OnLatencyArgumentsTests.cs test/Polly.Core.Tests/Simmy/Latency/OnLatencyInjectedArgumentsTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
namespace Polly.Core.Tests.Simmy.Latency;
44

5-
public class OnLatencyArgumentsTests
5+
public class OnLatencyInjectedArgumentsTests
66
{
77
[Fact]
88
public void Ctor_Ok()
99
{
10-
var args = new OnLatencyArguments(ResilienceContextPool.Shared.Get(), TimeSpan.FromSeconds(10));
10+
var args = new OnLatencyInjectedArguments(ResilienceContextPool.Shared.Get(), TimeSpan.FromSeconds(10));
1111
args.Context.Should().NotBeNull();
1212
args.Latency.Should().Be(TimeSpan.FromSeconds(10));
1313
}

test/Polly.Core.Tests/Simmy/Outcomes/OutcomeConstantsTests.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public class OutcomeConstantsTests
77
[Fact]
88
public void EnsureDefaults()
99
{
10-
OutcomeConstants.OnOutcomeInjectedEvent.Should().Be("OnOutcomeInjected");
10+
OutcomeConstants.DefaultName.Should().Be("Chaos.Outcome");
11+
OutcomeConstants.OnOutcomeInjectedEvent.Should().Be("Chaos.OnOutcome");
1112
}
1213
}

0 commit comments

Comments
 (0)