Search in sources :

Example 1 with CompositeRegistryEventConsumer

use of io.github.resilience4j.core.registry.CompositeRegistryEventConsumer in project resilience4j by resilience4j.

the class SpringBootCommonTest method testBulkHeadCommonConfig.

@Test
public void testBulkHeadCommonConfig() {
    BulkheadConfigurationOnMissingBean bulkheadConfigurationOnMissingBean = new BulkheadConfigurationOnMissingBean();
    assertThat(bulkheadConfigurationOnMissingBean.bulkheadRegistry(new BulkheadConfigurationProperties(), new DefaultEventConsumerRegistry<>(), new CompositeRegistryEventConsumer<>(Collections.emptyList()), new CompositeCustomizer<>(Collections.singletonList(BulkheadConfigCustomizer.of("backend", builder -> builder.maxConcurrentCalls(10)))))).isNotNull();
    assertThat(bulkheadConfigurationOnMissingBean.threadPoolBulkheadRegistry(new ThreadPoolBulkheadConfigurationProperties(), new DefaultEventConsumerRegistry<>(), new CompositeRegistryEventConsumer<>(Collections.emptyList()), new CompositeCustomizer<>(Collections.singletonList(ThreadPoolBulkheadConfigCustomizer.of("backend", builder -> builder.coreThreadPoolSize(10)))))).isNotNull();
    assertThat(bulkheadConfigurationOnMissingBean.reactorBulkHeadAspectExt()).isNotNull();
    assertThat(bulkheadConfigurationOnMissingBean.rxJava2BulkHeadAspectExt()).isNotNull();
    final DefaultSpelResolver spelResolver = new DefaultSpelResolver(new SpelExpressionParser(), new StandardReflectionParameterNameDiscoverer(), new GenericApplicationContext());
    final FallbackDecorators fallbackDecorators = new FallbackDecorators(Collections.singletonList(new CompletionStageFallbackDecorator()));
    assertThat(bulkheadConfigurationOnMissingBean.bulkheadAspect(new BulkheadConfigurationProperties(), ThreadPoolBulkheadRegistry.ofDefaults(), BulkheadRegistry.ofDefaults(), Collections.emptyList(), new FallbackExecutor(spelResolver, fallbackDecorators), spelResolver)).isNotNull();
    assertThat(bulkheadConfigurationOnMissingBean.bulkheadRegistryEventConsumer(Optional.empty())).isNotNull();
}
Also used : RetryConfigurationProperties(io.github.resilience4j.retry.configure.RetryConfigurationProperties) Arrays(java.util.Arrays) BulkheadRegistry(io.github.resilience4j.bulkhead.BulkheadRegistry) BulkheadConfigCustomizer(io.github.resilience4j.common.bulkhead.configuration.BulkheadConfigCustomizer) FallbackDecorators(io.github.resilience4j.fallback.FallbackDecorators) TimeLimiterConfigurationProperties(io.github.resilience4j.timelimiter.configure.TimeLimiterConfigurationProperties) BulkheadConfigurationProperties(io.github.resilience4j.bulkhead.configure.BulkheadConfigurationProperties) TimeLimiterRegistry(io.github.resilience4j.timelimiter.TimeLimiterRegistry) AbstractRetryConfigurationOnMissingBean(io.github.resilience4j.retry.autoconfigure.AbstractRetryConfigurationOnMissingBean) CircuitBreakerRegistry(io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry) TimeLimiterConfigCustomizer(io.github.resilience4j.common.timelimiter.configuration.TimeLimiterConfigCustomizer) DefaultSpelResolver(io.github.resilience4j.spelresolver.DefaultSpelResolver) Duration(java.time.Duration) AbstractRateLimiterConfigurationOnMissingBean(io.github.resilience4j.ratelimiter.autoconfigure.AbstractRateLimiterConfigurationOnMissingBean) CircuitBreakerConfigurationProperties(io.github.resilience4j.circuitbreaker.configure.CircuitBreakerConfigurationProperties) ThreadPoolBulkheadConfigCustomizer(io.github.resilience4j.common.bulkhead.configuration.ThreadPoolBulkheadConfigCustomizer) RetryRegistry(io.github.resilience4j.retry.RetryRegistry) AssertionsForClassTypes.assertThat(org.assertj.core.api.AssertionsForClassTypes.assertThat) RateLimiterRegistry(io.github.resilience4j.ratelimiter.RateLimiterRegistry) CompositeRegistryEventConsumer(io.github.resilience4j.core.registry.CompositeRegistryEventConsumer) CompositeCustomizer(io.github.resilience4j.common.CompositeCustomizer) AbstractBulkheadConfigurationOnMissingBean(io.github.resilience4j.bulkhead.autoconfigure.AbstractBulkheadConfigurationOnMissingBean) StandardReflectionParameterNameDiscoverer(org.springframework.core.StandardReflectionParameterNameDiscoverer) Test(org.junit.Test) AbstractCircuitBreakerConfigurationOnMissingBean(io.github.resilience4j.circuitbreaker.autoconfigure.AbstractCircuitBreakerConfigurationOnMissingBean) CircuitBreakerConfigCustomizer(io.github.resilience4j.common.circuitbreaker.configuration.CircuitBreakerConfigCustomizer) DefaultEventConsumerRegistry(io.github.resilience4j.consumer.DefaultEventConsumerRegistry) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) AbstractTimeLimiterConfigurationOnMissingBean(io.github.resilience4j.timelimiter.autoconfigure.AbstractTimeLimiterConfigurationOnMissingBean) FallbackExecutor(io.github.resilience4j.fallback.FallbackExecutor) RateLimiterConfigurationProperties(io.github.resilience4j.ratelimiter.configure.RateLimiterConfigurationProperties) Optional(java.util.Optional) ThreadPoolBulkheadConfigurationProperties(io.github.resilience4j.common.bulkhead.configuration.ThreadPoolBulkheadConfigurationProperties) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) CompletionStageFallbackDecorator(io.github.resilience4j.fallback.CompletionStageFallbackDecorator) Collections(java.util.Collections) ThreadPoolBulkheadRegistry(io.github.resilience4j.bulkhead.ThreadPoolBulkheadRegistry) StandardReflectionParameterNameDiscoverer(org.springframework.core.StandardReflectionParameterNameDiscoverer) CompletionStageFallbackDecorator(io.github.resilience4j.fallback.CompletionStageFallbackDecorator) AbstractBulkheadConfigurationOnMissingBean(io.github.resilience4j.bulkhead.autoconfigure.AbstractBulkheadConfigurationOnMissingBean) FallbackDecorators(io.github.resilience4j.fallback.FallbackDecorators) BulkheadConfigurationProperties(io.github.resilience4j.bulkhead.configure.BulkheadConfigurationProperties) ThreadPoolBulkheadConfigurationProperties(io.github.resilience4j.common.bulkhead.configuration.ThreadPoolBulkheadConfigurationProperties) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) ThreadPoolBulkheadConfigurationProperties(io.github.resilience4j.common.bulkhead.configuration.ThreadPoolBulkheadConfigurationProperties) DefaultSpelResolver(io.github.resilience4j.spelresolver.DefaultSpelResolver) FallbackExecutor(io.github.resilience4j.fallback.FallbackExecutor) Test(org.junit.Test)

