diff --git a/src/rm.DelegatingHandlers/ExponentialBackoffWithJitterRetryHandler.cs b/src/rm.DelegatingHandlers/ExponentialBackoffWithJitterRetryHandler.cs index 42ebf3f..6627ead 100644 --- a/src/rm.DelegatingHandlers/ExponentialBackoffWithJitterRetryHandler.cs +++ b/src/rm.DelegatingHandlers/ExponentialBackoffWithJitterRetryHandler.cs @@ -77,10 +77,8 @@ protected override async Task 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); diff --git a/tests/rm.DelegatingHandlersTest/ExponentialBackoffWithJitterRetryHandlerTests.cs b/tests/rm.DelegatingHandlersTest/ExponentialBackoffWithJitterRetryHandlerTests.cs index 4f62ece..5a5f38a 100644 --- a/tests/rm.DelegatingHandlersTest/ExponentialBackoffWithJitterRetryHandlerTests.cs +++ b/tests/rm.DelegatingHandlersTest/ExponentialBackoffWithJitterRetryHandlerTests.cs @@ -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(); - var shortCircuitingResponseHandler = new ShortCircuitingResponseHandler( - new ShortCircuitingResponseHandlerSettings - { - StatusCode = statusCode, - Content = content, - }); - var clockMock = fixture.Freeze>(); - 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(); - 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)