-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Introduce Polly.Testing package
#1394
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c8b0c4d
Introduce `Polly.Testing` package
martintmk 653cfa2
Cleanup
martintmk 7bb65ca
cleanup
martintmk adff8b6
Fix docs
martintmk 96d8647
Cleanup
martintmk 197c2ad
Update the build
martintmk 04836a6
PR comments
martintmk 6af3c08
Kill mutants
martintmk 5b8fa65
Add new test
martintmk adb42c2
cleanup
martintmk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| namespace Polly.Testing; | ||
|
|
||
| /// <summary> | ||
| /// Describes the pipeline of a resilience strategy. | ||
| /// </summary> | ||
| /// <param name="Strategies">The strategies the pipeline is composed of.</param> | ||
| /// <param name="HasTelemetry">Gets a value indicating whether the pipeline has telemetry enabled.</param> | ||
| /// <param name="IsReloadable">Gets a value indicating whether the resilience strategy is reloadable.</param> | ||
| public record class InnerStrategiesDescriptor(IReadOnlyList<ResilienceStrategyDescriptor> Strategies, bool HasTelemetry, bool IsReloadable); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFrameworks>netstandard2.0</TargetFrameworks> | ||
| <AssemblyTitle>Polly.Testing</AssemblyTitle> | ||
| <RootNamespace>Polly.Testing</RootNamespace> | ||
| <Nullable>enable</Nullable> | ||
| <GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
| <ProjectType>Library</ProjectType> | ||
| <MutationScore>100</MutationScore> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\Polly.Core\Polly.Core.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # About Polly.Testing | ||
|
|
||
| This package exposes API and utilities that can be used to assert the composition of resilience strategies. | ||
martincostello marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ``` csharp | ||
| // Build your resilience strategy. | ||
| ResilienceStrategy strategy = new ResilienceStrategyBuilder() | ||
| .AddRetry(new RetryStrategyOptions | ||
| { | ||
| RetryCount = 4 | ||
| }) | ||
| .AddTimeout(TimeSpan.FromSeconds(1)) | ||
| .ConfigureTelemetry(NullLoggerFactory.Instance) | ||
| .Build(); | ||
|
|
||
| // Retrieve inner strategies. | ||
| InnerStrategiesDescriptor descriptor = strategy.GetInnerStrategies(); | ||
|
|
||
| // Assert the composition. | ||
| Assert.True(descriptor.HasTelemetry); | ||
| Assert.Equal(2, descriptor.Strategies.Count); | ||
|
|
||
| var retryOptions = Assert.IsType<RetryStrategyOptions>(descriptor.Strategies[0]); | ||
| Assert.Equal(4, retryOptions.RetryCount); | ||
|
|
||
| var timeoutOptions = Assert.IsType<TimeoutStrategyOptions>(descriptor.Strategies[0]); | ||
| Assert.Equal(TimeSpan.FromSeconds(1), timeoutOptions.Timeout); | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| namespace Polly.Testing; | ||
|
|
||
| /// <summary> | ||
| /// This class provides additional information about a <see cref="ResilienceStrategy"/>. | ||
| /// </summary> | ||
| /// <param name="Options">The options used by the resilience strategy, if any.</param> | ||
| /// <param name="StrategyType">The type of the strategy.</param> | ||
| public record ResilienceStrategyDescriptor(ResilienceStrategyOptions? Options, Type StrategyType) | ||
| { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| using Polly.Utils; | ||
|
|
||
| namespace Polly.Testing; | ||
|
|
||
| /// <summary> | ||
| /// The test-related extensions for <see cref="ResilienceStrategy"/> and <see cref="ResilienceStrategy{TResult}"/>. | ||
| /// </summary> | ||
| public static class ResilienceStrategyExtensions | ||
| { | ||
| /// <summary> | ||
| /// Gets the inner strategies the <paramref name="strategy"/> is composed of. | ||
| /// </summary> | ||
| /// <typeparam name="TResult">The type of result.</typeparam> | ||
| /// <param name="strategy">The strategy instance.</param> | ||
| /// <returns>A list of inner strategies.</returns> | ||
| /// <exception cref="ArgumentNullException">Thrown when <paramref name="strategy"/> is <see langword="null"/>.</exception> | ||
| public static InnerStrategiesDescriptor GetInnerStrategies<TResult>(this ResilienceStrategy<TResult> strategy) | ||
| { | ||
| Guard.NotNull(strategy); | ||
|
|
||
| return strategy.Strategy.GetInnerStrategies(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the inner strategies the <paramref name="strategy"/> is composed of. | ||
| /// </summary> | ||
| /// <param name="strategy">The strategy instance.</param> | ||
| /// <returns>A list of inner strategies.</returns> | ||
| /// <exception cref="ArgumentNullException">Thrown when <paramref name="strategy"/> is <see langword="null"/>.</exception> | ||
| public static InnerStrategiesDescriptor GetInnerStrategies(this ResilienceStrategy strategy) | ||
| { | ||
| Guard.NotNull(strategy); | ||
|
|
||
| var strategies = new List<ResilienceStrategy>(); | ||
| strategy.ExpandStrategies(strategies); | ||
|
|
||
| var innerStrategies = strategies.Select(s => new ResilienceStrategyDescriptor(s.Options, s.GetType())).ToList(); | ||
|
|
||
| return new InnerStrategiesDescriptor( | ||
| innerStrategies.Where(s => !ShouldSkip(s.StrategyType)).ToList().AsReadOnly(), | ||
martincostello marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| HasTelemetry: innerStrategies.Exists(s => s.StrategyType.FullName == "Polly.Extensions.Telemetry.TelemetryResilienceStrategy"), | ||
| IsReloadable: innerStrategies.Exists(s => s.StrategyType == typeof(ReloadableResilienceStrategy))); | ||
| } | ||
|
|
||
| private static bool ShouldSkip(Type type) => type == typeof(ReloadableResilienceStrategy) || type.FullName == "Polly.Extensions.Telemetry.TelemetryResilienceStrategy"; | ||
|
|
||
| private static void ExpandStrategies(this ResilienceStrategy strategy, List<ResilienceStrategy> strategies) | ||
| { | ||
| if (strategy is ResilienceStrategyPipeline pipeline) | ||
| { | ||
| foreach (var inner in pipeline.Strategies) | ||
| { | ||
| inner.ExpandStrategies(strategies); | ||
| } | ||
| } | ||
| else if (strategy is ReloadableResilienceStrategy reloadable) | ||
| { | ||
| strategies.Add(reloadable); | ||
| ExpandStrategies(reloadable.Strategy, strategies); | ||
| } | ||
| else | ||
| { | ||
| strategies.Add(strategy); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <TargetFrameworks>net7.0;net6.0</TargetFrameworks> | ||
| <TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('Windows'))">$(TargetFrameworks);net481</TargetFrameworks> | ||
| <ProjectType>Test</ProjectType> | ||
| <Nullable>enable</Nullable> | ||
| <Threshold>100</Threshold> | ||
| <NoWarn>$(NoWarn);SA1600;SA1204</NoWarn> | ||
| <Include>[Polly.Testing]*</Include> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\src\Polly.Extensions\Polly.Extensions.csproj" /> | ||
| <ProjectReference Include="..\..\src\Polly.RateLimiting\Polly.RateLimiting.csproj" /> | ||
| <ProjectReference Include="..\..\src\Polly.Testing\Polly.Testing.csproj" /> | ||
| </ItemGroup> | ||
| </Project> |
83 changes: 83 additions & 0 deletions
83
test/Polly.Testing.Tests/ResilienceStrategyExtensionsTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| using System; | ||
| using FluentAssertions; | ||
| using Microsoft.Extensions.Logging.Abstractions; | ||
| using Polly.CircuitBreaker; | ||
| using Polly.Fallback; | ||
| using Polly.Hedging; | ||
| using Polly.RateLimiting; | ||
| using Polly.Registry; | ||
| using Polly.Retry; | ||
| using Polly.Timeout; | ||
|
|
||
| namespace Polly.Testing.Tests; | ||
|
|
||
| public class ResilienceStrategyExtensionsTests | ||
| { | ||
| [Fact] | ||
| public void GetInnerStrategies_Ok() | ||
| { | ||
| // arrange | ||
| var strategy = new ResilienceStrategyBuilder<string>() | ||
| .AddFallback(new() | ||
| { | ||
| FallbackAction = _ => Outcome.FromResultAsTask("dummy"), | ||
| }) | ||
| .AddRetry(new()) | ||
| .AddAdvancedCircuitBreaker(new()) | ||
| .AddTimeout(TimeSpan.FromSeconds(1)) | ||
| .AddHedging(new()) | ||
| .AddConcurrencyLimiter(10) | ||
| .AddStrategy(new CustomStrategy()) | ||
| .ConfigureTelemetry(NullLoggerFactory.Instance) | ||
| .Build(); | ||
|
|
||
| // act | ||
| var descriptor = strategy.GetInnerStrategies(); | ||
|
|
||
| // assert | ||
| descriptor.HasTelemetry.Should().BeTrue(); | ||
| descriptor.Strategies.Should().HaveCount(7); | ||
| descriptor.Strategies[0].Options.Should().BeOfType<FallbackStrategyOptions<string>>(); | ||
| descriptor.Strategies[1].Options.Should().BeOfType<RetryStrategyOptions<string>>(); | ||
| descriptor.Strategies[2].Options.Should().BeOfType<AdvancedCircuitBreakerStrategyOptions<string>>(); | ||
| descriptor.Strategies[3].Options.Should().BeOfType<TimeoutStrategyOptions>(); | ||
| descriptor.Strategies[3].Options | ||
| .Should() | ||
| .BeOfType<TimeoutStrategyOptions>().Subject.Timeout | ||
| .Should().Be(TimeSpan.FromSeconds(1)); | ||
|
|
||
| descriptor.Strategies[4].Options.Should().BeOfType<HedgingStrategyOptions<string>>(); | ||
| descriptor.Strategies[5].Options.Should().BeOfType<RateLimiterStrategyOptions>(); | ||
| descriptor.Strategies[6].StrategyType.Should().Be(typeof(CustomStrategy)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GetInnerStrategies_Reloadable_Ok() | ||
| { | ||
| // arrange | ||
| var strategy = new ResilienceStrategyRegistry<string>().GetOrAddStrategy("dummy", (builder, context) => | ||
| { | ||
| context.EnableReloads(() => () => CancellationToken.None); | ||
|
|
||
| builder | ||
| .AddConcurrencyLimiter(10) | ||
| .AddStrategy(new CustomStrategy()); | ||
| }); | ||
|
|
||
| // act | ||
| var descriptor = strategy.GetInnerStrategies(); | ||
|
|
||
| // assert | ||
| descriptor.IsReloadable.Should().BeTrue(); | ||
| descriptor.HasTelemetry.Should().BeFalse(); | ||
| descriptor.Strategies.Should().HaveCount(2); | ||
| descriptor.Strategies[0].Options.Should().BeOfType<RateLimiterStrategyOptions>(); | ||
| descriptor.Strategies[1].StrategyType.Should().Be(typeof(CustomStrategy)); | ||
| } | ||
|
|
||
| private sealed class CustomStrategy : ResilienceStrategy | ||
| { | ||
| protected override ValueTask<Outcome<TResult>> ExecuteCoreAsync<TResult, TState>(Func<ResilienceContext, TState, ValueTask<Outcome<TResult>>> callback, ResilienceContext context, TState state) | ||
| => throw new NotSupportedException(); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.