From ebe1cea7d6582a5be2d2cd7350632382979e6ada Mon Sep 17 00:00:00 2001 From: Chris R Date: Wed, 13 Oct 2021 15:26:11 -0700 Subject: [PATCH 1/2] Fix multiple dictionary add in polly --- .../Polly/src/PolicyHttpMessageHandler.cs | 2 +- .../test/PolicyHttpMessageHandlerTest.cs | 52 +++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/HttpClientFactory/Polly/src/PolicyHttpMessageHandler.cs b/src/HttpClientFactory/Polly/src/PolicyHttpMessageHandler.cs index 8f973a98db21..c32c52fc2f1d 100644 --- a/src/HttpClientFactory/Polly/src/PolicyHttpMessageHandler.cs +++ b/src/HttpClientFactory/Polly/src/PolicyHttpMessageHandler.cs @@ -169,7 +169,7 @@ protected virtual async Task SendCoreAsync(HttpRequestMessa var result = await base.SendAsync(request, cancellationToken); - request.Properties.Add(PriorResponseKey, result); + request.Properties[PriorResponseKey] = result; return result; } diff --git a/src/HttpClientFactory/Polly/test/PolicyHttpMessageHandlerTest.cs b/src/HttpClientFactory/Polly/test/PolicyHttpMessageHandlerTest.cs index 98965d119d54..6967a42c449e 100644 --- a/src/HttpClientFactory/Polly/test/PolicyHttpMessageHandlerTest.cs +++ b/src/HttpClientFactory/Polly/test/PolicyHttpMessageHandlerTest.cs @@ -147,6 +147,58 @@ public async Task SendAsync_StaticPolicy_PolicyTriggers_CanReexecuteSendAsync_Fi Assert.True(fakeContent.Disposed); } + [Fact] + public async Task SendAsync_MultipleHandlers_StaticPolicy_PolicyTriggers_CanReexecuteSendAsync_FirstResponseDisposed() + { + // Arrange + var policy1 = HttpPolicyExtensions.HandleTransientHttpError() + .RetryAsync(retryCount: 1); + var policy2 = HttpPolicyExtensions.HandleTransientHttpError() + .CircuitBreakerAsync(handledEventsAllowedBeforeBreaking: 2, durationOfBreak: TimeSpan.FromSeconds(10)); + + var callCount = 0; + var fakeContent = new FakeContent(); + var firstResponse = new HttpResponseMessage() + { + StatusCode = System.Net.HttpStatusCode.InternalServerError, + Content = fakeContent, + }; + var expected = new HttpResponseMessage(); + + var handler1 = new PolicyHttpMessageHandler(policy1); + var handler2 = new PolicyHttpMessageHandler(policy2); + handler1.InnerHandler = handler2; + handler2.InnerHandler = new TestHandler() + { + OnSendAsync = (req, ct) => + { + if (callCount == 0) + { + callCount++; + return Task.FromResult(firstResponse); + } + else if (callCount == 1) + { + callCount++; + return Task.FromResult(expected); + } + else + { + throw new InvalidOperationException(); + } + } + }; + var invoke = new HttpMessageInvoker(handler1); + + // Act + var response = await invoke.SendAsync(new HttpRequestMessage(), CancellationToken.None); + + // Assert + Assert.Equal(2, callCount); + Assert.Same(expected, response); + Assert.True(fakeContent.Disposed); + } + [Fact] public async Task SendAsync_DynamicPolicy_PolicySelectorReturnsNull_ThrowsException() { From f4fb12f32f0d29f2bbab5202f4a2bd3d920129db Mon Sep 17 00:00:00 2001 From: Chris Ross Date: Wed, 13 Oct 2021 16:06:57 -0700 Subject: [PATCH 2/2] Update src/HttpClientFactory/Polly/test/PolicyHttpMessageHandlerTest.cs --- .../Polly/test/PolicyHttpMessageHandlerTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/HttpClientFactory/Polly/test/PolicyHttpMessageHandlerTest.cs b/src/HttpClientFactory/Polly/test/PolicyHttpMessageHandlerTest.cs index 6967a42c449e..7b800b2cf135 100644 --- a/src/HttpClientFactory/Polly/test/PolicyHttpMessageHandlerTest.cs +++ b/src/HttpClientFactory/Polly/test/PolicyHttpMessageHandlerTest.cs @@ -148,7 +148,7 @@ public async Task SendAsync_StaticPolicy_PolicyTriggers_CanReexecuteSendAsync_Fi } [Fact] - public async Task SendAsync_MultipleHandlers_StaticPolicy_PolicyTriggers_CanReexecuteSendAsync_FirstResponseDisposed() + public async Task MultipleHandlers_CanReexecuteSendAsync_FirstResponseDisposed() { // Arrange var policy1 = HttpPolicyExtensions.HandleTransientHttpError()