Example 2 with CompositeRegistryEventConsumer

use of io.github.resilience4j.core.registry.CompositeRegistryEventConsumer 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 3 with CompositeRegistryEventConsumer

use of io.github.resilience4j.core.registry.CompositeRegistryEventConsumer in project resilience4j by resilience4j.

the class BulkHeadConfigurationTest method testCreateBulkHeadRegistryWithUnknownConfig.

@Test
public void testCreateBulkHeadRegistryWithUnknownConfig() {
    BulkheadConfigurationProperties bulkheadConfigurationProperties = new BulkheadConfigurationProperties();
    io.github.resilience4j.common.bulkhead.configuration.BulkheadConfigurationProperties.InstanceProperties instanceProperties = new io.github.resilience4j.common.bulkhead.configuration.BulkheadConfigurationProperties.InstanceProperties();
    instanceProperties.setBaseConfig("unknownConfig");
    bulkheadConfigurationProperties.getInstances().put("backend", instanceProperties);
    BulkheadConfiguration bulkheadConfiguration = new BulkheadConfiguration();
    DefaultEventConsumerRegistry<BulkheadEvent> eventConsumerRegistry = new DefaultEventConsumerRegistry<>();
    // When
    assertThatThrownBy(() -> bulkheadConfiguration.bulkheadRegistry(bulkheadConfigurationProperties, eventConsumerRegistry, new CompositeRegistryEventConsumer<>(emptyList()), new CompositeCustomizer<>(Collections.emptyList()))).isInstanceOf(ConfigurationNotFoundException.class).hasMessage("Configuration with name 'unknownConfig' does not exist");
}
Also used : BulkheadEvent(io.github.resilience4j.bulkhead.event.BulkheadEvent) DefaultEventConsumerRegistry(io.github.resilience4j.consumer.DefaultEventConsumerRegistry) CompositeRegistryEventConsumer(io.github.resilience4j.core.registry.CompositeRegistryEventConsumer) ThreadPoolBulkheadConfiguration(io.github.resilience4j.bulkhead.configure.threadpool.ThreadPoolBulkheadConfiguration) ConfigurationNotFoundException(io.github.resilience4j.core.ConfigurationNotFoundException) ThreadPoolBulkheadConfigurationProperties(io.github.resilience4j.common.bulkhead.configuration.ThreadPoolBulkheadConfigurationProperties) CompositeCustomizer(io.github.resilience4j.common.CompositeCustomizer) io.github.resilience4j.bulkhead(io.github.resilience4j.bulkhead) Test(org.junit.Test)

