Skip to content

Commit

Permalink
Tests and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
martintmk committed Aug 8, 2023
1 parent 4833268 commit cabfd57
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
19 changes: 17 additions & 2 deletions src/Polly.Testing/ResilienceStrategyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static InnerStrategiesDescriptor GetInnerStrategies<TResult>(this Resilie
{
Guard.NotNull(strategy);

return strategy.Strategy.GetInnerStrategies();
return GetInnerStrategiesCore<TResult>(strategy.Strategy);
}

/// <summary>
Expand All @@ -33,17 +33,32 @@ public static InnerStrategiesDescriptor GetInnerStrategies(this ResilienceStrate
{
Guard.NotNull(strategy);

return GetInnerStrategiesCore<object>(strategy);
}

private static InnerStrategiesDescriptor GetInnerStrategiesCore<T>(ResilienceStrategy strategy)
{
var strategies = new List<ResilienceStrategy>();
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<T>(s))).ToList();

return new InnerStrategiesDescriptor(
innerStrategies.Where(s => !ShouldSkip(s.StrategyType)).ToList().AsReadOnly(),
hasTelemetry: innerStrategies.Exists(s => s.StrategyType.FullName == TelemetryResilienceStrategy),
isReloadable: innerStrategies.Exists(s => s.StrategyType == typeof(ReloadableResilienceStrategy)));
}

private static Type GetStrategyType<T>(ResilienceStrategy strategy)
{
if (strategy is ReactiveResilienceStrategyBridge<T> 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<ResilienceStrategy> strategies)
Expand Down
44 changes: 43 additions & 1 deletion test/Polly.Testing.Tests/ResilienceStrategyExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>()
Expand All @@ -39,19 +39,61 @@ public void GetInnerStrategies_Ok()
descriptor.IsReloadable.Should().BeFalse();
descriptor.Strategies.Should().HaveCount(7);
descriptor.Strategies[0].Options.Should().BeOfType<FallbackStrategyOptions<string>>();
descriptor.Strategies[0].StrategyType.FullName.Should().Contain("Fallback");
descriptor.Strategies[1].Options.Should().BeOfType<RetryStrategyOptions<string>>();
descriptor.Strategies[1].StrategyType.FullName.Should().Contain("Retry");
descriptor.Strategies[2].Options.Should().BeOfType<CircuitBreakerStrategyOptions<string>>();
descriptor.Strategies[2].StrategyType.FullName.Should().Contain("CircuitBreaker");
descriptor.Strategies[3].Options.Should().BeOfType<TimeoutStrategyOptions>();
descriptor.Strategies[3].StrategyType.FullName.Should().Contain("Timeout");
descriptor.Strategies[3].Options
.Should()
.BeOfType<TimeoutStrategyOptions>().Subject.Timeout
.Should().Be(TimeSpan.FromSeconds(1));

descriptor.Strategies[4].Options.Should().BeOfType<HedgingStrategyOptions<string>>();
descriptor.Strategies[4].StrategyType.FullName.Should().Contain("Hedging");
descriptor.Strategies[5].Options.Should().BeOfType<RateLimiterStrategyOptions>();
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<RetryStrategyOptions>();
descriptor.Strategies[0].StrategyType.FullName.Should().Contain("Retry");
descriptor.Strategies[1].Options.Should().BeOfType<CircuitBreakerStrategyOptions>();
descriptor.Strategies[1].StrategyType.FullName.Should().Contain("CircuitBreaker");
descriptor.Strategies[2].Options.Should().BeOfType<TimeoutStrategyOptions>();
descriptor.Strategies[2].StrategyType.FullName.Should().Contain("Timeout");
descriptor.Strategies[2].Options
.Should()
.BeOfType<TimeoutStrategyOptions>().Subject.Timeout
.Should().Be(TimeSpan.FromSeconds(1));

descriptor.Strategies[3].Options.Should().BeOfType<RateLimiterStrategyOptions>();
descriptor.Strategies[3].StrategyType.FullName.Should().Contain("RateLimiter");
descriptor.Strategies[4].StrategyType.Should().Be(typeof(CustomStrategy));
}

[Fact]
public void GetInnerStrategies_SingleStrategy_Ok()
{
Expand Down

0 comments on commit cabfd57

Please sign in to comment.