Java Code Examples for io.github.resilience4j.circuitbreaker.CircuitBreaker#onError()

The following examples show how to use io.github.resilience4j.circuitbreaker.CircuitBreaker#onError() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CircularEventConsumerTest.java    From resilience4j with Apache License 2.0 7 votes vote down vote up
@Test
public void shouldNotBufferEvents() {
    CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("testName");
    CircularEventConsumer<CircuitBreakerEvent> ringBuffer = new CircularEventConsumer<>(2);
    assertThat(ringBuffer.getBufferedEvents()).isEmpty();

    circuitBreaker.onError(0, TimeUnit.NANOSECONDS, new RuntimeException("Bla"));
    circuitBreaker.onError(0, TimeUnit.NANOSECONDS, new RuntimeException("Bla"));
    circuitBreaker.onError(0, TimeUnit.NANOSECONDS, new RuntimeException("Bla"));
    //Subscription is too late
    circuitBreaker.getEventPublisher().onEvent(ringBuffer);

    CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
    assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(3);
    assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(3);
    //Because Subscription was too late
    assertThat(ringBuffer.getBufferedEvents()).hasSize(0);
}
 
Example 2
Source File: CircuitBreakerExceptionHandlingTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldRecordRuntimeExceptionAsFailureAndBusinessExceptionAsSuccess() {
    CircuitBreaker circuitBreaker = new CircuitBreakerStateMachine("testName", custom()
        .slidingWindowSize(5)
        .recordException(ex -> !(ex instanceof BusinessException))
        .build());

    assertThat(circuitBreaker.tryAcquirePermission()).isEqualTo(true);
    circuitBreaker.onError(0, TimeUnit.NANOSECONDS, new RuntimeException());

    // Call 2 is a failure
    assertThat(circuitBreaker.tryAcquirePermission()).isEqualTo(true);
    circuitBreaker.onError(0, TimeUnit.NANOSECONDS, new BusinessException("test"));

    assertThat(circuitBreaker.getMetrics().getNumberOfFailedCalls()).isEqualTo(1);
    assertThat(circuitBreaker.getMetrics().getNumberOfSuccessfulCalls()).isEqualTo(1);
    assertThat(circuitBreaker.getMetrics().getNumberOfBufferedCalls()).isEqualTo(2);
}
 
Example 3
Source File: CircuitBreakerExceptionHandlingTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldRecordIOExceptionAsFailureAndBusinessExceptionAsSuccess() {
    CircuitBreaker circuitBreaker = new CircuitBreakerStateMachine("testName", custom()
        .slidingWindowSize(5)
        .recordExceptions(IOException.class)
        .build());

    assertThat(circuitBreaker.tryAcquirePermission()).isEqualTo(true);
    circuitBreaker.onError(0, TimeUnit.NANOSECONDS, new IOException());

    // Call 2 is a failure
    assertThat(circuitBreaker.tryAcquirePermission()).isEqualTo(true);
    circuitBreaker.onError(0, TimeUnit.NANOSECONDS, new BusinessException("test"));

    assertThat(circuitBreaker.getMetrics().getNumberOfFailedCalls()).isEqualTo(1);
    assertThat(circuitBreaker.getMetrics().getNumberOfSuccessfulCalls()).isEqualTo(1);
    assertThat(circuitBreaker.getMetrics().getNumberOfBufferedCalls()).isEqualTo(2);
}
 
Example 4
Source File: CircuitBreakerExceptionHandlingTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldRecordBusinessExceptionAsFailure() {
    CircuitBreaker circuitBreaker = new CircuitBreakerStateMachine("testName", custom()
        .slidingWindowSize(5)
        .recordException(ex -> "record".equals(ex.getMessage()))
        .build());

    assertThat(circuitBreaker.tryAcquirePermission()).isEqualTo(true);
    circuitBreaker.onError(0, TimeUnit.NANOSECONDS, new BusinessException("record"));

    // Call 2 is a failure
    assertThat(circuitBreaker.tryAcquirePermission()).isEqualTo(true);
    circuitBreaker.onError(0, TimeUnit.NANOSECONDS, new BusinessException("bla"));

    assertThat(circuitBreaker.getMetrics().getNumberOfFailedCalls()).isEqualTo(1);
    assertThat(circuitBreaker.getMetrics().getNumberOfSuccessfulCalls()).isEqualTo(1);
    assertThat(circuitBreaker.getMetrics().getNumberOfBufferedCalls()).isEqualTo(2);
}
 
