From 04bd9ecccb26477156c5723be5188772d7b22151 Mon Sep 17 00:00:00 2001 From: martintmk <103487740+martintmk@users.noreply.github.com> Date: Mon, 19 Jun 2023 14:01:29 +0200 Subject: [PATCH] Add new issue test that demonstrates library scenario (#1322) --- ...s.OnCircuitBreakWithServiceProvider_796.cs | 2 +- ...uesTests.OverrideLibraryStrategies_1072.cs | 85 +++++++++++++++++++ .../Issues/IssuesTests.cs | 2 +- 3 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 test/Polly.Extensions.Tests/Issues/IssuesTests.OverrideLibraryStrategies_1072.cs diff --git a/test/Polly.Extensions.Tests/Issues/IssuesTests.OnCircuitBreakWithServiceProvider_796.cs b/test/Polly.Extensions.Tests/Issues/IssuesTests.OnCircuitBreakWithServiceProvider_796.cs index 996f31094f5..b8ca882a11f 100644 --- a/test/Polly.Extensions.Tests/Issues/IssuesTests.OnCircuitBreakWithServiceProvider_796.cs +++ b/test/Polly.Extensions.Tests/Issues/IssuesTests.OnCircuitBreakWithServiceProvider_796.cs @@ -3,7 +3,7 @@ using Polly.Extensions.DependencyInjection; using Polly.Registry; -namespace Polly.Core.Tests.Issues; +namespace Polly.Extensions.Tests.Issues; public partial class IssuesTests { diff --git a/test/Polly.Extensions.Tests/Issues/IssuesTests.OverrideLibraryStrategies_1072.cs b/test/Polly.Extensions.Tests/Issues/IssuesTests.OverrideLibraryStrategies_1072.cs new file mode 100644 index 00000000000..d7913f161ee --- /dev/null +++ b/test/Polly.Extensions.Tests/Issues/IssuesTests.OverrideLibraryStrategies_1072.cs @@ -0,0 +1,85 @@ +using System.Net.Sockets; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; +using Polly.Registry; +using Polly.Retry; + +namespace Polly.Extensions.Tests.Issues; + +public partial class IssuesTests +{ + [InlineData(true)] + [InlineData(false)] + [Theory] + public void OverrideLibraryStrategies_898(bool overrideStrategy) + { + // arrange + var services = new ServiceCollection(); + var failFirstCall = true; + AddLibraryServices(services); + + if (overrideStrategy) + { + // This call overrides the strategy that the library uses. The last call to AddResilienceStrategy wins. + services.AddResilienceStrategy("library-strategy", builder => builder.AddRetry(new() + { + ShouldHandle = args => args.Exception switch + { + InvalidOperationException => PredicateResult.True, + SocketException => PredicateResult.True, + _ => PredicateResult.False + }, + BaseDelay = TimeSpan.Zero + })); + } + + var serviceProvider = services.BuildServiceProvider(); + var api = serviceProvider.GetRequiredService(); + + // act && assert + if (overrideStrategy) + { + // The library now also handles SocketException. + api.Invoking(a => a.ExecuteLibrary(UnstableCall)).Should().NotThrow(); + + } + else + { + // Originally, the library strategy only handled InvalidOperationException. + api.Invoking(a => a.ExecuteLibrary(UnstableCall)).Should().Throw(); + } + + void UnstableCall() + { + if (failFirstCall) + { + failFirstCall = false; + + // This exception was not originally handled by strategy. + throw new SocketException(); + } + } + } + + private static void AddLibraryServices(IServiceCollection services) + { + services.TryAddSingleton(); + services.AddResilienceStrategy("library-strategy", builder => builder.AddRetry(new() + { + ShouldHandle = args => args.Exception switch + { + InvalidOperationException => PredicateResult.True, + _ => PredicateResult.False + } + })); + } + + public class LibraryApi + { + private readonly ResilienceStrategy _strategy; + + public LibraryApi(ResilienceStrategyProvider provider) => _strategy = provider.Get("library-strategy"); + + public void ExecuteLibrary(Action execute) => _strategy.Execute(execute); + } +} diff --git a/test/Polly.Extensions.Tests/Issues/IssuesTests.cs b/test/Polly.Extensions.Tests/Issues/IssuesTests.cs index dc5b18507ad..14187487992 100644 --- a/test/Polly.Extensions.Tests/Issues/IssuesTests.cs +++ b/test/Polly.Extensions.Tests/Issues/IssuesTests.cs @@ -1,4 +1,4 @@ -namespace Polly.Core.Tests.Issues; +namespace Polly.Extensions.Tests.Issues; public partial class IssuesTests {