use of io.vavr.CheckedRunnable in project resilience4j by resilience4j.
the class EventPublisherTest method shouldReturnAfterTwoAttempts.
@Test
public void shouldReturnAfterTwoAttempts() {
// Given the HelloWorldService throws an exception
BDDMockito.willThrow(new WebServiceException("BAM!")).willNothing().given(helloWorldService).sayHelloWorld();
// Create a Retry with default configuration
Retry retry = Retry.ofDefaults("id");
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(2)).sayHelloWorld();
// and the result should be a sucess
Assertions.assertThat(result.isSuccess()).isTrue();
Assertions.assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION);
testSubscriber.assertValueCount(1).assertValues(RetryEvent.Type.SUCCESS);
}
use of io.vavr.CheckedRunnable in project resilience4j by resilience4j.
the class RunnableRetryTest method shouldReturnAfterOneAttempt.
@Test
public void shouldReturnAfterOneAttempt() {
// Given the HelloWorldService throws an exception
BDDMockito.willThrow(new WebServiceException("BAM!")).given(helloWorldService).sayHelloWorld();
// Create a Retry with default configuration
RetryConfig config = RetryConfig.custom().maxAttempts(1).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 1 time
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.vavr.CheckedRunnable 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);
}
Aggregations