use of io.github.resilience4j.retry.Retry in project resilience4j by resilience4j.
the class RunnableRetryTest method shouldReturnAfterThreeAttempts.
@Test
public void shouldReturnAfterThreeAttempts() {
// 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
CheckedRunnable retryableRunnable = Retry.decorateCheckedRunnable(retry, helloWorldService::sayHelloWorld);
// When
Try<Void> result = Try.run(retryableRunnable);
// 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 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);
}
use of io.github.resilience4j.retry.Retry in project resilience4j by resilience4j.
the class SupplierRetryTest method testDecorateCallable.
@Test
public void testDecorateCallable() 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
Callable<String> callable = Retry.decorateCallable(retry, helloWorldService::returnHelloWorldWithException);
// When
String result = callable.call();
// 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);
}
use of io.github.resilience4j.retry.Retry in project resilience4j by resilience4j.
the class SupplierRetryTest method shouldNotRetry.
@Test
public void shouldNotRetry() {
// Given the HelloWorldService returns Hello world
BDDMockito.given(helloWorldService.returnHelloWorld()).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 1 time
BDDMockito.then(helloWorldService).should(Mockito.times(1)).returnHelloWorld();
assertThat(result).isEqualTo("Hello world");
assertThat(sleptTime).isEqualTo(0);
}
use of io.github.resilience4j.retry.Retry in project resilience4j by resilience4j.
the class SupplierRetryTest method testDecorateSupplierAndInvokeTwice.
@Test
public void testDecorateSupplierAndInvokeTwice() {
// Given the HelloWorldService throws an exception
BDDMockito.given(helloWorldService.returnHelloWorld()).willThrow(new WebServiceException("BAM!")).willReturn("Hello world").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();
String result2 = supplier.get();
// Then the helloWorldService should be invoked 2 times
BDDMockito.then(helloWorldService).should(Mockito.times(4)).returnHelloWorld();
assertThat(result).isEqualTo("Hello world");
assertThat(result2).isEqualTo("Hello world");
assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION * 2);
assertThat(retry.getMetrics().getNumberOfSuccessfulCallsWithRetryAttempt()).isEqualTo(2);
}
Aggregations