Skip to content

Commit

Permalink
dh: Tweak Retry handler to log retryAttempt 0 also
Browse files Browse the repository at this point in the history
  • Loading branch information
rmandvikar committed Oct 8, 2023
1 parent 030a119 commit 90b9e89
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,8 @@ protected override async Task<HttpResponseMessage> SendAsync(
var tuple = await retryPolicy.ExecuteAsync(
action: async (context, ct) =>
{
if (context.TryGetValue(ContextKey.RetryAttempt, out var retryAttempt))
{
request.Properties[RequestProperties.PollyRetryAttempt] = retryAttempt;
}
var retryAttempt = context.TryGetValue(ContextKey.RetryAttempt, out var retryAttemptObj) ? retryAttemptObj : 0;
request.Properties[RequestProperties.PollyRetryAttempt] = retryAttempt;
var response = await base.SendAsync(request, ct)
.ConfigureAwait(false);
return (response, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,40 +179,7 @@ public void Does_Not_Retry_On_TaskCanceledException()
}

[Test]
public async Task When_0_Retries_PollyRetryAttempt_Property_Is_Not_Present()
{
var fixture = new Fixture().Customize(new AutoMoqCustomization());

var statusCode = (HttpStatusCode)542;
var content = fixture.Create<string>();
var shortCircuitingResponseHandler = new ShortCircuitingResponseHandler(
new ShortCircuitingResponseHandlerSettings
{
StatusCode = statusCode,
Content = content,
});
var clockMock = fixture.Freeze<Mock<ISystemClock>>();
clockMock.Setup(x => x.UtcNow).Returns(DateTimeOffsetValues.Chernobyl);
var retryHandler = new ExponentialBackoffWithJitterRetryHandler(
new RetrySettings
{
RetryCount = 0,
RetryDelayInMilliseconds = 0,
},
clockMock.Object);

using var invoker = HttpMessageInvokerFactory.Create(
retryHandler, shortCircuitingResponseHandler);

using var requestMessage = fixture.Create<HttpRequestMessage>();
using var _ = await invoker.SendAsync(requestMessage, CancellationToken.None);

#pragma warning disable CS0618 // Type or member is obsolete
Assert.IsFalse(requestMessage.Properties.ContainsKey(RequestProperties.PollyRetryAttempt));
#pragma warning restore CS0618 // Type or member is obsolete
}

[Test]
[TestCase(0)]
[TestCase(1)]
[TestCase(2)]
public async Task When_N_Retries_PollyRetryAttempt_Property_Is_Present(int retryCount)
Expand Down

0 comments on commit 90b9e89

Please sign in to comment.