diff --git a/test/Polly.RateLimiting.Tests/RateLimiterCompositeStrategyBuilderExtensionsTests.cs b/test/Polly.RateLimiting.Tests/RateLimiterCompositeStrategyBuilderExtensionsTests.cs index 875757ee90e..ae9582e54f0 100644 --- a/test/Polly.RateLimiting.Tests/RateLimiterCompositeStrategyBuilderExtensionsTests.cs +++ b/test/Polly.RateLimiting.Tests/RateLimiterCompositeStrategyBuilderExtensionsTests.cs @@ -7,12 +7,12 @@ namespace Polly.RateLimiting.Tests; public class RateLimiterCompositeStrategyBuilderExtensionsTests { - public static readonly TheoryData>> Data = new() + public static readonly TheoryData> Data = new() { builder => { builder.AddConcurrencyLimiter(2, 2); - AssertRateLimiterStrategy(builder); + AssertRateLimiterStrategy(builder, strategy => strategy.Limiter.Limiter.Should().BeOfType()); }, builder => { @@ -23,21 +23,21 @@ public class RateLimiterCompositeStrategyBuilderExtensionsTests QueueLimit = 2 }); - AssertRateLimiterStrategy(builder); + AssertRateLimiterStrategy(builder, strategy => strategy.Limiter.Limiter.Should().BeOfType()); }, builder => { var expected = Substitute.For(); builder.AddRateLimiter(expected); - AssertRateLimiterStrategy(builder); + AssertRateLimiterStrategy(builder, strategy => strategy.Limiter.Limiter.Should().Be(expected)); } }; [MemberData(nameof(Data))] [Theory(Skip = "https://github.com/stryker-mutator/stryker-net/issues/2144")] - public void AddRateLimiter_Extensions_Ok(Action> configure) + public void AddRateLimiter_Extensions_Ok(Action configure) { - var builder = new CompositeStrategyBuilder(); + var builder = new CompositeStrategyBuilder(); configure(builder); @@ -61,13 +61,13 @@ public void AddConcurrencyLimiter_InvalidOptions_Throws() [Fact] public void AddRateLimiter_AllExtensions_Ok() { - foreach (var configure in Data.Select(v => v[0]).Cast>>()) + foreach (var configure in Data.Select(v => v[0]).Cast>()) { - var builder = new CompositeStrategyBuilder(); + var builder = new CompositeStrategyBuilder(); configure(builder); - builder.Build().GetInnerStrategies().Strategies.Single().StrategyType.Should().Be(typeof(RateLimiterResilienceStrategy)); + AssertRateLimiterStrategy(builder); } } @@ -136,8 +136,29 @@ public void AddRateLimiter_Options_Ok() .Be(); } - private static void AssertRateLimiterStrategy(CompositeStrategyBuilder builder) + private static void AssertRateLimiterStrategy(CompositeStrategyBuilder builder, Action? assert = null, bool hasEvents = false) + { + ResilienceStrategy strategy = builder.Build(); + var limiterStrategy = GetResilienceStrategy(strategy); + assert?.Invoke(limiterStrategy); + + if (hasEvents) + { + limiterStrategy.OnLeaseRejected.Should().NotBeNull(); + limiterStrategy + .OnLeaseRejected!(new OnRateLimiterRejectedArguments(ResilienceContextPool.Shared.Get(), Substitute.For(), null)) + .Preserve().GetAwaiter().GetResult(); + } + else + { + limiterStrategy.OnLeaseRejected.Should().BeNull(); + } + + strategy.GetInnerStrategies().Strategies.Single().StrategyType.Should().Be(typeof(RateLimiterResilienceStrategy)); + } + + private static RateLimiterResilienceStrategy GetResilienceStrategy(ResilienceStrategy strategy) { - builder.Build().GetInnerStrategies().Strategies.Single().StrategyType.Should().Be(typeof(RateLimiterResilienceStrategy)); + return (RateLimiterResilienceStrategy)strategy.GetType().GetRuntimeProperty("Strategy")!.GetValue(strategy)!; } }