Search in sources :

Example 1 with CircuitBreakerConfigurationProperties

use of io.github.resilience4j.circuitbreaker.configure.CircuitBreakerConfigurationProperties in project resilience4j by resilience4j.

the class SpringBootCommonTest method testCircuitBreakerCommonConfig.

@Test
public void testCircuitBreakerCommonConfig() {
    CircuitBreakerConfig circuitBreakerConfig = new CircuitBreakerConfig(new CircuitBreakerConfigurationProperties());
    assertThat(circuitBreakerConfig.reactorCircuitBreakerAspect()).isNotNull();
    assertThat(circuitBreakerConfig.rxJava2CircuitBreakerAspect()).isNotNull();
    assertThat(circuitBreakerConfig.circuitBreakerRegistry(new DefaultEventConsumerRegistry<>(), new CompositeRegistryEventConsumer<>(Collections.emptyList()), new CompositeCustomizer<>(Collections.singletonList(new TestCustomizer())))).isNotNull();
    final DefaultSpelResolver spelResolver = new DefaultSpelResolver(new SpelExpressionParser(), new StandardReflectionParameterNameDiscoverer(), new GenericApplicationContext());
    final FallbackDecorators fallbackDecorators = new FallbackDecorators(Collections.singletonList(new CompletionStageFallbackDecorator()));
    assertThat(circuitBreakerConfig.circuitBreakerAspect(CircuitBreakerRegistry.ofDefaults(), Collections.emptyList(), new FallbackExecutor(spelResolver, fallbackDecorators), spelResolver)).isNotNull();
    assertThat(circuitBreakerConfig.circuitBreakerRegistryEventConsumer(Optional.empty())).isNotNull();
}
Also used : CircuitBreakerConfigurationProperties(io.github.resilience4j.circuitbreaker.configure.CircuitBreakerConfigurationProperties) StandardReflectionParameterNameDiscoverer(org.springframework.core.StandardReflectionParameterNameDiscoverer) CompletionStageFallbackDecorator(io.github.resilience4j.fallback.CompletionStageFallbackDecorator) FallbackDecorators(io.github.resilience4j.fallback.FallbackDecorators) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) DefaultSpelResolver(io.github.resilience4j.spelresolver.DefaultSpelResolver) FallbackExecutor(io.github.resilience4j.fallback.FallbackExecutor) Test(org.junit.Test)

Example 2 with CircuitBreakerConfigurationProperties

use of io.github.resilience4j.circuitbreaker.configure.CircuitBreakerConfigurationProperties in project resilience4j by resilience4j.

the class CircuitBreakersHealthIndicatorTest method testHealthStatus.

@Test
public void testHealthStatus() {
    CircuitBreaker openCircuitBreaker = mock(CircuitBreaker.class);
    CircuitBreaker halfOpenCircuitBreaker = mock(CircuitBreaker.class);
    CircuitBreaker closeCircuitBreaker = mock(CircuitBreaker.class);
    Map<CircuitBreaker.State, CircuitBreaker> expectedStateToCircuitBreaker = new HashMap<>();
    expectedStateToCircuitBreaker.put(OPEN, openCircuitBreaker);
    expectedStateToCircuitBreaker.put(HALF_OPEN, halfOpenCircuitBreaker);
    expectedStateToCircuitBreaker.put(CLOSED, closeCircuitBreaker);
    CircuitBreakerConfigurationProperties.InstanceProperties instanceProperties = mock(CircuitBreakerConfigurationProperties.InstanceProperties.class);
    CircuitBreakerConfigurationProperties circuitBreakerProperties = mock(CircuitBreakerConfigurationProperties.class);
    // given
    CircuitBreakerRegistry registry = mock(CircuitBreakerRegistry.class);
    CircuitBreakerConfig config = mock(CircuitBreakerConfig.class);
    CircuitBreaker.Metrics metrics = mock(CircuitBreaker.Metrics.class);
    // when
    when(registry.getAllCircuitBreakers()).thenReturn(new HashSet<>(expectedStateToCircuitBreaker.values()));
    boolean allowHealthIndicatorToFail = true;
    expectedStateToCircuitBreaker.forEach((state, circuitBreaker) -> setCircuitBreakerWhen(state, circuitBreaker, config, metrics, instanceProperties, circuitBreakerProperties, allowHealthIndicatorToFail));
    CircuitBreakersHealthIndicator healthIndicator = new CircuitBreakersHealthIndicator(registry, circuitBreakerProperties, new SimpleStatusAggregator());
    // then
    Health health = healthIndicator.health();
    then(health.getStatus()).isEqualTo(Status.DOWN);
    then(health.getDetails()).containsKeys(OPEN.name(), HALF_OPEN.name(), CLOSED.name());
    assertState(OPEN, Status.DOWN, health.getDetails());
    assertState(HALF_OPEN, new Status("CIRCUIT_HALF_OPEN"), health.getDetails());
    assertState(CLOSED, Status.UP, health.getDetails());
}
Also used : CircuitBreakerConfigurationProperties(io.github.resilience4j.circuitbreaker.configure.CircuitBreakerConfigurationProperties) Status(org.springframework.boot.actuate.health.Status) CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) SimpleStatusAggregator(org.springframework.boot.actuate.health.SimpleStatusAggregator) Health(org.springframework.boot.actuate.health.Health) State(io.github.resilience4j.circuitbreaker.CircuitBreaker.State) CircuitBreakerRegistry(io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry) CircuitBreakerConfig(io.github.resilience4j.circuitbreaker.CircuitBreakerConfig) Test(org.junit.Test)

