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

Fix the behavior of OnHedging event #1625

Merged
merged 7 commits into from
Sep 26, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
improve test
martintmk committed Sep 26, 2023
commit 150bdacad3ce7ec742a4083fbcdd8afa1cc0d610
19 changes: 17 additions & 2 deletions test/Polly.Core.Tests/Hedging/HedgingResilienceStrategyTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using NSubstitute;
using Polly.Hedging;
using Polly.Hedging.Utils;
using Polly.Telemetry;
@@ -852,17 +853,31 @@ public async Task ExecuteAsync_EnsureExceptionStackTracePreserved()
[Fact]
public async Task ExecuteAsync_EnsureOnHedgingCalled()
{
var primaryContext = ResilienceContextPool.Shared.Get();
var key = new ResiliencePropertyKey<string>("my-key");
primaryContext.Properties.Set(key, "my-value");

var attempts = new List<int>();
_options.OnHedging = args =>
{
args.PrimaryContext.Should().Be(primaryContext);
args.ActionContext.Should().NotBe(args.PrimaryContext);
args.PrimaryContext.Properties.GetValue(key, string.Empty).Should().Be("my-value");
args.ActionContext.Properties.GetValue(key, string.Empty).Should().Be("my-value");
args.ActionContext.Properties.Set(key, "new-value");
attempts.Add(args.AttemptNumber);
return default;
};

ConfigureHedging(res => res.Result == Failure, args => () => Outcome.FromResultAsValueTask(Failure));
ConfigureHedging(res => res.Result == Failure,
args => () =>
{
args.ActionContext.Properties.GetValue(key, string.Empty).Should().Be("new-value");
return Outcome.FromResultAsValueTask(Failure);
});

var strategy = Create();
await strategy.ExecuteAsync(_ => new ValueTask<string>(Failure));
await strategy.ExecuteAsync(_ => new ValueTask<string>(Failure), primaryContext);

attempts.Should().HaveCount(_options.MaxHedgedAttempts);
attempts.Should().BeInAscendingOrder();