-
-
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 1 commit
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,17 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFrameworks>net7.0;net6.0;netstandard2.0;net472;net462</TargetFrameworks> | ||
martincostello marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| <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,3 @@ | ||
| # About Polly.Testing | ||
|
|
||
|
|
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,12 @@ | ||||||
| namespace Polly.Testing; | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// This class provides additional information about <see cref="ResilienceStrategy"/>. | ||||||
|
||||||
| /// This class provides additional information about <see cref="ResilienceStrategy"/>. | |
| /// This class provides additional information about a <see cref="ResilienceStrategy"/>. |
martincostello marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
martincostello marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
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,78 @@ | ||
| using Polly.CircuitBreaker; | ||
| using Polly.Fallback; | ||
| using Polly.Hedging; | ||
| using Polly.Retry; | ||
| using Polly.Timeout; | ||
| 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 IReadOnlyList<ResilienceStrategyDescriptor> 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 IReadOnlyList<ResilienceStrategyDescriptor> GetInnerStrategies(this ResilienceStrategy strategy) | ||
| { | ||
| Guard.NotNull(strategy); | ||
|
|
||
| var strategies = new List<ResilienceStrategy>(); | ||
| strategy.ExpandStrategies(strategies); | ||
|
|
||
| return strategies.Select(s => new ResilienceStrategyDescriptor(s.Options, GetType(s), s.GetType())).ToList(); | ||
| } | ||
|
|
||
| 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); | ||
| } | ||
| } | ||
|
|
||
| private static ResilienceStrategyType GetType(ResilienceStrategy strategy) => strategy switch | ||
| { | ||
| TimeoutResilienceStrategy => ResilienceStrategyType.Timeout, | ||
| ReloadableResilienceStrategy => ResilienceStrategyType.Reload, | ||
| _ when strategy.GetType().FullName == "Polly.RateLimiting.RateLimiterResilienceStrategy" => ResilienceStrategyType.RateLimiter, | ||
| _ when strategy.GetType().FullName == "Polly.Extensions.Telemetry.TelemetryResilienceStrategy" => ResilienceStrategyType.Telemetry, | ||
| _ when strategy.GetType().IsGenericType && strategy.GetType().GetGenericTypeDefinition() == typeof(RetryResilienceStrategy<>) => ResilienceStrategyType.Retry, | ||
| _ when strategy.GetType().IsGenericType && strategy.GetType().GetGenericTypeDefinition() == typeof(CircuitBreakerResilienceStrategy<>) => ResilienceStrategyType.CircuitBreaker, | ||
| _ when strategy.GetType().IsGenericType && strategy.GetType().GetGenericTypeDefinition() == typeof(HedgingResilienceStrategy<>) => ResilienceStrategyType.Hedging, | ||
| _ when strategy.GetType().IsGenericType && strategy.GetType().GetGenericTypeDefinition() == typeof(FallbackResilienceStrategy<>) => ResilienceStrategyType.Fallback, | ||
|
|
||
| _ => ResilienceStrategyType.Custom | ||
| }; | ||
martincostello marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
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,52 @@ | ||
| namespace Polly.Testing; | ||
|
|
||
| /// <summary> | ||
| /// The type of resilience strategy. | ||
| /// </summary> | ||
| public enum ResilienceStrategyType | ||
martincostello marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| /// <summary> | ||
| /// The strategy is custome one, i.e. defined in the external library and not built-in into Polly. | ||
| /// </summary> | ||
| Custom, | ||
|
|
||
| /// <summary> | ||
| /// The retry strategy. | ||
| /// </summary> | ||
| Retry, | ||
|
|
||
| /// <summary> | ||
| /// The timeout strategy. | ||
| /// </summary> | ||
| Timeout, | ||
|
|
||
| /// <summary> | ||
| /// The hedging strategy. | ||
| /// </summary> | ||
| Hedging, | ||
|
|
||
| /// <summary> | ||
| /// The circuit breaker strategy. | ||
| /// </summary> | ||
| CircuitBreaker, | ||
|
|
||
| /// <summary> | ||
| /// The fallback strategy. | ||
| /// </summary> | ||
| Fallback, | ||
|
|
||
| /// <summary> | ||
| /// The rate limiter strategy. | ||
| /// </summary> | ||
| RateLimiter, | ||
|
|
||
| /// <summary> | ||
| /// The telemetry strategy. | ||
| /// </summary> | ||
| Telemetry, | ||
|
|
||
| /// <summary> | ||
| /// The reload strategy. | ||
| /// </summary> | ||
| Reload | ||
| } | ||
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> |
55 changes: 55 additions & 0 deletions
55
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,55 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using FluentAssertions; | ||
| using Microsoft.Extensions.Logging.Abstractions; | ||
| 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 strategies = strategy.GetInnerStrategies(); | ||
|
|
||
| // assert | ||
| strategies.Should().HaveCount(8); | ||
| strategies[0].Type.Should().Be(ResilienceStrategyType.Telemetry); | ||
| strategies[1].Type.Should().Be(ResilienceStrategyType.Fallback); | ||
| strategies[2].Type.Should().Be(ResilienceStrategyType.Retry); | ||
| strategies[3].Type.Should().Be(ResilienceStrategyType.CircuitBreaker); | ||
| strategies[4].Type.Should().Be(ResilienceStrategyType.Timeout); | ||
| strategies[4].Options | ||
| .Should() | ||
| .BeOfType<TimeoutStrategyOptions>().Subject.Timeout | ||
| .Should().Be(TimeSpan.FromSeconds(1)); | ||
|
|
||
| strategies[5].Type.Should().Be(ResilienceStrategyType.Hedging); | ||
| strategies[6].Type.Should().Be(ResilienceStrategyType.RateLimiter); | ||
| strategies[7].Type.Should().Be(ResilienceStrategyType.Custom); | ||
| strategies[7].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.