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

The following examples show how to use io.github.resilience4j.retry.Retry#of() . 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: RetryTransformerTest.java    From resilience4j with Apache License 2.0 7 votes vote down vote up
@Test
public void retryOnResultFailAfterMaxAttemptsUsingMaybe() throws InterruptedException {
    RetryConfig config = RetryConfig.<String>custom()
        .retryOnResult("retry"::equals)
        .waitDuration(Duration.ofMillis(50))
        .maxAttempts(3).build();
    Retry retry = Retry.of("testName", config);
    given(helloWorldService.returnHelloWorld())
        .willReturn("retry");

    Maybe.fromCallable(helloWorldService::returnHelloWorld)
        .compose(RetryTransformer.of(retry))
        .test()
        .await()
        .assertValueCount(1)
        .assertValue("retry")
        .assertComplete();

    then(helloWorldService).should(times(3)).returnHelloWorld();
}
 
Example 2
Source File: RetryTransformerTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void doNotRetryFromPredicateUsingSingle() {
    RetryConfig config = RetryConfig.custom()
        .retryOnException(t -> t instanceof IOException)
        .waitDuration(Duration.ofMillis(50))
        .maxAttempts(3).build();
    Retry retry = Retry.of("testName", config);
    given(helloWorldService.returnHelloWorld())
        .willThrow(new HelloWorldException());
    Single.fromCallable(helloWorldService::returnHelloWorld)
        .compose(RetryTransformer.of(retry))
        .test()
        .assertError(HelloWorldException.class)
        .assertNotComplete();

    then(helloWorldService).should().returnHelloWorld();
    Retry.Metrics metrics = retry.getMetrics();

    assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(1);
    assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(0);
}
 
Example 3
Source File: RetryTransformerTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void doNotRetryFromPredicateUsingObservable() {
    RetryConfig config = RetryConfig.custom()
        .retryOnException(t -> t instanceof IOException)
        .waitDuration(Duration.ofMillis(50))
        .maxAttempts(3).build();
    Retry retry = Retry.of("testName", config);
    given(helloWorldService.returnHelloWorld())
        .willThrow(new HelloWorldException());

    Observable.fromCallable(helloWorldService::returnHelloWorld)
        .compose(RetryTransformer.of(retry))
        .test()
        .assertError(HelloWorldException.class)
        .assertNotComplete()
        .assertSubscribed();

    then(helloWorldService).should().returnHelloWorld();
    Retry.Metrics metrics = retry.getMetrics();
    assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(1);
    assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(0);
}
 
Example 4
Source File: RetryOperatorTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void retryOnResultFailAfterMaxAttemptsUsingFlux() {
    RetryConfig config = RetryConfig.<String>custom()
        .retryOnResult("retry"::equals)
        .waitDuration(Duration.ofMillis(10))
        .maxAttempts(3).build();
    Retry retry = Retry.of("testName", config);

    StepVerifier.create(Flux.just("retry")
        .transformDeferred(RetryOperator.of(retry)))
        .expectSubscription()
        .expectNextCount(1)
        .expectComplete()
        .verify(Duration.ofSeconds(1));

    Retry.Metrics metrics = retry.getMetrics();
    assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(0);
    assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(1);
}
 
Example 5
Source File: RetryTransformerTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void doNotRetryFromPredicateUsingFlowable() {
    RetryConfig config = RetryConfig.custom()
        .retryOnException(t -> t instanceof IOException)
        .waitDuration(Duration.ofMillis(50))
        .maxAttempts(3).build();
    Retry retry = Retry.of("testName", config);
    given(helloWorldService.returnHelloWorld())
        .willThrow(new HelloWorldException());

    Flowable.fromCallable(helloWorldService::returnHelloWorld)
        .compose(RetryTransformer.of(retry))
        .test()
        .assertError(HelloWorldException.class)
        .assertNotComplete();

    then(helloWorldService).should().returnHelloWorld();
    Retry.Metrics metrics = retry.getMetrics();
    assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(1);
    assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(0);
}
 
