use of io.github.resilience4j.core.EventConsumer in project resilience4j by resilience4j.
the class CircuitBreakerAutoTransitionStateMachineTest method setUp.
@Before
public void setUp() {
CircuitBreakerConfig circuitBreakerConfigGroupA = CircuitBreakerConfig.custom().failureRateThreshold(50).ringBufferSizeInClosedState(5).ringBufferSizeInHalfOpenState(3).enableAutomaticTransitionFromOpenToHalfOpen().waitDurationInOpenState(Duration.ofSeconds(2)).recordFailure(error -> !(error instanceof NumberFormatException)).build();
CircuitBreakerConfig circuitBreakerConfigGroupB = CircuitBreakerConfig.custom().failureRateThreshold(50).ringBufferSizeInClosedState(5).ringBufferSizeInHalfOpenState(3).enableAutomaticTransitionFromOpenToHalfOpen().waitDurationInOpenState(Duration.ofSeconds(1)).recordFailure(error -> !(error instanceof NumberFormatException)).build();
// Instantiate multiple circuit breakers in two groups, A & B
for (int i = 0; i < TOTAL_NUMBER_CIRCUIT_BREAKERS; i++) {
stateTransitionFromOpenToHalfOpen.put(i, 0);
// On state transition from OPEN to HALF_OPEN, increment a count
int finalI = i;
EventConsumer<CircuitBreakerOnStateTransitionEvent> eventConsumer = transition -> {
if (transition.getStateTransition().getFromState().equals(CircuitBreaker.State.OPEN) && transition.getStateTransition().getToState().equals(CircuitBreaker.State.HALF_OPEN)) {
Integer currentCount = stateTransitionFromOpenToHalfOpen.get(finalI);
stateTransitionFromOpenToHalfOpen.put(finalI, currentCount + 1);
}
};
CircuitBreaker circuitBreaker;
if (i < TOTAL_NUMBER_CIRCUIT_BREAKERS / 2) {
circuitBreaker = new CircuitBreakerStateMachine("testNameA" + i, circuitBreakerConfigGroupA);
circuitBreaker.getEventPublisher().onStateTransition(eventConsumer);
circuitBreakersGroupA.add(circuitBreaker);
} else {
circuitBreaker = new CircuitBreakerStateMachine("testNameB" + i, circuitBreakerConfigGroupB);
circuitBreaker.getEventPublisher().onStateTransition(eventConsumer);
circuitBreakersGroupB.add(circuitBreaker);
}
}
}
Aggregations