Search in sources :

Example 1 with State

use of io.github.resilience4j.circuitbreaker.CircuitBreaker.State 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 2 with State

use of io.github.resilience4j.circuitbreaker.CircuitBreaker.State 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

CircuitBreaker (io.github.resilience4j.circuitbreaker.CircuitBreaker)2 State (io.github.resilience4j.circuitbreaker.CircuitBreaker.State)2 CircuitBreakerConfig (io.github.resilience4j.circuitbreaker.CircuitBreakerConfig)2 CircuitBreakerRegistry (io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry)2 CircuitBreakerConfigurationProperties (io.github.resilience4j.circuitbreaker.configure.CircuitBreakerConfigurationProperties)2 Test (org.junit.Test)2 Health (org.springframework.boot.actuate.health.Health)2 SimpleStatusAggregator (org.springframework.boot.actuate.health.SimpleStatusAggregator)2 Status (org.springframework.boot.actuate.health.Status)2