Skip to content

Commit

Permalink
Add new issue test that demonstrates library scenario (#1322)
Browse files Browse the repository at this point in the history
  • Loading branch information
martintmk authored Jun 19, 2023
1 parent b73f456 commit 04bd9ec
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Polly.Extensions.DependencyInjection;
using Polly.Registry;

namespace Polly.Core.Tests.Issues;
namespace Polly.Extensions.Tests.Issues;

public partial class IssuesTests
{
Expand Down
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);
}
}
2 changes: 1 addition & 1 deletion test/Polly.Extensions.Tests/Issues/IssuesTests.cs
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
{
Expand Down

0 comments on commit 04bd9ec

Please sign in to comment.