Skip to content

Commit

Permalink
Add integration test
Browse files Browse the repository at this point in the history
Add an integration test for when `BreakDurationGenerator` is specified to guard against the issue reported by #1850.
  • Loading branch information
martincostello committed Jan 4, 2024
1 parent 403c969 commit 9d7729d
Showing 1 changed file with 57 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,66 @@ public void AddCircuitBreaker_IntegrationTest()
int closed = 0;
int halfOpened = 0;

var breakDuration = TimeSpan.FromSeconds(1);

var options = new CircuitBreakerStrategyOptions
{
FailureRatio = 0.5,
MinimumThroughput = 10,
SamplingDuration = TimeSpan.FromSeconds(10),
BreakDuration = breakDuration,
ShouldHandle = args => new ValueTask<bool>(args.Outcome.Result is -1),
OnOpened = _ => { opened++; return default; },
OnClosed = _ => { closed++; return default; },
OnHalfOpened = (_) => { halfOpened++; return default; }
};

var timeProvider = new FakeTimeProvider();
var strategy = new ResiliencePipelineBuilder { TimeProvider = timeProvider }.AddCircuitBreaker(options).Build();

for (int i = 0; i < 10; i++)
{
strategy.Execute(_ => -1);
}

// Circuit opened
opened.Should().Be(1);
halfOpened.Should().Be(0);
closed.Should().Be(0);
Assert.Throws<BrokenCircuitException>(() => strategy.Execute(_ => 0));

// Circuit Half Opened
timeProvider.Advance(breakDuration);
strategy.Execute(_ => -1);
Assert.Throws<BrokenCircuitException>(() => strategy.Execute(_ => 0));
opened.Should().Be(2);
halfOpened.Should().Be(1);
closed.Should().Be(0);

// Now close it
timeProvider.Advance(breakDuration);
strategy.Execute(_ => 0);
opened.Should().Be(2);
halfOpened.Should().Be(2);
closed.Should().Be(1);
}

[Fact]
public void AddCircuitBreaker_IntegrationTest_WithBreakDurationGenerator()
{
int opened = 0;
int closed = 0;
int halfOpened = 0;

var breakDuration = TimeSpan.FromSeconds(1);

var options = new CircuitBreakerStrategyOptions
{
FailureRatio = 0.5,
MinimumThroughput = 10,
SamplingDuration = TimeSpan.FromSeconds(10),
BreakDuration = TimeSpan.FromSeconds(1),
BreakDuration = TimeSpan.FromSeconds(30), // Intentionally long to check it isn't used
BreakDurationGenerator = (_) => new ValueTask<TimeSpan>(breakDuration),
ShouldHandle = args => new ValueTask<bool>(args.Outcome.Result is -1),
OnOpened = _ => { opened++; return default; },
OnClosed = _ => { closed++; return default; },
Expand All @@ -95,15 +149,15 @@ public void AddCircuitBreaker_IntegrationTest()
Assert.Throws<BrokenCircuitException>(() => strategy.Execute(_ => 0));

// Circuit Half Opened
timeProvider.Advance(options.BreakDuration);
timeProvider.Advance(breakDuration);
strategy.Execute(_ => -1);
Assert.Throws<BrokenCircuitException>(() => strategy.Execute(_ => 0));
opened.Should().Be(2);
halfOpened.Should().Be(1);
closed.Should().Be(0);

// Now close it
timeProvider.Advance(options.BreakDuration);
timeProvider.Advance(breakDuration);
strategy.Execute(_ => 0);
opened.Should().Be(2);
halfOpened.Should().Be(2);
Expand Down

0 comments on commit 9d7729d

Please sign in to comment.