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

The following examples show how to use io.github.resilience4j.retry.Retry#ofDefaults() . 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
@Test
public void testDecorateSupplierAndInvokeTwice() {
    given(helloWorldService.returnHelloWorld())
        .willThrow(new HelloWorldException())
        .willReturn("Hello world")
        .willThrow(new HelloWorldException())
        .willReturn("Hello world");
    Retry retry = Retry.ofDefaults("id");
    Supplier<String> supplier = Retry
        .decorateSupplier(retry, helloWorldService::returnHelloWorld);

    String result = supplier.get();
    String result2 = supplier.get();

    then(helloWorldService).should(times(4)).returnHelloWorld();
    assertThat(result).isEqualTo("Hello world");
    assertThat(result2).isEqualTo("Hello world");
    assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION * 2);
    assertThat(retry.getMetrics().getNumberOfSuccessfulCallsWithRetryAttempt()).isEqualTo(2);
}
 
Example 2
Source File: EventPublisherTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReturnAfterTwoAttempts() {
    willThrow(new HelloWorldException()).willDoNothing().given(helloWorldService)
        .sayHelloWorld();
    Retry retry = Retry.ofDefaults("id");
    TestSubscriber<RetryEvent.Type> testSubscriber = toFlowable(retry.getEventPublisher())
        .map(RetryEvent::getEventType)
        .test();
    CheckedRunnable retryableRunnable = Retry
        .decorateCheckedRunnable(retry, helloWorldService::sayHelloWorld);

    Try<Void> result = Try.run(retryableRunnable);

    then(helloWorldService).should(times(2)).sayHelloWorld();
    assertThat(result.isSuccess()).isTrue();
    assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION);
    testSubscriber.assertValueCount(2)
        .assertValues(RetryEvent.Type.RETRY, RetryEvent.Type.SUCCESS);
}
 
Example 3
Source File: EventPublisherTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReturnAfterThreeAttempts() {
    willThrow(new HelloWorldException()).given(helloWorldService).sayHelloWorld();
    Retry retry = Retry.ofDefaults("id");
    TestSubscriber<RetryEvent.Type> testSubscriber = toFlowable(retry.getEventPublisher())
        .map(RetryEvent::getEventType)
        .test();
    CheckedRunnable retryableRunnable = Retry
        .decorateCheckedRunnable(retry, helloWorldService::sayHelloWorld);

    Try<Void> result = Try.run(retryableRunnable);

    then(helloWorldService).should(times(3)).sayHelloWorld();
    assertThat(result.isFailure()).isTrue();
    assertThat(result.failed().get()).isInstanceOf(HelloWorldException.class);
    assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION * 2);
    testSubscriber.assertValueCount(3)
        .assertValues(RetryEvent.Type.RETRY, RetryEvent.Type.RETRY, RetryEvent.Type.ERROR);
}
 
Example 4
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 5
Source File: RunnableRetryTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnAfterThreeAttempts() {
    willThrow(new HelloWorldException()).given(helloWorldService).sayHelloWorld();
    Retry retry = Retry.ofDefaults("id");
    CheckedRunnable retryableRunnable = Retry
        .decorateCheckedRunnable(retry, helloWorldService::sayHelloWorld);

    Try<Void> result = Try.run(retryableRunnable);

    then(helloWorldService).should(times(3)).sayHelloWorld();
    assertThat(result.isFailure()).isTrue();
    assertThat(result.failed().get()).isInstanceOf(HelloWorldException.class);
    assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION * 2);
}
 
Example 6
Source File: RunnableRetryTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testExecuteRunnable() {
    Retry retry = Retry.ofDefaults("id");

    retry.executeRunnable(helloWorldService::sayHelloWorld);

    then(helloWorldService).should().sayHelloWorld();
    assertThat(sleptTime).isEqualTo(0);
}
 
