Search in sources :

Example 11 with RetryConfig

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

the class RetryTransformerTest method shouldReturnOnCompleteUsingFlowable.

@Test
public void shouldReturnOnCompleteUsingFlowable() {
    // 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 12 with RetryConfig

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

the class RunnableRetryTest method shouldReturnAfterOneAttempt.

@Test
public void shouldReturnAfterOneAttempt() {
    // Given the HelloWorldService throws an exception
    BDDMockito.willThrow(new WebServiceException("BAM!")).given(helloWorldService).sayHelloWorld();
    // Create a Retry with default configuration
    RetryConfig config = RetryConfig.custom().maxAttempts(1).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 helloWorldService should be invoked 1 time
    BDDMockito.then(helloWorldService).should(Mockito.times(1)).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(0);
}
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 13 with RetryConfig

use of io.github.resilience4j.retry.RetryConfig 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 14 with RetryConfig

use of io.github.resilience4j.retry.RetryConfig 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 15 with RetryConfig

use of io.github.resilience4j.retry.RetryConfig 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)

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