use of io.vavr.CheckedFunction0 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.vavr.CheckedFunction0 in project resilience4j by resilience4j.
the class RateLimiterTest method decorateCheckedSupplier.
@Test
public void decorateCheckedSupplier() throws Throwable {
CheckedFunction0 supplier = mock(CheckedFunction0.class);
CheckedFunction0 decorated = RateLimiter.decorateCheckedSupplier(limit, supplier);
when(limit.getPermission(config.getTimeoutDuration())).thenReturn(false);
Try decoratedSupplierResult = Try.of(decorated);
then(decoratedSupplierResult.isFailure()).isTrue();
then(decoratedSupplierResult.getCause()).isInstanceOf(RequestNotPermitted.class);
verify(supplier, never()).apply();
when(limit.getPermission(config.getTimeoutDuration())).thenReturn(true);
Try secondSupplierResult = Try.of(decorated);
then(secondSupplierResult.isSuccess()).isTrue();
verify(supplier, times(1)).apply();
}
Aggregations