Example 6
Source File: RetryTransformerTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void retryOnResultUsingFlowable() throws InterruptedException {
    RetryConfig config = RetryConfig.<String>custom()
        .retryOnResult("retry"::equals)
        .waitDuration(Duration.ofMillis(50))
        .maxAttempts(3).build();
    Retry retry = Retry.of("testName", config);
    given(helloWorldService.returnHelloWorld())
        .willReturn("retry")
        .willReturn("success");

    Flowable.fromCallable(helloWorldService::returnHelloWorld)
        .compose(RetryTransformer.of(retry))
        .test()
        .await()
        .assertValueCount(1)
        .assertValue("success")
        .assertComplete();

    then(helloWorldService).should(times(2)).returnHelloWorld();
    Retry.Metrics metrics = retry.getMetrics();
    assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(0);
    assertThat(metrics.getNumberOfSuccessfulCallsWithRetryAttempt()).isEqualTo(1);
}
 
Example 7
Source File: RetryOperatorTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void returnOnErrorUsingMono() {
    RetryConfig config = retryConfig();
    Retry retry = Retry.of("testName", config);
    RetryOperator<String> retryOperator = RetryOperator.of(retry);
    given(helloWorldService.returnHelloWorld())
        .willThrow(new HelloWorldException());

    StepVerifier.create(Mono.fromCallable(helloWorldService::returnHelloWorld)
        .transformDeferred(retryOperator))
        .expectSubscription()
        .expectError(HelloWorldException.class)
        .verify(Duration.ofSeconds(1));

    StepVerifier.create(Mono.fromCallable(helloWorldService::returnHelloWorld)
        .transformDeferred(retryOperator))
        .expectSubscription()
        .expectError(HelloWorldException.class)
        .verify(Duration.ofSeconds(1));

    then(helloWorldService).should(times(6)).returnHelloWorld();
    Retry.Metrics metrics = retry.getMetrics();
    assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(2);
    assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(0);
}
 
Example 8
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 9
Source File: RetryTransformerTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void doNotRetryFromPredicateUsingObservable() {
    RetryConfig config = RetryConfig.custom()
        .retryOnException(t -> t instanceof IOException)
        .waitDuration(Duration.ofMillis(50))
        .maxAttempts(3).build();
    Retry retry = Retry.of("testName", config);
    given(helloWorldService.returnHelloWorld())
        .willThrow(new HelloWorldException());

    Observable.fromCallable(helloWorldService::returnHelloWorld)
        .compose(RetryTransformer.of(retry))
        .test()
        .assertError(HelloWorldException.class)
        .assertNotComplete();

    then(helloWorldService).should().returnHelloWorld();
    Retry.Metrics metrics = retry.getMetrics();
    assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(1);
    assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(0);
}
 
Example 10
Source File: RetryTransformerTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void doNotRetryFromPredicateUsingFlowable() {
    RetryConfig config = RetryConfig.custom()
        .retryOnException(t -> t instanceof IOException)
        .waitDuration(Duration.ofMillis(50))
        .maxAttempts(3).build();
    Retry retry = Retry.of("testName", config);
    given(helloWorldService.returnHelloWorld())
        .willThrow(new HelloWorldException());

    Flowable.fromCallable(helloWorldService::returnHelloWorld)
        .compose(RetryTransformer.of(retry))
        .test()
        .assertError(HelloWorldException.class)
        .assertNotComplete()
        .assertSubscribed();

    then(helloWorldService).should().returnHelloWorld();
    Retry.Metrics metrics = retry.getMetrics();
    assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(1);
    assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(0);
}
 
