Search in sources :

Example 1 with CircuitBreakerEventDTO

use of io.github.resilience4j.circuitbreaker.monitoring.endpoint.CircuitBreakerEventDTO in project resilience4j by resilience4j.

the class CircuitBreakerEventEmitterTest method testEmitter.

@Test
public void testEmitter() throws IOException {
    CircuitBreakerConfig config = CircuitBreakerConfig.custom().ringBufferSizeInClosedState(3).ringBufferSizeInHalfOpenState(2).failureRateThreshold(66).waitDurationInOpenState(Duration.ofSeconds(1)).recordFailure(e -> !(e instanceof IllegalArgumentException)).build();
    CircuitBreaker circuitBreaker = CircuitBreakerRegistry.ofDefaults().circuitBreaker("test", config);
    Runnable run = decorateRunnable(circuitBreaker, () -> System.out.println("."));
    Runnable fail = decorateRunnable(circuitBreaker, () -> {
        throw new ConcurrentModificationException();
    });
    Runnable ignore = decorateRunnable(circuitBreaker, () -> {
        throw new IllegalArgumentException();
    });
    SseEmitter sseEmitter = createSseEmitter(ReactorAdapter.toFlux(circuitBreaker.getEventPublisher()));
    TestHandler handler = new TestHandler();
    sseEmitter.initialize(handler);
    exec(run, 2);
    exec(ignore, 1);
    exec(fail, 3);
    circuitBreaker.reset();
    exec(run, 2);
    circuitBreaker.reset();
    sseEmitter.complete();
    assert handler.isCompleted;
    exec(run, 2);
    List<CircuitBreakerEvent.Type> events = handler.events.stream().map(CircuitBreakerEventDTO::getType).collect(toList());
    then(events).containsExactly(SUCCESS, SUCCESS, IGNORED_ERROR, ERROR, ERROR, STATE_TRANSITION, NOT_PERMITTED, STATE_TRANSITION, RESET, SUCCESS, SUCCESS, RESET);
}
Also used : CircuitBreakerEventEmitter.createSseEmitter(io.github.resilience4j.circuitbreaker.monitoring.endpoint.CircuitBreakerEventEmitter.createSseEmitter) CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) CircuitBreaker.decorateRunnable(io.github.resilience4j.circuitbreaker.CircuitBreaker.decorateRunnable) MediaType(org.springframework.http.MediaType) Test(org.junit.Test) IOException(java.io.IOException) BDDAssertions.then(org.assertj.core.api.BDDAssertions.then) CircuitBreakerRegistry(io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry) ArrayList(java.util.ArrayList) CircuitBreakerConfig(io.github.resilience4j.circuitbreaker.CircuitBreakerConfig) Try(io.vavr.control.Try) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) CircuitBreakerEventDTO(io.github.resilience4j.circuitbreaker.monitoring.endpoint.CircuitBreakerEventDTO) Duration(java.time.Duration) ReactorAdapter(io.github.resilience4j.adapter.ReactorAdapter) Type(io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent.Type) APPLICATION_JSON(org.springframework.http.MediaType.APPLICATION_JSON) ConcurrentModificationException(java.util.ConcurrentModificationException) CircuitBreakerEvent(io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent) CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) ConcurrentModificationException(java.util.ConcurrentModificationException) MediaType(org.springframework.http.MediaType) Type(io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent.Type) CircuitBreaker.decorateRunnable(io.github.resilience4j.circuitbreaker.CircuitBreaker.decorateRunnable) CircuitBreakerEventEmitter.createSseEmitter(io.github.resilience4j.circuitbreaker.monitoring.endpoint.CircuitBreakerEventEmitter.createSseEmitter) CircuitBreakerConfig(io.github.resilience4j.circuitbreaker.CircuitBreakerConfig) Test(org.junit.Test)

Aggregations

ReactorAdapter (io.github.resilience4j.adapter.ReactorAdapter)1 CircuitBreaker (io.github.resilience4j.circuitbreaker.CircuitBreaker)1 CircuitBreaker.decorateRunnable (io.github.resilience4j.circuitbreaker.CircuitBreaker.decorateRunnable)1 CircuitBreakerConfig (io.github.resilience4j.circuitbreaker.CircuitBreakerConfig)1 CircuitBreakerRegistry (io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry)1 CircuitBreakerEvent (io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent)1 Type (io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent.Type)1 CircuitBreakerEventDTO (io.github.resilience4j.circuitbreaker.monitoring.endpoint.CircuitBreakerEventDTO)1 CircuitBreakerEventEmitter.createSseEmitter (io.github.resilience4j.circuitbreaker.monitoring.endpoint.CircuitBreakerEventEmitter.createSseEmitter)1 Try (io.vavr.control.Try)1 IOException (java.io.IOException)1 Duration (java.time.Duration)1 ArrayList (java.util.ArrayList)1 ConcurrentModificationException (java.util.ConcurrentModificationException)1 List (java.util.List)1 Collectors.toList (java.util.stream.Collectors.toList)1 BDDAssertions.then (org.assertj.core.api.BDDAssertions.then)1 Test (org.junit.Test)1 MediaType (org.springframework.http.MediaType)1 APPLICATION_JSON (org.springframework.http.MediaType.APPLICATION_JSON)1