From cabfd574ea18a9578c84cbbaa2ff4aced18db064 Mon Sep 17 00:00:00 2001 From: Martin Tomka Date: Tue, 8 Aug 2023 10:39:50 +0200 Subject: [PATCH] Tests and fixes --- .../ResilienceStrategyExtensions.cs | 19 +++++++- .../ResilienceStrategyExtensionsTests.cs | 44 ++++++++++++++++++- 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/src/Polly.Testing/ResilienceStrategyExtensions.cs b/src/Polly.Testing/ResilienceStrategyExtensions.cs index 9f8f0b3bc89..ccffe1175a3 100644 --- a/src/Polly.Testing/ResilienceStrategyExtensions.cs +++ b/src/Polly.Testing/ResilienceStrategyExtensions.cs @@ -20,7 +20,7 @@ public static InnerStrategiesDescriptor GetInnerStrategies(this Resilie { Guard.NotNull(strategy); - return strategy.Strategy.GetInnerStrategies(); + return GetInnerStrategiesCore(strategy.Strategy); } /// @@ -33,10 +33,15 @@ public static InnerStrategiesDescriptor GetInnerStrategies(this ResilienceStrate { Guard.NotNull(strategy); + return GetInnerStrategiesCore(strategy); + } + + private static InnerStrategiesDescriptor GetInnerStrategiesCore(ResilienceStrategy strategy) + { var strategies = new List(); strategy.ExpandStrategies(strategies); - var innerStrategies = strategies.Select(s => new ResilienceStrategyDescriptor(s.Options, s.GetType())).ToList(); + var innerStrategies = strategies.Select(s => new ResilienceStrategyDescriptor(s.Options, GetStrategyType(s))).ToList(); return new InnerStrategiesDescriptor( innerStrategies.Where(s => !ShouldSkip(s.StrategyType)).ToList().AsReadOnly(), @@ -44,6 +49,16 @@ public static InnerStrategiesDescriptor GetInnerStrategies(this ResilienceStrate isReloadable: innerStrategies.Exists(s => s.StrategyType == typeof(ReloadableResilienceStrategy))); } + private static Type GetStrategyType(ResilienceStrategy strategy) + { + if (strategy is ReactiveResilienceStrategyBridge bridge) + { + return bridge.Strategy.GetType(); + } + + return strategy.GetType(); + } + private static bool ShouldSkip(Type type) => type == typeof(ReloadableResilienceStrategy) || type.FullName == TelemetryResilienceStrategy; private static void ExpandStrategies(this ResilienceStrategy strategy, List strategies) diff --git a/test/Polly.Testing.Tests/ResilienceStrategyExtensionsTests.cs b/test/Polly.Testing.Tests/ResilienceStrategyExtensionsTests.cs index cdd589871e3..822c6917a7c 100644 --- a/test/Polly.Testing.Tests/ResilienceStrategyExtensionsTests.cs +++ b/test/Polly.Testing.Tests/ResilienceStrategyExtensionsTests.cs @@ -14,7 +14,7 @@ namespace Polly.Testing.Tests; public class ResilienceStrategyExtensionsTests { [Fact] - public void GetInnerStrategies_Ok() + public void GetInnerStrategies_Generic_Ok() { // arrange var strategy = new CompositeStrategyBuilder() @@ -39,19 +39,61 @@ public void GetInnerStrategies_Ok() descriptor.IsReloadable.Should().BeFalse(); descriptor.Strategies.Should().HaveCount(7); descriptor.Strategies[0].Options.Should().BeOfType>(); + descriptor.Strategies[0].StrategyType.FullName.Should().Contain("Fallback"); descriptor.Strategies[1].Options.Should().BeOfType>(); + descriptor.Strategies[1].StrategyType.FullName.Should().Contain("Retry"); descriptor.Strategies[2].Options.Should().BeOfType>(); + descriptor.Strategies[2].StrategyType.FullName.Should().Contain("CircuitBreaker"); descriptor.Strategies[3].Options.Should().BeOfType(); + descriptor.Strategies[3].StrategyType.FullName.Should().Contain("Timeout"); descriptor.Strategies[3].Options .Should() .BeOfType().Subject.Timeout .Should().Be(TimeSpan.FromSeconds(1)); descriptor.Strategies[4].Options.Should().BeOfType>(); + descriptor.Strategies[4].StrategyType.FullName.Should().Contain("Hedging"); descriptor.Strategies[5].Options.Should().BeOfType(); + descriptor.Strategies[5].StrategyType.FullName.Should().Contain("RateLimiter"); descriptor.Strategies[6].StrategyType.Should().Be(typeof(CustomStrategy)); } + [Fact] + public void GetInnerStrategies_NonGeneric_Ok() + { + // arrange + var strategy = new CompositeStrategyBuilder() + .AddRetry(new()) + .AddCircuitBreaker(new()) + .AddTimeout(TimeSpan.FromSeconds(1)) + .AddConcurrencyLimiter(10) + .AddStrategy(new CustomStrategy()) + .ConfigureTelemetry(NullLoggerFactory.Instance) + .Build(); + + // act + var descriptor = strategy.GetInnerStrategies(); + + // assert + descriptor.HasTelemetry.Should().BeTrue(); + descriptor.IsReloadable.Should().BeFalse(); + descriptor.Strategies.Should().HaveCount(5); + descriptor.Strategies[0].Options.Should().BeOfType(); + descriptor.Strategies[0].StrategyType.FullName.Should().Contain("Retry"); + descriptor.Strategies[1].Options.Should().BeOfType(); + descriptor.Strategies[1].StrategyType.FullName.Should().Contain("CircuitBreaker"); + descriptor.Strategies[2].Options.Should().BeOfType(); + descriptor.Strategies[2].StrategyType.FullName.Should().Contain("Timeout"); + descriptor.Strategies[2].Options + .Should() + .BeOfType().Subject.Timeout + .Should().Be(TimeSpan.FromSeconds(1)); + + descriptor.Strategies[3].Options.Should().BeOfType(); + descriptor.Strategies[3].StrategyType.FullName.Should().Contain("RateLimiter"); + descriptor.Strategies[4].StrategyType.Should().Be(typeof(CustomStrategy)); + } + [Fact] public void GetInnerStrategies_SingleStrategy_Ok() {