Skip to content

Instantly share code, notes, and snippets.

@xlucasdemelo
Created May 26, 2020 22:49
Show Gist options
  • Save xlucasdemelo/5edcbcd98e735849df030cc57fda5d43 to your computer and use it in GitHub Desktop.
Save xlucasdemelo/5edcbcd98e735849df030cc57fda5d43 to your computer and use it in GitHub Desktop.
@Test
public void countBased_example() throws Exception {
MockResponse mockResponse = new MockResponse()
.setResponseCode(500)
.addHeader("Content-Type", "application/json;charset=utf-8");
CircuitBreaker circuitBreaker = CircuitBreaker.of("books", CircuitBreakerConfig.custom()
.slidingWindowSize(4)
.permittedNumberOfCallsInHalfOpenState(2)
.waitDurationInOpenState(Duration.ofMillis(100))
.build());
this.captureEvents(circuitBreaker.getEventPublisher());
Supplier<String> withCircuitBreaker = CircuitBreaker.decorateSupplier(circuitBreaker, bookClient::findAll);
this.repeat(10, withCircuitBreaker, mockResponse);
Thread.sleep(1000);
this.repeat(4, withCircuitBreaker, mockResponse);
// then
Assertions.assertThat(this.events)
.extracting(CircuitBreakerEvent::getEventType)
.containsExactly(
// try x10
CircuitBreakerEvent.Type.ERROR,
CircuitBreakerEvent.Type.ERROR,
CircuitBreakerEvent.Type.ERROR,
CircuitBreakerEvent.Type.ERROR, // ringBufferInClosedState is full
CircuitBreakerEvent.Type.FAILURE_RATE_EXCEEDED,
CircuitBreakerEvent.Type.STATE_TRANSITION, // from CLOSED to OPEN
CircuitBreakerEvent.Type.NOT_PERMITTED,
CircuitBreakerEvent.Type.NOT_PERMITTED,
CircuitBreakerEvent.Type.NOT_PERMITTED,
CircuitBreakerEvent.Type.NOT_PERMITTED,
CircuitBreakerEvent.Type.NOT_PERMITTED,
CircuitBreakerEvent.Type.NOT_PERMITTED,
// sleep waitDurationInOpenState
CircuitBreakerEvent.Type.STATE_TRANSITION, // from OPEN to HALF_OPEN
// try x4
CircuitBreakerEvent.Type.ERROR,
CircuitBreakerEvent.Type.ERROR, // ringBufferInHalfOpenState is full
CircuitBreakerEvent.Type.STATE_TRANSITION, // from HALF_OPEN to OPEN
CircuitBreakerEvent.Type.NOT_PERMITTED,
CircuitBreakerEvent.Type.NOT_PERMITTED
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment