Skip to content

Commit

Permalink
Issue ReactiveX#544: Fixes bug that CircuitBreaker stays in half open…
Browse files Browse the repository at this point in the history
… when exceptions are ignored. (ReactiveX#545)
  • Loading branch information
wl1244hotmai authored and RobWin committed Jul 15, 2019
1 parent 1a87d08 commit 5dbf2d1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ private void handleThrowable(long durationInNanos, Predicate<Throwable> recordFa
publishCircuitErrorEvent(name, durationInNanos, throwable);
stateReference.get().onError(throwable);
} else {
releasePermission();
publishCircuitIgnoredErrorEvent(name, durationInNanos, throwable);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,38 @@ public void shouldForceOpenCircuitBreaker() {
assertCircuitBreakerMetricsEqualTo(-1f, 0, 0, 4, 0, 2L);
}

@Test
public void shouldReleasePermissionWhenExceptionIgnored() {
circuitBreaker.transitionToOpenState();
circuitBreaker.transitionToHalfOpenState();
assertThat(circuitBreaker.getState()).isEqualTo(CircuitBreaker.State.HALF_OPEN);

assertThat(circuitBreaker.tryAcquirePermission()).isEqualTo(true);
circuitBreaker.onSuccess(0); // Should create a CircuitBreakerOnSuccessEvent
assertThat(circuitBreaker.getState()).isEqualTo(CircuitBreaker.State.HALF_OPEN);
assertCircuitBreakerMetricsEqualTo(-1f, 1, 1, 4, 0, 0L);

assertThat(circuitBreaker.tryAcquirePermission()).isEqualTo(true);
circuitBreaker.onSuccess(0); // Should create a CircuitBreakerOnSuccessEvent
assertThat(circuitBreaker.getState()).isEqualTo(CircuitBreaker.State.HALF_OPEN);
assertCircuitBreakerMetricsEqualTo(-1f, 2, 2, 4, 0, 0L);

assertThat(circuitBreaker.tryAcquirePermission()).isEqualTo(true);
circuitBreaker.onSuccess(0); // Should create a CircuitBreakerOnSuccessEvent
assertThat(circuitBreaker.getState()).isEqualTo(CircuitBreaker.State.HALF_OPEN);
assertCircuitBreakerMetricsEqualTo(-1f, 3, 3, 4, 0, 0L);

assertThat(circuitBreaker.tryAcquirePermission()).isEqualTo(true);
circuitBreaker.onError(0, new NumberFormatException()); // Should create a CircuitBreakerOnErrorEvent
assertThat(circuitBreaker.getState()).isEqualTo(CircuitBreaker.State.HALF_OPEN);
assertCircuitBreakerMetricsEqualTo(-1f, 3, 3, 4, 0, 0L);

assertThat(circuitBreaker.tryAcquirePermission()).isEqualTo(true);
circuitBreaker.onError(0, new RuntimeException()); // Should create a CircuitBreakerOnErrorEvent
assertThat(circuitBreaker.getState()).isEqualTo(CircuitBreaker.State.CLOSED);
assertCircuitBreakerMetricsEqualTo(-1f, 3, 4, 5, 1, 0L);
}

private void assertCircuitBreakerMetricsEqualTo(Float expectedFailureRate, Integer expectedSuccessCalls, Integer expectedBufferedCalls, Integer expectedMaxBufferedCalls, Integer expectedFailedCalls, Long expectedNotPermittedCalls) {
final CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
assertThat(metrics.getFailureRate()).isEqualTo(expectedFailureRate);
Expand Down

0 comments on commit 5dbf2d1

Please sign in to comment.