Example 11
Source File: RetryOperatorTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void doNotRetryFromPredicateUsingMono() {
    RetryConfig config = RetryConfig.custom()
        .retryOnException(t -> t instanceof IOException)
        .waitDuration(Duration.ofMillis(50))
        .maxAttempts(3).build();
    Retry retry = Retry.of("testName", config);
    given(helloWorldService.returnHelloWorld())
        .willThrow(new HelloWorldException());

    StepVerifier.create(Mono.fromCallable(helloWorldService::returnHelloWorld)
        .transformDeferred(RetryOperator.of(retry)))
        .expectSubscription()
        .expectError(HelloWorldException.class)
        .verify(Duration.ofSeconds(1));

    then(helloWorldService).should().returnHelloWorld();
    Retry.Metrics metrics = retry.getMetrics();
    assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(1);
    assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(0);
}
 
Example 12
Source File: SupplierRetryTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldRetryDecoratedTry() {
    given(helloWorldService.returnTry()).willReturn(Try.success("Hello world"));
    final RetryConfig tryAgain = RetryConfig.<String>custom()
        .retryOnResult(s -> s.contains("Hello world"))
        .maxAttempts(2).build();
    Retry retry = Retry.of("id", tryAgain);

    Try<String> result = retry.executeTrySupplier(helloWorldService::returnTry);

    then(helloWorldService).should(times(2)).returnTry();
    assertThat(result.get()).isEqualTo("Hello world");
}
 
Example 13
Source File: RetryTransformerTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void returnOnErrorUsingMaybe() throws InterruptedException {
    RetryConfig config = retryConfig();
    Retry retry = Retry.of("testName", config);
    given(helloWorldService.returnHelloWorld())
        .willThrow(new HelloWorldException());
    Maybe.fromCallable(helloWorldService::returnHelloWorld)
        .compose(RetryTransformer.of(retry))
        .test()
        .await()
        .assertError(HelloWorldException.class)
        .assertNotComplete()
        .assertSubscribed();
    Maybe.fromCallable(helloWorldService::returnHelloWorld)
        .compose(RetryTransformer.of(retry))
        .test()
        .await()
        .assertError(HelloWorldException.class)
        .assertNotComplete()
        .assertSubscribed();

    then(helloWorldService).should(times(6)).returnHelloWorld();

    Retry.Metrics metrics = retry.getMetrics();
    assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(2);
    assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(0);
}
 
Example 14
Source File: SupplierRetryTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldFailToRetryDecoratedTry() {
    given(helloWorldService.returnTry()).willReturn(Try.failure(new HelloWorldException()));
    final RetryConfig tryAgain = RetryConfig.<String>custom()
        .maxAttempts(2).build();
    Retry retry = Retry.of("id", tryAgain);

    Try<String> result = retry.executeTrySupplier(helloWorldService::returnTry);

    then(helloWorldService).should(times(2)).returnTry();
    assertThat(result.isFailure()).isTrue();
    assertThat(result.getCause()).isInstanceOf(HelloWorldException.class);
}
 
Example 15
Source File: SupplierRetryTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldRetryWithResult() {
    given(helloWorldService.returnHelloWorld()).willReturn("Hello world");
    final RetryConfig tryAgain = RetryConfig.<String>custom()
        .retryOnResult(s -> s.contains("Hello world"))
        .maxAttempts(2).build();
    Retry retry = Retry.of("id", tryAgain);
    Supplier<String> supplier = Retry
        .decorateSupplier(retry, helloWorldService::returnHelloWorld);

    String result = supplier.get();

    then(helloWorldService).should(times(2)).returnHelloWorld();
    assertThat(result).isEqualTo("Hello world");
}
 
