use of io.github.resilience4j.consumer.DefaultEventConsumerRegistry in project resilience4j by resilience4j.
the class CircuitBreakerConfigurationTest method testCreateCircuitBreakerRegistryWithSharedConfigs.
@Test
public void testCreateCircuitBreakerRegistryWithSharedConfigs() {
InstanceProperties defaultProperties = new InstanceProperties();
defaultProperties.setSlidingWindowSize(1000);
defaultProperties.setPermittedNumberOfCallsInHalfOpenState(100);
InstanceProperties sharedProperties = new InstanceProperties();
sharedProperties.setSlidingWindowSize(1337);
sharedProperties.setPermittedNumberOfCallsInHalfOpenState(1000);
InstanceProperties backendWithDefaultConfig = new InstanceProperties();
backendWithDefaultConfig.setBaseConfig("default");
backendWithDefaultConfig.setPermittedNumberOfCallsInHalfOpenState(99);
InstanceProperties backendWithSharedConfig = new InstanceProperties();
backendWithSharedConfig.setBaseConfig("sharedConfig");
backendWithSharedConfig.setPermittedNumberOfCallsInHalfOpenState(999);
CircuitBreakerConfigurationProperties circuitBreakerConfigurationProperties = new CircuitBreakerConfigurationProperties();
circuitBreakerConfigurationProperties.getConfigs().put("default", defaultProperties);
circuitBreakerConfigurationProperties.getConfigs().put("sharedConfig", sharedProperties);
circuitBreakerConfigurationProperties.getInstances().put("backendWithDefaultConfig", backendWithDefaultConfig);
circuitBreakerConfigurationProperties.getInstances().put("backendWithSharedConfig", backendWithSharedConfig);
CircuitBreakerConfiguration circuitBreakerConfiguration = new CircuitBreakerConfiguration(circuitBreakerConfigurationProperties);
DefaultEventConsumerRegistry<CircuitBreakerEvent> eventConsumerRegistry = new DefaultEventConsumerRegistry<>();
CircuitBreakerRegistry circuitBreakerRegistry = circuitBreakerConfiguration.circuitBreakerRegistry(eventConsumerRegistry, new CompositeRegistryEventConsumer<>(emptyList()), compositeCircuitBreakerCustomizerTestInstance());
assertThat(circuitBreakerRegistry.getAllCircuitBreakers().size()).isEqualTo(2);
// Should get default config and overwrite setPermittedNumberOfCallsInHalfOpenState
CircuitBreaker circuitBreaker1 = circuitBreakerRegistry.circuitBreaker("backendWithDefaultConfig");
assertThat(circuitBreaker1).isNotNull();
assertThat(circuitBreaker1.getCircuitBreakerConfig().getSlidingWindowSize()).isEqualTo(1000);
assertThat(circuitBreaker1.getCircuitBreakerConfig().getPermittedNumberOfCallsInHalfOpenState()).isEqualTo(99);
// Should get shared config and overwrite setPermittedNumberOfCallsInHalfOpenState
CircuitBreaker circuitBreaker2 = circuitBreakerRegistry.circuitBreaker("backendWithSharedConfig");
assertThat(circuitBreaker2).isNotNull();
assertThat(circuitBreaker2.getCircuitBreakerConfig().getSlidingWindowSize()).isEqualTo(1337);
assertThat(circuitBreaker2.getCircuitBreakerConfig().getPermittedNumberOfCallsInHalfOpenState()).isEqualTo(999);
// Unknown backend should get default config of Registry
CircuitBreaker circuitBreaker3 = circuitBreakerRegistry.circuitBreaker("unknownBackend");
assertThat(circuitBreaker3).isNotNull();
assertThat(circuitBreaker3.getCircuitBreakerConfig().getSlidingWindowSize()).isEqualTo(1000);
assertThat(eventConsumerRegistry.getAllEventConsumer()).hasSize(3);
}
use of io.github.resilience4j.consumer.DefaultEventConsumerRegistry in project resilience4j by resilience4j.
the class CircuitBreakerConfigurationTest method testCreateCircuitBreakerRegistry.
@Test
public void testCreateCircuitBreakerRegistry() {
InstanceProperties instanceProperties1 = new InstanceProperties();
instanceProperties1.setSlidingWindowSize(1000);
InstanceProperties instanceProperties2 = new InstanceProperties();
instanceProperties2.setSlidingWindowSize(1337);
CircuitBreakerConfigurationProperties circuitBreakerConfigurationProperties = new CircuitBreakerConfigurationProperties();
circuitBreakerConfigurationProperties.setCircuitBreakerAspectOrder(400);
circuitBreakerConfigurationProperties.getInstances().put("backend1", instanceProperties1);
circuitBreakerConfigurationProperties.getInstances().put("backend2", instanceProperties2);
CircuitBreakerConfiguration circuitBreakerConfiguration = new CircuitBreakerConfiguration(circuitBreakerConfigurationProperties);
DefaultEventConsumerRegistry<CircuitBreakerEvent> eventConsumerRegistry = new DefaultEventConsumerRegistry<>();
CircuitBreakerRegistry circuitBreakerRegistry = circuitBreakerConfiguration.circuitBreakerRegistry(eventConsumerRegistry, new CompositeRegistryEventConsumer<>(emptyList()), compositeCircuitBreakerCustomizerTestInstance());
assertThat(circuitBreakerConfigurationProperties.getCircuitBreakerAspectOrder()).isEqualTo(400);
assertThat(circuitBreakerRegistry.getAllCircuitBreakers().size()).isEqualTo(2);
CircuitBreaker circuitBreaker1 = circuitBreakerRegistry.circuitBreaker("backend1");
assertThat(circuitBreaker1).isNotNull();
assertThat(circuitBreaker1.getCircuitBreakerConfig().getSlidingWindowSize()).isEqualTo(1000);
CircuitBreaker circuitBreaker2 = circuitBreakerRegistry.circuitBreaker("backend2");
assertThat(circuitBreaker2).isNotNull();
assertThat(circuitBreaker2.getCircuitBreakerConfig().getSlidingWindowSize()).isEqualTo(1337);
assertThat(eventConsumerRegistry.getAllEventConsumer()).hasSize(2);
}
use of io.github.resilience4j.consumer.DefaultEventConsumerRegistry in project resilience4j by resilience4j.
the class TimeLimiterConfigurationTest method testTimeLimiterRegistry.
@Test
public void testTimeLimiterRegistry() {
// Given
io.github.resilience4j.common.timelimiter.configuration.TimeLimiterConfigurationProperties.InstanceProperties instanceProperties1 = new io.github.resilience4j.common.timelimiter.configuration.TimeLimiterConfigurationProperties.InstanceProperties();
instanceProperties1.setTimeoutDuration(Duration.ofSeconds(3));
io.github.resilience4j.common.timelimiter.configuration.TimeLimiterConfigurationProperties.InstanceProperties instanceProperties2 = new io.github.resilience4j.common.timelimiter.configuration.TimeLimiterConfigurationProperties.InstanceProperties();
instanceProperties2.setTimeoutDuration(Duration.ofSeconds(2));
TimeLimiterConfigurationProperties timeLimiterConfigurationProperties = new TimeLimiterConfigurationProperties();
timeLimiterConfigurationProperties.getInstances().put("backend1", instanceProperties1);
timeLimiterConfigurationProperties.getInstances().put("backend2", instanceProperties2);
timeLimiterConfigurationProperties.setTimeLimiterAspectOrder(200);
TimeLimiterConfiguration timeLimiterConfiguration = new TimeLimiterConfiguration();
DefaultEventConsumerRegistry<TimeLimiterEvent> eventConsumerRegistry = new DefaultEventConsumerRegistry<>();
// When
TimeLimiterRegistry timeLimiterRegistry = timeLimiterConfiguration.timeLimiterRegistry(timeLimiterConfigurationProperties, eventConsumerRegistry, new CompositeRegistryEventConsumer<>(emptyList()), compositeTimeLimiterCustomizerTestInstance());
// Then
assertThat(timeLimiterConfigurationProperties.getTimeLimiterAspectOrder()).isEqualTo(200);
assertThat(timeLimiterRegistry.getAllTimeLimiters().size()).isEqualTo(2);
TimeLimiter timeLimiter1 = timeLimiterRegistry.timeLimiter("backend1");
assertThat(timeLimiter1).isNotNull();
assertThat(timeLimiter1.getTimeLimiterConfig().getTimeoutDuration()).isEqualTo(Duration.ofSeconds(3));
TimeLimiter timeLimiter2 = timeLimiterRegistry.timeLimiter("backend2");
assertThat(timeLimiter2).isNotNull();
assertThat(timeLimiter2.getTimeLimiterConfig().getTimeoutDuration()).isEqualTo(Duration.ofSeconds(2));
assertThat(eventConsumerRegistry.getAllEventConsumer()).hasSize(2);
}
use of io.github.resilience4j.consumer.DefaultEventConsumerRegistry in project resilience4j by resilience4j.
the class TimeLimiterConfigurationTest method testCreateTimeLimiterRegistryWithSharedConfigs.
@Test
public void testCreateTimeLimiterRegistryWithSharedConfigs() {
// Given
io.github.resilience4j.common.timelimiter.configuration.TimeLimiterConfigurationProperties.InstanceProperties defaultProperties = new io.github.resilience4j.common.timelimiter.configuration.TimeLimiterConfigurationProperties.InstanceProperties();
defaultProperties.setTimeoutDuration(Duration.ofSeconds(3));
defaultProperties.setCancelRunningFuture(true);
io.github.resilience4j.common.timelimiter.configuration.TimeLimiterConfigurationProperties.InstanceProperties sharedProperties = new io.github.resilience4j.common.timelimiter.configuration.TimeLimiterConfigurationProperties.InstanceProperties();
sharedProperties.setTimeoutDuration(Duration.ofSeconds(2));
sharedProperties.setCancelRunningFuture(false);
io.github.resilience4j.common.timelimiter.configuration.TimeLimiterConfigurationProperties.InstanceProperties backendWithDefaultConfig = new io.github.resilience4j.common.timelimiter.configuration.TimeLimiterConfigurationProperties.InstanceProperties();
backendWithDefaultConfig.setBaseConfig("default");
backendWithDefaultConfig.setTimeoutDuration(Duration.ofSeconds(5));
io.github.resilience4j.common.timelimiter.configuration.TimeLimiterConfigurationProperties.InstanceProperties backendWithSharedConfig = new io.github.resilience4j.common.timelimiter.configuration.TimeLimiterConfigurationProperties.InstanceProperties();
backendWithSharedConfig.setBaseConfig("sharedConfig");
backendWithSharedConfig.setCancelRunningFuture(true);
TimeLimiterConfigurationProperties timeLimiterConfigurationProperties = new TimeLimiterConfigurationProperties();
timeLimiterConfigurationProperties.getConfigs().put("default", defaultProperties);
timeLimiterConfigurationProperties.getConfigs().put("sharedConfig", sharedProperties);
timeLimiterConfigurationProperties.getInstances().put("backendWithDefaultConfig", backendWithDefaultConfig);
timeLimiterConfigurationProperties.getInstances().put("backendWithSharedConfig", backendWithSharedConfig);
TimeLimiterConfiguration timeLimiterConfiguration = new TimeLimiterConfiguration();
DefaultEventConsumerRegistry<TimeLimiterEvent> eventConsumerRegistry = new DefaultEventConsumerRegistry<>();
// When
TimeLimiterRegistry timeLimiterRegistry = timeLimiterConfiguration.timeLimiterRegistry(timeLimiterConfigurationProperties, eventConsumerRegistry, new CompositeRegistryEventConsumer<>(emptyList()), compositeTimeLimiterCustomizerTestInstance());
// Then
assertThat(timeLimiterRegistry.getAllTimeLimiters().size()).isEqualTo(2);
// Should get default config and overwrite timeout duration
TimeLimiter timeLimiter1 = timeLimiterRegistry.timeLimiter("backendWithDefaultConfig");
assertThat(timeLimiter1).isNotNull();
assertThat(timeLimiter1.getTimeLimiterConfig().getTimeoutDuration()).isEqualTo(Duration.ofSeconds(5));
assertThat(timeLimiter1.getTimeLimiterConfig().shouldCancelRunningFuture()).isEqualTo(true);
// Should get shared config and overwrite cancelRunningFuture
TimeLimiter timeLimiter2 = timeLimiterRegistry.timeLimiter("backendWithSharedConfig");
assertThat(timeLimiter2).isNotNull();
assertThat(timeLimiter2.getTimeLimiterConfig().getTimeoutDuration()).isEqualTo(Duration.ofSeconds(2));
assertThat(timeLimiter2.getTimeLimiterConfig().shouldCancelRunningFuture()).isEqualTo(true);
// Unknown backend should get default config of Registry
TimeLimiter timeLimiter3 = timeLimiterRegistry.timeLimiter("unknownBackend");
assertThat(timeLimiter3).isNotNull();
assertThat(timeLimiter3.getTimeLimiterConfig().getTimeoutDuration()).isEqualTo(Duration.ofSeconds(3));
assertThat(eventConsumerRegistry.getAllEventConsumer()).hasSize(3);
}
use of io.github.resilience4j.consumer.DefaultEventConsumerRegistry in project resilience4j by resilience4j.
the class RetryConfigurationTest method testCreateRetryRegistryWithSharedConfigs.
@Test
public void testCreateRetryRegistryWithSharedConfigs() {
InstanceProperties defaultProperties = new InstanceProperties();
defaultProperties.setMaxAttempts(3);
defaultProperties.setWaitDuration(Duration.ofMillis(100L));
InstanceProperties sharedProperties = new InstanceProperties();
sharedProperties.setMaxAttempts(2);
sharedProperties.setWaitDuration(Duration.ofMillis(100L));
InstanceProperties backendWithDefaultConfig = new InstanceProperties();
backendWithDefaultConfig.setBaseConfig("default");
backendWithDefaultConfig.setWaitDuration(Duration.ofMillis(200L));
InstanceProperties backendWithSharedConfig = new InstanceProperties();
backendWithSharedConfig.setBaseConfig("sharedConfig");
backendWithSharedConfig.setWaitDuration(Duration.ofMillis(300L));
RetryConfigurationProperties retryConfigurationProperties = new RetryConfigurationProperties();
retryConfigurationProperties.getConfigs().put("default", defaultProperties);
retryConfigurationProperties.getConfigs().put("sharedConfig", sharedProperties);
retryConfigurationProperties.getInstances().put("backendWithDefaultConfig", backendWithDefaultConfig);
retryConfigurationProperties.getInstances().put("backendWithSharedConfig", backendWithSharedConfig);
RetryConfiguration retryConfiguration = new RetryConfiguration();
DefaultEventConsumerRegistry<RetryEvent> eventConsumerRegistry = new DefaultEventConsumerRegistry<>();
RetryRegistry retryRegistry = retryConfiguration.retryRegistry(retryConfigurationProperties, eventConsumerRegistry, new CompositeRegistryEventConsumer<>(emptyList()), compositeRetryCustomizerTest());
assertThat(retryRegistry.getAllRetries().size()).isEqualTo(2);
// Should get default config and overwrite max attempt and wait time
Retry retry1 = retryRegistry.retry("backendWithDefaultConfig");
assertThat(retry1).isNotNull();
assertThat(retry1.getRetryConfig().getMaxAttempts()).isEqualTo(3);
assertThat(retry1.getRetryConfig().getIntervalBiFunction().apply(1, null)).isEqualTo(200L);
// Should get shared config and overwrite wait time
Retry retry2 = retryRegistry.retry("backendWithSharedConfig");
assertThat(retry2).isNotNull();
assertThat(retry2.getRetryConfig().getMaxAttempts()).isEqualTo(2);
assertThat(retry2.getRetryConfig().getIntervalBiFunction().apply(1, null)).isEqualTo(300L);
// Unknown backend should get default config of Registry
Retry retry3 = retryRegistry.retry("unknownBackend");
assertThat(retry3).isNotNull();
assertThat(retry3.getRetryConfig().getMaxAttempts()).isEqualTo(3);
assertThat(eventConsumerRegistry.getAllEventConsumer()).hasSize(3);
}
Aggregations