From 90b9e895e749f9cdffdeb54040add5b6181d8139 Mon Sep 17 00:00:00 2001 From: hippy Date: Sun, 8 Oct 2023 10:28:11 -0700 Subject: [PATCH] dh: Tweak Retry handler to log retryAttempt 0 also --- ...xponentialBackoffWithJitterRetryHandler.cs | 6 ++-- ...ntialBackoffWithJitterRetryHandlerTests.cs | 35 +------------------ 2 files changed, 3 insertions(+), 38 deletions(-) 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)