Example 16
Source File: RetryTransformerTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void returnOnCompleteUsingSingle() throws InterruptedException {
    RetryConfig config = retryConfig();
    Retry retry = Retry.of("testName", config);
    given(helloWorldService.returnHelloWorld())
        .willReturn("Hello world")
        .willThrow(new HelloWorldException())
        .willThrow(new HelloWorldException())
        .willReturn("Hello world");

    Single.fromCallable(helloWorldService::returnHelloWorld)
        .compose(RetryTransformer.of(retry))
        .test()
        .await()
        .assertValueCount(1)
        .assertValues("Hello world")
        .assertComplete();
    Single.fromCallable(helloWorldService::returnHelloWorld)
        .compose(RetryTransformer.of(retry))
        .test()
        .await()
        .assertValueCount(1)
        .assertValues("Hello world")
        .assertComplete();

    then(helloWorldService).should(times(4)).returnHelloWorld();
    Retry.Metrics metrics = retry.getMetrics();
    assertThat(metrics.getNumberOfSuccessfulCallsWithoutRetryAttempt()).isEqualTo(1);
    assertThat(metrics.getNumberOfSuccessfulCallsWithRetryAttempt()).isEqualTo(1);
    assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(0);
    assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(0);
}
 
Example 17
Source File: SupplierRetryTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldIgnoreExceptionOfDecoratedEither() {
    given(helloWorldService.returnEither()).willReturn(Either.left(new HelloWorldException()));
    final RetryConfig tryAgain = RetryConfig.<String>custom()
        .ignoreExceptions(HelloWorldException.class)
        .maxAttempts(2).build();
    Retry retry = Retry.of("id", tryAgain);

    Either<HelloWorldException, String> result = retry
        .executeEitherSupplier(helloWorldService::returnEither);

    then(helloWorldService).should().returnEither();
    assertThat(result.isLeft()).isTrue();
    assertThat(result.getLeft()).isInstanceOf(HelloWorldException.class);
}
 
Example 18
Source File: RetryTransformerTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void returnOnErrorUsingFlowable() throws InterruptedException {
    RetryConfig config = retryConfig();
    Retry retry = Retry.of("testName", config);
    RetryTransformer<Object> retryTransformer = RetryTransformer.of(retry);
    given(helloWorldService.returnHelloWorld())
        .willThrow(new HelloWorldException());

    Flowable.fromCallable(helloWorldService::returnHelloWorld)
        .compose(retryTransformer)
        .test()
        .await()
        .assertError(HelloWorldException.class)
        .assertNotComplete()
        .assertSubscribed();
    Flowable.fromCallable(helloWorldService::returnHelloWorld)
        .compose(retryTransformer)
        .test()
        .await()
        .assertError(HelloWorldException.class)
        .assertNotComplete()
        .assertSubscribed();

    then(helloWorldService).should(times(6)).returnHelloWorld();
    Retry.Metrics metrics = retry.getMetrics();
    assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(2);
    assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(0);
}
 
Example 19
Source File: SupplierRetryTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotRetryWithResult() {
    given(helloWorldService.returnHelloWorld()).willReturn("Hello world");
    final RetryConfig tryAgain = RetryConfig.<String>custom()
        .retryOnResult(s -> s.contains("tryAgain"))
        .maxAttempts(2).build();
    Retry retry = Retry.of("id", tryAgain);
    Supplier<String> supplier = Retry
        .decorateSupplier(retry, helloWorldService::returnHelloWorld);

    String result = supplier.get();
    then(helloWorldService).should().returnHelloWorld();
    assertThat(result).isEqualTo("Hello world");
    assertThat(sleptTime).isEqualTo(0);
}
 
Example 20
Source File: BackoffWithJitterTest.java    From tutorials with MIT License 5 votes vote down vote up
private Function<String, String> getRetryablePingPongFn(IntervalFunction intervalFn) {
    RetryConfig retryConfig = RetryConfig.custom()
            .maxAttempts(MAX_RETRIES)
            .intervalFunction(intervalFn)
            .retryExceptions(PingPongServiceException.class)
            .build();
    Retry retry = Retry.of("pingpong", retryConfig);
    return Retry.decorateFunction(retry, ping -> {
        log.info("Invoked at {}", LocalDateTime.now());
        return service.call(ping);
    });
}