-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
70 additions
and
0 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
src/Polly.Extensions.Tests/Issues/IssuesTests.OnCircuitBreakWithServiceProvider_796.cs
This file contains 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,70 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Polly.CircuitBreaker; | ||
using Polly.Extensions.DependencyInjection; | ||
using Polly.Registry; | ||
|
||
namespace Polly.Core.Tests.Issues; | ||
|
||
public class IssuesTests | ||
{ | ||
[Fact] | ||
public async Task OnCircuitBreakWithServiceProvider_796() | ||
{ | ||
var contextChecked = false; | ||
var options = new AdvancedCircuitBreakerStrategyOptions | ||
{ | ||
FailureThreshold = 1, | ||
MinimumThroughput = 10, | ||
}; | ||
|
||
options.OnOpened.Register(async (_, args) => | ||
{ | ||
args.Context.Properties.GetValue(PollyDependencyInjectionKeys.ServiceProvider, null!).Should().NotBeNull(); | ||
contextChecked = true; | ||
|
||
// do asynchronous call | ||
await Task.Yield(); | ||
}); | ||
|
||
// handle string results | ||
options.ShouldHandle.HandleResult("error"); | ||
|
||
// create the strategy | ||
var serviceCollection = new ServiceCollection().AddResilienceStrategy("my-strategy", context => | ||
{ | ||
context.Builder | ||
.AddStrategy(new ServiceProviderStrategy(context.ServiceProvider)) | ||
.AddAdvancedCircuitBreaker(options); | ||
}); | ||
|
||
// retrieve the provider | ||
var strategyProvider = serviceCollection.BuildServiceProvider().GetRequiredService<ResilienceStrategyProvider<string>>(); | ||
var strategy = strategyProvider.Get("my-strategy"); | ||
|
||
// now trigger the circuit breaker by evaluating multiple result types | ||
for (int i = 0; i < 10; i++) | ||
{ | ||
await strategy.ExecuteAsync(_ => Task.FromResult("error")); | ||
} | ||
|
||
// now the circuit breaker should be open | ||
await strategy.Invoking(s => s.ExecuteAsync(_ => Task.FromResult("valid-result"))).Should().ThrowAsync<BrokenCircuitException>(); | ||
|
||
// check that service provider was received in the context | ||
contextChecked.Should().BeTrue(); | ||
} | ||
|
||
private class ServiceProviderStrategy : ResilienceStrategy | ||
{ | ||
private readonly IServiceProvider _serviceProvider; | ||
|
||
public ServiceProviderStrategy(IServiceProvider serviceProvider) => _serviceProvider = serviceProvider; | ||
|
||
protected override ValueTask<TResult> ExecuteCoreAsync<TResult, TState>(Func<ResilienceContext, TState, ValueTask<TResult>> callback, ResilienceContext context, TState state) | ||
{ | ||
context.Properties.Set(PollyDependencyInjectionKeys.ServiceProvider, _serviceProvider); | ||
return callback(context, state); | ||
} | ||
} | ||
|
||
} |