Example 3 with CircuitBreakerConfigurationProperties

use of io.github.resilience4j.circuitbreaker.configure.CircuitBreakerConfigurationProperties in project resilience4j by resilience4j.

the class CircuitBreakersHealthIndicatorTest method healthMetricsAndConfig.

@Test
public void healthMetricsAndConfig() {
    // given
    CircuitBreakerConfig config = mock(CircuitBreakerConfig.class);
    CircuitBreakerRegistry registry = mock(CircuitBreakerRegistry.class);
    CircuitBreaker.Metrics metrics = mock(CircuitBreaker.Metrics.class);
    CircuitBreaker circuitBreaker = mock(CircuitBreaker.class);
    CircuitBreakerConfigurationProperties.InstanceProperties instanceProperties = mock(CircuitBreakerConfigurationProperties.InstanceProperties.class);
    CircuitBreakerConfigurationProperties circuitBreakerProperties = mock(CircuitBreakerConfigurationProperties.class);
    CircuitBreakersHealthIndicator healthIndicator = new CircuitBreakersHealthIndicator(registry, circuitBreakerProperties, new SimpleStatusAggregator());
    // when
    when(config.getFailureRateThreshold()).thenReturn(30f);
    when(metrics.getFailureRate()).thenReturn(20f);
    when(metrics.getSlowCallRate()).thenReturn(20f);
    when(config.getSlowCallRateThreshold()).thenReturn(50f);
    when(metrics.getNumberOfBufferedCalls()).thenReturn(100);
    when(metrics.getNumberOfFailedCalls()).thenReturn(20);
    when(metrics.getNumberOfSlowCalls()).thenReturn(20);
    when(metrics.getNumberOfNotPermittedCalls()).thenReturn(0L);
    when(registry.getAllCircuitBreakers()).thenReturn(Set.of(circuitBreaker));
    when(circuitBreaker.getName()).thenReturn("test");
    when(circuitBreakerProperties.findCircuitBreakerProperties("test")).thenReturn(Optional.of(instanceProperties));
    when(instanceProperties.getRegisterHealthIndicator()).thenReturn(true);
    when(instanceProperties.getAllowHealthIndicatorToFail()).thenReturn(true);
    when(circuitBreaker.getMetrics()).thenReturn(metrics);
    when(circuitBreaker.getCircuitBreakerConfig()).thenReturn(config);
    when(circuitBreaker.getState()).thenReturn(CLOSED, OPEN, HALF_OPEN, CLOSED);
    // then
    Health health = healthIndicator.health();
    then(health.getStatus()).isEqualTo(Status.UP);
    then(health.getDetails()).containsKey("test");
    then(health.getDetails().get("test")).isInstanceOf(Health.class);
    then(((Health) health.getDetails().get("test")).getDetails()).contains(entry("failureRate", "20.0%"), entry("slowCallRate", "20.0%"), entry("slowCallRateThreshold", "50.0%"), entry("failureRateThreshold", "30.0%"), entry("bufferedCalls", 100), entry("slowCalls", 20), entry("failedCalls", 20), entry("notPermittedCalls", 0L));
}
Also used : CircuitBreakerConfigurationProperties(io.github.resilience4j.circuitbreaker.configure.CircuitBreakerConfigurationProperties) CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) SimpleStatusAggregator(org.springframework.boot.actuate.health.SimpleStatusAggregator) Health(org.springframework.boot.actuate.health.Health) CircuitBreakerRegistry(io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry) CircuitBreakerConfig(io.github.resilience4j.circuitbreaker.CircuitBreakerConfig) Test(org.junit.Test)

Example 4 with CircuitBreakerConfigurationProperties

use of io.github.resilience4j.circuitbreaker.configure.CircuitBreakerConfigurationProperties in project resilience4j by resilience4j.

the class RefreshScopedCircuitBreakerConfigurationTest method testCircuitBreakerCloudCommonConfig.

@Test
public void testCircuitBreakerCloudCommonConfig() {
    CircuitBreakerConfig circuitBreakerConfig = new CircuitBreakerConfig(new CircuitBreakerConfigurationProperties());
    assertThat(circuitBreakerConfig.circuitBreakerRegistry(new DefaultEventConsumerRegistry<>(), new CompositeRegistryEventConsumer<>(emptyList()), new CompositeCustomizer<>(Collections.emptyList()))).isNotNull();
}
Also used : CircuitBreakerConfigurationProperties(io.github.resilience4j.circuitbreaker.configure.CircuitBreakerConfigurationProperties) Test(org.junit.Test)

