Search in sources :

Example 11 with ThreadPoolBulkhead

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

the class DecoratorsTest method testDecorateCompletionStageWithCallNotPermittedExceptionFallback.

@Test
public void testDecorateCompletionStageWithCallNotPermittedExceptionFallback() throws ExecutionException, InterruptedException {
    CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend");
    circuitBreaker.transitionToOpenState();
    ThreadPoolBulkhead bulkhead = ThreadPoolBulkhead.ofDefaults("helloBackend");
    CompletionStage<String> completionStage = Decorators.ofSupplier(() -> helloWorldService.returnHelloWorld()).withThreadPoolBulkhead(bulkhead).withCircuitBreaker(circuitBreaker).withFallback(CallNotPermittedException.class, (e) -> "Fallback").get();
    String result = completionStage.toCompletableFuture().get();
    assertThat(result).isEqualTo("Fallback");
    CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
    assertThat(metrics.getNumberOfNotPermittedCalls()).isEqualTo(1);
}
Also used : ThreadPoolBulkhead(io.github.resilience4j.bulkhead.ThreadPoolBulkhead) 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 12 with ThreadPoolBulkhead

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

the class DecoratorsTest method shouldThrowTimeoutException.

@Test
public void shouldThrowTimeoutException() {
    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).get();
    assertThatThrownBy(() -> completionStage.toCompletableFuture().get()).hasCauseInstanceOf(TimeoutException.class);
    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) Test(org.junit.Test)

Example 13 with ThreadPoolBulkhead

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

the class FixedThreadPoolBulkheadTest method shouldCreateThreadPoolBulkheadRegistryWithRegistryStore.

@Test
public void shouldCreateThreadPoolBulkheadRegistryWithRegistryStore() {
    RegistryEventConsumer<ThreadPoolBulkhead> registryEventConsumer = getNoOpsRegistryEventConsumer();
    List<RegistryEventConsumer<ThreadPoolBulkhead>> registryEventConsumers = new ArrayList<>();
    registryEventConsumers.add(registryEventConsumer);
    Map<String, ThreadPoolBulkheadConfig> configs = new HashMap<>();
    final ThreadPoolBulkheadConfig defaultConfig = ThreadPoolBulkheadConfig.ofDefaults();
    configs.put("default", defaultConfig);
    final InMemoryThreadPoolBulkheadRegistry inMemoryThreadPoolBulkheadRegistry = new InMemoryThreadPoolBulkheadRegistry(configs, registryEventConsumers, Map.of("Tag1", "Tag1Value"), new InMemoryRegistryStore<>());
    AssertionsForClassTypes.assertThat(inMemoryThreadPoolBulkheadRegistry).isNotNull();
    AssertionsForClassTypes.assertThat(inMemoryThreadPoolBulkheadRegistry.getDefaultConfig()).isEqualTo(defaultConfig);
    AssertionsForClassTypes.assertThat(inMemoryThreadPoolBulkheadRegistry.getConfiguration("testNotFound")).isEmpty();
    inMemoryThreadPoolBulkheadRegistry.addConfiguration("testConfig", defaultConfig);
    AssertionsForClassTypes.assertThat(inMemoryThreadPoolBulkheadRegistry.getConfiguration("testConfig")).isNotNull();
}
Also used : ThreadPoolBulkhead(io.github.resilience4j.bulkhead.ThreadPoolBulkhead) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ThreadPoolBulkheadConfig(io.github.resilience4j.bulkhead.ThreadPoolBulkheadConfig) Test(org.junit.Test)

Example 14 with ThreadPoolBulkhead

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

the class FixedThreadPoolBulkheadTest method testCreateWithDefaults.

@Test
public void testCreateWithDefaults() {
    ThreadPoolBulkhead bulkhead = ThreadPoolBulkhead.ofDefaults("test");
    assertThat(bulkhead).isNotNull();
    assertThat(bulkhead.getBulkheadConfig()).isNotNull();
    assertThat(bulkhead.getBulkheadConfig().getMaxThreadPoolSize()).isEqualTo(ThreadPoolBulkheadConfig.DEFAULT_MAX_THREAD_POOL_SIZE);
    assertThat(bulkhead.getBulkheadConfig().getCoreThreadPoolSize()).isEqualTo(ThreadPoolBulkheadConfig.DEFAULT_CORE_THREAD_POOL_SIZE);
    assertThat(bulkhead.getBulkheadConfig().getQueueCapacity()).isEqualTo(ThreadPoolBulkheadConfig.DEFAULT_QUEUE_CAPACITY);
}
Also used : ThreadPoolBulkhead(io.github.resilience4j.bulkhead.ThreadPoolBulkhead) Test(org.junit.Test)

Example 15 with ThreadPoolBulkhead

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

the class TaggedThreadPoolBulkheadMetricsPublisherTest method shouldReplaceMetrics.

@Test
public void shouldReplaceMetrics() {
    Collection<Gauge> gauges = meterRegistry.get(DEFAULT_MAX_THREAD_POOL_SIZE_METRIC_NAME).gauges();
    Optional<Gauge> successful = findMeterByNamesTag(gauges, bulkhead.getName());
    assertThat(successful).isPresent();
    assertThat(successful.get().value()).isEqualTo(bulkhead.getMetrics().getMaximumThreadPoolSize());
    ThreadPoolBulkhead newBulkhead = ThreadPoolBulkhead.of(bulkhead.getName(), ThreadPoolBulkheadConfig.custom().maxThreadPoolSize(Runtime.getRuntime().availableProcessors() + 1).build());
    bulkheadRegistry.replace(bulkhead.getName(), newBulkhead);
    gauges = meterRegistry.get(DEFAULT_MAX_THREAD_POOL_SIZE_METRIC_NAME).gauges();
    successful = findMeterByNamesTag(gauges, newBulkhead.getName());
    assertThat(successful).isPresent();
    assertThat(successful.get().value()).isEqualTo(newBulkhead.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