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

The following examples show how to use io.github.resilience4j.retry.Retry#decorateCheckedSupplier() . 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 7 votes vote down vote up
@Test
public void shouldReturnAfterOneAttemptAndIgnoreException() {
    given(helloWorldService.returnHelloWorld()).willThrow(new HelloWorldException());
    RetryConfig config = RetryConfig.custom()
        .retryOnException(throwable -> API.Match(throwable).of(
            API.Case($(Predicates.instanceOf(HelloWorldException.class)), false),
            API.Case($(), true)))
        .build();
    Retry retry = Retry.of("id", config);
    CheckedFunction0<String> retryableSupplier = Retry
        .decorateCheckedSupplier(retry, helloWorldService::returnHelloWorld);

    Try<String> result = Try.of(retryableSupplier);

    // Because the exception should be rethrown immediately.
    then(helloWorldService).should().returnHelloWorld();
    assertThat(result.isFailure()).isTrue();
    assertThat(result.failed().get()).isInstanceOf(HelloWorldException.class);
    assertThat(sleptTime).isEqualTo(0);
}
 
Example 2
Source File: SupplierRetryTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReturnAfterThreeAttemptsAndRecoverWithResult() {
    given(helloWorldService.returnHelloWorld())
        .willThrow(new HelloWorldException())
        .willReturn("Hello world")
        .willThrow(new HelloWorldException());
    final RetryConfig tryAgain = RetryConfig.<String>custom()
        .retryOnResult(s -> s.contains("Hello world"))
        .maxAttempts(3).build();
    Retry retry = Retry.of("id", tryAgain);
    CheckedFunction0<String> retryableSupplier = Retry
        .decorateCheckedSupplier(retry, helloWorldService::returnHelloWorld);

    Try<String> result = Try.of(retryableSupplier)
        .recover((throwable) -> "Hello world from recovery function");

    then(helloWorldService).should(times(3)).returnHelloWorld();
    assertThat(result.get()).isEqualTo("Hello world from recovery function");
    assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION * 2);
}
 
Example 3
Source File: SupplierRetryTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldTakeIntoAccountBackoffFunction() {
    given(helloWorldService.returnHelloWorld()).willThrow(new HelloWorldException());
    RetryConfig config = RetryConfig.custom()
        .intervalFunction(IntervalFunction.ofExponentialBackoff(500, 2.0))
        .build();
    Retry retry = Retry.of("id", config);
    CheckedFunction0<String> retryableSupplier = Retry
        .decorateCheckedSupplier(retry, helloWorldService::returnHelloWorld);

    Try.of(retryableSupplier);

    then(helloWorldService).should(times(3)).returnHelloWorld();
    assertThat(sleptTime).isEqualTo(
        RetryConfig.DEFAULT_WAIT_DURATION +
            RetryConfig.DEFAULT_WAIT_DURATION * 2);
}
 
Example 4
Source File: SupplierRetryTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnSuccessfullyAfterSecondAttempt() {
    given(helloWorldService.returnHelloWorld())
        .willThrow(new HelloWorldException())
        .willReturn("Hello world");
    Retry retry = Retry.ofDefaults("id");
    CheckedFunction0<String> retryableSupplier = Retry
        .decorateCheckedSupplier(retry, helloWorldService::returnHelloWorld);

    Try<String> result = Try.of(retryableSupplier);

    then(helloWorldService).should(times(2)).returnHelloWorld();
    assertThat(result.get()).isEqualTo("Hello world");
    assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION);
}
 
Example 5
Source File: SupplierRetryTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnAfterThreeAttempts() {
    given(helloWorldService.returnHelloWorld()).willThrow(new HelloWorldException());
    Retry retry = Retry.ofDefaults("id");
    CheckedFunction0<String> retryableSupplier = Retry
        .decorateCheckedSupplier(retry, helloWorldService::returnHelloWorld);

    Try<String> result = Try.of(retryableSupplier);

    then(helloWorldService).should(times(3)).returnHelloWorld();
    assertThat(result.isFailure()).isTrue();
    assertThat(result.failed().get()).isInstanceOf(HelloWorldException.class);
    assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION * 2);
}
 
Example 6
Source File: SupplierRetryTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnAfterOneAttempt() {
    given(helloWorldService.returnHelloWorld()).willThrow(new HelloWorldException());
    RetryConfig config = RetryConfig.custom().maxAttempts(1).build();
    Retry retry = Retry.of("id", config);
    CheckedFunction0<String> retryableSupplier = Retry
        .decorateCheckedSupplier(retry, helloWorldService::returnHelloWorld);

    Try<String> result = Try.of(retryableSupplier);

    then(helloWorldService).should().returnHelloWorld();
    assertThat(result.isFailure()).isTrue();
    assertThat(result.failed().get()).isInstanceOf(HelloWorldException.class);
    assertThat(sleptTime).isEqualTo(0);
}
 
Example 7
Source File: SupplierRetryTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnAfterThreeAttemptsAndRecover() {
    given(helloWorldService.returnHelloWorld()).willThrow(new HelloWorldException());
    Retry retry = Retry.ofDefaults("id");
    CheckedFunction0<String> retryableSupplier = Retry
        .decorateCheckedSupplier(retry, helloWorldService::returnHelloWorld);

    Try<String> result = Try.of(retryableSupplier)
        .recover((throwable) -> "Hello world from recovery function");
    assertThat(retry.getMetrics().getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(1);

    then(helloWorldService).should(times(3)).returnHelloWorld();
    assertThat(result.get()).isEqualTo("Hello world from recovery function");
    assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION * 2);
}
 
Example 8
Source File: Decorators.java    From resilience4j with Apache License 2.0 4 votes vote down vote up
public DecorateCheckedSupplier<T> withRetry(Retry retryContext) {
    supplier = Retry.decorateCheckedSupplier(retryContext, supplier);
    return this;
}