Java Code Examples for io.github.resilience4j.retry.Retry#decorateCompletionStage()

The following examples show how to use io.github.resilience4j.retry.Retry#decorateCompletionStage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SupplierRetryTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
private void shouldCompleteFutureAfterAttemptsInCaseOfRetyOnResultAtAsyncStage(int noOfAttempts,
    String retryResponse) {
    given(helloWorldServiceAsync.returnHelloWorld())
        .willReturn(completedFuture("Hello world"));
    Retry retryContext = Retry.of(
        "id",
        RetryConfig
            .<String>custom()
            .maxAttempts(noOfAttempts)
            .retryOnResult(s -> s.contains(retryResponse))
            .build());
    Supplier<CompletionStage<String>> supplier = Retry.decorateCompletionStage(
        retryContext,
        scheduler,
        () -> helloWorldServiceAsync.returnHelloWorld());

    Try<String> resultTry = Try.of(() -> awaitResult(supplier.get()));

    then(helloWorldServiceAsync).should(times(noOfAttempts)).returnHelloWorld();
    assertThat(resultTry.isSuccess()).isTrue();
}
 
Example 2
Source File: CompletionStageRetryTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldNotRetryWithThatResult() {
    given(helloWorldService.returnHelloWorld())
        .willReturn(completedFuture("Hello world"));
    final RetryConfig retryConfig = RetryConfig.<String>custom()
        .retryOnResult(s -> s.contains("NoRetry"))
        .maxAttempts(1)
        .build();
    Retry retryContext = Retry.of("id", retryConfig);

    Supplier<CompletionStage<String>> supplier = Retry.decorateCompletionStage(
        retryContext,
        scheduler,
        () -> helloWorldService.returnHelloWorld());
    String result = awaitResult(supplier);

    then(helloWorldService).should().returnHelloWorld();
    assertThat(result).isEqualTo("Hello world");
}
 
Example 3
Source File: CompletionStageRetryTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldRetryInCaseOfAnExceptionAtAsyncStage() {
    CompletableFuture<String> failedFuture = new CompletableFuture<>();
    failedFuture.completeExceptionally(new HelloWorldException());
    given(helloWorldService.returnHelloWorld())
        .willReturn(failedFuture)
        .willReturn(completedFuture("Hello world"));
    Retry retryContext = Retry.ofDefaults("id");

    Supplier<CompletionStage<String>> supplier = Retry.decorateCompletionStage(
        retryContext,
        scheduler,
        () -> helloWorldService.returnHelloWorld());
    String result = awaitResult(supplier.get());

    then(helloWorldService).should(times(2)).returnHelloWorld();
    assertThat(result).isEqualTo("Hello world");
}
 
Example 4
Source File: CompletionStageRetryTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
private void shouldCompleteFutureAfterAttemptsInCaseOfExceptionAtSyncStage(int noOfAttempts) {
    given(helloWorldService.returnHelloWorld())
        .willThrow(new HelloWorldException());
    Retry retryContext = Retry.of(
        "id",
        RetryConfig
            .custom()
            .maxAttempts(noOfAttempts)
            .build());
    Supplier<CompletionStage<String>> supplier = Retry.decorateCompletionStage(
        retryContext,
        scheduler,
        () -> helloWorldService.returnHelloWorld());

    Try<String> resultTry = Try.of(() -> awaitResult(supplier.get()));

    then(helloWorldService).should(times(noOfAttempts)).returnHelloWorld();
    assertThat(resultTry.isFailure()).isTrue();
    assertThat(resultTry.getCause().getCause()).isInstanceOf(HelloWorldException.class);
}
 
Example 5
Source File: CompletionStageRetryTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
private void shouldCompleteFutureAfterAttemptsInCaseOfExceptionAtAsyncStage(int noOfAttempts) {
    CompletableFuture<String> failedFuture = new CompletableFuture<>();
    failedFuture.completeExceptionally(new HelloWorldException());
    given(helloWorldService.returnHelloWorld())
        .willReturn(failedFuture);
    Retry retryContext = Retry.of(
        "id",
        RetryConfig
            .custom()
            .maxAttempts(noOfAttempts)
            .build());
    Supplier<CompletionStage<String>> supplier = Retry.decorateCompletionStage(
        retryContext,
        scheduler,
        () -> helloWorldService.returnHelloWorld());

    Try<String> resultTry = Try.of(() -> awaitResult(supplier.get()));

    then(helloWorldService).should(times(noOfAttempts)).returnHelloWorld();
    assertThat(resultTry.isFailure()).isTrue();
    assertThat(resultTry.getCause().getCause()).isInstanceOf(HelloWorldException.class);
}
 
Example 6
Source File: CompletionStageRetryTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
private void shouldCompleteFutureAfterAttemptsInCaseOfRetyOnResultAtAsyncStage(int noOfAttempts,
    String retryResponse) {
    given(helloWorldService.returnHelloWorld())
        .willReturn(completedFuture("Hello world"));
    Retry retryContext = Retry.of(
        "id",
        RetryConfig
            .<String>custom()
            .maxAttempts(noOfAttempts)
            .retryOnResult(s -> s.contains(retryResponse))
            .build());
    Supplier<CompletionStage<String>> supplier = Retry.decorateCompletionStage(
        retryContext,
        scheduler,
        () -> helloWorldService.returnHelloWorld());

    Try<String> resultTry = Try.of(() -> awaitResult(supplier.get()));

    then(helloWorldService).should(times(noOfAttempts)).returnHelloWorld();
    assertThat(resultTry.isSuccess()).isTrue();
}
 
Example 7
Source File: CompletionStageRetryTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotRetry() {
    given(helloWorldService.returnHelloWorld())
        .willReturn(completedFuture("Hello world"));
    Retry retryContext = Retry.ofDefaults("id");
    Supplier<CompletionStage<String>> supplier = Retry.decorateCompletionStage(
        retryContext,
        scheduler,
        () -> helloWorldService.returnHelloWorld());

    String result = awaitResult(supplier);

    then(helloWorldService).should().returnHelloWorld();
    assertThat(result).isEqualTo("Hello world");
}
 
Example 8
Source File: CompletionStageRetryTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotRetryWhenReturnVoid() {
    given(helloWorldService.sayHelloWorld())
        .willReturn(completedFuture(null));
    Retry retryContext = Retry.ofDefaults("id");

    Supplier<CompletionStage<Void>> supplier = Retry.decorateCompletionStage(
        retryContext,
        scheduler,
        () -> helloWorldService.sayHelloWorld());
    awaitResult(supplier);

    then(helloWorldService).should().sayHelloWorld();
}
 
Example 9
Source File: Decorators.java    From resilience4j with Apache License 2.0 4 votes vote down vote up
public DecorateCompletionStage<T> withRetry(Retry retryContext,
    ScheduledExecutorService scheduler) {
    stageSupplier = Retry.decorateCompletionStage(retryContext, scheduler, stageSupplier);
    return this;
}