Example 5
Source File: CircuitBreakerExceptionHandlingTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldIgnoreNumberFormatException() {
    CircuitBreaker circuitBreaker = new CircuitBreakerStateMachine("testName", custom()
        .failureRateThreshold(50)
        .slidingWindowSize(5)
        .waitDurationInOpenState(Duration.ofSeconds(5))
        .ignoreExceptions(NumberFormatException.class)
        .build());

    assertThat(circuitBreaker.tryAcquirePermission()).isEqualTo(true);
    circuitBreaker.onError(0, TimeUnit.NANOSECONDS, new RuntimeException());

    // Call 2 is a failure
    assertThat(circuitBreaker.tryAcquirePermission()).isEqualTo(true);
    circuitBreaker.onError(0, TimeUnit.NANOSECONDS, new NumberFormatException());

    assertThat(circuitBreaker.getMetrics().getNumberOfFailedCalls()).isEqualTo(1);
    assertThat(circuitBreaker.getMetrics().getNumberOfSuccessfulCalls()).isEqualTo(0);
    assertThat(circuitBreaker.getMetrics().getNumberOfBufferedCalls()).isEqualTo(1);
}
 
Example 6
Source File: CircularEventConsumerTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldBufferErrorEvents() {
    // tag::shouldBufferEvents[]
    CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("testName");
    CircularEventConsumer<CircuitBreakerEvent> ringBuffer = new CircularEventConsumer<>(2);
    circuitBreaker.getEventPublisher().onEvent(ringBuffer);
    // end::shouldBufferEvents[]
    assertThat(ringBuffer.getBufferedEvents()).isEmpty();

    circuitBreaker.onError(0, TimeUnit.NANOSECONDS, new RuntimeException("Bla"));
    circuitBreaker.onError(0, TimeUnit.NANOSECONDS, new RuntimeException("Bla"));
    circuitBreaker.onError(0, TimeUnit.NANOSECONDS, new RuntimeException("Bla"));

    CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
    assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(3);
    assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(3);
    //Because capacity is 2
    assertThat(ringBuffer.getBufferedEvents()).hasSize(2);
}
 
Example 7
Source File: CircularEventConsumerTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldBufferAllEvents() {
    CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom()
        .slidingWindowSize(3)
        .ignoreExceptions(IOException.class)
        .build();
    CircuitBreaker circuitBreaker = CircuitBreaker.of("testName", circuitBreakerConfig);
    CircularEventConsumer<CircuitBreakerEvent> ringBuffer = new CircularEventConsumer<>(10);
    circuitBreaker.getEventPublisher().onEvent(ringBuffer);
    assertThat(ringBuffer.getBufferedEvents()).isEmpty();

    circuitBreaker.onSuccess(0, TimeUnit.NANOSECONDS);
    circuitBreaker.onError(0, TimeUnit.NANOSECONDS, new RuntimeException("Bla"));
    circuitBreaker.onError(0, TimeUnit.NANOSECONDS, new IOException("Bla"));
    circuitBreaker.onError(0, TimeUnit.NANOSECONDS, new RuntimeException("Bla"));

    CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
    assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(3);
    assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(2);
    circuitBreaker.reset();
    CircuitBreaker.Metrics resetMetrics = circuitBreaker.getMetrics();
    assertThat(resetMetrics.getNumberOfBufferedCalls()).isEqualTo(0);
    assertThat(resetMetrics.getNumberOfFailedCalls()).isEqualTo(0);
    //Because circuit emits 2 error events and one state transition event
    assertThat(ringBuffer.getBufferedEvents()).hasSize(8);
    assertThat(ringBuffer.getBufferedEvents()).extracting("eventType")
        .containsExactly(Type.SUCCESS, Type.ERROR, Type.IGNORED_ERROR, Type.ERROR,
            Type.FAILURE_RATE_EXCEEDED, Type.STATE_TRANSITION, Type.STATE_TRANSITION,
            Type.RESET);
}
 
