use of io.github.resilience4j.consumer.DefaultEventConsumerRegistry in project resilience4j by resilience4j.
the class RetryConfigurationTest method testRetryRegistry.
@Test
public void testRetryRegistry() {
InstanceProperties instanceProperties1 = new InstanceProperties();
instanceProperties1.setMaxAttempts(3);
InstanceProperties instanceProperties2 = new InstanceProperties();
instanceProperties2.setMaxAttempts(2);
RetryConfigurationProperties retryConfigurationProperties = new RetryConfigurationProperties();
retryConfigurationProperties.getInstances().put("backend1", instanceProperties1);
retryConfigurationProperties.getInstances().put("backend2", instanceProperties2);
retryConfigurationProperties.setRetryAspectOrder(200);
RetryConfiguration retryConfiguration = new RetryConfiguration();
DefaultEventConsumerRegistry<RetryEvent> eventConsumerRegistry = new DefaultEventConsumerRegistry<>();
RetryRegistry retryRegistry = retryConfiguration.retryRegistry(retryConfigurationProperties, eventConsumerRegistry, new CompositeRegistryEventConsumer<>(emptyList()), compositeRetryCustomizerTest());
assertThat(retryConfigurationProperties.getRetryAspectOrder()).isEqualTo(200);
assertThat(retryRegistry.getAllRetries().size()).isEqualTo(2);
Retry retry1 = retryRegistry.retry("backend1");
assertThat(retry1).isNotNull();
assertThat(retry1.getRetryConfig().getMaxAttempts()).isEqualTo(3);
Retry retry2 = retryRegistry.retry("backend2");
assertThat(retry2).isNotNull();
assertThat(retry2.getRetryConfig().getMaxAttempts()).isEqualTo(2);
assertThat(eventConsumerRegistry.getAllEventConsumer()).hasSize(2);
}
use of io.github.resilience4j.consumer.DefaultEventConsumerRegistry 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");
}
use of io.github.resilience4j.consumer.DefaultEventConsumerRegistry in project resilience4j by resilience4j.
the class BulkHeadConfigurationTest method testCreateBulkHeadRegistryWithSharedConfigs.
@Test
public void testCreateBulkHeadRegistryWithSharedConfigs() {
// Given
io.github.resilience4j.common.bulkhead.configuration.BulkheadConfigurationProperties.InstanceProperties defaultProperties = new io.github.resilience4j.common.bulkhead.configuration.BulkheadConfigurationProperties.InstanceProperties();
defaultProperties.setMaxConcurrentCalls(3);
defaultProperties.setMaxWaitDuration(Duration.ofMillis(50L));
assertThat(defaultProperties.getEventConsumerBufferSize()).isNull();
io.github.resilience4j.common.bulkhead.configuration.BulkheadConfigurationProperties.InstanceProperties sharedProperties = new io.github.resilience4j.common.bulkhead.configuration.BulkheadConfigurationProperties.InstanceProperties();
sharedProperties.setMaxConcurrentCalls(2);
sharedProperties.setMaxWaitDuration(Duration.ofMillis(100L));
assertThat(sharedProperties.getEventConsumerBufferSize()).isNull();
io.github.resilience4j.common.bulkhead.configuration.BulkheadConfigurationProperties.InstanceProperties backendWithDefaultConfig = new io.github.resilience4j.common.bulkhead.configuration.BulkheadConfigurationProperties.InstanceProperties();
backendWithDefaultConfig.setBaseConfig("default");
backendWithDefaultConfig.setMaxWaitDuration(Duration.ofMillis(200L));
assertThat(backendWithDefaultConfig.getEventConsumerBufferSize()).isNull();
io.github.resilience4j.common.bulkhead.configuration.BulkheadConfigurationProperties.InstanceProperties backendWithSharedConfig = new io.github.resilience4j.common.bulkhead.configuration.BulkheadConfigurationProperties.InstanceProperties();
backendWithSharedConfig.setBaseConfig("sharedConfig");
backendWithSharedConfig.setMaxWaitDuration(Duration.ofMillis(300L));
assertThat(backendWithSharedConfig.getEventConsumerBufferSize()).isNull();
BulkheadConfigurationProperties bulkheadConfigurationProperties = new BulkheadConfigurationProperties();
bulkheadConfigurationProperties.getConfigs().put("default", defaultProperties);
bulkheadConfigurationProperties.getConfigs().put("sharedConfig", sharedProperties);
bulkheadConfigurationProperties.getInstances().put("backendWithDefaultConfig", backendWithDefaultConfig);
bulkheadConfigurationProperties.getInstances().put("backendWithSharedConfig", backendWithSharedConfig);
BulkheadConfiguration bulkheadConfiguration = new BulkheadConfiguration();
DefaultEventConsumerRegistry<BulkheadEvent> eventConsumerRegistry = new DefaultEventConsumerRegistry<>();
// When
BulkheadRegistry bulkheadRegistry = bulkheadConfiguration.bulkheadRegistry(bulkheadConfigurationProperties, eventConsumerRegistry, new CompositeRegistryEventConsumer<>(emptyList()), new CompositeCustomizer<>(Collections.emptyList()));
// Then
assertThat(bulkheadRegistry.getAllBulkheads().size()).isEqualTo(2);
// Should get default config and overwrite max calls and wait time
Bulkhead bulkhead1 = bulkheadRegistry.bulkhead("backendWithDefaultConfig");
assertThat(bulkhead1).isNotNull();
assertThat(bulkhead1.getBulkheadConfig().getMaxConcurrentCalls()).isEqualTo(3);
assertThat(bulkhead1.getBulkheadConfig().getMaxWaitDuration().toMillis()).isEqualTo(200L);
// Should get shared config and overwrite wait time
Bulkhead bulkhead2 = bulkheadRegistry.bulkhead("backendWithSharedConfig");
assertThat(bulkhead2).isNotNull();
assertThat(bulkhead2.getBulkheadConfig().getMaxConcurrentCalls()).isEqualTo(2);
assertThat(bulkhead2.getBulkheadConfig().getMaxWaitDuration().toMillis()).isEqualTo(300L);
// Unknown backend should get default config of Registry
Bulkhead bulkhead3 = bulkheadRegistry.bulkhead("unknownBackend");
assertThat(bulkhead3).isNotNull();
assertThat(bulkhead3.getBulkheadConfig().getMaxWaitDuration().toMillis()).isEqualTo(50L);
assertThat(eventConsumerRegistry.getAllEventConsumer()).hasSize(3);
}
use of io.github.resilience4j.consumer.DefaultEventConsumerRegistry 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);
}
use of io.github.resilience4j.consumer.DefaultEventConsumerRegistry in project resilience4j by resilience4j.
the class RateLimiterConfigurationTest method testCreateRateLimiterRegistryWithSharedConfigs.
@Test
public void testCreateRateLimiterRegistryWithSharedConfigs() {
// Given
io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties.InstanceProperties defaultProperties = new io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties.InstanceProperties();
defaultProperties.setLimitForPeriod(3);
defaultProperties.setLimitRefreshPeriod(Duration.ofNanos(5000000));
defaultProperties.setSubscribeForEvents(true);
io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties.InstanceProperties sharedProperties = new io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties.InstanceProperties();
sharedProperties.setLimitForPeriod(2);
sharedProperties.setLimitRefreshPeriod(Duration.ofNanos(6000000));
sharedProperties.setSubscribeForEvents(true);
io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties.InstanceProperties backendWithDefaultConfig = new io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties.InstanceProperties();
backendWithDefaultConfig.setBaseConfig("default");
backendWithDefaultConfig.setLimitForPeriod(200);
backendWithDefaultConfig.setSubscribeForEvents(true);
io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties.InstanceProperties backendWithSharedConfig = new io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties.InstanceProperties();
backendWithSharedConfig.setBaseConfig("sharedConfig");
backendWithSharedConfig.setLimitForPeriod(300);
backendWithSharedConfig.setSubscribeForEvents(true);
RateLimiterConfigurationProperties rateLimiterConfigurationProperties = new RateLimiterConfigurationProperties();
rateLimiterConfigurationProperties.getConfigs().put("default", defaultProperties);
rateLimiterConfigurationProperties.getConfigs().put("sharedConfig", sharedProperties);
rateLimiterConfigurationProperties.getInstances().put("backendWithDefaultConfig", backendWithDefaultConfig);
rateLimiterConfigurationProperties.getInstances().put("backendWithSharedConfig", backendWithSharedConfig);
RateLimiterConfiguration rateLimiterConfiguration = new RateLimiterConfiguration();
DefaultEventConsumerRegistry<RateLimiterEvent> eventConsumerRegistry = new DefaultEventConsumerRegistry<>();
// When
RateLimiterRegistry rateLimiterRegistry = rateLimiterConfiguration.rateLimiterRegistry(rateLimiterConfigurationProperties, eventConsumerRegistry, new CompositeRegistryEventConsumer<>(emptyList()), compositeRateLimiterCustomizerTest());
// Then
assertThat(rateLimiterRegistry.getAllRateLimiters().size()).isEqualTo(2);
// Should get default config and override LimitForPeriod
RateLimiter rateLimiter1 = rateLimiterRegistry.rateLimiter("backendWithDefaultConfig");
assertThat(rateLimiter1).isNotNull();
assertThat(rateLimiter1.getRateLimiterConfig().getLimitForPeriod()).isEqualTo(200);
assertThat(rateLimiter1.getRateLimiterConfig().getLimitRefreshPeriod()).isEqualTo(Duration.ofMillis(5));
// Should get shared config and override LimitForPeriod
RateLimiter rateLimiter2 = rateLimiterRegistry.rateLimiter("backendWithSharedConfig");
assertThat(rateLimiter2).isNotNull();
assertThat(rateLimiter2.getRateLimiterConfig().getLimitForPeriod()).isEqualTo(300);
assertThat(rateLimiter2.getRateLimiterConfig().getLimitRefreshPeriod()).isEqualTo(Duration.ofMillis(6));
// Unknown backend should get default config of Registry
RateLimiter rerateLimiter3 = rateLimiterRegistry.rateLimiter("unknownBackend");
assertThat(rerateLimiter3).isNotNull();
assertThat(rerateLimiter3.getRateLimiterConfig().getLimitForPeriod()).isEqualTo(3);
assertThat(eventConsumerRegistry.getAllEventConsumer()).hasSize(2);
}
Aggregations