Search in sources :

Example 1 with ThreadPoolBulkheadConfiguration

use of io.github.resilience4j.bulkhead.configure.threadpool.ThreadPoolBulkheadConfiguration in project resilience4j by resilience4j.

the class BulkHeadConfigurationTest method testCreateThreadPoolBulkHeadRegistryWithSharedConfigs.

@Test
public void testCreateThreadPoolBulkHeadRegistryWithSharedConfigs() {
    // Given
    ThreadPoolBulkheadConfigurationProperties.InstanceProperties defaultProperties = new ThreadPoolBulkheadConfigurationProperties.InstanceProperties();
    defaultProperties.setCoreThreadPoolSize(1);
    defaultProperties.setQueueCapacity(1);
    defaultProperties.setKeepAliveDuration(Duration.ofNanos(5));
    defaultProperties.setMaxThreadPoolSize(10);
    ThreadPoolBulkheadConfigurationProperties.InstanceProperties sharedProperties = new ThreadPoolBulkheadConfigurationProperties.InstanceProperties();
    sharedProperties.setCoreThreadPoolSize(2);
    sharedProperties.setQueueCapacity(2);
    sharedProperties.setContextPropagators(TestThreadLocalContextPropagator.class);
    ThreadPoolBulkheadConfigurationProperties.InstanceProperties backendWithDefaultConfig = new ThreadPoolBulkheadConfigurationProperties.InstanceProperties();
    backendWithDefaultConfig.setBaseConfig("default");
    backendWithDefaultConfig.setCoreThreadPoolSize(3);
    ThreadPoolBulkheadConfigurationProperties.InstanceProperties backendWithSharedConfig = new ThreadPoolBulkheadConfigurationProperties.InstanceProperties();
    backendWithSharedConfig.setBaseConfig("sharedConfig");
    backendWithSharedConfig.setCoreThreadPoolSize(4);
    backendWithSharedConfig.setContextPropagators(TestThreadLocalContextPropagator.class);
    ThreadPoolBulkheadConfigurationProperties bulkheadConfigurationProperties = new ThreadPoolBulkheadConfigurationProperties();
    bulkheadConfigurationProperties.getConfigs().put("default", defaultProperties);
    bulkheadConfigurationProperties.getConfigs().put("sharedConfig", sharedProperties);
    bulkheadConfigurationProperties.getBackends().put("backendWithDefaultConfig", backendWithDefaultConfig);
    bulkheadConfigurationProperties.getBackends().put("backendWithSharedConfig", backendWithSharedConfig);
    ThreadPoolBulkheadConfiguration threadPoolBulkheadConfiguration = new ThreadPoolBulkheadConfiguration();
    DefaultEventConsumerRegistry<BulkheadEvent> eventConsumerRegistry = new DefaultEventConsumerRegistry<>();
    // When
    try {
        ThreadPoolBulkheadRegistry bulkheadRegistry = threadPoolBulkheadConfiguration.threadPoolBulkheadRegistry(bulkheadConfigurationProperties, eventConsumerRegistry, new CompositeRegistryEventConsumer<>(emptyList()), new CompositeCustomizer<>(Collections.emptyList()));
        // Then
        assertThat(bulkheadRegistry.getAllBulkheads().size()).isEqualTo(2);
        // Should get default config and core number
        ThreadPoolBulkhead bulkhead1 = bulkheadRegistry.bulkhead("backendWithDefaultConfig");
        assertThat(bulkhead1).isNotNull();
        assertThat(bulkhead1.getBulkheadConfig().getCoreThreadPoolSize()).isEqualTo(3);
        assertThat(bulkhead1.getBulkheadConfig().getQueueCapacity()).isEqualTo(1);
        assertThat(bulkhead1.getBulkheadConfig().getContextPropagator()).isNotNull();
        assertThat(bulkhead1.getBulkheadConfig().getContextPropagator()).isEmpty();
        // Should get shared config and overwrite core number
        ThreadPoolBulkhead bulkhead2 = bulkheadRegistry.bulkhead("backendWithSharedConfig");
        assertThat(bulkhead2).isNotNull();
        assertThat(bulkhead2.getBulkheadConfig().getCoreThreadPoolSize()).isEqualTo(4);
        assertThat(bulkhead2.getBulkheadConfig().getQueueCapacity()).isEqualTo(2);
        assertThat(bulkhead2.getBulkheadConfig().getContextPropagator()).isNotNull();
        assertThat(bulkhead2.getBulkheadConfig().getContextPropagator().size()).isEqualTo(2);
        List<Class<? extends ContextPropagator>> ctxPropagators = bulkhead2.getBulkheadConfig().getContextPropagator().stream().map(ctx -> ctx.getClass()).collect(Collectors.toList());
        assertThat(ctxPropagators).containsExactlyInAnyOrder(TestThreadLocalContextPropagator.class, TestThreadLocalContextPropagator.class);
        // Unknown backend should get default config of Registry
        ThreadPoolBulkhead bulkhead3 = bulkheadRegistry.bulkhead("unknownBackend");
        assertThat(bulkhead3).isNotNull();
        assertThat(bulkhead3.getBulkheadConfig().getCoreThreadPoolSize()).isEqualTo(1);
        assertThat(bulkhead3.getBulkheadConfig().getContextPropagator()).isNotNull();
        assertThat(bulkhead3.getBulkheadConfig().getContextPropagator()).isEmpty();
        assertThat(eventConsumerRegistry.getAllEventConsumer()).hasSize(3);
    } catch (Exception e) {
        System.out.println("exception in testCreateThreadPoolBulkHeadRegistryWithSharedConfigs():" + e);
    }
}
Also used : TestThreadLocalContextPropagator(io.github.resilience4j.TestThreadLocalContextPropagator) CompositeRegistryEventConsumer(io.github.resilience4j.core.registry.CompositeRegistryEventConsumer) CompositeCustomizer(io.github.resilience4j.common.CompositeCustomizer) Collections.emptyList(java.util.Collections.emptyList) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ContextPropagator(io.github.resilience4j.core.ContextPropagator) Test(org.junit.Test) BulkheadEvent(io.github.resilience4j.bulkhead.event.BulkheadEvent) Collectors(java.util.stream.Collectors) DefaultEventConsumerRegistry(io.github.resilience4j.consumer.DefaultEventConsumerRegistry) ConfigurationNotFoundException(io.github.resilience4j.core.ConfigurationNotFoundException) List(java.util.List) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Duration(java.time.Duration) ThreadPoolBulkheadConfigurationProperties(io.github.resilience4j.common.bulkhead.configuration.ThreadPoolBulkheadConfigurationProperties) ThreadPoolBulkheadConfiguration(io.github.resilience4j.bulkhead.configure.threadpool.ThreadPoolBulkheadConfiguration) Collections(java.util.Collections) io.github.resilience4j.bulkhead(io.github.resilience4j.bulkhead) BulkheadEvent(io.github.resilience4j.bulkhead.event.BulkheadEvent) DefaultEventConsumerRegistry(io.github.resilience4j.consumer.DefaultEventConsumerRegistry) ConfigurationNotFoundException(io.github.resilience4j.core.ConfigurationNotFoundException) TestThreadLocalContextPropagator(io.github.resilience4j.TestThreadLocalContextPropagator) ContextPropagator(io.github.resilience4j.core.ContextPropagator) ThreadPoolBulkheadConfigurationProperties(io.github.resilience4j.common.bulkhead.configuration.ThreadPoolBulkheadConfigurationProperties) ThreadPoolBulkheadConfiguration(io.github.resilience4j.bulkhead.configure.threadpool.ThreadPoolBulkheadConfiguration) Test(org.junit.Test)

