Skip to content

Instantly share code, notes, and snippets.

@xlucasdemelo
Created May 26, 2020 22:53
Show Gist options
  • Save xlucasdemelo/4fa09e948d8ce37b688a722e133ffb2d to your computer and use it in GitHub Desktop.
Save xlucasdemelo/4fa09e948d8ce37b688a722e133ffb2d to your computer and use it in GitHub Desktop.
@Test
public void retry(){
MockResponse mockResponse = new MockResponse()
.setResponseCode(500)
.addHeader("Content-Type", "application/json;charset=utf-8");
MockResponse mockResponseSuccess = new MockResponse()
.setResponseCode(200)
.addHeader("Content-Type", "application/json;charset=utf-8");
List<MockResponse> responseList = Array.of( mockResponse, mockResponse, mockResponse, mockResponseSuccess).collect(Collectors.toList());
CircuitBreaker circuitBreaker = CircuitBreaker.of("books", CircuitBreakerConfig.custom()
.waitDurationInOpenState(Duration.ofMillis(100))
.build());
RetryConfig config = RetryConfig.custom()
.maxAttempts(4)
.waitDuration(Duration.ofMillis(2000))
.build();
// Create a RetryRegistry with a custom global configuration
RetryRegistry registry = RetryRegistry.of(config);
Retry retry = registry.retry("books", config);
this.captureEvents(circuitBreaker.getEventPublisher());
Supplier<String> withCircuitBreaker = CircuitBreaker.decorateSupplier(circuitBreaker, bookClient::findAll);
withCircuitBreaker = Retry
.decorateSupplier(retry, withCircuitBreaker);
this.enqueueList(responseList);
Try.ofSupplier(withCircuitBreaker).get();
// then
Assertions.assertThat(this.events)
.extracting(CircuitBreakerEvent::getEventType)
.containsExactly(
// try x10
CircuitBreakerEvent.Type.ERROR,
CircuitBreakerEvent.Type.ERROR,
CircuitBreakerEvent.Type.ERROR,
CircuitBreakerEvent.Type.SUCCESS
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment