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

The following examples show how to use io.github.resilience4j.circuitbreaker.CircuitBreaker#onSuccess() . 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: TaggedCircuitBreakerMetricsPublisherTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAddMetricsForANewlyCreatedCircuitBreaker() {
    CircuitBreaker newCircuitBreaker = circuitBreakerRegistry.circuitBreaker("backendB");
    newCircuitBreaker.onSuccess(0, TimeUnit.NANOSECONDS);

    assertThat(taggedCircuitBreakerMetricsPublisher.meterIdMap)
        .containsKeys("backendA", "backendB");
    assertThat(taggedCircuitBreakerMetricsPublisher.meterIdMap.get("backendA")).hasSize(16);
    assertThat(taggedCircuitBreakerMetricsPublisher.meterIdMap.get("backendB")).hasSize(16);

    List<Meter> meters = meterRegistry.getMeters();
    assertThat(meters).hasSize(32);

    Collection<Gauge> gauges = meterRegistry.get(DEFAULT_CIRCUIT_BREAKER_BUFFERED_CALLS)
        .gauges();

    Optional<Gauge> successful = findMeterByKindAndNameTags(gauges, "successful",
        newCircuitBreaker.getName());
    assertThat(successful).isPresent();
    assertThat(successful.get().value())
        .isEqualTo(newCircuitBreaker.getMetrics().getNumberOfSuccessfulCalls());
}
 
Example 2
Source File: TaggedCircuitBreakerMetricsTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAddMetricsForANewlyCreatedCircuitBreaker() {
    CircuitBreaker newCircuitBreaker = circuitBreakerRegistry.circuitBreaker("backendB");
    newCircuitBreaker.onSuccess(0, TimeUnit.NANOSECONDS);

    assertThat(taggedCircuitBreakerMetrics.meterIdMap).containsKeys("backendA", "backendB");
    assertThat(taggedCircuitBreakerMetrics.meterIdMap.get("backendA")).hasSize(16);
    assertThat(taggedCircuitBreakerMetrics.meterIdMap.get("backendB")).hasSize(16);

    List<Meter> meters = meterRegistry.getMeters();
    assertThat(meters).hasSize(32);

    Collection<Gauge> gauges = meterRegistry.get(DEFAULT_CIRCUIT_BREAKER_BUFFERED_CALLS)
        .gauges();

    Optional<Gauge> successful = MetricsTestHelper
        .findMeterByKindAndNameTags(gauges, "successful",
        newCircuitBreaker.getName());
    assertThat(successful).isPresent();
    assertThat(successful.get().value())
        .isEqualTo(newCircuitBreaker.getMetrics().getNumberOfSuccessfulCalls());
}
 
Example 3
Source File: CircuitBreakerMetricsPublisherTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void customMetricNameBucketsOverrideDefaultOnes() {
    CollectorRegistry registry = new CollectorRegistry();

    CircuitBreakerMetricsPublisher circuitBreakerMetricsPublisher = new CircuitBreakerMetricsPublisher(
        MetricNames.ofDefaults(),
        MetricOptions.custom().buckets(new double[]{.005, .01}).build());

    circuitBreakerMetricsPublisher.register(registry);
    CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry
        .of(CircuitBreakerConfig.ofDefaults(), circuitBreakerMetricsPublisher);
    CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker("backendA");

    circuitBreaker.onSuccess(2000, TimeUnit.NANOSECONDS);

    assertThat(registry.getSampleValue(
        DEFAULT_CIRCUIT_BREAKER_CALLS + "_bucket",
        new String[]{"name", "kind", "le"},
        new String[]{circuitBreaker.getName(), "successful", "0.01"}
    )).isEqualTo(1d);

    assertThat(registry.getSampleValue(
        DEFAULT_CIRCUIT_BREAKER_CALLS + "_bucket",
        new String[]{"name", "kind", "le"},
        new String[]{circuitBreaker.getName(), "successful", "0.025"}
    )).isNull();
}
 
Example 4
Source File: TaggedCircuitBreakerMetricsPublisherTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testReplaceNewMeter(){
    CircuitBreaker oldOne = CircuitBreaker.of("backendC", CircuitBreakerConfig.ofDefaults());
    // add meters of old
    taggedCircuitBreakerMetricsPublisher.addMetrics(meterRegistry, oldOne);
    // one success call
    oldOne.onSuccess(0, TimeUnit.NANOSECONDS);

    assertThat(taggedCircuitBreakerMetricsPublisher.meterIdMap).containsKeys("backendC");
    assertThat(taggedCircuitBreakerMetricsPublisher.meterIdMap.get("backendC")).hasSize(16);
    Collection<Gauge> gauges = meterRegistry.get(DEFAULT_CIRCUIT_BREAKER_BUFFERED_CALLS).gauges();
    Optional<Gauge> successful = findMeterByKindAndNameTags(gauges, "successful", oldOne.getName());
    assertThat(successful).isPresent();
    assertThat(successful.get().value())
        .isEqualTo(oldOne.getMetrics().getNumberOfSuccessfulCalls());

    CircuitBreaker newOne = CircuitBreaker.of("backendC", CircuitBreakerConfig.ofDefaults());

    // add meters of new
    taggedCircuitBreakerMetricsPublisher.addMetrics(meterRegistry, newOne);
    // three success call
    newOne.onSuccess(0, TimeUnit.NANOSECONDS);
    newOne.onSuccess(0, TimeUnit.NANOSECONDS);
    newOne.onSuccess(0, TimeUnit.NANOSECONDS);

    assertThat(taggedCircuitBreakerMetricsPublisher.meterIdMap).containsKeys("backendC");
    assertThat(taggedCircuitBreakerMetricsPublisher.meterIdMap.get("backendC")).hasSize(16);
    gauges = meterRegistry.get(DEFAULT_CIRCUIT_BREAKER_BUFFERED_CALLS).gauges();
    successful = findMeterByKindAndNameTags(gauges, "successful", newOne.getName());
    assertThat(successful).isPresent();
    assertThat(successful.get().value())
        .isEqualTo(newOne.getMetrics().getNumberOfSuccessfulCalls());

}
 
Example 5
Source File: TaggedCircuitBreakerMetricsTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldAddCustomTags() {
    CircuitBreaker circuitBreakerF = circuitBreakerRegistry.circuitBreaker("backendF", io.vavr.collection.HashMap.of("key1", "value1"));
    circuitBreakerF.onSuccess(0, TimeUnit.NANOSECONDS);
    assertThat(taggedCircuitBreakerMetrics.meterIdMap).containsKeys("backendA", "backendF");
    assertThat(taggedCircuitBreakerMetrics.meterIdMap.get("backendF")).hasSize(16);
    List<Meter> meters = meterRegistry.getMeters();
    assertThat(meters).hasSize(32);
    final RequiredSearch match = meterRegistry.get(DEFAULT_CIRCUIT_BREAKER_BUFFERED_CALLS).tags("key1", "value1");
    assertThat(match).isNotNull();
}
 
Example 6
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 7
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));
}