Search in sources :

Example 1 with ThreadPoolBulkhead

use of io.github.resilience4j.bulkhead.ThreadPoolBulkhead in project resilience4j by resilience4j.

the class DecoratorsTest method testDecorateCallableWithBulkheadFullExceptionFallback.

@Test
public void testDecorateCallableWithBulkheadFullExceptionFallback() throws ExecutionException, InterruptedException {
    ThreadPoolBulkhead bulkhead = ThreadPoolBulkhead.ofDefaults("helloBackend");
    ThreadPoolBulkhead bulkheadMock = spy(bulkhead);
    given(bulkheadMock.submit(any(Callable.class))).willThrow(BulkheadFullException.createBulkheadFullException(bulkhead));
    CompletionStage<String> completionStage = Decorators.ofCallable(() -> helloWorldService.returnHelloWorldWithException()).withThreadPoolBulkhead(bulkheadMock).withFallback(BulkheadFullException.class, (e) -> "Fallback").get();
    String result = completionStage.toCompletableFuture().get();
    assertThat(result).isEqualTo("Fallback");
}
Also used : ThreadPoolBulkhead(io.github.resilience4j.bulkhead.ThreadPoolBulkhead) 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) BulkheadFullException(io.github.resilience4j.bulkhead.BulkheadFullException) Test(org.junit.Test)

Example 2 with ThreadPoolBulkhead

use of io.github.resilience4j.bulkhead.ThreadPoolBulkhead in project resilience4j by resilience4j.

the class DecoratorsTest method testDecorateRunnableWithBulkheadFullExceptionFallback.

@Test
public void testDecorateRunnableWithBulkheadFullExceptionFallback() throws ExecutionException, InterruptedException {
    ThreadPoolBulkhead bulkhead = ThreadPoolBulkhead.ofDefaults("helloBackend");
    ThreadPoolBulkhead bulkheadMock = spy(bulkhead);
    given(bulkheadMock.submit(any(Callable.class))).willThrow(BulkheadFullException.createBulkheadFullException(bulkhead));
    CompletionStage<Void> completionStage = Decorators.ofRunnable(() -> helloWorldService.sayHelloWorld()).withThreadPoolBulkhead(bulkheadMock).withFallback(BulkheadFullException.class, (e) -> {
        helloWorldService.sayHelloWorld();
        return null;
    }).get();
    completionStage.toCompletableFuture().get();
    then(helloWorldService).should(times(1)).sayHelloWorld();
}
Also used : ThreadPoolBulkhead(io.github.resilience4j.bulkhead.ThreadPoolBulkhead) 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) BulkheadFullException(io.github.resilience4j.bulkhead.BulkheadFullException) Test(org.junit.Test)

Example 3 with ThreadPoolBulkhead

use of io.github.resilience4j.bulkhead.ThreadPoolBulkhead in project resilience4j by resilience4j.

the class DecoratorsTest method shouldThrowTimeoutExceptionAndPropagateMDCContext.

@Test
public void shouldThrowTimeoutExceptionAndPropagateMDCContext() {
    TimeLimiter timeLimiter = TimeLimiter.of("helloBackend", TimeLimiterConfig.custom().timeoutDuration(Duration.ofMillis(100)).build());
    CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend");
    ThreadPoolBulkhead bulkhead = ThreadPoolBulkhead.ofDefaults("helloBackend");
    MDC.put("key", "ValueShouldPropagateThreadBoundary");
    MDC.put("key2", "value2");
    final Map<String, String> contextMap = MDC.getCopyOfContextMap();
    ContextAwareScheduledThreadPoolExecutor scheduledThreadPool = ContextAwareScheduledThreadPoolExecutor.newScheduledThreadPool().corePoolSize(1).build();
    CompletionStage<String> completionStage = Decorators.ofCallable(() -> {
        assertThat(Thread.currentThread().getName()).isEqualTo("bulkhead-helloBackend-1");
        Thread.sleep(1000);
        return "Bla";
    }).withThreadPoolBulkhead(bulkhead).withTimeLimiter(timeLimiter, scheduledThreadPool).withCircuitBreaker(circuitBreaker).get();
    final CompletableFuture<String> completableFuture = completionStage.toCompletableFuture().exceptionally(throwable -> {
        if (throwable != null) {
            assertThat(Thread.currentThread().getName()).isEqualTo("ContextAwareScheduledThreadPool-1");
            assertThat(MDC.getCopyOfContextMap()).hasSize(2).containsExactlyEntriesOf(contextMap);
            return MDC.getCopyOfContextMap().get("key");
        }
        return null;
    });
    waitAtMost(2, TimeUnit.SECONDS).until(matches(() -> assertThat(completableFuture).isCompletedWithValue("ValueShouldPropagateThreadBoundary")));
    CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
    assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1);
    assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(1);
}
Also used : TimeLimiter(io.github.resilience4j.timelimiter.TimeLimiter) ThreadPoolBulkhead(io.github.resilience4j.bulkhead.ThreadPoolBulkhead) CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) ContextAwareScheduledThreadPoolExecutor(io.github.resilience4j.core.ContextAwareScheduledThreadPoolExecutor) Test(org.junit.Test)

Example 4 with ThreadPoolBulkhead

use of io.github.resilience4j.bulkhead.ThreadPoolBulkhead in project resilience4j by resilience4j.

the class DecoratorsTest method testDecorateCompletionStageWithTimeoutExceptionFallback.

