use of io.github.resilience4j.core.functions.CheckedSupplier in project resilience4j by resilience4j.
the class DecoratorsTest method testDecorateCheckedSupplierWithFallback.
@Test
public void testDecorateCheckedSupplierWithFallback() throws Throwable {
CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend");
circuitBreaker.transitionToOpenState();
CheckedSupplier<String> checkedSupplier = Decorators.ofCheckedSupplier(() -> helloWorldService.returnHelloWorldWithException()).withCircuitBreaker(circuitBreaker).withFallback(CallNotPermittedException.class, e -> "Fallback").decorate();
String result = checkedSupplier.get();
assertThat(result).isEqualTo("Fallback");
CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
assertThat(metrics.getNumberOfNotPermittedCalls()).isEqualTo(1);
then(helloWorldService).should(never()).returnHelloWorld();
}
use of io.github.resilience4j.core.functions.CheckedSupplier in project resilience4j by resilience4j.
the class SupplierRetryTest method shouldReturnAfterOneAttemptAndIgnoreException.
@Test
public void shouldReturnAfterOneAttemptAndIgnoreException() {
given(helloWorldService.returnHelloWorld()).willThrow(new HelloWorldException());
RetryConfig config = RetryConfig.custom().retryOnException(throwable -> API.Match(throwable).of(API.Case($(Predicates.instanceOf(HelloWorldException.class)), false), API.Case($(), true))).build();
Retry retry = Retry.of("id", config);
CheckedSupplier<String> retryableSupplier = Retry.decorateCheckedSupplier(retry, helloWorldService::returnHelloWorld);
Try<String> result = Try.of(() -> retryableSupplier.get());
// Because the exception should be rethrown immediately.
then(helloWorldService).should().returnHelloWorld();
assertThat(result.isFailure()).isTrue();
assertThat(result.failed().get()).isInstanceOf(HelloWorldException.class);
assertThat(sleptTime).isEqualTo(0);
}
use of io.github.resilience4j.core.functions.CheckedSupplier in project resilience4j by resilience4j.
the class RateLimiterTest method decorateCheckedSupplier.
@Test
public void decorateCheckedSupplier() throws Throwable {
CheckedSupplier supplier = mock(CheckedSupplier.class);
CheckedSupplier decorated = RateLimiter.decorateCheckedSupplier(limit, supplier);
given(limit.acquirePermission(1)).willReturn(false);
Try decoratedSupplierResult = Try.of(() -> decorated.get());
assertThat(decoratedSupplierResult.isFailure()).isTrue();
assertThat(decoratedSupplierResult.getCause()).isInstanceOf(RequestNotPermitted.class);
then(supplier).should(never()).get();
given(limit.acquirePermission(1)).willReturn(true);
Try secondSupplierResult = Try.of(() -> decorated.get());
assertThat(secondSupplierResult.isSuccess()).isTrue();
then(supplier).should().get();
}
Aggregations