Search in sources :

Example 1 with CheckedSupplier

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();
}
Also used : CallNotPermittedException(io.github.resilience4j.circuitbreaker.CallNotPermittedException) CheckedFunction(io.github.resilience4j.core.functions.CheckedFunction) BulkheadFullException(io.github.resilience4j.bulkhead.BulkheadFullException) RateLimiter(io.github.resilience4j.ratelimiter.RateLimiter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Bulkhead(io.github.resilience4j.bulkhead.Bulkhead) TimeLimiterConfig(io.github.resilience4j.timelimiter.TimeLimiterConfig) RequestNotPermitted(io.github.resilience4j.ratelimiter.RequestNotPermitted) TestThreadLocalContextHolder(io.github.resilience4j.test.TestContextPropagators.TestThreadLocalContextPropagatorWithHolder.TestThreadLocalContextHolder) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) BDDMockito.given(org.mockito.BDDMockito.given) Arrays.asList(java.util.Arrays.asList) Cache(io.github.resilience4j.cache.Cache) Duration(java.time.Duration) Map(java.util.Map) HelloWorldService(io.github.resilience4j.test.HelloWorldService) RateLimiterConfig(io.github.resilience4j.ratelimiter.RateLimiterConfig) HelloWorldException(io.github.resilience4j.test.HelloWorldException) Before(org.junit.Before) ThreadPoolBulkhead(io.github.resilience4j.bulkhead.ThreadPoolBulkhead) CheckedRunnable(io.github.resilience4j.core.functions.CheckedRunnable) CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) CheckedSupplier(io.github.resilience4j.core.functions.CheckedSupplier) Retry(io.github.resilience4j.retry.Retry) Awaitility.matches(com.jayway.awaitility.Awaitility.matches) java.util.concurrent(java.util.concurrent) ContextAwareScheduledThreadPoolExecutor(io.github.resilience4j.core.ContextAwareScheduledThreadPoolExecutor) BDDMockito.then(org.mockito.BDDMockito.then) Test(org.junit.Test) IOException(java.io.IOException) AsyncHelloWorldService(io.github.resilience4j.test.AsyncHelloWorldService) TestThreadLocalContextPropagatorWithHolder(io.github.resilience4j.test.TestContextPropagators.TestThreadLocalContextPropagatorWithHolder) Awaitility.waitAtMost(com.jayway.awaitility.Awaitility.waitAtMost) Mockito(org.mockito.Mockito) Try(io.vavr.control.Try) TimeLimiter(io.github.resilience4j.timelimiter.TimeLimiter) MDC(org.slf4j.MDC) CallNotPermittedException(io.github.resilience4j.circuitbreaker.CallNotPermittedException) CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) Test(org.junit.Test)

Example 2 with CheckedSupplier

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);
}
Also used : RetryConfig(io.github.resilience4j.retry.RetryConfig) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Callable(java.util.concurrent.Callable) API(io.vavr.API) MaxRetriesExceededException(io.github.resilience4j.retry.MaxRetriesExceededException) Function(java.util.function.Function) Supplier(java.util.function.Supplier) AsyncUtils.awaitResult(io.github.resilience4j.retry.utils.AsyncUtils.awaitResult) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) BDDMockito.given(org.mockito.BDDMockito.given) HelloWorldService(io.github.resilience4j.test.HelloWorldService) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) HelloWorldException(io.github.resilience4j.test.HelloWorldException) Before(org.junit.Before) API.$(io.vavr.API.$) Predicates(io.vavr.Predicates) CheckedSupplier(io.github.resilience4j.core.functions.CheckedSupplier) Retry(io.github.resilience4j.retry.Retry) IntervalFunction(io.github.resilience4j.core.IntervalFunction) BDDMockito.then(org.mockito.BDDMockito.then) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) AsyncHelloWorldService(io.github.resilience4j.test.AsyncHelloWorldService) Executors(java.util.concurrent.Executors) Try(io.vavr.control.Try) CompletionStage(java.util.concurrent.CompletionStage) IntervalBiFunction(io.github.resilience4j.core.IntervalBiFunction) Mockito.mock(org.mockito.Mockito.mock) RetryConfig(io.github.resilience4j.retry.RetryConfig) HelloWorldException(io.github.resilience4j.test.HelloWorldException) Retry(io.github.resilience4j.retry.Retry) Test(org.junit.Test)

Example 3 with CheckedSupplier

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();
}
Also used : Try(io.vavr.control.Try) CheckedSupplier(io.github.resilience4j.core.functions.CheckedSupplier) Test(org.junit.Test)

Aggregations

CheckedSupplier (io.github.resilience4j.core.functions.CheckedSupplier)3 Try (io.vavr.control.Try)3 Test (org.junit.Test)3 Retry (io.github.resilience4j.retry.Retry)2 AsyncHelloWorldService (io.github.resilience4j.test.AsyncHelloWorldService)2 HelloWorldException (io.github.resilience4j.test.HelloWorldException)2 HelloWorldService (io.github.resilience4j.test.HelloWorldService)2 Function (java.util.function.Function)2 Supplier (java.util.function.Supplier)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)2 Before (org.junit.Before)2 BDDMockito.given (org.mockito.BDDMockito.given)2 BDDMockito.then (org.mockito.BDDMockito.then)2 Awaitility.matches (com.jayway.awaitility.Awaitility.matches)1 Awaitility.waitAtMost (com.jayway.awaitility.Awaitility.waitAtMost)1 Bulkhead (io.github.resilience4j.bulkhead.Bulkhead)1 BulkheadFullException (io.github.resilience4j.bulkhead.BulkheadFullException)1 ThreadPoolBulkhead (io.github.resilience4j.bulkhead.ThreadPoolBulkhead)1 Cache (io.github.resilience4j.cache.Cache)1