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);
}
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);
}
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();
}
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);
}
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());
}
Aggregations