Example 5 with CircuitBreakerConfigurationProperties

use of io.github.resilience4j.circuitbreaker.configure.CircuitBreakerConfigurationProperties in project resilience4j by resilience4j.

the class CircuitBreakersHealthIndicatorTest method healthIndicatorMaxImpactCanBeOverridden.

@Test
public void healthIndicatorMaxImpactCanBeOverridden() {
    CircuitBreaker openCircuitBreaker = mock(CircuitBreaker.class);
    CircuitBreaker halfOpenCircuitBreaker = mock(CircuitBreaker.class);
    CircuitBreaker closeCircuitBreaker = mock(CircuitBreaker.class);
    Map<CircuitBreaker.State, CircuitBreaker> expectedStateToCircuitBreaker = new HashMap<>();
    expectedStateToCircuitBreaker.put(OPEN, openCircuitBreaker);
    expectedStateToCircuitBreaker.put(HALF_OPEN, halfOpenCircuitBreaker);
    expectedStateToCircuitBreaker.put(CLOSED, closeCircuitBreaker);
    CircuitBreakerConfigurationProperties.InstanceProperties instanceProperties = mock(CircuitBreakerConfigurationProperties.InstanceProperties.class);
    CircuitBreakerConfigurationProperties circuitBreakerProperties = mock(CircuitBreakerConfigurationProperties.class);
    // given
    CircuitBreakerRegistry registry = mock(CircuitBreakerRegistry.class);
    CircuitBreakerConfig config = mock(CircuitBreakerConfig.class);
    CircuitBreaker.Metrics metrics = mock(CircuitBreaker.Metrics.class);
    // when
    when(registry.getAllCircuitBreakers()).thenReturn(new HashSet<>(expectedStateToCircuitBreaker.values()));
    // do not allow health indicator to fail
    boolean allowHealthIndicatorToFail = false;
    expectedStateToCircuitBreaker.forEach((state, circuitBreaker) -> setCircuitBreakerWhen(state, circuitBreaker, config, metrics, instanceProperties, circuitBreakerProperties, allowHealthIndicatorToFail));
    CircuitBreakersHealthIndicator healthIndicator = new CircuitBreakersHealthIndicator(registry, circuitBreakerProperties, new SimpleStatusAggregator());
    // then
    Health health = healthIndicator.health();
    then(health.getStatus()).isEqualTo(Status.UP);
    then(health.getDetails()).containsKeys(OPEN.name(), HALF_OPEN.name(), CLOSED.name());
    assertState(OPEN, new Status("CIRCUIT_OPEN"), health.getDetails());
    assertState(HALF_OPEN, new Status("CIRCUIT_HALF_OPEN"), health.getDetails());
    assertState(CLOSED, Status.UP, health.getDetails());
}
Also used : CircuitBreakerConfigurationProperties(io.github.resilience4j.circuitbreaker.configure.CircuitBreakerConfigurationProperties) Status(org.springframework.boot.actuate.health.Status) CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) SimpleStatusAggregator(org.springframework.boot.actuate.health.SimpleStatusAggregator) Health(org.springframework.boot.actuate.health.Health) State(io.github.resilience4j.circuitbreaker.CircuitBreaker.State) CircuitBreakerRegistry(io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry) CircuitBreakerConfig(io.github.resilience4j.circuitbreaker.CircuitBreakerConfig) Test(org.junit.Test)

Aggregations

CircuitBreakerConfigurationProperties (io.github.resilience4j.circuitbreaker.configure.CircuitBreakerConfigurationProperties)5 Test (org.junit.Test)5 CircuitBreaker (io.github.resilience4j.circuitbreaker.CircuitBreaker)3 CircuitBreakerConfig (io.github.resilience4j.circuitbreaker.CircuitBreakerConfig)3 CircuitBreakerRegistry (io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry)3 Health (org.springframework.boot.actuate.health.Health)3 SimpleStatusAggregator (org.springframework.boot.actuate.health.SimpleStatusAggregator)3 State (io.github.resilience4j.circuitbreaker.CircuitBreaker.State)2 Status (org.springframework.boot.actuate.health.Status)2 CompletionStageFallbackDecorator (io.github.resilience4j.fallback.CompletionStageFallbackDecorator)1 FallbackDecorators (io.github.resilience4j.fallback.FallbackDecorators)1 FallbackExecutor (io.github.resilience4j.fallback.FallbackExecutor)1 DefaultSpelResolver (io.github.resilience4j.spelresolver.DefaultSpelResolver)1 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)1 StandardReflectionParameterNameDiscoverer (org.springframework.core.StandardReflectionParameterNameDiscoverer)1 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)1