/** * Creates a Retry with a custom Retry configuration. * * @param id the ID of the Retry * @param retryConfigSupplier a supplier of a custom Retry configuration * * @return a Retry with a custom Retry configuration. */ static AsyncRetry of(String id, Supplier<RetryConfig> retryConfigSupplier){ return of(id, retryConfigSupplier.get()); }
/** * {@inheritDoc} */ @Override public AsyncRetry retry(String name, RetryConfig customRetryConfig) { return retries.computeIfAbsent(Objects.requireNonNull(name, "Name must not be null"), (k) -> AsyncRetry.of(name, customRetryConfig)); }
/** * {@inheritDoc} */ @Override public AsyncRetry retry(String name) { return retries.computeIfAbsent(Objects.requireNonNull(name, "Name must not be null"), (k) -> AsyncRetry.of(name, defaultRetryConfig)); }
/** * {@inheritDoc} */ @Override public AsyncRetry retry(String name, Supplier<RetryConfig> retryConfigSupplier) { return retries.computeIfAbsent(Objects.requireNonNull(name, "Name must not be null"), (k) -> AsyncRetry.of(name, retryConfigSupplier.get())); } }
/** * Creates a Retry with default configuration. * * @param id the ID of the Retry * @return a Retry with default configuration */ static AsyncRetry ofDefaults(String id){ return of(id, RetryConfig.ofDefaults()); }
private void shouldCompleteFutureAfterAttemptsInCaseOfExceptionAtAsyncStage(int noOfAttempts) { CompletableFuture<String> failedFuture = new CompletableFuture<>(); failedFuture.completeExceptionally(new WebServiceException("BAM!")); // Given the HelloWorldService throws an exception BDDMockito.given(helloWorldService.returnHelloWorld()) .willReturn(failedFuture); // Create a Retry with default configuration AsyncRetry retryContext = AsyncRetry.of( "id", RetryConfig .custom() .maxAttempts(noOfAttempts) .build()); // Decorate the invocation of the HelloWorldService Supplier<CompletionStage<String>> supplier = AsyncRetry.decorateCompletionStage( retryContext, scheduler, () -> helloWorldService.returnHelloWorld()); // When Try<String> resultTry = Try.of(() -> awaitResult(supplier.get())); // Then the helloWorldService should be invoked n + 1 times BDDMockito.then(helloWorldService).should(Mockito.times(noOfAttempts)).returnHelloWorld(); Assertions.assertThat(resultTry.isFailure()).isTrue(); Assertions.assertThat(resultTry.getCause().getCause()).isInstanceOf(WebServiceException.class); }
private void shouldCompleteFutureAfterAttemptsInCaseOfExceptionAtSyncStage(int noOfAttempts) { // Given the HelloWorldService throws an exception BDDMockito.given(helloWorldService.returnHelloWorld()) .willThrow(new WebServiceException("BAM!")); // Create a Retry with default configuration AsyncRetry retryContext = AsyncRetry.of( "id", RetryConfig .custom() .maxAttempts(noOfAttempts) .build()); // Decorate the invocation of the HelloWorldService Supplier<CompletionStage<String>> supplier = AsyncRetry.decorateCompletionStage( retryContext, scheduler, () -> helloWorldService.returnHelloWorld()); // When Try<String> resultTry = Try.of(() -> awaitResult(supplier.get())); // Then the helloWorldService should be invoked n + 1 times BDDMockito.then(helloWorldService).should(Mockito.times(noOfAttempts)).returnHelloWorld(); Assertions.assertThat(resultTry.isFailure()).isTrue(); Assertions.assertThat(resultTry.getCause().getCause()).isInstanceOf(WebServiceException.class); }
@Test public void shouldNotRetryWithThatResult() throws InterruptedException, ExecutionException, TimeoutException { // Given the HelloWorldService returns Hello world BDDMockito.given(helloWorldService.returnHelloWorld()) .willReturn(completedFuture("Hello world")); // Create a Retry with default configuration final RetryConfig retryConfig = RetryConfig.<String>custom().retryOnResult(s -> s.contains("NoRetry")) .maxAttempts(1) .build(); AsyncRetry retryContext = AsyncRetry.of("id", retryConfig); // Decorate the invocation of the HelloWorldService Supplier<CompletionStage<String>> supplier = AsyncRetry.decorateCompletionStage( retryContext, scheduler, () -> helloWorldService.returnHelloWorld()); // When String result = awaitResult(supplier); // Then the helloWorldService should be invoked 1 time BDDMockito.then(helloWorldService).should(Mockito.times(1)).returnHelloWorld(); Assertions.assertThat(result).isEqualTo("Hello world"); // for code quality scan , it does not not recognize assertJ do not why Assert.assertEquals(result, "Hello world"); }
private void shouldCompleteFutureAfterAttemptsInCaseOfRetyOnResultAtAsyncStage(int noOfAttempts, String retryResponse) { // Given the HelloWorldService throws an exception BDDMockito.given(helloWorldService.returnHelloWorld()) .willReturn(completedFuture("Hello world")); // Create a Retry with default configuration AsyncRetry retryContext = AsyncRetry.of( "id", RetryConfig .<String>custom() .maxAttempts(noOfAttempts) .retryOnResult(s -> s.contains(retryResponse)) .build()); // Decorate the invocation of the HelloWorldService Supplier<CompletionStage<String>> supplier = AsyncRetry.decorateCompletionStage( retryContext, scheduler, () -> helloWorldService.returnHelloWorld()); // When Try<String> resultTry = Try.of(() -> awaitResult(supplier.get())); // Then the helloWorldService should be invoked n + 1 times BDDMockito.then(helloWorldService).should(Mockito.times(noOfAttempts)).returnHelloWorld(); Assert.assertTrue(resultTry.isSuccess()); }
@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(); }