Example 4 with CompositeRegistryEventConsumer

use of io.github.resilience4j.core.registry.CompositeRegistryEventConsumer in project resilience4j by resilience4j.

the class CircuitBreakerConfigurationTest method testCreateCircuitBreakerRegistryWithUnknownConfig.

@Test
public void testCreateCircuitBreakerRegistryWithUnknownConfig() {
    CircuitBreakerConfigurationProperties circuitBreakerConfigurationProperties = new CircuitBreakerConfigurationProperties();
    InstanceProperties instanceProperties = new InstanceProperties();
    instanceProperties.setBaseConfig("unknownConfig");
    circuitBreakerConfigurationProperties.getInstances().put("backend", instanceProperties);
    CircuitBreakerConfiguration circuitBreakerConfiguration = new CircuitBreakerConfiguration(circuitBreakerConfigurationProperties);
    DefaultEventConsumerRegistry<CircuitBreakerEvent> eventConsumerRegistry = new DefaultEventConsumerRegistry<>();
    assertThatThrownBy(() -> circuitBreakerConfiguration.circuitBreakerRegistry(eventConsumerRegistry, new CompositeRegistryEventConsumer<>(emptyList()), compositeCircuitBreakerCustomizerTestInstance())).isInstanceOf(ConfigurationNotFoundException.class).hasMessage("Configuration with name 'unknownConfig' does not exist");
}
Also used : CircuitBreakerEvent(io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent) InstanceProperties(io.github.resilience4j.common.circuitbreaker.configuration.CircuitBreakerConfigurationProperties.InstanceProperties) DefaultEventConsumerRegistry(io.github.resilience4j.consumer.DefaultEventConsumerRegistry) CompositeRegistryEventConsumer(io.github.resilience4j.core.registry.CompositeRegistryEventConsumer) ConfigurationNotFoundException(io.github.resilience4j.core.ConfigurationNotFoundException) Test(org.junit.Test)

Example 5 with CompositeRegistryEventConsumer

