Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/Polly.Core/Hedging/Controller/TaskExecution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ private async Task ExecutePrimaryActionAsync<TState>(Func<ResilienceContext, TSt

private async Task UpdateOutcomeAsync(Outcome<T> outcome)
{
var args = new HedgingPredicateArguments<T>(Context, outcome);
var args = new HedgingPredicateArguments<T>(Context, outcome, AttemptNumber);
Outcome = outcome;
IsHandled = await _handler.ShouldHandle(args).ConfigureAwait(Context.ContinueOnCapturedContext);
TelemetryUtil.ReportExecutionAttempt(_telemetry, Context, outcome, AttemptNumber, ExecutionTime, IsHandled);
Expand Down
14 changes: 14 additions & 0 deletions src/Polly.Core/Hedging/HedgingPredicateArguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ public HedgingPredicateArguments(ResilienceContext context, Outcome<TResult> out
Outcome = outcome;
}

/// <summary>
/// Initializes a new instance of the <see cref="HedgingPredicateArguments{TResult}"/> struct.
/// </summary>
/// <param name="outcome">The context in which the resilience operation or event occurred.</param>
/// <param name="context">The outcome of the resilience operation or event.</param>
/// <param name="attemptNumber">The zero-based attempt number.</param>
public HedgingPredicateArguments(ResilienceContext context, Outcome<TResult> outcome, int attemptNumber)
: this(context, outcome) => AttemptNumber = attemptNumber;

/// <summary>
/// Gets the outcome of the user-specified callback.
/// </summary>
Expand All @@ -31,4 +40,9 @@ public HedgingPredicateArguments(ResilienceContext context, Outcome<TResult> out
/// Gets the context of this event.
/// </summary>
public ResilienceContext Context { get; }

/// <summary>
/// Gets the zero-based attempt number.
/// </summary>
public int? AttemptNumber { get; } = null;
}
2 changes: 2 additions & 0 deletions src/Polly.Core/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
#nullable enable
Polly.Hedging.HedgingPredicateArguments<TResult>.AttemptNumber.get -> int?
Polly.Hedging.HedgingPredicateArguments<TResult>.HedgingPredicateArguments(Polly.ResilienceContext! context, Polly.Outcome<TResult> outcome, int attemptNumber) -> void
16 changes: 16 additions & 0 deletions test/Polly.Core.Tests/Hedging/HedgingPredicateArgumentsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,21 @@ public static void Ctor_Ok()
// Assert
args.Context.ShouldBe(context);
args.Outcome.Result.ShouldBe(1);
args.AttemptNumber.ShouldBeNull();
}

[Fact]
public static void Ctor_With_AttemptNumber_Ok()
{
// Arrange
var context = ResilienceContextPool.Shared.Get();

// Act
var args = new HedgingPredicateArguments<int>(context, Outcome.FromResult(1), 10);

// Assert
args.Context.ShouldBe(context);
args.Outcome.Result.ShouldBe(1);
args.AttemptNumber.ShouldBe(10);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,40 @@ public async Task AddHedging_IntegrationTestWithRealDelay()
var result = await strategy.ExecuteAsync(token => new ValueTask<string>("error"));
result.ShouldBe("success");
}

[Fact]
public async Task AddHedging_AttemptNumbers_Are_Incremented()
{
const string Error = "error";
const string Success = "success";
int shouldHandleAttemptNumber = 0;
int actionGeneratorAttemptNumber = 1;

var strategy = _builder.AddHedging(new()
{
MaxHedgedAttempts = 4,
ShouldHandle = args =>
{
args.AttemptNumber.ShouldBe(shouldHandleAttemptNumber++);
return args.Outcome.Result switch
{
Error => PredicateResult.True(),
_ => PredicateResult.False()
};
},
ActionGenerator = args =>
{
args.AttemptNumber.ShouldBe(actionGeneratorAttemptNumber++);
return async () =>
{
await Task.Delay(20);
return Outcome.FromResult(args.AttemptNumber == 3 ? Success : Error);
};
}
})
.Build();

var result = await strategy.ExecuteAsync(token => new ValueTask<string>(Error));
result.ShouldBe(Success);
}
}
6 changes: 3 additions & 3 deletions test/Polly.Core.Tests/Hedging/HedgingStrategyOptionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ public async Task ShouldHandle_EnsureDefaults()
var options = new HedgingStrategyOptions<int>();
var context = ResilienceContextPool.Shared.Get();

(await options.ShouldHandle(new HedgingPredicateArguments<int>(context, Outcome.FromResult(0)))).ShouldBe(false);
(await options.ShouldHandle(new HedgingPredicateArguments<int>(context, Outcome.FromException<int>(new OperationCanceledException())))).ShouldBe(false);
(await options.ShouldHandle(new HedgingPredicateArguments<int>(context, Outcome.FromException<int>(new InvalidOperationException())))).ShouldBe(true);
(await options.ShouldHandle(new HedgingPredicateArguments<int>(context, Outcome.FromResult(0), 0))).ShouldBe(false);
(await options.ShouldHandle(new HedgingPredicateArguments<int>(context, Outcome.FromException<int>(new OperationCanceledException()), 1))).ShouldBe(false);
(await options.ShouldHandle(new HedgingPredicateArguments<int>(context, Outcome.FromException<int>(new InvalidOperationException()), 2))).ShouldBe(true);
}

[Fact]
Expand Down
2 changes: 1 addition & 1 deletion test/Polly.Core.Tests/PredicateBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public async Task Operator_HedgingStrategyOptions_Ok()
ShouldHandle = new PredicateBuilder<string>().HandleResult("error")
};

var handled = await options.ShouldHandle(new(context, CreateOutcome("error")));
var handled = await options.ShouldHandle(new(context, CreateOutcome("error"), 0));

handled.ShouldBeTrue();
}
Expand Down