Search in sources :

Example 31 with Retry

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

the class RunnableRetryTest method shouldReturnAfterThreeAttempts.

@Test
public void shouldReturnAfterThreeAttempts() {
    // 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
    CheckedRunnable retryableRunnable = Retry.decorateCheckedRunnable(retry, helloWorldService::sayHelloWorld);
    // When
    Try<Void> result = Try.run(retryableRunnable);
    // 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 32 with Retry

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

the class SupplierRetryTest method shouldReturnAfterOneAttempt.

@Test
public void shouldReturnAfterOneAttempt() {
    // Given the HelloWorldService throws an exception
    BDDMockito.given(helloWorldService.returnHelloWorld()).willThrow(new WebServiceException("BAM!"));
    // Create a Retry with custom configuration
    RetryConfig config = RetryConfig.custom().maxAttempts(1).build();
    Retry retry = Retry.of("id", config);
    // 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 1 time
    BDDMockito.then(helloWorldService).should(Mockito.times(1)).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(0);
}
Also used : RetryConfig(io.github.resilience4j.retry.RetryConfig) WebServiceException(javax.xml.ws.WebServiceException) Retry(io.github.resilience4j.retry.Retry) Test(org.junit.Test)

Example 33 with Retry

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

the class SupplierRetryTest method testDecorateCallable.

@Test
public void testDecorateCallable() throws Exception {
    // Given the HelloWorldService throws an exception
    BDDMockito.given(helloWorldService.returnHelloWorldWithException()).willThrow(new WebServiceException("BAM!")).willReturn("Hello world");
    // Create a Retry with default configuration
    Retry retry = Retry.ofDefaults("id");
    // Decorate the invocation of the HelloWorldService
    Callable<String> callable = Retry.decorateCallable(retry, helloWorldService::returnHelloWorldWithException);
    // When
    String result = callable.call();
    // Then the helloWorldService should be invoked 2 times
    BDDMockito.then(helloWorldService).should(Mockito.times(2)).returnHelloWorldWithException();
    assertThat(result).isEqualTo("Hello world");
    assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION);
}
Also used : WebServiceException(javax.xml.ws.WebServiceException) Retry(io.github.resilience4j.retry.Retry) Test(org.junit.Test)

Example 34 with Retry

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

the class SupplierRetryTest method shouldNotRetry.

@Test
public void shouldNotRetry() {
    // Given the HelloWorldService returns Hello world
    BDDMockito.given(helloWorldService.returnHelloWorld()).willReturn("Hello world");
    // Create a Retry with default configuration
    Retry retry = Retry.ofDefaults("id");
    // Decorate the invocation of the HelloWorldService
    Supplier<String> supplier = Retry.decorateSupplier(retry, helloWorldService::returnHelloWorld);
    // When
    String result = supplier.get();
    // Then the helloWorldService should be invoked 1 time
    BDDMockito.then(helloWorldService).should(Mockito.times(1)).returnHelloWorld();
    assertThat(result).isEqualTo("Hello world");
    assertThat(sleptTime).isEqualTo(0);
}
Also used : Retry(io.github.resilience4j.retry.Retry) Test(org.junit.Test)

Example 35 with Retry

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

the class SupplierRetryTest method testDecorateSupplierAndInvokeTwice.

@Test
public void testDecorateSupplierAndInvokeTwice() {
    // Given the HelloWorldService throws an exception
    BDDMockito.given(helloWorldService.returnHelloWorld()).willThrow(new WebServiceException("BAM!")).willReturn("Hello world").willThrow(new WebServiceException("BAM!")).willReturn("Hello world");
    // Create a Retry with default configuration
    Retry retry = Retry.ofDefaults("id");
    // Decorate the invocation of the HelloWorldService
    Supplier<String> supplier = Retry.decorateSupplier(retry, helloWorldService::returnHelloWorld);
    // When
    String result = supplier.get();
    String result2 = supplier.get();
    // Then the helloWorldService should be invoked 2 times
    BDDMockito.then(helloWorldService).should(Mockito.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);
}
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