Search in sources :

Example 6 with Retry

use of io.github.resilience4j.retry.Retry in project resilience4j by resilience4j.

the class RunnableRetryTest method testExecuteRunnable.

@Test
public void testExecuteRunnable() {
    // Create a Retry with default configuration
    Retry retry = Retry.ofDefaults("id");
    // Decorate the invocation of the HelloWorldService
    retry.executeRunnable(helloWorldService::sayHelloWorld);
    // Then the helloWorldService should be invoked 1 time
    BDDMockito.then(helloWorldService).should(Mockito.times(1)).sayHelloWorld();
    Assertions.assertThat(sleptTime).isEqualTo(0);
}
Also used : Retry(io.github.resilience4j.retry.Retry) Test(org.junit.Test)

Example 7 with Retry

use of io.github.resilience4j.retry.Retry in project resilience4j by resilience4j.

the class RunnableRetryTest method shouldTakeIntoAccountBackoffFunction.

@Test
public void shouldTakeIntoAccountBackoffFunction() {
    // Given the HelloWorldService throws an exception
    BDDMockito.willThrow(new WebServiceException("BAM!")).given(helloWorldService).sayHelloWorld();
    // Create a Retry with a backoff function squaring the interval
    RetryConfig config = RetryConfig.custom().intervalFunction(IntervalFunction.of(Duration.ofMillis(500), x -> x * x)).build();
    Retry retry = Retry.of("id", config);
    // Decorate the invocation of the HelloWorldService
    CheckedRunnable retryableRunnable = Retry.decorateCheckedRunnable(retry, helloWorldService::sayHelloWorld);
    // When
    Try<Void> result = Try.run(retryableRunnable);
    // Then the slept time should be according to the backoff function
    BDDMockito.then(helloWorldService).should(Mockito.times(3)).sayHelloWorld();
    Assertions.assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION + RetryConfig.DEFAULT_WAIT_DURATION * RetryConfig.DEFAULT_WAIT_DURATION);
}
Also used : RetryConfig(io.github.resilience4j.retry.RetryConfig) WebServiceException(javax.xml.ws.WebServiceException) CheckedRunnable(io.vavr.CheckedRunnable) Retry(io.github.resilience4j.retry.Retry) Test(org.junit.Test)

Example 8 with Retry

use of io.github.resilience4j.retry.Retry in project resilience4j by resilience4j.

the class RunnableRetryTest method testDecorateRunnable.

@Test
public void testDecorateRunnable() {
    // Given the HelloWorldService throws an exception
    BDDMockito.willThrow(new WebServiceException("BAM!")).given(helloWorldService).sayHelloWorld();
    // Create a Retry with default configuration
    Retry retry = Retry.ofDefaults("id");
    // Decorate the invocation of the HelloWorldService
    Runnable runnable = Retry.decorateRunnable(retry, helloWorldService::sayHelloWorld);
    // When
    Try<Void> result = Try.run(runnable::run);
    // Then the helloWorldService should be invoked 3 times
    BDDMockito.then(helloWorldService).should(Mockito.times(3)).sayHelloWorld();
    // and the result should be a failure
    Assertions.assertThat(result.isFailure()).isTrue();
    // and the returned exception should be of type RuntimeException
    Assertions.assertThat(result.failed().get()).isInstanceOf(WebServiceException.class);
    Assertions.assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION * 2);
}
Also used : WebServiceException(javax.xml.ws.WebServiceException) CheckedRunnable(io.vavr.CheckedRunnable) Retry(io.github.resilience4j.retry.Retry) Test(org.junit.Test)

Example 9 with Retry

use of io.github.resilience4j.retry.Retry in project resilience4j by resilience4j.

the class SupplierRetryTest method shouldReturnAfterThreeAttempts.

@Test
public void shouldReturnAfterThreeAttempts() {
    // Given the HelloWorldService throws an exception
    BDDMockito.given(helloWorldService.returnHelloWorld()).willThrow(new WebServiceException("BAM!"));
    // Create a Retry with default configuration
    Retry retry = Retry.ofDefaults("id");
    // Decorate the invocation of the HelloWorldService
    CheckedFunction0<String> retryableSupplier = Retry.decorateCheckedSupplier(retry, helloWorldService::returnHelloWorld);
    // When
    Try<String> result = Try.of(retryableSupplier);
    // Then the helloWorldService should be invoked 3 times
    BDDMockito.then(helloWorldService).should(Mockito.times(3)).returnHelloWorld();
    // and the result should be a failure
    assertThat(result.isFailure()).isTrue();
    // and the returned exception should be of type RuntimeException
    assertThat(result.failed().get()).isInstanceOf(WebServiceException.class);
    assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION * 2);
}
Also used : WebServiceException(javax.xml.ws.WebServiceException) Retry(io.github.resilience4j.retry.Retry) Test(org.junit.Test)

Example 10 with Retry

use of io.github.resilience4j.retry.Retry in project resilience4j by resilience4j.

the class SupplierRetryTest method shouldReturnAfterThreeAttemptsAndRecover.

@Test
public void shouldReturnAfterThreeAttemptsAndRecover() {
    // Given the HelloWorldService throws an exception
    BDDMockito.given(helloWorldService.returnHelloWorld()).willThrow(new WebServiceException("BAM!"));
    // Create a Retry with default configuration
    Retry retry = Retry.ofDefaults("id");
    // Decorate the invocation of the HelloWorldService
    CheckedFunction0<String> retryableSupplier = Retry.decorateCheckedSupplier(retry, helloWorldService::returnHelloWorld);
    // When
    Try<String> result = Try.of(retryableSupplier).recover((throwable) -> "Hello world from recovery function");
    assertThat(retry.getMetrics().getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(1);
    // Then the helloWorldService should be invoked 3 times
    BDDMockito.then(helloWorldService).should(Mockito.times(3)).returnHelloWorld();
    // and the returned exception should be of type RuntimeException
    assertThat(result.get()).isEqualTo("Hello world from recovery function");
    assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION * 2);
}
Also used : WebServiceException(javax.xml.ws.WebServiceException) Retry(io.github.resilience4j.retry.Retry) Test(org.junit.Test)

Aggregations

Retry (io.github.resilience4j.retry.Retry)39 Test (org.junit.Test)34 WebServiceException (javax.xml.ws.WebServiceException)29 RetryConfig (io.github.resilience4j.retry.RetryConfig)17 CheckedRunnable (io.vavr.CheckedRunnable)9 RetryRegistry (io.github.resilience4j.retry.RetryRegistry)6 IOException (java.io.IOException)4 IntervalFunction (io.github.resilience4j.retry.IntervalFunction)2 HelloWorldService (io.github.resilience4j.test.HelloWorldService)2 API (io.vavr.API)2 Predicates (io.vavr.Predicates)2 Try (io.vavr.control.Try)2 Duration (java.time.Duration)2 RetryHandler (org.apache.servicecomb.governance.handler.RetryHandler)2 Before (org.junit.Before)2 BDDMockito (org.mockito.BDDMockito)2 Mockito (org.mockito.Mockito)2 RxJava2Adapter.toFlowable (io.github.resilience4j.adapter.RxJava2Adapter.toFlowable)1 CircularEventConsumer (io.github.resilience4j.consumer.CircularEventConsumer)1 EventConsumerRegistry (io.github.resilience4j.consumer.EventConsumerRegistry)1