From 91e5384b8a77bf19d618587e4929bc687658f8ed Mon Sep 17 00:00:00 2001 From: martintmk <103487740+martintmk@users.noreply.github.com> Date: Mon, 19 Jun 2023 17:14:45 +0200 Subject: [PATCH] API feedback (#1327) --- .../ResilienceStrategyProviderBenchmark.cs | 4 +- .../TelemetryBenchmark.cs | 2 +- .../Utils/Helper.MultipleStrategies.cs | 2 +- samples/GenericStrategies/Program.cs | 2 +- samples/Intro/Program.cs | 4 +- ...akerResilienceStrategyBuilderExtensions.cs | 7 +- .../Registry/ResilienceStrategyProvider.cs | 12 +- .../Registry/ResilienceStrategyRegistry.cs | 16 +-- .../ResilienceStrategyBuilderBase.cs | 10 +- .../ResilienceStrategyBuilderExtensions.cs | 25 +++- ...etryResilienceStrategyBuilderExtensions.cs | 4 +- .../Retry/RetryStrategyOptions.TResult.cs | 9 ++ .../PollyServiceCollectionExtensions.cs | 2 +- ...etryResilienceStrategyBuilderExtensions.cs | 6 +- ...iterResilienceStrategyBuilderExtensions.cs | 3 +- .../ResilienceStrategyProviderTests.cs | 12 +- .../ResilienceStrategyRegistryTests.cs | 108 +++++++++--------- .../PollyServiceCollectionExtensionTests.cs | 34 +++--- ...s.OnCircuitBreakWithServiceProvider_796.cs | 2 +- ...uesTests.OverrideLibraryStrategies_1072.cs | 2 +- .../ReloadableResilienceStrategyTests.cs | 2 +- ...esilienceStrategyBuilderExtensionsTests.cs | 18 +-- 22 files changed, 152 insertions(+), 134 deletions(-) diff --git a/bench/Polly.Core.Benchmarks/ResilienceStrategyProviderBenchmark.cs b/bench/Polly.Core.Benchmarks/ResilienceStrategyProviderBenchmark.cs index 11171032cbf..52073e39182 100644 --- a/bench/Polly.Core.Benchmarks/ResilienceStrategyProviderBenchmark.cs +++ b/bench/Polly.Core.Benchmarks/ResilienceStrategyProviderBenchmark.cs @@ -17,8 +17,8 @@ public void Setup() } [Benchmark] - public void Get_Ok() => _provider!.Get("dummy"); + public void Get_Ok() => _provider!.GetStrategy("dummy"); [Benchmark] - public void Get_Generic_Ok() => _provider!.Get("dummy"); + public void Get_Generic_Ok() => _provider!.GetStrategy("dummy"); } diff --git a/bench/Polly.Core.Benchmarks/TelemetryBenchmark.cs b/bench/Polly.Core.Benchmarks/TelemetryBenchmark.cs index 32f1d1fd19c..e1fa5a0b602 100644 --- a/bench/Polly.Core.Benchmarks/TelemetryBenchmark.cs +++ b/bench/Polly.Core.Benchmarks/TelemetryBenchmark.cs @@ -61,7 +61,7 @@ private ResilienceStrategy Build(ResilienceStrategyBuilder builder) }); } - builder.EnableTelemetry(options); + builder.ConfigureTelemetry(options); } return builder.Build(); diff --git a/bench/Polly.Core.Benchmarks/Utils/Helper.MultipleStrategies.cs b/bench/Polly.Core.Benchmarks/Utils/Helper.MultipleStrategies.cs index 8a549667e75..a6f774a9de2 100644 --- a/bench/Polly.Core.Benchmarks/Utils/Helper.MultipleStrategies.cs +++ b/bench/Polly.Core.Benchmarks/Utils/Helper.MultipleStrategies.cs @@ -44,7 +44,7 @@ internal static partial class Helper if (telemetry) { - builder.EnableTelemetry(NullLoggerFactory.Instance); + builder.ConfigureTelemetry(NullLoggerFactory.Instance); } }), _ => throw new NotSupportedException() diff --git a/samples/GenericStrategies/Program.cs b/samples/GenericStrategies/Program.cs index 7aa5bb22319..08607324620 100644 --- a/samples/GenericStrategies/Program.cs +++ b/samples/GenericStrategies/Program.cs @@ -66,7 +66,7 @@ var response = await strategy.ExecuteAsync( async token => { - await Task.Delay(10); + await Task.Delay(10, token); // This causes the action fail, thus using the fallback strategy above return new HttpResponseMessage(HttpStatusCode.InternalServerError); }, diff --git a/samples/Intro/Program.cs b/samples/Intro/Program.cs index 91fe0100080..4dadeac7b6f 100644 --- a/samples/Intro/Program.cs +++ b/samples/Intro/Program.cs @@ -22,13 +22,13 @@ strategy.Execute(() => { }); // Asynchronously -await strategy.ExecuteAsync(async token => { await Task.Delay(10); }, CancellationToken.None); +await strategy.ExecuteAsync(async token => { await Task.Delay(10, token); }, CancellationToken.None); // Synchronously with result strategy.Execute(token => "some-result"); // Asynchronously with result -await strategy.ExecuteAsync(async token => { await Task.Delay(10); return "some-result"; }, CancellationToken.None); +await strategy.ExecuteAsync(async token => { await Task.Delay(10, token); return "some-result"; }, CancellationToken.None); // Use state to avoid lambda allocation strategy.Execute(static state => state, "my-state"); diff --git a/src/Polly.Core/CircuitBreaker/CircuitBreakerResilienceStrategyBuilderExtensions.cs b/src/Polly.Core/CircuitBreaker/CircuitBreakerResilienceStrategyBuilderExtensions.cs index b9ead87a5a6..0cd6f7586d1 100644 --- a/src/Polly.Core/CircuitBreaker/CircuitBreakerResilienceStrategyBuilderExtensions.cs +++ b/src/Polly.Core/CircuitBreaker/CircuitBreakerResilienceStrategyBuilderExtensions.cs @@ -102,7 +102,7 @@ public static ResilienceStrategyBuilder AddSimpleCircuitBreaker(this ResilienceS private static TBuilder AddAdvancedCircuitBreakerCore(this TBuilder builder, AdvancedCircuitBreakerStrategyOptions options) where TBuilder : ResilienceStrategyBuilderBase { - builder.AddStrategy( + return builder.AddStrategy( context => { var behavior = new AdvancedCircuitBehavior( @@ -113,15 +113,12 @@ private static TBuilder AddAdvancedCircuitBreakerCore(this TB return CreateStrategy(context, options, behavior); }, options); - - return builder; } private static TBuilder AddSimpleCircuitBreakerCore(this TBuilder builder, SimpleCircuitBreakerStrategyOptions options) where TBuilder : ResilienceStrategyBuilderBase { - builder.AddStrategy(context => CreateStrategy(context, options, new ConsecutiveFailuresCircuitBehavior(options.FailureThreshold)), options); - return builder; + return builder.AddStrategy(context => CreateStrategy(context, options, new ConsecutiveFailuresCircuitBehavior(options.FailureThreshold)), options); } internal static CircuitBreakerResilienceStrategy CreateStrategy(ResilienceStrategyBuilderContext context, CircuitBreakerStrategyOptions options, CircuitBehavior behavior) diff --git a/src/Polly.Core/Registry/ResilienceStrategyProvider.cs b/src/Polly.Core/Registry/ResilienceStrategyProvider.cs index d18bef44760..efb94db849f 100644 --- a/src/Polly.Core/Registry/ResilienceStrategyProvider.cs +++ b/src/Polly.Core/Registry/ResilienceStrategyProvider.cs @@ -17,9 +17,9 @@ public abstract class ResilienceStrategyProvider /// The key used to identify the resilience strategy. /// The resilience strategy associated with the specified key. /// Thrown when no resilience strategy is found for the specified key. - public virtual ResilienceStrategy Get(TKey key) + public virtual ResilienceStrategy GetStrategy(TKey key) { - if (TryGet(key, out var strategy)) + if (TryGetStrategy(key, out var strategy)) { return strategy; } @@ -35,9 +35,9 @@ public virtual ResilienceStrategy Get(TKey key) /// The key used to identify the resilience strategy. /// The resilience strategy associated with the specified key. /// Thrown when no resilience strategy is found for the specified key. - public virtual ResilienceStrategy Get(TKey key) + public virtual ResilienceStrategy GetStrategy(TKey key) { - if (TryGet(key, out var strategy)) + if (TryGetStrategy(key, out var strategy)) { return strategy; } @@ -52,7 +52,7 @@ public virtual ResilienceStrategy Get(TKey key) /// The key used to identify the resilience strategy. /// The output resilience strategy if found, otherwise. /// if the strategy was found, otherwise. - public abstract bool TryGet(TKey key, [NotNullWhen(true)] out ResilienceStrategy? strategy); + public abstract bool TryGetStrategy(TKey key, [NotNullWhen(true)] out ResilienceStrategy? strategy); /// /// Tries to get a generic resilience strategy from the provider using the specified key. @@ -61,5 +61,5 @@ public virtual ResilienceStrategy Get(TKey key) /// The key used to identify the resilience strategy. /// The output resilience strategy if found, otherwise. /// if the strategy was found, otherwise. - public abstract bool TryGet(TKey key, [NotNullWhen(true)] out ResilienceStrategy? strategy); + public abstract bool TryGetStrategy(TKey key, [NotNullWhen(true)] out ResilienceStrategy? strategy); } diff --git a/src/Polly.Core/Registry/ResilienceStrategyRegistry.cs b/src/Polly.Core/Registry/ResilienceStrategyRegistry.cs index 2e3e420694d..df58391bb7a 100644 --- a/src/Polly.Core/Registry/ResilienceStrategyRegistry.cs +++ b/src/Polly.Core/Registry/ResilienceStrategyRegistry.cs @@ -65,7 +65,7 @@ public ResilienceStrategyRegistry(ResilienceStrategyRegistryOptions option /// The resilience strategy instance. /// if the strategy was added successfully, otherwise. /// Thrown when is . - public bool TryAdd(TKey key, ResilienceStrategy strategy) + public bool TryAddStrategy(TKey key, ResilienceStrategy strategy) { Guard.NotNull(strategy); @@ -80,7 +80,7 @@ public bool TryAdd(TKey key, ResilienceStrategy strategy) /// The resilience strategy instance. /// if the strategy was added successfully, otherwise. /// Thrown when is . - public bool TryAdd(TKey key, ResilienceStrategy strategy) + public bool TryAddStrategy(TKey key, ResilienceStrategy strategy) { Guard.NotNull(strategy); @@ -92,7 +92,7 @@ public bool TryAdd(TKey key, ResilienceStrategy strategy) /// /// The key used to identify the resilience strategy. /// if the strategy was removed successfully, otherwise. - public bool Remove(TKey key) => _strategies.TryRemove(key, out _); + public bool RemoveStrategy(TKey key) => _strategies.TryRemove(key, out _); /// /// Removes a generic resilience strategy from the registry. @@ -100,16 +100,16 @@ public bool TryAdd(TKey key, ResilienceStrategy strategy) /// The type of result that the resilience strategy handles. /// The key used to identify the resilience strategy. /// if the strategy was removed successfully, otherwise. - public bool Remove(TKey key) => GetGenericRegistry().Remove(key); + public bool RemoveStrategy(TKey key) => GetGenericRegistry().Remove(key); /// - public override bool TryGet(TKey key, [NotNullWhen(true)] out ResilienceStrategy? strategy) + public override bool TryGetStrategy(TKey key, [NotNullWhen(true)] out ResilienceStrategy? strategy) { return GetGenericRegistry().TryGet(key, out strategy); } /// - public override bool TryGet(TKey key, [NotNullWhen(true)] out ResilienceStrategy? strategy) + public override bool TryGetStrategy(TKey key, [NotNullWhen(true)] out ResilienceStrategy? strategy) { if (_strategies.TryGetValue(key, out strategy)) { @@ -192,7 +192,7 @@ public bool TryAddBuilder(TKey key, Action /// This method only clears the cached strategies, the registered builders are kept unchanged. /// - public void Clear() => _strategies.Clear(); + public void ClearStrategies() => _strategies.Clear(); /// /// Clears all cached generic strategies. @@ -201,7 +201,7 @@ public bool TryAddBuilder(TKey key, Action /// This method only clears the cached strategies, the registered builders are kept unchanged. /// - public void Clear() => GetGenericRegistry().Clear(); + public void ClearStrategies() => GetGenericRegistry().Clear(); private static ResilienceStrategy CreateStrategy( Func activator, diff --git a/src/Polly.Core/ResilienceStrategyBuilderBase.cs b/src/Polly.Core/ResilienceStrategyBuilderBase.cs index ed6e17f5c54..fc3fba5f79a 100644 --- a/src/Polly.Core/ResilienceStrategyBuilderBase.cs +++ b/src/Polly.Core/ResilienceStrategyBuilderBase.cs @@ -72,15 +72,7 @@ private protected ResilienceStrategyBuilderBase(ResilienceStrategyBuilderBase ot internal abstract bool IsGenericBuilder { get; } - /// - /// Adds a strategy to the builder. - /// - /// The factory that creates a resilience strategy. - /// The options associated with the strategy. If none are provided the default instance of is created. - /// Thrown when is null. - /// Thrown when this builder was already used to create a strategy. The builder cannot be modified after it has been used. - /// Thrown when the are invalid. - public void AddStrategy(Func factory, ResilienceStrategyOptions options) + internal void AddStrategyCore(Func factory, ResilienceStrategyOptions options) { Guard.NotNull(factory); Guard.NotNull(options); diff --git a/src/Polly.Core/ResilienceStrategyBuilderExtensions.cs b/src/Polly.Core/ResilienceStrategyBuilderExtensions.cs index 03f37e77fd0..ff8e9cdeef3 100644 --- a/src/Polly.Core/ResilienceStrategyBuilderExtensions.cs +++ b/src/Polly.Core/ResilienceStrategyBuilderExtensions.cs @@ -1,3 +1,5 @@ +using System.ComponentModel.DataAnnotations; + namespace Polly; /// @@ -20,7 +22,28 @@ public static TBuilder AddStrategy(this TBuilder builder, ResilienceSt Guard.NotNull(builder); Guard.NotNull(strategy); - builder.AddStrategy(_ => strategy, EmptyOptions.Instance); + return builder.AddStrategy(_ => strategy, EmptyOptions.Instance); + } + + /// + /// Adds a strategy to the builder. + /// + /// The builder type. + /// The builder instance. + /// The factory that creates a resilience strategy. + /// The options associated with the strategy. If none are provided the default instance of is created. + /// The same builder instance. + /// Thrown when , or is . + /// Thrown when this builder was already used to create a strategy. The builder cannot be modified after it has been used. + /// Thrown when is invalid. + public static TBuilder AddStrategy(this TBuilder builder, Func factory, ResilienceStrategyOptions options) + where TBuilder : ResilienceStrategyBuilderBase + { + Guard.NotNull(builder); + Guard.NotNull(factory); + Guard.NotNull(options); + + builder.AddStrategyCore(factory, options); return builder; } diff --git a/src/Polly.Core/Retry/RetryResilienceStrategyBuilderExtensions.cs b/src/Polly.Core/Retry/RetryResilienceStrategyBuilderExtensions.cs index e47ce44bfe1..f4b16215ec5 100644 --- a/src/Polly.Core/Retry/RetryResilienceStrategyBuilderExtensions.cs +++ b/src/Polly.Core/Retry/RetryResilienceStrategyBuilderExtensions.cs @@ -78,7 +78,7 @@ public static ResilienceStrategyBuilder AddRetry(this Resilien private static TBuilder AddRetryCore(this TBuilder builder, RetryStrategyOptions options) where TBuilder : ResilienceStrategyBuilderBase { - builder.AddStrategy(context => + return builder.AddStrategy(context => new RetryResilienceStrategy( options.BaseDelay, options.BackoffType, @@ -90,8 +90,6 @@ private static TBuilder AddRetryCore(this TBuilder builder, R context.Telemetry, RandomUtil.Instance), options); - - return builder; } private static void ConfigureShouldRetry(Action> shouldRetry, RetryStrategyOptions options) diff --git a/src/Polly.Core/Retry/RetryStrategyOptions.TResult.cs b/src/Polly.Core/Retry/RetryStrategyOptions.TResult.cs index 3555cfc3513..9aa3b8981b2 100644 --- a/src/Polly.Core/Retry/RetryStrategyOptions.TResult.cs +++ b/src/Polly.Core/Retry/RetryStrategyOptions.TResult.cs @@ -27,6 +27,9 @@ public class RetryStrategyOptions : ResilienceStrategyOptions /// Gets or sets the type of the back-off. /// /// + /// + /// This property is ignored when is set. + /// /// Defaults to . /// public RetryBackoffType BackoffType { get; set; } = RetryConstants.DefaultBackoffType; @@ -51,6 +54,9 @@ public class RetryStrategyOptions : ResilienceStrategyOptions /// /// /// + /// This property is ignored when is set. + /// + /// /// Defaults to 2 seconds. /// /// @@ -71,6 +77,9 @@ public class RetryStrategyOptions : ResilienceStrategyOptions /// Gets or sets the generator instance that is used to calculate the time between retries. /// /// + /// + /// The generator has precedence over and . + /// /// Defaults to . /// public Func, ValueTask>? RetryDelayGenerator { get; set; } diff --git a/src/Polly.Extensions/DependencyInjection/PollyServiceCollectionExtensions.cs b/src/Polly.Extensions/DependencyInjection/PollyServiceCollectionExtensions.cs index 0f4c577357b..dcb8648a63a 100644 --- a/src/Polly.Extensions/DependencyInjection/PollyServiceCollectionExtensions.cs +++ b/src/Polly.Extensions/DependencyInjection/PollyServiceCollectionExtensions.cs @@ -219,7 +219,7 @@ private static void AddResilienceStrategyBuilder(this IServiceCollection service { var builder = new ResilienceStrategyBuilder(); builder.Properties.Set(PollyDependencyInjectionKeys.ServiceProvider, serviceProvider); - builder.EnableTelemetry(serviceProvider.GetRequiredService>().Value); + builder.ConfigureTelemetry(serviceProvider.GetRequiredService>().Value); return builder; }); } diff --git a/src/Polly.Extensions/Telemetry/TelemetryResilienceStrategyBuilderExtensions.cs b/src/Polly.Extensions/Telemetry/TelemetryResilienceStrategyBuilderExtensions.cs index 58a1a9c9660..b79940b6342 100644 --- a/src/Polly.Extensions/Telemetry/TelemetryResilienceStrategyBuilderExtensions.cs +++ b/src/Polly.Extensions/Telemetry/TelemetryResilienceStrategyBuilderExtensions.cs @@ -22,13 +22,13 @@ public static class TelemetryResilienceStrategyBuilderExtensions /// Additionally, the telemetry strategy that logs and meters the executions is added to the beginning of the strategy pipeline. /// /// Thrown when or is . - public static TBuilder EnableTelemetry(this TBuilder builder, ILoggerFactory loggerFactory) + public static TBuilder ConfigureTelemetry(this TBuilder builder, ILoggerFactory loggerFactory) where TBuilder : ResilienceStrategyBuilderBase { Guard.NotNull(builder); Guard.NotNull(loggerFactory); - return builder.EnableTelemetry(new TelemetryOptions { LoggerFactory = loggerFactory }); + return builder.ConfigureTelemetry(new TelemetryOptions { LoggerFactory = loggerFactory }); } /// @@ -43,7 +43,7 @@ public static TBuilder EnableTelemetry(this TBuilder builder, ILoggerF /// Additionally, the telemetry strategy that logs and meters the executions is added to the beginning of the strategy pipeline. /// /// Thrown when or is . - public static TBuilder EnableTelemetry(this TBuilder builder, TelemetryOptions options) + public static TBuilder ConfigureTelemetry(this TBuilder builder, TelemetryOptions options) where TBuilder : ResilienceStrategyBuilderBase { Guard.NotNull(builder); diff --git a/src/Polly.RateLimiting/RateLimiterResilienceStrategyBuilderExtensions.cs b/src/Polly.RateLimiting/RateLimiterResilienceStrategyBuilderExtensions.cs index 94d79d5e1f4..78cf683715f 100644 --- a/src/Polly.RateLimiting/RateLimiterResilienceStrategyBuilderExtensions.cs +++ b/src/Polly.RateLimiting/RateLimiterResilienceStrategyBuilderExtensions.cs @@ -101,7 +101,7 @@ public static TBuilder AddRateLimiter( Guard.NotNull(builder); Guard.NotNull(options); - builder.AddStrategy( + return builder.AddStrategy( context => { return new RateLimiterResilienceStrategy( @@ -110,6 +110,5 @@ public static TBuilder AddRateLimiter( context.Telemetry); }, options); - return builder; } } diff --git a/test/Polly.Core.Tests/Registry/ResilienceStrategyProviderTests.cs b/test/Polly.Core.Tests/Registry/ResilienceStrategyProviderTests.cs index 84fb3800ed0..74525e49288 100644 --- a/test/Polly.Core.Tests/Registry/ResilienceStrategyProviderTests.cs +++ b/test/Polly.Core.Tests/Registry/ResilienceStrategyProviderTests.cs @@ -9,7 +9,7 @@ public class ResilienceStrategyProviderTests public void Get_DoesNotExist_Throws() { new Provider() - .Invoking(o => o.Get("not-exists")) + .Invoking(o => o.GetStrategy("not-exists")) .Should() .Throw() .WithMessage("Unable to find a resilience strategy associated with the key 'not-exists'. Please ensure that either the resilience strategy or the builder is registered."); @@ -19,7 +19,7 @@ public void Get_DoesNotExist_Throws() public void Get_GenericDoesNotExist_Throws() { new Provider() - .Invoking(o => o.Get("not-exists")) + .Invoking(o => o.GetStrategy("not-exists")) .Should() .Throw() .WithMessage("Unable to find a generic resilience strategy of 'String' associated with the key 'not-exists'. " + @@ -31,7 +31,7 @@ public void Get_Exist_Ok() { var provider = new Provider { Strategy = new TestResilienceStrategy() }; - provider.Get("exists").Should().Be(provider.Strategy); + provider.GetStrategy("exists").Should().Be(provider.Strategy); } [Fact] @@ -39,7 +39,7 @@ public void Get_GenericExist_Ok() { var provider = new Provider { GenericStrategy = new TestResilienceStrategy() }; - provider.Get("exists").Should().Be(provider.GenericStrategy); + provider.GetStrategy("exists").Should().Be(provider.GenericStrategy); } private class Provider : ResilienceStrategyProvider @@ -48,13 +48,13 @@ private class Provider : ResilienceStrategyProvider public object? GenericStrategy { get; set; } - public override bool TryGet(string key, [NotNullWhen(true)] out ResilienceStrategy? strategy) + public override bool TryGetStrategy(string key, [NotNullWhen(true)] out ResilienceStrategy? strategy) { strategy = Strategy; return Strategy != null; } - public override bool TryGet(string key, [NotNullWhen(true)] out ResilienceStrategy? strategy) + public override bool TryGetStrategy(string key, [NotNullWhen(true)] out ResilienceStrategy? strategy) { strategy = (ResilienceStrategy?)GenericStrategy; return GenericStrategy != null; diff --git a/test/Polly.Core.Tests/Registry/ResilienceStrategyRegistryTests.cs b/test/Polly.Core.Tests/Registry/ResilienceStrategyRegistryTests.cs index 22d111065f1..0f141b11d50 100644 --- a/test/Polly.Core.Tests/Registry/ResilienceStrategyRegistryTests.cs +++ b/test/Polly.Core.Tests/Registry/ResilienceStrategyRegistryTests.cs @@ -45,15 +45,15 @@ public void Clear_Ok() registry.TryAddBuilder("C", (b, _) => b.AddStrategy(new TestResilienceStrategy())); - registry.TryAdd("A", new TestResilienceStrategy()); - registry.TryAdd("B", new TestResilienceStrategy()); - registry.TryAdd("C", new TestResilienceStrategy()); + registry.TryAddStrategy("A", new TestResilienceStrategy()); + registry.TryAddStrategy("B", new TestResilienceStrategy()); + registry.TryAddStrategy("C", new TestResilienceStrategy()); - registry.Clear(); + registry.ClearStrategies(); - registry.TryGet("A", out _).Should().BeFalse(); - registry.TryGet("B", out _).Should().BeFalse(); - registry.TryGet("C", out _).Should().BeTrue(); + registry.TryGetStrategy("A", out _).Should().BeFalse(); + registry.TryGetStrategy("B", out _).Should().BeFalse(); + registry.TryGetStrategy("C", out _).Should().BeTrue(); } [Fact] @@ -63,15 +63,15 @@ public void Clear_Generic_Ok() registry.TryAddBuilder("C", (b, _) => b.AddStrategy(new TestResilienceStrategy())); - registry.TryAdd("A", new TestResilienceStrategy()); - registry.TryAdd("B", new TestResilienceStrategy()); - registry.TryAdd("C", new TestResilienceStrategy()); + registry.TryAddStrategy("A", new TestResilienceStrategy()); + registry.TryAddStrategy("B", new TestResilienceStrategy()); + registry.TryAddStrategy("C", new TestResilienceStrategy()); - registry.Clear(); + registry.ClearStrategies(); - registry.TryGet("A", out _).Should().BeFalse(); - registry.TryGet("B", out _).Should().BeFalse(); - registry.TryGet("C", out _).Should().BeTrue(); + registry.TryGetStrategy("A", out _).Should().BeFalse(); + registry.TryGetStrategy("B", out _).Should().BeFalse(); + registry.TryGetStrategy("C", out _).Should().BeTrue(); } [Fact] @@ -79,14 +79,14 @@ public void Remove_Ok() { var registry = new ResilienceStrategyRegistry(); - registry.TryAdd("A", new TestResilienceStrategy()); - registry.TryAdd("B", new TestResilienceStrategy()); + registry.TryAddStrategy("A", new TestResilienceStrategy()); + registry.TryAddStrategy("B", new TestResilienceStrategy()); - registry.Remove("A").Should().BeTrue(); - registry.Remove("A").Should().BeFalse(); + registry.RemoveStrategy("A").Should().BeTrue(); + registry.RemoveStrategy("A").Should().BeFalse(); - registry.TryGet("A", out _).Should().BeFalse(); - registry.TryGet("B", out _).Should().BeTrue(); + registry.TryGetStrategy("A", out _).Should().BeFalse(); + registry.TryGetStrategy("B", out _).Should().BeTrue(); } [Fact] @@ -94,14 +94,14 @@ public void Remove_Generic_Ok() { var registry = new ResilienceStrategyRegistry(); - registry.TryAdd("A", new TestResilienceStrategy()); - registry.TryAdd("B", new TestResilienceStrategy()); + registry.TryAddStrategy("A", new TestResilienceStrategy()); + registry.TryAddStrategy("B", new TestResilienceStrategy()); - registry.Remove("A").Should().BeTrue(); - registry.Remove("A").Should().BeFalse(); + registry.RemoveStrategy("A").Should().BeTrue(); + registry.RemoveStrategy("A").Should().BeFalse(); - registry.TryGet("A", out _).Should().BeFalse(); - registry.TryGet("B", out _).Should().BeTrue(); + registry.TryGetStrategy("A", out _).Should().BeFalse(); + registry.TryGetStrategy("B", out _).Should().BeTrue(); } [Fact] @@ -113,7 +113,7 @@ public void RemoveBuilder_Ok() registry.RemoveBuilder("A").Should().BeTrue(); registry.RemoveBuilder("A").Should().BeFalse(); - registry.TryGet("A", out _).Should().BeFalse(); + registry.TryGetStrategy("A", out _).Should().BeFalse(); } [Fact] @@ -125,7 +125,7 @@ public void RemoveBuilder_Generic_Ok() registry.RemoveBuilder("A").Should().BeTrue(); registry.RemoveBuilder("A").Should().BeFalse(); - registry.TryGet("A", out _).Should().BeFalse(); + registry.TryGetStrategy("A", out _).Should().BeFalse(); } [Fact] @@ -140,10 +140,10 @@ public void GetStrategy_BuilderMultiInstance_EnsureMultipleInstances() { var key = StrategyId.Create(builderName, i.ToString(CultureInfo.InvariantCulture)); - strategies.Add(registry.Get(key)); + strategies.Add(registry.GetStrategy(key)); // call again, the strategy should be already cached - strategies.Add(registry.Get(key)); + strategies.Add(registry.GetStrategy(key)); } strategies.Should().HaveCount(100); @@ -161,10 +161,10 @@ public void GetStrategy_GenericBuilderMultiInstance_EnsureMultipleInstances() { var key = StrategyId.Create(builderName, i.ToString(CultureInfo.InvariantCulture)); - strategies.Add(registry.Get(key)); + strategies.Add(registry.GetStrategy(key)); // call again, the strategy should be already cached - strategies.Add(registry.Get(key)); + strategies.Add(registry.GetStrategy(key)); } strategies.Should().HaveCount(100); @@ -188,10 +188,10 @@ public void AddBuilder_GetStrategy_EnsureCalled() var key2 = StrategyId.Create("A", "Instance1"); var key3 = StrategyId.Create("A", "Instance2"); var keys = new[] { key1, key2, key3 }; - var strategies = keys.ToDictionary(k => k, registry.Get); + var strategies = keys.ToDictionary(k => k, registry.GetStrategy); foreach (var key in keys) { - registry.Get(key); + registry.GetStrategy(key); } called.Should().Be(3); @@ -217,10 +217,10 @@ public void AddBuilder_GenericGetStrategy_EnsureCalled() var key2 = StrategyId.Create("A", "Instance1"); var key3 = StrategyId.Create("A", "Instance2"); var keys = new[] { key1, key2, key3 }; - var strategies = keys.ToDictionary(k => k, registry.Get); + var strategies = keys.ToDictionary(k => k, registry.GetStrategy); foreach (var key in keys) { - registry.Get(key); + registry.GetStrategy(key); } called.Should().Be(3); @@ -247,7 +247,7 @@ public void AddBuilder_EnsureStrategyKey() called = true; }); - registry.Get(StrategyId.Create("A", "Instance1")); + registry.GetStrategy(StrategyId.Create("A", "Instance1")); called.Should().BeTrue(); } @@ -258,8 +258,8 @@ public void AddBuilder_MultipleGeneric_EnsureDistinctInstances() registry.TryAddBuilder(StrategyId.Create("A"), (builder, _) => builder.AddStrategy(new TestResilienceStrategy())); registry.TryAddBuilder(StrategyId.Create("A"), (builder, _) => builder.AddStrategy(new TestResilienceStrategy())); - registry.Get(StrategyId.Create("A", "Instance1")).Should().BeSameAs(registry.Get(StrategyId.Create("A", "Instance1"))); - registry.Get(StrategyId.Create("A", "Instance1")).Should().BeSameAs(registry.Get(StrategyId.Create("A", "Instance1"))); + registry.GetStrategy(StrategyId.Create("A", "Instance1")).Should().BeSameAs(registry.GetStrategy(StrategyId.Create("A", "Instance1"))); + registry.GetStrategy(StrategyId.Create("A", "Instance1")).Should().BeSameAs(registry.GetStrategy(StrategyId.Create("A", "Instance1"))); } [Fact] @@ -279,7 +279,7 @@ public void AddBuilder_Generic_EnsureStrategyKey() called = true; }); - registry.Get(StrategyId.Create("A", "Instance1")); + registry.GetStrategy(StrategyId.Create("A", "Instance1")); called.Should().BeTrue(); } @@ -289,7 +289,7 @@ public void TryGet_NoBuilder_Null() var registry = CreateRegistry(); var key = StrategyId.Create("A"); - registry.TryGet(key, out var strategy).Should().BeFalse(); + registry.TryGetStrategy(key, out var strategy).Should().BeFalse(); strategy.Should().BeNull(); } @@ -299,7 +299,7 @@ public void TryGet_GenericNoBuilder_Null() var registry = CreateRegistry(); var key = StrategyId.Create("A"); - registry.TryGet(key, out var strategy).Should().BeFalse(); + registry.TryGetStrategy(key, out var strategy).Should().BeFalse(); strategy.Should().BeNull(); } @@ -309,9 +309,9 @@ public void TryGet_ExplicitStrategyAdded_Ok() var expectedStrategy = new TestResilienceStrategy(); var registry = CreateRegistry(); var key = StrategyId.Create("A", "Instance"); - registry.TryAdd(key, expectedStrategy).Should().BeTrue(); + registry.TryAddStrategy(key, expectedStrategy).Should().BeTrue(); - registry.TryGet(key, out var strategy).Should().BeTrue(); + registry.TryGetStrategy(key, out var strategy).Should().BeTrue(); strategy.Should().BeSameAs(expectedStrategy); } @@ -322,9 +322,9 @@ public void TryGet_GenericExplicitStrategyAdded_Ok() var expectedStrategy = new TestResilienceStrategy(); var registry = CreateRegistry(); var key = StrategyId.Create("A", "Instance"); - registry.TryAdd(key, expectedStrategy).Should().BeTrue(); + registry.TryAddStrategy(key, expectedStrategy).Should().BeTrue(); - registry.TryGet(key, out var strategy).Should().BeTrue(); + registry.TryGetStrategy(key, out var strategy).Should().BeTrue(); strategy.Should().BeSameAs(expectedStrategy); } @@ -335,11 +335,11 @@ public void TryAdd_Twice_SecondNotAdded() var expectedStrategy = new TestResilienceStrategy(); var registry = CreateRegistry(); var key = StrategyId.Create("A", "Instance"); - registry.TryAdd(key, expectedStrategy).Should().BeTrue(); + registry.TryAddStrategy(key, expectedStrategy).Should().BeTrue(); - registry.TryAdd(key, new TestResilienceStrategy()).Should().BeFalse(); + registry.TryAddStrategy(key, new TestResilienceStrategy()).Should().BeFalse(); - registry.TryGet(key, out var strategy).Should().BeTrue(); + registry.TryGetStrategy(key, out var strategy).Should().BeTrue(); strategy.Should().BeSameAs(expectedStrategy); } @@ -349,11 +349,11 @@ public void TryAdd_GenericTwice_SecondNotAdded() var expectedStrategy = new TestResilienceStrategy(); var registry = CreateRegistry(); var key = StrategyId.Create("A", "Instance"); - registry.TryAdd(key, expectedStrategy).Should().BeTrue(); + registry.TryAddStrategy(key, expectedStrategy).Should().BeTrue(); - registry.TryAdd(key, new TestResilienceStrategy()).Should().BeFalse(); + registry.TryAddStrategy(key, new TestResilienceStrategy()).Should().BeFalse(); - registry.TryGet(key, out var strategy).Should().BeTrue(); + registry.TryGetStrategy(key, out var strategy).Should().BeTrue(); strategy.Should().BeSameAs(expectedStrategy); } @@ -379,7 +379,7 @@ public void EnableReloads_Ok() }); // act - var strategy = registry.Get("dummy"); + var strategy = registry.GetStrategy("dummy"); // assert var tries = 0; @@ -415,7 +415,7 @@ public void EnableReloads_Generic_Ok() }); // act - var strategy = registry.Get("dummy"); + var strategy = registry.GetStrategy("dummy"); // assert var tries = 0; diff --git a/test/Polly.Extensions.Tests/DependencyInjection/PollyServiceCollectionExtensionTests.cs b/test/Polly.Extensions.Tests/DependencyInjection/PollyServiceCollectionExtensionTests.cs index e21ccb3fe25..b63246bb470 100644 --- a/test/Polly.Extensions.Tests/DependencyInjection/PollyServiceCollectionExtensionTests.cs +++ b/test/Polly.Extensions.Tests/DependencyInjection/PollyServiceCollectionExtensionTests.cs @@ -75,13 +75,13 @@ public void AddResilienceStrategy_MultipleRegistries_Ok() var serviceProvider = _services.BuildServiceProvider(); - serviceProvider.GetRequiredService>().Get(Key).Should().NotBeNull(); - serviceProvider.GetRequiredService>().Get(Key).Should().NotBeNull(); - serviceProvider.GetRequiredService>().Get(Key).Should().NotBeNull(); + serviceProvider.GetRequiredService>().GetStrategy(Key).Should().NotBeNull(); + serviceProvider.GetRequiredService>().GetStrategy(Key).Should().NotBeNull(); + serviceProvider.GetRequiredService>().GetStrategy(Key).Should().NotBeNull(); - serviceProvider.GetRequiredService>().Get(10).Should().NotBeNull(); - serviceProvider.GetRequiredService>().Get(10).Should().NotBeNull(); - serviceProvider.GetRequiredService>().Get(10).Should().NotBeNull(); + serviceProvider.GetRequiredService>().GetStrategy(10).Should().NotBeNull(); + serviceProvider.GetRequiredService>().GetStrategy(10).Should().NotBeNull(); + serviceProvider.GetRequiredService>().GetStrategy(10).Should().NotBeNull(); } [InlineData(true)] @@ -103,7 +103,7 @@ public void AddResilienceStrategy_EnsureContextFilled(bool generic) asserted = true; }); - CreateProvider().Get(Key); + CreateProvider().GetStrategy(Key); } else { @@ -116,7 +116,7 @@ public void AddResilienceStrategy_EnsureContextFilled(bool generic) asserted = true; }); - CreateProvider().Get(Key); + CreateProvider().GetStrategy(Key); } asserted.Should().BeTrue(); @@ -142,7 +142,7 @@ public void AddResilienceStrategy_EnsureTelemetryEnabled(bool hasLogging) }, new TestResilienceStrategyOptions())); - CreateProvider().Get(Key); + CreateProvider().GetStrategy(Key); var diagSource = telemetry!.GetType().GetProperty("DiagnosticSource", BindingFlags.Instance | BindingFlags.NonPublic)!.GetValue(telemetry); diagSource.Should().BeOfType(); @@ -171,7 +171,7 @@ public void AddResilienceStrategy_EnsureResilienceStrategyBuilderResolvedCorrect asserted = true; }); - CreateProvider().Get(Key); + CreateProvider().GetStrategy(Key); asserted.Should().BeTrue(); } @@ -194,8 +194,8 @@ public void AddResilienceStrategy_Single_Ok() var provider = CreateProvider(); - var strategy = provider.Get(Key); - provider.Get("my-strategy").Should().BeSameAs(provider.Get("my-strategy")); + var strategy = provider.GetStrategy(Key); + provider.GetStrategy("my-strategy").Should().BeSameAs(provider.GetStrategy("my-strategy")); } [InlineData(true)] @@ -210,13 +210,13 @@ public void AddResilienceStrategy_Twice_LastOneWins(bool generic) { AddResilienceStrategy(Key, _ => firstCalled = true); AddResilienceStrategy(Key, _ => secondCalled = true); - CreateProvider().Get(Key); + CreateProvider().GetStrategy(Key); } else { AddResilienceStrategy(Key, _ => firstCalled = true); AddResilienceStrategy(Key, _ => secondCalled = true); - CreateProvider().Get(Key); + CreateProvider().GetStrategy(Key); } firstCalled.Should().BeFalse(); @@ -243,9 +243,9 @@ public void AddResilienceStrategy_Multiple_Ok() return new object[] { - provider.Get(name), - provider.Get(name), - provider.Get(name) + provider.GetStrategy(name), + provider.GetStrategy(name), + provider.GetStrategy(name) }; }) .Distinct() diff --git a/test/Polly.Extensions.Tests/Issues/IssuesTests.OnCircuitBreakWithServiceProvider_796.cs b/test/Polly.Extensions.Tests/Issues/IssuesTests.OnCircuitBreakWithServiceProvider_796.cs index b8ca882a11f..e28042fa50e 100644 --- a/test/Polly.Extensions.Tests/Issues/IssuesTests.OnCircuitBreakWithServiceProvider_796.cs +++ b/test/Polly.Extensions.Tests/Issues/IssuesTests.OnCircuitBreakWithServiceProvider_796.cs @@ -39,7 +39,7 @@ public async Task OnCircuitBreakWithServiceProvider_796() // retrieve the provider var strategyProvider = serviceCollection.BuildServiceProvider().GetRequiredService>(); - var strategy = strategyProvider.Get("my-strategy"); + var strategy = strategyProvider.GetStrategy("my-strategy"); // now trigger the circuit breaker by evaluating multiple result types for (int i = 0; i < 10; i++) diff --git a/test/Polly.Extensions.Tests/Issues/IssuesTests.OverrideLibraryStrategies_1072.cs b/test/Polly.Extensions.Tests/Issues/IssuesTests.OverrideLibraryStrategies_1072.cs index d7913f161ee..01d9fb5c2e8 100644 --- a/test/Polly.Extensions.Tests/Issues/IssuesTests.OverrideLibraryStrategies_1072.cs +++ b/test/Polly.Extensions.Tests/Issues/IssuesTests.OverrideLibraryStrategies_1072.cs @@ -78,7 +78,7 @@ public class LibraryApi { private readonly ResilienceStrategy _strategy; - public LibraryApi(ResilienceStrategyProvider provider) => _strategy = provider.Get("library-strategy"); + public LibraryApi(ResilienceStrategyProvider provider) => _strategy = provider.GetStrategy("library-strategy"); public void ExecuteLibrary(Action execute) => _strategy.Execute(execute); } diff --git a/test/Polly.Extensions.Tests/ReloadableResilienceStrategyTests.cs b/test/Polly.Extensions.Tests/ReloadableResilienceStrategyTests.cs index 21f8cb0f1e4..2d477ec4b19 100644 --- a/test/Polly.Extensions.Tests/ReloadableResilienceStrategyTests.cs +++ b/test/Polly.Extensions.Tests/ReloadableResilienceStrategyTests.cs @@ -41,7 +41,7 @@ public void AddResilienceStrategy_EnsureReloadable(string? name) }); var serviceProvider = services.BuildServiceProvider(); - var strategy = serviceProvider.GetRequiredService>().Get("my-strategy"); + var strategy = serviceProvider.GetRequiredService>().GetStrategy("my-strategy"); var context = ResilienceContext.Get(); var registry = serviceProvider.GetRequiredService>(); diff --git a/test/Polly.Extensions.Tests/Telemetry/TelemetryResilienceStrategyBuilderExtensionsTests.cs b/test/Polly.Extensions.Tests/Telemetry/TelemetryResilienceStrategyBuilderExtensionsTests.cs index 600da828fe6..5cec61ad7bd 100644 --- a/test/Polly.Extensions.Tests/Telemetry/TelemetryResilienceStrategyBuilderExtensionsTests.cs +++ b/test/Polly.Extensions.Tests/Telemetry/TelemetryResilienceStrategyBuilderExtensionsTests.cs @@ -12,16 +12,16 @@ public class TelemetryResilienceStrategyBuilderExtensionsTests [InlineData(true)] [InlineData(false)] [Theory] - public void EnableTelemetry_EnsureDiagnosticSourceUpdated(bool generic) + public void ConfigureTelemetry_EnsureDiagnosticSourceUpdated(bool generic) { if (generic) { - _genericBuilder.EnableTelemetry(NullLoggerFactory.Instance); + _genericBuilder.ConfigureTelemetry(NullLoggerFactory.Instance); _genericBuilder.DiagnosticSource.Should().BeOfType(); } else { - _builder.EnableTelemetry(NullLoggerFactory.Instance); + _builder.ConfigureTelemetry(NullLoggerFactory.Instance); _builder.DiagnosticSource.Should().BeOfType(); _builder.AddStrategy(new TestResilienceStrategy()).Build().Should().NotBeOfType(); } @@ -30,18 +30,18 @@ public void EnableTelemetry_EnsureDiagnosticSourceUpdated(bool generic) [InlineData(true)] [InlineData(false)] [Theory] - public void EnableTelemetry_EnsureLogging(bool generic) + public void ConfigureTelemetry_EnsureLogging(bool generic) { using var factory = TestUtilities.CreateLoggerFactory(out var fakeLogger); if (generic) { - _genericBuilder.EnableTelemetry(factory); + _genericBuilder.ConfigureTelemetry(factory); _genericBuilder.AddStrategy(new TestResilienceStrategy()).Build().Execute(_ => string.Empty); } else { - _builder.EnableTelemetry(factory); + _builder.ConfigureTelemetry(factory); _builder.AddStrategy(new TestResilienceStrategy()).Build().Execute(_ => { }); } @@ -50,10 +50,10 @@ public void EnableTelemetry_EnsureLogging(bool generic) } [Fact] - public void EnableTelemetry_InvalidOptions_Throws() + public void ConfigureTelemetry_InvalidOptions_Throws() { _builder - .Invoking(b => b.EnableTelemetry(new TelemetryOptions + .Invoking(b => b.ConfigureTelemetry(new TelemetryOptions { LoggerFactory = null!, })).Should() @@ -66,7 +66,7 @@ The LoggerFactory field is required. """); _genericBuilder - .Invoking(b => b.EnableTelemetry(new TelemetryOptions + .Invoking(b => b.ConfigureTelemetry(new TelemetryOptions { LoggerFactory = null!, })).Should()