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);
}
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);
}
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);
}
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);
}
Aggregations