Skip to content

Commit

Permalink
Merge pull request ReactiveX#200 from rLitto/Feature/159_allow_circui…
Browse files Browse the repository at this point in the history
…t_breaker_disabled_forced_open

Feature/159 fixing build
  • Loading branch information
storozhukBM authored Jan 30, 2018
2 parents 51e9da0 + 8b98bba commit 5ddac72
Showing 1 changed file with 42 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@

import static java.lang.Thread.sleep;
import static org.assertj.core.api.BDDAssertions.assertThat;

public class CircuitBreakerStateMachineTest {

private CircuitBreaker circuitBreaker;

@Before
public void setUp(){
public void setUp() {
circuitBreaker = new CircuitBreakerStateMachine("testName", CircuitBreakerConfig.custom()
.failureRateThreshold(50)
.ringBufferSizeInClosedState(5)
Expand All @@ -59,33 +60,25 @@ public void testCircuitBreakerStateMachine() throws InterruptedException {
circuitBreaker.onError(0, new RuntimeException()); // Should create a CircuitBreakerOnErrorEvent (1)
assertThat(circuitBreaker.isCallPermitted()).isEqualTo(true);
assertThat(circuitBreaker.getState()).isEqualTo(CircuitBreaker.State.CLOSED);
assertThat(circuitBreaker.getMetrics().getNumberOfBufferedCalls()).isEqualTo(1);
assertThat(circuitBreaker.getMetrics().getNumberOfFailedCalls()).isEqualTo(1);
assertThat(circuitBreaker.getMetrics().getFailureRate()).isEqualTo(-1f);
assertCircuitBreakerMetricsEqualTo(-1f, null, 1, null, 1, 0L);

// Call 2 is a failure
circuitBreaker.onError(0, new RuntimeException()); // Should create a CircuitBreakerOnErrorEvent (2)
assertThat(circuitBreaker.isCallPermitted()).isEqualTo(true);
assertThat(circuitBreaker.getState()).isEqualTo(CircuitBreaker.State.CLOSED);
assertThat(circuitBreaker.getMetrics().getNumberOfBufferedCalls()).isEqualTo(2);
assertThat(circuitBreaker.getMetrics().getNumberOfFailedCalls()).isEqualTo(2);
assertThat(circuitBreaker.getMetrics().getFailureRate()).isEqualTo(-1f);
assertCircuitBreakerMetricsEqualTo(-1f, null, 2, null, 2, 0L);

// Call 3 is a failure
circuitBreaker.onError(0, new RuntimeException()); // Should create a CircuitBreakerOnErrorEvent (3)
assertThat(circuitBreaker.isCallPermitted()).isEqualTo(true);
assertThat(circuitBreaker.getState()).isEqualTo(CircuitBreaker.State.CLOSED);
assertThat(circuitBreaker.getMetrics().getNumberOfBufferedCalls()).isEqualTo(3);
assertThat(circuitBreaker.getMetrics().getNumberOfFailedCalls()).isEqualTo(3);
assertThat(circuitBreaker.getMetrics().getFailureRate()).isEqualTo(-1f);
assertCircuitBreakerMetricsEqualTo(-1f, null, 3, null, 3, 0L);

// Call 4 is a success
circuitBreaker.onSuccess(0); // Should create a CircuitBreakerOnSuccessEvent (4)
assertThat(circuitBreaker.isCallPermitted()).isEqualTo(true);
assertThat(circuitBreaker.getState()).isEqualTo(CircuitBreaker.State.CLOSED);
assertThat(circuitBreaker.getMetrics().getNumberOfBufferedCalls()).isEqualTo(4);
assertThat(circuitBreaker.getMetrics().getNumberOfFailedCalls()).isEqualTo(3);
assertThat(circuitBreaker.getMetrics().getFailureRate()).isEqualTo(-1f);
assertCircuitBreakerMetricsEqualTo(-1f, null, 4, null, 3, 0L);

// Call 5 is a success
circuitBreaker.onSuccess(0); // Should create a CircuitBreakerOnSuccessEvent (5)
Expand All @@ -94,6 +87,7 @@ public void testCircuitBreakerStateMachine() throws InterruptedException {
assertThat(circuitBreaker.getMetrics().getNumberOfBufferedCalls()).isEqualTo(5);
assertThat(circuitBreaker.getMetrics().getNumberOfFailedCalls()).isEqualTo(3);
assertThat(circuitBreaker.getMetrics().getFailureRate()).isEqualTo(60.0f);
assertCircuitBreakerMetricsEqualTo(60.0f, null, 5, null, 3, 0L);

sleep(500);

Expand All @@ -109,21 +103,16 @@ public void testCircuitBreakerStateMachine() throws InterruptedException {
// The CircuitBreaker switches to half open, because the wait duration of 1 second is elapsed
assertThat(circuitBreaker.isCallPermitted()).isEqualTo(true);
assertThat(circuitBreaker.getState()).isEqualTo(CircuitBreaker.State.HALF_OPEN); // Should create a CircuitBreakerOnStateTransitionEvent (9)
// Metrics are reseted
assertThat(circuitBreaker.getMetrics().getMaxNumberOfBufferedCalls()).isEqualTo(3);
assertThat(circuitBreaker.getMetrics().getNumberOfNotPermittedCalls()).isEqualTo(0);
assertThat(circuitBreaker.getMetrics().getNumberOfBufferedCalls()).isEqualTo(0);
// Metrics are resetted
assertThat(circuitBreaker.getMetrics().getNumberOfFailedCalls()).isEqualTo(0);
assertCircuitBreakerMetricsEqualTo(-1f, null, 0, 3, 0, 0L);

// A ring buffer with size 2 is used in half open state
// Call 1 is a failure
circuitBreaker.onError(0, new RuntimeException()); // Should create a CircuitBreakerOnErrorEvent (10)
assertThat(circuitBreaker.isCallPermitted()).isEqualTo(true);
assertThat(circuitBreaker.getState()).isEqualTo(CircuitBreaker.State.HALF_OPEN);
assertThat(circuitBreaker.getMetrics().getNumberOfBufferedCalls()).isEqualTo(1);
assertThat(circuitBreaker.getMetrics().getNumberOfFailedCalls()).isEqualTo(1);
assertThat(circuitBreaker.getMetrics().getNumberOfNotPermittedCalls()).isEqualTo(0);
assertThat(circuitBreaker.getMetrics().getFailureRate()).isEqualTo(-1f);
assertCircuitBreakerMetricsEqualTo(-1f, null, 1, null, 1, 0L);

// Call 2 is a failure
circuitBreaker.onError(0, new RuntimeException()); // Should create a CircuitBreakerOnErrorEvent (11)
Expand All @@ -146,16 +135,12 @@ public void testCircuitBreakerStateMachine() throws InterruptedException {
// Call 1 is a failure
circuitBreaker.onError(0, new RuntimeException()); // Should create a CircuitBreakerOnErrorEvent (15)
assertThat(circuitBreaker.getState()).isEqualTo(CircuitBreaker.State.HALF_OPEN);
assertThat(circuitBreaker.getMetrics().getNumberOfBufferedCalls()).isEqualTo(1);
assertThat(circuitBreaker.getMetrics().getNumberOfFailedCalls()).isEqualTo(1);
assertThat(circuitBreaker.getMetrics().getFailureRate()).isEqualTo(-1f);
assertCircuitBreakerMetricsEqualTo(-1f, null, 1, null, 1, null);

// Call 2 should be ignored, because it's a NumberFormatException
circuitBreaker.onError(0, new NumberFormatException()); // Should create a CircuitBreakerOnIgnoredErrorEvent (16)
assertThat(circuitBreaker.getState()).isEqualTo(CircuitBreaker.State.HALF_OPEN);
assertThat(circuitBreaker.getMetrics().getNumberOfBufferedCalls()).isEqualTo(1);
assertThat(circuitBreaker.getMetrics().getNumberOfFailedCalls()).isEqualTo(1);
assertThat(circuitBreaker.getMetrics().getFailureRate()).isEqualTo(-1f);
assertCircuitBreakerMetricsEqualTo(-1f, null, 1, null, 1, null);

// Call 3 is a success
circuitBreaker.onSuccess(0); // Should create a CircuitBreakerOnSuccessEvent (17)
Expand All @@ -166,10 +151,7 @@ public void testCircuitBreakerStateMachine() throws InterruptedException {
// The state machine transitions back to CLOSED state
assertThat(circuitBreaker.isCallPermitted()).isEqualTo(true);
assertThat(circuitBreaker.getState()).isEqualTo(CircuitBreaker.State.CLOSED); // Should create a CircuitBreakerOnStateTransitionEvent (19)
assertThat(circuitBreaker.getMetrics().getMaxNumberOfBufferedCalls()).isEqualTo(5);
assertThat(circuitBreaker.getMetrics().getNumberOfBufferedCalls()).isEqualTo(3);
assertThat(circuitBreaker.getMetrics().getNumberOfFailedCalls()).isEqualTo(1);
assertThat(circuitBreaker.getMetrics().getFailureRate()).isEqualTo(-1f);
assertCircuitBreakerMetricsEqualTo(-1f, null, 3, 5, 1, 0L);

circuitBreaker.reset(); // Should create a CircuitBreakerOnResetEvent (20)

Expand Down Expand Up @@ -202,23 +184,17 @@ public void testCircuitBreakerStateMachine() throws InterruptedException {
assertThat(circuitBreaker.isCallPermitted()).isEqualTo(true);
assertThat(circuitBreaker.getState()).isEqualTo(CircuitBreaker.State.CLOSED);
assertThatMetricsAreReset();
assertThat(circuitBreaker.getMetrics().getNumberOfSuccessfulCalls()).isEqualTo(0);

circuitBreaker.onSuccess(0); // Should create a CircuitBreakerOnSuccessEvent (23)
assertThat(circuitBreaker.getMetrics().getFailureRate()).isEqualTo(-1f);
assertThat(circuitBreaker.getMetrics().getNumberOfBufferedCalls()).isEqualTo(1);
assertThat(circuitBreaker.getMetrics().getNumberOfFailedCalls()).isEqualTo(0);
assertThat(circuitBreaker.getMetrics().getNumberOfNotPermittedCalls()).isEqualTo(0);
assertThat(circuitBreaker.getMetrics().getNumberOfSuccessfulCalls()).isEqualTo(1);
assertCircuitBreakerMetricsEqualTo(-1f, 1, 1, null, 0, 0L);


circuitBreaker.transitionToForcedOpenState(); // Should create a CircuitBreakerOnStateTransitionEvent (20)
// The ring buffer back to initial state
// The state machine transitions back to CLOSED state
assertThat(circuitBreaker.isCallPermitted()).isEqualTo(false);
assertThat(circuitBreaker.getState()).isEqualTo(CircuitBreaker.State.FORCED_OPEN); // Should create a CircuitBreakerOnStateTransitionEvent (21)
assertThatMetricsAreReset();
assertThat(circuitBreaker.getMetrics().getNumberOfSuccessfulCalls()).isEqualTo(0);
assertCircuitBreakerMetricsEqualTo(-1f, 0, 0, null, 0, 1L);

circuitBreaker.onSuccess(0); // Should not create a CircuitBreakerOnSuccessEvent
circuitBreaker.onSuccess(0); // Should not create a CircuitBreakerOnSuccessEvent
Expand All @@ -227,23 +203,40 @@ public void testCircuitBreakerStateMachine() throws InterruptedException {

assertThat(circuitBreaker.isCallPermitted()).isEqualTo(false);
assertThat(circuitBreaker.getState()).isEqualTo(CircuitBreaker.State.FORCED_OPEN); // Should create a CircuitBreakerOnStateTransitionEvent (21)
assertThatMetricsAreReset();
assertCircuitBreakerMetricsEqualTo(-1f, null, 0, null, 0, 2L);


circuitBreaker.transitionToOpenState(); // Should create a CircuitBreakerOnStateTransitionEvent (20)
assertThat(circuitBreaker.isCallPermitted()).isEqualTo(false);
assertThat(circuitBreaker.getState()).isEqualTo(CircuitBreaker.State.OPEN);
assertThat(circuitBreaker.getMetrics().getFailureRate()).isEqualTo(-1f);
assertThat(circuitBreaker.getMetrics().getNumberOfBufferedCalls()).isEqualTo(0);
assertThat(circuitBreaker.getMetrics().getNumberOfFailedCalls()).isEqualTo(0);
assertThat(circuitBreaker.getMetrics().getNumberOfNotPermittedCalls()).isEqualTo(1);
assertCircuitBreakerMetricsEqualTo(-1f, null, 0, null, 0, 3L);

}

private void assertThatMetricsAreReset() {
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(-1f);
assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(0);
assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(0);
assertThat(metrics.getNumberOfNotPermittedCalls()).isEqualTo(0);
if (expectedFailureRate != null) {
assertThat(metrics.getFailureRate()).isEqualTo(expectedFailureRate);
}
if (expectedSuccessCalls != null) {
assertThat(metrics.getNumberOfSuccessfulCalls()).isEqualTo(expectedSuccessCalls);
}
if (expectedBufferedCalls != null) {
assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(expectedBufferedCalls);
}
if (expectedMaxBufferedCalls != null) {
assertThat(metrics.getMaxNumberOfBufferedCalls()).isEqualTo(expectedMaxBufferedCalls);
}
if (expectedFailedCalls != null) {
assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(expectedFailedCalls);
}
if (expectedNotPermittedCalls != null) {
assertThat(metrics.getNumberOfNotPermittedCalls()).isEqualTo(expectedNotPermittedCalls);
}
}

private void assertThatMetricsAreReset() {
assertCircuitBreakerMetricsEqualTo(-1f, 0, 0, null, 0, 0L);
}

}

0 comments on commit 5ddac72

Please sign in to comment.