use of io.github.resilience4j.retry.Retry in project resilience4j by resilience4j.
the class SupplierRetryTest method testExecuteSupplier.
@Test
public void testExecuteSupplier() {
// 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
String result = retry.executeSupplier(helloWorldService::returnHelloWorld);
// Then the helloWorldService should be invoked 2 times
BDDMockito.then(helloWorldService).should(Mockito.times(2)).returnHelloWorld();
assertThat(result).isEqualTo("Hello world");
assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION);
}
use of io.github.resilience4j.retry.Retry 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.Retry in project resilience4j by resilience4j.
the class SupplierRetryTest method testDecorateSupplier.
@Test
public void testDecorateSupplier() {
// 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
Supplier<String> supplier = Retry.decorateSupplier(retry, helloWorldService::returnHelloWorld);
// When
String result = supplier.get();
// Then the helloWorldService should be invoked 2 times
BDDMockito.then(helloWorldService).should(Mockito.times(2)).returnHelloWorld();
assertThat(result).isEqualTo("Hello world");
assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION);
}
use of io.github.resilience4j.retry.Retry 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);
}
use of io.github.resilience4j.retry.Retry in project resilience4j by resilience4j.
the class SupplierRetryTest method testExecuteCallable.
@Test
public void testExecuteCallable() throws Exception {
// Given the HelloWorldService throws an exception
BDDMockito.given(helloWorldService.returnHelloWorldWithException()).willThrow(new WebServiceException("BAM!")).willReturn("Hello world");
// Create a Retry with default configuration
Retry retry = Retry.ofDefaults("id");
// Decorate the invocation of the HelloWorldService
String result = retry.executeCallable(helloWorldService::returnHelloWorldWithException);
// Then the helloWorldService should be invoked 2 times
BDDMockito.then(helloWorldService).should(Mockito.times(2)).returnHelloWorldWithException();
assertThat(result).isEqualTo("Hello world");
assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION);
}
Aggregations