use of io.github.resilience4j.core.registry.CompositeRegistryEventConsumer in project resilience4j by resilience4j.

the class RetryConfigurationTest method testCreateRetryRegistryWithUnknownConfig.

@Test
public void testCreateRetryRegistryWithUnknownConfig() {
    RetryConfigurationProperties retryConfigurationProperties = new RetryConfigurationProperties();
    InstanceProperties instanceProperties = new InstanceProperties();
    instanceProperties.setBaseConfig("unknownConfig");
    retryConfigurationProperties.getInstances().put("backend", instanceProperties);
    RetryConfiguration retryConfiguration = new RetryConfiguration();
    DefaultEventConsumerRegistry<RetryEvent> eventConsumerRegistry = new DefaultEventConsumerRegistry<>();
    assertThatThrownBy(() -> retryConfiguration.retryRegistry(retryConfigurationProperties, eventConsumerRegistry, new CompositeRegistryEventConsumer<>(emptyList()), compositeRetryCustomizerTest())).isInstanceOf(ConfigurationNotFoundException.class).hasMessage("Configuration with name 'unknownConfig' does not exist");
}
Also used : RetryEvent(io.github.resilience4j.retry.event.RetryEvent) InstanceProperties(io.github.resilience4j.common.retry.configuration.RetryConfigurationProperties.InstanceProperties) DefaultEventConsumerRegistry(io.github.resilience4j.consumer.DefaultEventConsumerRegistry) CompositeRegistryEventConsumer(io.github.resilience4j.core.registry.CompositeRegistryEventConsumer) ConfigurationNotFoundException(io.github.resilience4j.core.ConfigurationNotFoundException) Test(org.junit.Test)

Aggregations

DefaultEventConsumerRegistry (io.github.resilience4j.consumer.DefaultEventConsumerRegistry)6 CompositeRegistryEventConsumer (io.github.resilience4j.core.registry.CompositeRegistryEventConsumer)6 Test (org.junit.Test)6 CompositeCustomizer (io.github.resilience4j.common.CompositeCustomizer)4 ThreadPoolBulkheadConfigurationProperties (io.github.resilience4j.common.bulkhead.configuration.ThreadPoolBulkheadConfigurationProperties)4 ConfigurationNotFoundException (io.github.resilience4j.core.ConfigurationNotFoundException)4 Duration (java.time.Duration)3 Collections (java.util.Collections)3 io.github.resilience4j.bulkhead (io.github.resilience4j.bulkhead)2 BulkheadRegistry (io.github.resilience4j.bulkhead.BulkheadRegistry)2 ThreadPoolBulkheadRegistry (io.github.resilience4j.bulkhead.ThreadPoolBulkheadRegistry)2 AbstractBulkheadConfigurationOnMissingBean (io.github.resilience4j.bulkhead.autoconfigure.AbstractBulkheadConfigurationOnMissingBean)2 BulkheadConfigurationProperties (io.github.resilience4j.bulkhead.configure.BulkheadConfigurationProperties)2 ThreadPoolBulkheadConfiguration (io.github.resilience4j.bulkhead.configure.threadpool.ThreadPoolBulkheadConfiguration)2 BulkheadEvent (io.github.resilience4j.bulkhead.event.BulkheadEvent)2 CircuitBreakerRegistry (io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry)2 AbstractCircuitBreakerConfigurationOnMissingBean (io.github.resilience4j.circuitbreaker.autoconfigure.AbstractCircuitBreakerConfigurationOnMissingBean)2 CircuitBreakerConfigurationProperties (io.github.resilience4j.circuitbreaker.configure.CircuitBreakerConfigurationProperties)2 BulkheadConfigCustomizer (io.github.resilience4j.common.bulkhead.configuration.BulkheadConfigCustomizer)2 ThreadPoolBulkheadConfigCustomizer (io.github.resilience4j.common.bulkhead.configuration.ThreadPoolBulkheadConfigCustomizer)2