Example 2 with ThreadPoolBulkheadConfiguration

use of io.github.resilience4j.bulkhead.configure.threadpool.ThreadPoolBulkheadConfiguration in project resilience4j by resilience4j.

the class BulkHeadConfigurationTest method tesFixedThreadPoolBulkHeadRegistry.

@Test
public void tesFixedThreadPoolBulkHeadRegistry() {
    // Given
    ThreadPoolBulkheadConfigurationProperties.InstanceProperties backendProperties1 = new ThreadPoolBulkheadConfigurationProperties.InstanceProperties();
    backendProperties1.setCoreThreadPoolSize(1);
    backendProperties1.setContextPropagators(TestThreadLocalContextPropagator.class);
    ThreadPoolBulkheadConfigurationProperties.InstanceProperties backendProperties2 = new ThreadPoolBulkheadConfigurationProperties.InstanceProperties();
    backendProperties2.setCoreThreadPoolSize(2);
    ThreadPoolBulkheadConfigurationProperties bulkheadConfigurationProperties = new ThreadPoolBulkheadConfigurationProperties();
    bulkheadConfigurationProperties.getBackends().put("backend1", backendProperties1);
    bulkheadConfigurationProperties.getBackends().put("backend2", backendProperties2);
    ThreadPoolBulkheadConfiguration threadPoolBulkheadConfiguration = new ThreadPoolBulkheadConfiguration();
    DefaultEventConsumerRegistry<BulkheadEvent> eventConsumerRegistry = new DefaultEventConsumerRegistry<>();
    // When
    ThreadPoolBulkheadRegistry bulkheadRegistry = threadPoolBulkheadConfiguration.threadPoolBulkheadRegistry(bulkheadConfigurationProperties, eventConsumerRegistry, new CompositeRegistryEventConsumer<>(emptyList()), new CompositeCustomizer<>(Collections.emptyList()));
    // Then
    assertThat(bulkheadRegistry.getAllBulkheads().size()).isEqualTo(2);
    ThreadPoolBulkhead bulkhead1 = bulkheadRegistry.bulkhead("backend1");
    assertThat(bulkhead1).isNotNull();
    assertThat(bulkhead1.getBulkheadConfig().getCoreThreadPoolSize()).isEqualTo(1);
    assertThat(bulkhead1.getBulkheadConfig().getContextPropagator()).isNotNull();
    assertThat(bulkhead1.getBulkheadConfig().getContextPropagator().size()).isEqualTo(1);
    assertThat(bulkhead1.getBulkheadConfig().getContextPropagator().get(0).getClass()).isEqualTo(TestThreadLocalContextPropagator.class);
    ThreadPoolBulkhead bulkhead2 = bulkheadRegistry.bulkhead("backend2");
    assertThat(bulkhead2).isNotNull();
    assertThat(bulkhead2.getBulkheadConfig().getCoreThreadPoolSize()).isEqualTo(2);
    assertThat(bulkhead2.getBulkheadConfig().getContextPropagator()).isNotNull();
    assertThat(bulkhead2.getBulkheadConfig().getContextPropagator()).isEmpty();
    assertThat(eventConsumerRegistry.getAllEventConsumer()).hasSize(2);
}
Also used : ThreadPoolBulkheadConfigurationProperties(io.github.resilience4j.common.bulkhead.configuration.ThreadPoolBulkheadConfigurationProperties) ThreadPoolBulkheadConfiguration(io.github.resilience4j.bulkhead.configure.threadpool.ThreadPoolBulkheadConfiguration) BulkheadEvent(io.github.resilience4j.bulkhead.event.BulkheadEvent) DefaultEventConsumerRegistry(io.github.resilience4j.consumer.DefaultEventConsumerRegistry) Test(org.junit.Test)

Aggregations

ThreadPoolBulkheadConfiguration (io.github.resilience4j.bulkhead.configure.threadpool.ThreadPoolBulkheadConfiguration)2 BulkheadEvent (io.github.resilience4j.bulkhead.event.BulkheadEvent)2 ThreadPoolBulkheadConfigurationProperties (io.github.resilience4j.common.bulkhead.configuration.ThreadPoolBulkheadConfigurationProperties)2 DefaultEventConsumerRegistry (io.github.resilience4j.consumer.DefaultEventConsumerRegistry)2 Test (org.junit.Test)2 TestThreadLocalContextPropagator (io.github.resilience4j.TestThreadLocalContextPropagator)1 io.github.resilience4j.bulkhead (io.github.resilience4j.bulkhead)1 CompositeCustomizer (io.github.resilience4j.common.CompositeCustomizer)1 ConfigurationNotFoundException (io.github.resilience4j.core.ConfigurationNotFoundException)1 ContextPropagator (io.github.resilience4j.core.ContextPropagator)1 CompositeRegistryEventConsumer (io.github.resilience4j.core.registry.CompositeRegistryEventConsumer)1 Duration (java.time.Duration)1 Collections (java.util.Collections)1 Collections.emptyList (java.util.Collections.emptyList)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)1