@Test public void shouldUseCustomPrefix() { //Given AsyncRetryRegistry retryRegistry = AsyncRetryRegistry.ofDefaults(); AsyncRetry retry = retryRegistry.retry("testName"); metricRegistry.registerAll(AsyncRetryMetrics.ofAsyncRetryRegistry("testPrefix",retryRegistry)); // Given the HelloWorldService returns Hello world BDDMockito.given(helloWorldService.returnHelloWorld()).willReturn(CompletableFuture.completedFuture("Hello world")); String value = awaitResult(retry.executeCompletionStage(scheduler, 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 shouldRegisterMetricsWithoutRetry() { //Given AsyncRetryRegistry retryRegistry = AsyncRetryRegistry.ofDefaults(); AsyncRetry retry = retryRegistry.retry("testName"); metricRegistry.registerAll(AsyncRetryMetrics.ofAsyncRetryRegistry(retryRegistry)); // Given the HelloWorldService returns Hello world BDDMockito.given(helloWorldService.returnHelloWorld()) .willReturn(CompletableFuture.completedFuture("Hello world")); // Setup circuitbreaker with retry String value = awaitResult(retry.executeCompletionStage(scheduler, 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); }
@Test public void shouldConsumeOnSuccessEvent() throws Exception { CompletableFuture<String> failedFuture = new CompletableFuture<>(); failedFuture.completeExceptionally(new WebServiceException("BAM!")); CountDownLatch latch = new CountDownLatch(1); // Given the HelloWorldService returns Hello world given(helloWorldService.returnHelloWorld()) .willReturn(failedFuture) .willReturn(completedFuture("Hello world")); retry.getEventPublisher() .onSuccess(event -> { logger.info(event.getEventType().toString()); latch.countDown(); }); String result = awaitResult(retry.executeCompletionStage(scheduler, () -> helloWorldService.returnHelloWorld())); assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue(); assertThat(result).isEqualTo("Hello world"); then(helloWorldService).should(times(2)).returnHelloWorld(); then(logger).should(times(1)).info("SUCCESS"); }
@Test public void shouldRegisterMetricsWithRetry() { //Given AsyncRetryRegistry retryRegistry = AsyncRetryRegistry.ofDefaults(); AsyncRetry retry = retryRegistry.retry("testName"); metricRegistry.registerAll(AsyncRetryMetrics.ofAsyncRetryRegistry(retryRegistry)); // Given the HelloWorldService returns Hello world BDDMockito.given(helloWorldService.returnHelloWorld()) .willReturn(failedFuture(new WebServiceException("BAM!"))) .willReturn(CompletableFuture.completedFuture("Hello world")) .willReturn(failedFuture(new WebServiceException("BAM!"))) .willReturn(failedFuture(new WebServiceException("BAM!"))) .willReturn(failedFuture(new WebServiceException("BAM!"))); // Setup circuitbreaker with retry String value1 = awaitResult(retry.executeCompletionStage(scheduler, helloWorldService::returnHelloWorld)); Try.ofCallable(() -> awaitResult(AsyncRetry.decorateCompletionStage(retry, scheduler, helloWorldService::returnHelloWorld).get())); //Then assertThat(value1).isEqualTo("Hello world"); // Then the helloWorldService should be invoked 5 times 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 shouldConsumeOnErrorEvent() { CompletableFuture<String> failedFuture = new CompletableFuture<>(); failedFuture.completeExceptionally(new WebServiceException("BAM!")); given(helloWorldService.returnHelloWorld()) .willReturn(failedFuture); retry.getEventPublisher() .onError(event -> logger.info(event.getEventType().toString())); Try.of(() -> awaitResult(retry.executeCompletionStage(scheduler, () -> helloWorldService.returnHelloWorld()))); then(logger).should(times(1)).info("ERROR"); then(helloWorldService).should(times(3)).returnHelloWorld(); }
@Test public void shouldConsumeOnRetryEvent() { CompletableFuture<String> failedFuture = new CompletableFuture<>(); failedFuture.completeExceptionally(new WebServiceException("BAM!")); given(helloWorldService.returnHelloWorld()) .willReturn(failedFuture); retry.getEventPublisher() .onRetry(event -> logger.info(event.getEventType().toString())); Try.of(() -> awaitResult(retry.executeCompletionStage(scheduler, () -> helloWorldService.returnHelloWorld()))); then(logger).should(times(2)).info("RETRY"); then(helloWorldService).should(times(3)).returnHelloWorld(); }
@Test public void shouldConsumeIgnoredErrorEvent() { given(helloWorldService.returnHelloWorld()) .willThrow(new WebServiceException("BAM!")); RetryConfig retryConfig = RetryConfig.custom() .retryOnException(throwable -> Match(throwable).of( Case($(instanceOf(WebServiceException.class)), false), Case($(), true))) .build(); retry = AsyncRetry.of("testName", retryConfig); retry.getEventPublisher() .onIgnoredError(event -> logger.info(event.getEventType().toString())); Try.of(() -> awaitResult(retry.executeCompletionStage(scheduler, () -> helloWorldService.returnHelloWorld()))); then(logger).should(times(1)).info("IGNORED_ERROR"); then(helloWorldService).should(times(1)).returnHelloWorld(); }