use of io.github.resilience4j.retry.Retry in project resilience4j by resilience4j.
the class RunnableRetryTest method testExecuteRunnable.
@Test
public void testExecuteRunnable() {
// Create a Retry with default configuration
Retry retry = Retry.ofDefaults("id");
// Decorate the invocation of the HelloWorldService
retry.executeRunnable(helloWorldService::sayHelloWorld);
// Then the helloWorldService should be invoked 1 time
BDDMockito.then(helloWorldService).should(Mockito.times(1)).sayHelloWorld();
Assertions.assertThat(sleptTime).isEqualTo(0);
}
use of io.github.resilience4j.retry.Retry 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.Retry in project resilience4j by resilience4j.
the class RunnableRetryTest method testDecorateRunnable.
@Test
public void testDecorateRunnable() {
// Given the HelloWorldService throws an exception
BDDMockito.willThrow(new WebServiceException("BAM!")).given(helloWorldService).sayHelloWorld();
// Create a Retry with default configuration
Retry retry = Retry.ofDefaults("id");
// Decorate the invocation of the HelloWorldService
Runnable runnable = Retry.decorateRunnable(retry, helloWorldService::sayHelloWorld);
// When
Try<Void> result = Try.run(runnable::run);
// Then the helloWorldService should be invoked 3 times
BDDMockito.then(helloWorldService).should(Mockito.times(3)).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(RetryConfig.DEFAULT_WAIT_DURATION * 2);
}
use of io.github.resilience4j.retry.Retry in project resilience4j by resilience4j.
the class SupplierRetryTest method shouldReturnAfterThreeAttempts.
@Test
public void shouldReturnAfterThreeAttempts() {
// Given the HelloWorldService throws an exception
BDDMockito.given(helloWorldService.returnHelloWorld()).willThrow(new WebServiceException("BAM!"));
// 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 3 times
BDDMockito.then(helloWorldService).should(Mockito.times(3)).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(RetryConfig.DEFAULT_WAIT_DURATION * 2);
}
use of io.github.resilience4j.retry.Retry in project resilience4j by resilience4j.
the class SupplierRetryTest method shouldReturnAfterThreeAttemptsAndRecover.
@Test
public void shouldReturnAfterThreeAttemptsAndRecover() {
// Given the HelloWorldService throws an exception
BDDMockito.given(helloWorldService.returnHelloWorld()).willThrow(new WebServiceException("BAM!"));
// 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).recover((throwable) -> "Hello world from recovery function");
assertThat(retry.getMetrics().getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(1);
// Then the helloWorldService should be invoked 3 times
BDDMockito.then(helloWorldService).should(Mockito.times(3)).returnHelloWorld();
// and the returned exception should be of type RuntimeException
assertThat(result.get()).isEqualTo("Hello world from recovery function");
assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION * 2);
}
Aggregations