@Test
public void testDecorateCompletionStageWithTimeoutExceptionFallback() throws ExecutionException, InterruptedException {
    TimeLimiter timeLimiter = TimeLimiter.of("helloBackend", TimeLimiterConfig.custom().timeoutDuration(Duration.ofMillis(100)).build());
    CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend");
    ThreadPoolBulkhead bulkhead = ThreadPoolBulkhead.ofDefaults("helloBackend");
    CompletionStage<String> completionStage = Decorators.ofCallable(() -> {
        Thread.sleep(1000);
        return "Bla";
    }).withThreadPoolBulkhead(bulkhead).withTimeLimiter(timeLimiter, Executors.newSingleThreadScheduledExecutor()).withCircuitBreaker(circuitBreaker).withFallback(TimeoutException.class, (e) -> "Fallback").get();
    String result = completionStage.toCompletableFuture().get();
    assertThat(result).isEqualTo("Fallback");
    CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
    assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(1);
}
Also used : TimeLimiter(io.github.resilience4j.timelimiter.TimeLimiter) ThreadPoolBulkhead(io.github.resilience4j.bulkhead.ThreadPoolBulkhead) 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 5 with ThreadPoolBulkhead

use of io.github.resilience4j.bulkhead.ThreadPoolBulkhead in project resilience4j by resilience4j.

the class TaggedThreadPoolBulkheadMetricsPublisherTest method testReplaceNewMeter.

@Test
public void testReplaceNewMeter() {
    ThreadPoolBulkhead oldOne = ThreadPoolBulkhead.of("backendC", ThreadPoolBulkheadConfig.ofDefaults());
    // add meters of old
    taggedBulkheadMetricsPublisher.addMetrics(meterRegistry, oldOne);
    // one success call
    oldOne.executeSupplier(() -> "Bla");
    assertThat(taggedBulkheadMetricsPublisher.meterIdMap).containsKeys("backendC");
    assertThat(taggedBulkheadMetricsPublisher.meterIdMap.get("backendC")).hasSize(EXPECTED_METER_COUNT);
    Collection<Gauge> gauges = meterRegistry.get(DEFAULT_MAX_THREAD_POOL_SIZE_METRIC_NAME).gauges();
    Optional<Gauge> successful = findMeterByNamesTag(gauges, oldOne.getName());
    assertThat(successful).isPresent();
    assertThat(successful.get().value()).isEqualTo(oldOne.getMetrics().getMaximumThreadPoolSize());
    ThreadPoolBulkhead newOne = ThreadPoolBulkhead.of("backendC", ThreadPoolBulkheadConfig.ofDefaults());
    // add meters of new
    taggedBulkheadMetricsPublisher.addMetrics(meterRegistry, newOne);
    // three success call
    newOne.executeSupplier(() -> "Bla");
    newOne.executeSupplier(() -> "Bla");
    newOne.executeSupplier(() -> "Bla");
    assertThat(taggedBulkheadMetricsPublisher.meterIdMap).containsKeys("backendC");
    assertThat(taggedBulkheadMetricsPublisher.meterIdMap.get("backendC")).hasSize(EXPECTED_METER_COUNT);
    gauges = meterRegistry.get(DEFAULT_MAX_THREAD_POOL_SIZE_METRIC_NAME).gauges();
    successful = findMeterByNamesTag(gauges, newOne.getName());
    assertThat(successful).isPresent();
    assertThat(successful.get().value()).isEqualTo(newOne.getMetrics().getMaximumThreadPoolSize());
}
Also used : ThreadPoolBulkhead(io.github.resilience4j.bulkhead.ThreadPoolBulkhead) Gauge(io.micrometer.core.instrument.Gauge) Test(org.junit.Test)

Aggregations

ThreadPoolBulkhead (io.github.resilience4j.bulkhead.ThreadPoolBulkhead)17 Test (org.junit.Test)15 CircuitBreaker (io.github.resilience4j.circuitbreaker.CircuitBreaker)8 TimeLimiter (io.github.resilience4j.timelimiter.TimeLimiter)8 ContextAwareScheduledThreadPoolExecutor (io.github.resilience4j.core.ContextAwareScheduledThreadPoolExecutor)7 TestThreadLocalContextPropagatorWithHolder (io.github.resilience4j.test.TestContextPropagators.TestThreadLocalContextPropagatorWithHolder)6 Awaitility.matches (com.jayway.awaitility.Awaitility.matches)5 Awaitility.waitAtMost (com.jayway.awaitility.Awaitility.waitAtMost)5 Bulkhead (io.github.resilience4j.bulkhead.Bulkhead)5 BulkheadFullException (io.github.resilience4j.bulkhead.BulkheadFullException)5 Cache (io.github.resilience4j.cache.Cache)5 CallNotPermittedException (io.github.resilience4j.circuitbreaker.CallNotPermittedException)5 CheckedFunction (io.github.resilience4j.core.functions.CheckedFunction)5 CheckedRunnable (io.github.resilience4j.core.functions.CheckedRunnable)5 CheckedSupplier (io.github.resilience4j.core.functions.CheckedSupplier)5 RateLimiter (io.github.resilience4j.ratelimiter.RateLimiter)5 RateLimiterConfig (io.github.resilience4j.ratelimiter.RateLimiterConfig)5 RequestNotPermitted (io.github.resilience4j.ratelimiter.RequestNotPermitted)5 Retry (io.github.resilience4j.retry.Retry)5 AsyncHelloWorldService (io.github.resilience4j.test.AsyncHelloWorldService)5