Example 8
Source File: StateTransitionMetricsTest.java    From resilience4j with Apache License 2.0 4 votes vote down vote up
private static void circuitBreakerMetricsUsesFirstStateObjectInstance(
    CircuitBreaker circuitBreaker, MetricRegistry metricRegistry) throws Exception {
    SortedMap<String, Gauge> gauges = metricRegistry.getGauges();

    assertThat(circuitBreaker.getState(), equalTo(CircuitBreaker.State.CLOSED));
    assertThat(circuitBreaker.getMetrics().getNumberOfBufferedCalls(), equalTo(0));
    assertThat(circuitBreaker.getMetrics().getNumberOfFailedCalls(), equalTo(0));
    assertThat(circuitBreaker.getMetrics().getNumberOfSuccessfulCalls(), equalTo(0));
    assertThat(gauges.get("resilience4j.circuitbreaker.test.state").getValue(), equalTo(0));
    assertThat(gauges.get("resilience4j.circuitbreaker.test.buffered").getValue(), equalTo(0));
    assertThat(gauges.get("resilience4j.circuitbreaker.test.failed").getValue(), equalTo(0));
    assertThat(gauges.get("resilience4j.circuitbreaker.test.successful").getValue(),
        equalTo(0));

    circuitBreaker.onError(0, TimeUnit.NANOSECONDS, new RuntimeException());

    assertThat(circuitBreaker.getState(), equalTo(CircuitBreaker.State.CLOSED));
    assertThat(circuitBreaker.getMetrics().getNumberOfBufferedCalls(), equalTo(1));
    assertThat(circuitBreaker.getMetrics().getNumberOfFailedCalls(), equalTo(1));
    assertThat(circuitBreaker.getMetrics().getNumberOfSuccessfulCalls(), equalTo(0));
    assertThat(gauges.get("resilience4j.circuitbreaker.test.state").getValue(), equalTo(0));
    assertThat(gauges.get("resilience4j.circuitbreaker.test.buffered").getValue(), equalTo(1));
    assertThat(gauges.get("resilience4j.circuitbreaker.test.failed").getValue(), equalTo(1));
    assertThat(gauges.get("resilience4j.circuitbreaker.test.successful").getValue(),
        equalTo(0));

    for (int i = 0; i < 9; i++) {
        circuitBreaker.onError(0, TimeUnit.NANOSECONDS, new RuntimeException());
    }

    assertThat(circuitBreaker.getState(), equalTo(CircuitBreaker.State.OPEN));
    assertThat(circuitBreaker.getMetrics().getNumberOfBufferedCalls(), equalTo(10));
    assertThat(circuitBreaker.getMetrics().getNumberOfFailedCalls(), equalTo(10));
    assertThat(circuitBreaker.getMetrics().getNumberOfSuccessfulCalls(), equalTo(0));
    assertThat(gauges.get("resilience4j.circuitbreaker.test.state").getValue(), equalTo(1));
    assertThat(gauges.get("resilience4j.circuitbreaker.test.buffered").getValue(), equalTo(10));
    assertThat(gauges.get("resilience4j.circuitbreaker.test.failed").getValue(), equalTo(10));
    assertThat(gauges.get("resilience4j.circuitbreaker.test.successful").getValue(),
        equalTo(0));

    await().atMost(1500, TimeUnit.MILLISECONDS)
        .until(() -> {
            circuitBreaker.tryAcquirePermission();
            return circuitBreaker.getState().equals(CircuitBreaker.State.HALF_OPEN);
        });

    circuitBreaker.onSuccess(0, TimeUnit.NANOSECONDS);
    assertThat(circuitBreaker.getState(), equalTo(CircuitBreaker.State.HALF_OPEN));
    assertThat(circuitBreaker.getMetrics().getNumberOfBufferedCalls(), equalTo(1));
    assertThat(circuitBreaker.getMetrics().getNumberOfFailedCalls(), equalTo(0));
    assertThat(circuitBreaker.getMetrics().getNumberOfSuccessfulCalls(), equalTo(1));

    assertThat(gauges.get("resilience4j.circuitbreaker.test.state").getValue(), equalTo(2));
    assertThat(gauges.get("resilience4j.circuitbreaker.test.buffered").getValue(), equalTo(1));
    assertThat(gauges.get("resilience4j.circuitbreaker.test.failed").getValue(), equalTo(0));
    assertThat(gauges.get("resilience4j.circuitbreaker.test.successful").getValue(),
        equalTo(1));
    circuitBreaker.onSuccess(0, TimeUnit.NANOSECONDS);
    assertThat(circuitBreaker.getState(), equalTo(CircuitBreaker.State.HALF_OPEN));
    assertThat(circuitBreaker.getMetrics().getNumberOfBufferedCalls(), equalTo(2));
    assertThat(circuitBreaker.getMetrics().getNumberOfFailedCalls(), equalTo(0));
    assertThat(circuitBreaker.getMetrics().getNumberOfSuccessfulCalls(), equalTo(2));

    assertThat(gauges.get("resilience4j.circuitbreaker.test.state").getValue(), equalTo(2));
    assertThat(gauges.get("resilience4j.circuitbreaker.test.buffered").getValue(), equalTo(2));
    assertThat(gauges.get("resilience4j.circuitbreaker.test.failed").getValue(), equalTo(0));
    assertThat(gauges.get("resilience4j.circuitbreaker.test.successful").getValue(),
        equalTo(2));
    circuitBreaker.onSuccess(0, TimeUnit.NANOSECONDS);
    assertThat(circuitBreaker.getState(), equalTo(CircuitBreaker.State.CLOSED));
    assertThat(circuitBreaker.getMetrics().getNumberOfBufferedCalls(), equalTo(0));
    assertThat(circuitBreaker.getMetrics().getNumberOfFailedCalls(), equalTo(0));
    assertThat(circuitBreaker.getMetrics().getNumberOfSuccessfulCalls(), equalTo(0));

    assertThat(gauges.get("resilience4j.circuitbreaker.test.state").getValue(), equalTo(0));
    assertThat(gauges.get("resilience4j.circuitbreaker.test.buffered").getValue(), equalTo(0));
    assertThat(gauges.get("resilience4j.circuitbreaker.test.failed").getValue(), equalTo(0));
    assertThat(gauges.get("resilience4j.circuitbreaker.test.successful").getValue(),
        equalTo(0));
}