Skip to content

Commit

Permalink
Introduce unit-tests for issues fixed in v8
Browse files Browse the repository at this point in the history
  • Loading branch information
martintmk committed Apr 25, 2023
1 parent a1ef1bb commit 04df38e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Polly.CircuitBreaker;

namespace Polly.Core.Tests.Issues;

public partial class IssuesTests
{
[Fact]
public void CircuitBreakerStateSharing_959()
{
var options = new AdvancedCircuitBreakerStrategyOptions
{
FailureThreshold = 1,
MinimumThroughput = 10
};

// handle int results
options.ShouldHandle.HandleResult(-1);

// handle string results
options.ShouldHandle.HandleResult("error");

// create the strategy
var strategy = new ResilienceStrategyBuilder { TimeProvider = TimeProvider.Object }.AddAdvancedCircuitBreaker(options).Build();

// now trigger the circuit breaker by evaluating multiple result types
for (int i = 0; i < 5; i++)
{
strategy.Execute(_ => -1);
strategy.Execute(_ => "error");
}

// now the circuit breaker should be open
strategy.Invoking(s => s.Execute(_ => 0)).Should().Throw<BrokenCircuitException>();
strategy.Invoking(s => s.Execute(_ => "valid-result")).Should().Throw<BrokenCircuitException>();

// now wait
TimeProvider.AdvanceTime(options.BreakDuration);

// OK, circuit is open now
strategy.Execute(_ => 0);
strategy.Execute(_ => "valid-result");
}
}
6 changes: 6 additions & 0 deletions src/Polly.Core.Tests/Issues/IssuesTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Polly.Core.Tests.Issues;

public partial class IssuesTests
{
private FakeTimeProvider TimeProvider { get; } = new FakeTimeProvider().SetupUtcNow();
}

0 comments on commit 04df38e

Please sign in to comment.