Search in sources :

Example 36 with Retry

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

the class SupplierRetryTest method shouldReturnSuccessfullyAfterSecondAttempt.

@Test
public void shouldReturnSuccessfullyAfterSecondAttempt() {
    // Given the HelloWorldService throws an exception
    BDDMockito.given(helloWorldService.returnHelloWorld()).willThrow(new WebServiceException("BAM!")).willReturn("Hello world");
    // 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 2 times
    BDDMockito.then(helloWorldService).should(Mockito.times(2)).returnHelloWorld();
    assertThat(result.get()).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 37 with Retry

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

the class RetryTransformerTest method shouldReturnOnErrorUsingSingle.

@Test
public void shouldReturnOnErrorUsingSingle() {
    // Given
    RetryConfig config = RetryConfig.ofDefaults();
    Retry retry = Retry.of("testName", config);
    RetryTransformer<Object> retryTransformer = RetryTransformer.of(retry);
    given(helloWorldService.returnHelloWorld()).willThrow(new WebServiceException("BAM!"));
    // When
    Single.fromCallable(helloWorldService::returnHelloWorld).compose(retryTransformer).test().assertError(WebServiceException.class).assertNotComplete().assertSubscribed();
    Single.fromCallable(helloWorldService::returnHelloWorld).compose(retryTransformer).test().assertError(WebServiceException.class).assertNotComplete().assertSubscribed();
    // Then
    BDDMockito.then(helloWorldService).should(Mockito.times(6)).returnHelloWorld();
    Retry.Metrics metrics = retry.getMetrics();
    assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(2);
    assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).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 38 with Retry

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

the class RetryTransformerTest method shouldReturnOnErrorUsingObservable.

@Test
public void shouldReturnOnErrorUsingObservable() {
    // Given
    RetryConfig config = RetryConfig.ofDefaults();
    Retry retry = Retry.of("testName", config);
    RetryTransformer<Object> retryTransformer = RetryTransformer.of(retry);
    given(helloWorldService.returnHelloWorld()).willThrow(new WebServiceException("BAM!"));
    // When
    Observable.fromCallable(helloWorldService::returnHelloWorld).compose(retryTransformer).test().assertError(WebServiceException.class).assertNotComplete().assertSubscribed();
    Observable.fromCallable(helloWorldService::returnHelloWorld).compose(retryTransformer).test().assertError(WebServiceException.class).assertNotComplete().assertSubscribed();
    // Then
    BDDMockito.then(helloWorldService).should(Mockito.times(6)).returnHelloWorld();
    Retry.Metrics metrics = retry.getMetrics();
    assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(2);
    assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).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 39 with Retry

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

the class RetryTransformerTest method shouldNotRetryFromPredicateUsingSingle.

@Test
public void shouldNotRetryFromPredicateUsingSingle() {
    // Given
    RetryConfig config = RetryConfig.custom().retryOnException(t -> t instanceof IOException).maxAttempts(3).build();
    Retry retry = Retry.of("testName", config);
    given(helloWorldService.returnHelloWorld()).willThrow(new WebServiceException("BAM!"));
    // When
    Single.fromCallable(helloWorldService::returnHelloWorld).compose(RetryTransformer.of(retry)).test().assertError(WebServiceException.class).assertNotComplete().assertSubscribed();
    // Then
    BDDMockito.then(helloWorldService).should(Mockito.times(1)).returnHelloWorld();
    Retry.Metrics metrics = retry.getMetrics();
    assertThat(metrics.getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(1);
    assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(0);
}
Also used : RetryConfig(io.github.resilience4j.retry.RetryConfig) WebServiceException(javax.xml.ws.WebServiceException) IOException(java.io.IOException) 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