@Test public void shouldBeNotTheSameRetry() { Retry retry = retryRegistry.retry("testName"); Retry retry2 = retryRegistry.retry("otherTestName"); Assertions.assertThat(retry).isNotSameAs(retry2); Assertions.assertThat(retryRegistry.getAllRetries()).hasSize(2); }
@Before public void setUp() { retryRegistry = RetryRegistry.ofDefaults(); }
@Test public void canBuildRetryRegistryWithConfig() { RetryConfig config = RetryConfig.custom().maxAttempts(1000).waitDuration(Duration.ofSeconds(300)).build(); retryRegistry = RetryRegistry.of(config); Retry retry = retryRegistry.retry("testName", () -> config); Assertions.assertThat(retry).isNotNull(); Assertions.assertThat(retryRegistry.getAllRetries()).hasSize(1); } }
@Test public void shouldRegisterMetrics() { RetryRegistry retryRegistry = RetryRegistry.ofDefaults(); retryRegistry.retry("testName"); RetryMetrics retryMetrics = RetryMetrics.ofRetryRegistry(retryRegistry); retryMetrics.bindTo(meterRegistry); final List<String> metricNames = meterRegistry.getMeters() .stream() .map(Meter::getId) .map(Meter.Id::getName) .collect(Collectors.toList()); final List<String> expectedMetrics = newArrayList( "resilience4j.retry.testName.successful_calls_with_retry", "resilience4j.retry.testName.failed_calls_with_retry", "resilience4j.retry.testName.successful_calls_without_retry", "resilience4j.retry.testName.failed_calls_without_retry"); assertThat(metricNames).hasSameElementsAs(expectedMetrics); }
/** * Creates a new instance RetryMetrics {@link RetryMetrics} with * a {@link RateLimiterRegistry} as a source. * * @param retryRegistry the registry of retries * * @return a new RetryMetrics instance */ public static RetryMetrics ofRetryRegistry(RetryRegistry retryRegistry) { return new RetryMetrics(retryRegistry.getAllRetries()); }
@Test public void shouldReturnTheCorrectName() { Retry retry = retryRegistry.retry("testName"); Assertions.assertThat(retry).isNotNull(); Assertions.assertThat(retry.getName()).isEqualTo("testName"); }
@Test public void shouldRegisterMetricsWithoutRetry() throws Throwable { //Given RetryRegistry retryRegistry = RetryRegistry.ofDefaults(); Retry retry = retryRegistry.retry("testName"); metricRegistry.registerAll(RetryMetrics.ofRetryRegistry(retryRegistry)); // Given the HelloWorldService returns Hello world BDDMockito.given(helloWorldService.returnHelloWorld()).willReturn("Hello world"); // Setup circuitbreaker with retry String value = retry.executeSupplier(helloWorldService::returnHelloWorld); //Then assertThat(value).isEqualTo("Hello world"); // Then the helloWorldService should be invoked 1 time BDDMockito.then(helloWorldService).should(times(1)).returnHelloWorld(); assertThat(metricRegistry.getMetrics()).hasSize(4); assertThat(metricRegistry.getGauges().get("resilience4j.retry.testName." + SUCCESSFUL_CALLS_WITH_RETRY).getValue()).isEqualTo(0L); assertThat(metricRegistry.getGauges().get("resilience4j.retry.testName." + SUCCESSFUL_CALLS_WITHOUT_RETRY).getValue()).isEqualTo(1L); assertThat(metricRegistry.getGauges().get("resilience4j.retry.testName." + FAILED_CALLS_WITH_RETRY).getValue()).isEqualTo(0L); assertThat(metricRegistry.getGauges().get("resilience4j.retry.testName." + FAILED_CALLS_WITHOUT_RETRY).getValue()).isEqualTo(0L); }
public static RetryMetrics ofRetryRegistry(String prefix, RetryRegistry retryRegistry) { return new RetryMetrics(prefix, retryRegistry.getAllRetries()); }
@Test public void shouldUseCustomPrefix() throws Throwable { //Given RetryRegistry retryRegistry = RetryRegistry.ofDefaults(); Retry retry = retryRegistry.retry("testName"); metricRegistry.registerAll(RetryMetrics.ofRetryRegistry("testPrefix",retryRegistry)); // Given the HelloWorldService returns Hello world BDDMockito.given(helloWorldService.returnHelloWorld()).willReturn("Hello world"); String value = retry.executeSupplier(helloWorldService::returnHelloWorld); //Then assertThat(value).isEqualTo("Hello world"); // Then the helloWorldService should be invoked 1 time BDDMockito.then(helloWorldService).should(times(1)).returnHelloWorld(); assertThat(metricRegistry.getMetrics()).hasSize(4); assertThat(metricRegistry.getGauges().get("testPrefix.testName." + SUCCESSFUL_CALLS_WITH_RETRY).getValue()).isEqualTo(0L); assertThat(metricRegistry.getGauges().get("testPrefix.testName." + SUCCESSFUL_CALLS_WITHOUT_RETRY).getValue()).isEqualTo(1L); assertThat(metricRegistry.getGauges().get("testPrefix.testName." + FAILED_CALLS_WITH_RETRY).getValue()).isEqualTo(0L); assertThat(metricRegistry.getGauges().get("testPrefix.testName." + FAILED_CALLS_WITHOUT_RETRY).getValue()).isEqualTo(0L); } }
@Test public void shouldBeTheSameRetry() { Retry retry = retryRegistry.retry("testName"); Retry retry2 = retryRegistry.retry("testName"); Assertions.assertThat(retry).isSameAs(retry2); Assertions.assertThat(retryRegistry.getAllRetries()).hasSize(1); }
public static RetryMetrics ofRetryRegistry(RetryRegistry retryRegistry) { return new RetryMetrics(retryRegistry.getAllRetries()); }
OptionalBinder.newOptionalBinder(binder(), RetryRegistry.class).setDefault().toInstance(RetryRegistry.ofDefaults()); OptionalBinder.newOptionalBinder(binder(), BulkheadRegistry.class).setDefault().toInstance(BulkheadRegistry.ofDefaults());
@Test public void shouldRegisterMetricsWithRetry() throws Throwable { //Given RetryRegistry retryRegistry = RetryRegistry.ofDefaults(); Retry retry = retryRegistry.retry("testName"); metricRegistry.registerAll(RetryMetrics.ofRetryRegistry(retryRegistry)); // Given the HelloWorldService returns Hello world BDDMockito.given(helloWorldService.returnHelloWorld()) .willThrow(new WebServiceException("BAM!")) .willReturn("Hello world") .willThrow(new WebServiceException("BAM!")) .willThrow(new WebServiceException("BAM!")) .willThrow(new WebServiceException("BAM!")); // Setup circuitbreaker with retry String value1 = retry.executeSupplier(helloWorldService::returnHelloWorld); Try.ofSupplier(Retry.decorateSupplier(retry, helloWorldService::returnHelloWorld)); //Then assertThat(value1).isEqualTo("Hello world"); // Then the helloWorldService should be invoked 1 time BDDMockito.then(helloWorldService).should(times(5)).returnHelloWorld(); assertThat(metricRegistry.getMetrics()).hasSize(4); assertThat(metricRegistry.getGauges().get("resilience4j.retry.testName." + SUCCESSFUL_CALLS_WITH_RETRY).getValue()).isEqualTo(1L); assertThat(metricRegistry.getGauges().get("resilience4j.retry.testName." + SUCCESSFUL_CALLS_WITHOUT_RETRY).getValue()).isEqualTo(0L); assertThat(metricRegistry.getGauges().get("resilience4j.retry.testName." + FAILED_CALLS_WITH_RETRY).getValue()).isEqualTo(1L); assertThat(metricRegistry.getGauges().get("resilience4j.retry.testName." + FAILED_CALLS_WITHOUT_RETRY).getValue()).isEqualTo(0L); }
@Test public void canBuildRetryFromRegistryWithConfigSupplier() { RetryConfig config = RetryConfig.custom().maxAttempts(1000).waitDuration(Duration.ofSeconds(300)).build(); Retry retry = retryRegistry.retry("testName", () -> config); Assertions.assertThat(retry).isNotNull(); Assertions.assertThat(retryRegistry.getAllRetries()).hasSize(1); }
); chain1.get("stream/events", ctx -> { Seq<Flux<RetryEvent>> eventStreams = retryRegistry.getAllRetries().map(retry -> ReactorAdapter.toFlux(retry.getEventPublisher())); Function<RetryEvent, String> data = r -> Jackson.getObjectWriter(chain1.getRegistry()).writeValueAsString(RetryEventDTO.createRetryEventDTO(r)); ServerSentEvents events = ServerSentEvents.serverSentEvents(Flux.merge(eventStreams), e -> e.id(RetryEvent::getName).event(c -> c.getEventType().name()).data(data)); chain1.get("stream/events/:name", ctx -> { String rateLimiterName = ctx.getPathTokens().get("name"); Retry retry = retryRegistry.getAllRetries() .find(rL -> rL.getName().equals(rateLimiterName)) .getOrElseThrow(() -> String retryName = ctx.getPathTokens().get("name"); String eventType = ctx.getPathTokens().get("type"); Retry retry = retryRegistry.getAllRetries() .find(rL -> rL.getName().equals(retryName)) .getOrElseThrow(() ->
OptionalBinder.newOptionalBinder(binder(), RetryRegistry.class).setDefault().toInstance(RetryRegistry.ofDefaults()); OptionalBinder.newOptionalBinder(binder(), BulkheadRegistry.class).setDefault().toInstance(BulkheadRegistry.ofDefaults());
public Object invoke(MethodInvocation invocation) throws Throwable { Retry annotation = invocation.getMethod().getAnnotation(Retry.class); io.github.resilience4j.retry.Retry retry = registry.retry(annotation.name()); if (retry == null) { return invocation.proceed(); registry = RetryRegistry.ofDefaults();
@Test public void canBuildRetryFromRegistryWithConfig() { RetryConfig config = RetryConfig.custom().maxAttempts(1000).waitDuration(Duration.ofSeconds(300)).build(); Retry retry = retryRegistry.retry("testName", config); Assertions.assertThat(retry).isNotNull(); Assertions.assertThat(retryRegistry.getAllRetries()).hasSize(1); }