Example 7
Source File: RunnableRetryTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testDecorateRunnable() {
    willThrow(new HelloWorldException()).given(helloWorldService).sayHelloWorld();
    Retry retry = Retry.ofDefaults("id");
    Runnable runnable = Retry.decorateRunnable(retry, helloWorldService::sayHelloWorld);

    Try<Void> result = Try.run(runnable::run);

    then(helloWorldService).should(times(3)).sayHelloWorld();
    assertThat(result.isFailure()).isTrue();
    assertThat(result.failed().get()).isInstanceOf(HelloWorldException.class);
    assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION * 2);
}
 
Example 8
Source File: RunnableRetryTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotRetry() {
    Retry retryContext = Retry.ofDefaults("id");
    Runnable runnable = Retry.decorateRunnable(retryContext, helloWorldService::sayHelloWorld);

    runnable.run();

    then(helloWorldService).should().sayHelloWorld();
    assertThat(sleptTime).isEqualTo(0);
}
 
Example 9
Source File: RxJava2RetryAspectExtTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testReactorTypes() throws Throwable {
    Retry retry = Retry.ofDefaults("test");

    when(proceedingJoinPoint.proceed()).thenReturn(Single.just("Test"));
    assertThat(rxJava2RetryAspectExt.handle(proceedingJoinPoint, retry, "testMethod"))
        .isNotNull();

    when(proceedingJoinPoint.proceed()).thenReturn(Flowable.just("Test"));
    assertThat(rxJava2RetryAspectExt.handle(proceedingJoinPoint, retry, "testMethod"))
        .isNotNull();
}
 
Example 10
Source File: CompletionStageRetryTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void shouldRethrowExceptionInCaseOfExceptionAtSyncStage() {
    given(helloWorldService.returnHelloWorld())
        .willThrow(new IllegalArgumentException("BAM!"));
    Retry retry = Retry.ofDefaults("id");

    retry.executeCompletionStage(
        scheduler,
        () -> helloWorldService.returnHelloWorld());
}
 
Example 11
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 12
Source File: Resilience4jFeignRetryTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    retry = Retry.ofDefaults("test");
    final FeignDecorators decorators = FeignDecorators.builder()
        .withRetry(retry)
        .build();
    testService = Resilience4jFeign.builder(decorators)
        .target(TestService.class, MOCK_URL);
}
 
Example 13
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 14
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 15
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 16
Source File: ReactorRetryAspectExtTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testReactorTypes() throws Throwable {
    Retry retry = Retry.ofDefaults("test");

    when(proceedingJoinPoint.proceed()).thenReturn(Mono.just("Test"));
    assertThat(reactorRetryAspectExt.handle(proceedingJoinPoint, retry, "testMethod"))
        .isNotNull();

    when(proceedingJoinPoint.proceed()).thenReturn(Flux.just("Test"));
    assertThat(reactorRetryAspectExt.handle(proceedingJoinPoint, retry, "testMethod"))
        .isNotNull();
}
 
Example 17
Source File: SupplierRetryTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testExecuteCallable() throws Exception {
    given(helloWorldService.returnHelloWorldWithException())
        .willThrow(new HelloWorldException())
        .willReturn("Hello world");
    Retry retry = Retry.ofDefaults("id");

    String result = retry.executeCallable(helloWorldService::returnHelloWorldWithException);

    then(helloWorldService).should(times(2)).returnHelloWorldWithException();
    assertThat(result).isEqualTo("Hello world");
    assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION);
}
 
Example 18
Source File: SupplierRetryTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testDecorateCallable() throws Exception {
    given(helloWorldService.returnHelloWorldWithException())
        .willThrow(new HelloWorldException())
        .willReturn("Hello world");
    Retry retry = Retry.ofDefaults("id");
    Callable<String> callable = Retry
        .decorateCallable(retry, helloWorldService::returnHelloWorldWithException);

    String result = callable.call();

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

    String result = supplier.get();

    then(helloWorldService).should(times(2)).returnHelloWorld();
    assertThat(result).isEqualTo("Hello world");
    assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION);
}
 
Example 20
Source File: SupplierRetryTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotRetry() {
    given(helloWorldService.returnHelloWorld()).willReturn("Hello world");
    Retry retry = Retry.ofDefaults("id");
    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);
}