Search in sources :

Example 6 with RetryConfig

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

the class RetryTransformerTest method shouldNotRetryFromPredicateUsingObservable.

@Test
public void shouldNotRetryFromPredicateUsingObservable() {
    // 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
    Observable.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)

Example 7 with RetryConfig

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

the class RetryTransformerTest method shouldReturnOnErrorUsingFlowable.

@Test
public void shouldReturnOnErrorUsingFlowable() {
    // 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
    Flowable.fromCallable(helloWorldService::returnHelloWorld).compose(retryTransformer).test().assertError(WebServiceException.class).assertNotComplete().assertSubscribed();
    Flowable.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 8 with RetryConfig

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

the class RetryTransformerTest method shouldReturnOnCompleteUsingSingle.

@Test
public void shouldReturnOnCompleteUsingSingle() {
    // Given
    RetryConfig config = RetryConfig.ofDefaults();
    Retry retry = Retry.of("testName", config);
    RetryTransformer<Object> retryTransformer = RetryTransformer.of(retry);
    given(helloWorldService.returnHelloWorld()).willReturn("Hello world").willThrow(new WebServiceException("BAM!")).willThrow(new WebServiceException("BAM!")).willReturn("Hello world");
    // When
    Single.fromCallable(helloWorldService::returnHelloWorld).compose(retryTransformer).test().assertValueCount(1).assertValues("Hello world").assertComplete();
    Single.fromCallable(helloWorldService::returnHelloWorld).compose(retryTransformer).test().assertValueCount(1).assertValues("Hello world").assertComplete();
    // Then
    BDDMockito.then(helloWorldService).should(Mockito.times(4)).returnHelloWorld();
    Retry.Metrics metrics = retry.getMetrics();
    assertThat(metrics.getNumberOfSuccessfulCallsWithoutRetryAttempt()).isEqualTo(1);
    assertThat(metrics.getNumberOfSuccessfulCallsWithRetryAttempt()).isEqualTo(1);
    assertThat(metrics.getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(0);
    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 9 with RetryConfig

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

the class RetryTransformerTest method shouldNotRetryFromPredicateUsingFlowable.

@Test
public void shouldNotRetryFromPredicateUsingFlowable() {
    // 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
    Flowable.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)

Example 10 with RetryConfig

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

the class RetryTransformerTest method shouldReturnOnCompleteUsingObservable.

@Test
public void shouldReturnOnCompleteUsingObservable() {
    // 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)

Aggregations

Retry (io.github.resilience4j.retry.Retry)16 RetryConfig (io.github.resilience4j.retry.RetryConfig)16 WebServiceException (javax.xml.ws.WebServiceException)16 Test (org.junit.Test)16 CheckedRunnable (io.vavr.CheckedRunnable)4 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 Before (org.junit.Before)2 BDDMockito (org.mockito.BDDMockito)2 Mockito (org.mockito.Mockito)2 API.$ (io.vavr.API.$)1 CheckedFunction0 (io.vavr.CheckedFunction0)1 Duration (java.time.Duration)1 Callable (java.util.concurrent.Callable)1 Supplier (java.util.function.Supplier)1 Assertions (org.assertj.core.api.Assertions)1