-
-
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.
Add new issue test that demonstrates library scenario (#1322)
- Loading branch information
Showing
3 changed files
with
87 additions
and
2 deletions.
There are no files selected for viewing
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
85 changes: 85 additions & 0 deletions
85
test/Polly.Extensions.Tests/Issues/IssuesTests.OverrideLibraryStrategies_1072.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,85 @@ | ||
using System.Net.Sockets; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
using Polly.Registry; | ||
using Polly.Retry; | ||
|
||
namespace Polly.Extensions.Tests.Issues; | ||
|
||
public partial class IssuesTests | ||
{ | ||
[InlineData(true)] | ||
[InlineData(false)] | ||
[Theory] | ||
public void OverrideLibraryStrategies_898(bool overrideStrategy) | ||
{ | ||
// arrange | ||
var services = new ServiceCollection(); | ||
var failFirstCall = true; | ||
AddLibraryServices(services); | ||
|
||
if (overrideStrategy) | ||
{ | ||
// This call overrides the strategy that the library uses. The last call to AddResilienceStrategy wins. | ||
services.AddResilienceStrategy("library-strategy", builder => builder.AddRetry(new() | ||
{ | ||
ShouldHandle = args => args.Exception switch | ||
{ | ||
InvalidOperationException => PredicateResult.True, | ||
SocketException => PredicateResult.True, | ||
_ => PredicateResult.False | ||
}, | ||
BaseDelay = TimeSpan.Zero | ||
})); | ||
} | ||
|
||
var serviceProvider = services.BuildServiceProvider(); | ||
var api = serviceProvider.GetRequiredService<LibraryApi>(); | ||
|
||
// act && assert | ||
if (overrideStrategy) | ||
{ | ||
// The library now also handles SocketException. | ||
api.Invoking(a => a.ExecuteLibrary(UnstableCall)).Should().NotThrow(); | ||
|
||
} | ||
else | ||
{ | ||
// Originally, the library strategy only handled InvalidOperationException. | ||
api.Invoking(a => a.ExecuteLibrary(UnstableCall)).Should().Throw<SocketException>(); | ||
} | ||
|
||
void UnstableCall() | ||
{ | ||
if (failFirstCall) | ||
{ | ||
failFirstCall = false; | ||
|
||
// This exception was not originally handled by strategy. | ||
throw new SocketException(); | ||
} | ||
} | ||
} | ||
|
||
private static void AddLibraryServices(IServiceCollection services) | ||
{ | ||
services.TryAddSingleton<LibraryApi>(); | ||
services.AddResilienceStrategy("library-strategy", builder => builder.AddRetry(new() | ||
{ | ||
ShouldHandle = args => args.Exception switch | ||
{ | ||
InvalidOperationException => PredicateResult.True, | ||
_ => PredicateResult.False | ||
} | ||
})); | ||
} | ||
|
||
public class LibraryApi | ||
{ | ||
private readonly ResilienceStrategy _strategy; | ||
|
||
public LibraryApi(ResilienceStrategyProvider<string> provider) => _strategy = provider.Get("library-strategy"); | ||
|
||
public void ExecuteLibrary(Action execute) => _strategy.Execute(execute); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
namespace Polly.Core.Tests.Issues; | ||
namespace Polly.Extensions.Tests.Issues; | ||
|
||
public partial class IssuesTests | ||
{ | ||
|