use of io.github.resilience4j.retry.RetryConfig in project resilience4j by resilience4j.
the class EventPublisherTest method shouldIgnoreError.
@Test
public void shouldIgnoreError() {
// Given the HelloWorldService throws an exception
BDDMockito.willThrow(new WebServiceException("BAM!")).willNothing().given(helloWorldService).sayHelloWorld();
// Create a Retry with default configuration
RetryConfig config = RetryConfig.custom().retryOnException(t -> t instanceof IOException).maxAttempts(3).build();
Retry retry = Retry.of("id", config);
TestSubscriber<RetryEvent.Type> testSubscriber = toFlowable(retry.getEventPublisher()).map(RetryEvent::getEventType).test();
// 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 2 times
BDDMockito.then(helloWorldService).should(Mockito.times(1)).sayHelloWorld();
// and the result should be a sucess
Assertions.assertThat(result.isFailure()).isTrue();
Assertions.assertThat(sleptTime).isEqualTo(0);
testSubscriber.assertValueCount(1).assertValues(RetryEvent.Type.IGNORED_ERROR);
}
use of io.github.resilience4j.retry.RetryConfig in project resilience4j by resilience4j.
the class RunnableRetryTest method shouldReturnAfterOneAttemptAndIgnoreException.
@Test
public void shouldReturnAfterOneAttemptAndIgnoreException() {
// Given the HelloWorldService throws an exception
BDDMockito.willThrow(new WebServiceException("BAM!")).given(helloWorldService).sayHelloWorld();
// Create a Retry with default configuration
RetryConfig config = RetryConfig.custom().retryOnException(throwable -> Match(throwable).of(Case($(Predicates.instanceOf(WebServiceException.class)), false), Case($(), true))).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 only once, because the exception should be rethrown immediately.
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);
}
use of io.github.resilience4j.retry.RetryConfig in project resilience4j by resilience4j.
the class RunnableRetryTest method shouldTakeIntoAccountBackoffFunction.
@Test
public void shouldTakeIntoAccountBackoffFunction() {
// Given the HelloWorldService throws an exception
BDDMockito.willThrow(new WebServiceException("BAM!")).given(helloWorldService).sayHelloWorld();
// Create a Retry with a backoff function squaring the interval
RetryConfig config = RetryConfig.custom().intervalFunction(IntervalFunction.of(Duration.ofMillis(500), x -> x * x)).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 slept time should be according to the backoff function
BDDMockito.then(helloWorldService).should(Mockito.times(3)).sayHelloWorld();
Assertions.assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION + RetryConfig.DEFAULT_WAIT_DURATION * RetryConfig.DEFAULT_WAIT_DURATION);
}
use of io.github.resilience4j.retry.RetryConfig in project resilience4j by resilience4j.
the class SupplierRetryTest method shouldTakeIntoAccountBackoffFunction.
@Test
public void shouldTakeIntoAccountBackoffFunction() {
// Given the HelloWorldService throws an exception
BDDMockito.given(helloWorldService.returnHelloWorld()).willThrow(new WebServiceException("BAM!"));
// Create a Retry with a backoff function doubling the interval
RetryConfig config = RetryConfig.custom().intervalFunction(IntervalFunction.ofExponentialBackoff(500, 2.0)).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 slept time should be according to the backoff function
BDDMockito.then(helloWorldService).should(Mockito.times(3)).returnHelloWorld();
assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION + RetryConfig.DEFAULT_WAIT_DURATION * 2);
}
use of io.github.resilience4j.retry.RetryConfig in project resilience4j by resilience4j.
the class SupplierRetryTest method shouldReturnAfterOneAttemptAndIgnoreException.
@Test
public void shouldReturnAfterOneAttemptAndIgnoreException() {
// Given the HelloWorldService throws an exception
BDDMockito.given(helloWorldService.returnHelloWorld()).willThrow(new WebServiceException("BAM!"));
// Create a Retry with default configuration
RetryConfig config = RetryConfig.custom().retryOnException(throwable -> API.Match(throwable).of(API.Case($(Predicates.instanceOf(WebServiceException.class)), false), API.Case($(), true))).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 only once, because the exception should be rethrown immediately.
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);
}
Aggregations