@Test public void shouldBeTheSameRetry() { AsyncRetry retry = retryRegistry.retry("testName"); AsyncRetry retry2 = retryRegistry.retry("testName"); Assertions.assertThat(retry).isSameAs(retry2); Assertions.assertThat(retryRegistry.getAllRetries()).hasSize(1); }
@Before public void setUp() { retryRegistry = AsyncRetryRegistry.ofDefaults(); }
@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 canBuildRetryRegistryWithConfig() { RetryConfig config = RetryConfig.custom().maxAttempts(1000).waitDuration(Duration.ofSeconds(300)).build(); retryRegistry = AsyncRetryRegistry.of(config); AsyncRetry retry = retryRegistry.retry("testName", () -> config); Assertions.assertThat(retry).isNotNull(); Assertions.assertThat(retryRegistry.getAllRetries()).hasSize(1); } }
public static AsyncRetryMetrics ofAsyncRetryRegistry(AsyncRetryRegistry retryRegistry) { return new AsyncRetryMetrics(retryRegistry.getAllRetries()); }
@Test public void shouldReturnTheCorrectName() { AsyncRetry retry = retryRegistry.retry("testName"); Assertions.assertThat(retry).isNotNull(); Assertions.assertThat(retry.getName()).isEqualTo("testName"); }
@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); }
public static AsyncRetryMetrics ofAsyncRetryRegistry(String prefix, AsyncRetryRegistry retryRegistry) { return new AsyncRetryMetrics(prefix, retryRegistry.getAllRetries()); }
@Test public void shouldBeNotTheSameRetry() { AsyncRetry retry = retryRegistry.retry("testName"); AsyncRetry retry2 = retryRegistry.retry("otherTestName"); Assertions.assertThat(retry).isNotSameAs(retry2); Assertions.assertThat(retryRegistry.getAllRetries()).hasSize(2); }
@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 canBuildRetryFromRegistryWithConfigSupplier() { RetryConfig config = RetryConfig.custom().maxAttempts(1000).waitDuration(Duration.ofSeconds(300)).build(); AsyncRetry retry = retryRegistry.retry("testName", () -> config); Assertions.assertThat(retry).isNotNull(); Assertions.assertThat(retryRegistry.getAllRetries()).hasSize(1); }
@Test public void canBuildRetryFromRegistryWithConfig() { RetryConfig config = RetryConfig.custom().maxAttempts(1000).waitDuration(Duration.ofSeconds(300)).build(); AsyncRetry retry = retryRegistry.retry("testName", config); Assertions.assertThat(retry).isNotNull(); Assertions.assertThat(retryRegistry.getAllRetries()).hasSize(1); }