Search in sources :

Example 1 with BulkheadRegistry

use of io.github.resilience4j.bulkhead.BulkheadRegistry in project resilience4j by resilience4j.

the class BulkheadMetricsTest method shouldUseCustomPrefix.

@Test
public void shouldUseCustomPrefix() throws Throwable {
    // Given
    BulkheadRegistry bulkheadRegistry = BulkheadRegistry.ofDefaults();
    Bulkhead bulkhead = bulkheadRegistry.bulkhead("testBulkhead");
    metricRegistry.registerAll(BulkheadMetrics.ofIterable("testPre", bulkheadRegistry.getAllBulkheads()));
    // Given latch to verify bulkhead
    CountDownLatch countDownLatch = new CountDownLatch(1);
    // Given the HelloWorldService returns Hello world
    BDDMockito.given(helloWorldService.returnHelloWorld()).will(invocation -> {
        if (countDownLatch.await(10, TimeUnit.SECONDS)) {
            return "Hello world";
        } else {
            throw new IllegalStateException("Timeout - test failure");
        }
    });
    // When
    Future<String> future = executorService.submit(() -> bulkhead.executeSupplier(helloWorldService::returnHelloWorld));
    // Then metrics are present and show value
    assertThat(metricRegistry.getMetrics()).hasSize(1);
    assertThat(metricRegistry.getGauges().get("testPre.testBulkhead.available_concurrent_calls").getValue()).isIn(DEFAULT_MAX_CONCURRENT_CALLS, DEFAULT_MAX_CONCURRENT_CALLS - 1);
    // Then release latch and verify result
    countDownLatch.countDown();
    assertThat(future.get(10, TimeUnit.SECONDS)).isEqualTo("Hello world");
    BDDMockito.then(helloWorldService).should(times(1)).returnHelloWorld();
    // Then check metrics again
    assertThat(metricRegistry.getMetrics()).hasSize(1);
    assertThat(metricRegistry.getGauges().get("testPre.testBulkhead.available_concurrent_calls").getValue()).isIn(DEFAULT_MAX_CONCURRENT_CALLS, DEFAULT_MAX_CONCURRENT_CALLS);
}
Also used : Bulkhead(io.github.resilience4j.bulkhead.Bulkhead) BulkheadRegistry(io.github.resilience4j.bulkhead.BulkheadRegistry) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with BulkheadRegistry

use of io.github.resilience4j.bulkhead.BulkheadRegistry in project resilience4j by resilience4j.

the class BulkheadMetricsTest method shouldRegisterMetrics.

@Test
public void shouldRegisterMetrics() throws Throwable {
    // Given
    BulkheadRegistry bulkheadRegistry = BulkheadRegistry.ofDefaults();
    Bulkhead bulkhead = bulkheadRegistry.bulkhead("testBulkhead");
    metricRegistry.registerAll(BulkheadMetrics.ofBulkhead(bulkhead));
    // Given latch to verify bulkhead
    CountDownLatch countDownLatch = new CountDownLatch(1);
    // Given the HelloWorldService returns Hello world
    BDDMockito.given(helloWorldService.returnHelloWorld()).will(invocation -> {
        if (countDownLatch.await(10, TimeUnit.SECONDS)) {
            return "Hello world";
        } else {
            throw new IllegalStateException("Timeout - test failure");
        }
    });
    // When
    Future<String> future = executorService.submit(() -> bulkhead.executeSupplier(helloWorldService::returnHelloWorld));
    // Then metrics are present and show value
    assertThat(metricRegistry.getMetrics()).hasSize(1);
    assertThat(metricRegistry.getGauges().get("resilience4j.bulkhead.testBulkhead.available_concurrent_calls").getValue()).isIn(DEFAULT_MAX_CONCURRENT_CALLS, DEFAULT_MAX_CONCURRENT_CALLS - 1);
    // Then release latch and verify result
    countDownLatch.countDown();
    assertThat(future.get(10, TimeUnit.SECONDS)).isEqualTo("Hello world");
    BDDMockito.then(helloWorldService).should(times(1)).returnHelloWorld();
    // Then check metrics again
    assertThat(metricRegistry.getMetrics()).hasSize(1);
    assertThat(metricRegistry.getGauges().get("resilience4j.bulkhead.testBulkhead.available_concurrent_calls").getValue()).isIn(DEFAULT_MAX_CONCURRENT_CALLS, DEFAULT_MAX_CONCURRENT_CALLS);
}
Also used : Bulkhead(io.github.resilience4j.bulkhead.Bulkhead) BulkheadRegistry(io.github.resilience4j.bulkhead.BulkheadRegistry) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 3 with BulkheadRegistry

use of io.github.resilience4j.bulkhead.BulkheadRegistry in project resilience4j by resilience4j.

the class BulkheadMetricsTest method shouldRegisterMetrics.

@Test
public void shouldRegisterMetrics() {
    BulkheadRegistry bulkheadRegistry = BulkheadRegistry.ofDefaults();
    bulkheadRegistry.bulkhead("testName");
    BulkheadMetrics bulkheadMetrics = BulkheadMetrics.ofBulkheadRegistry(bulkheadRegistry);
    bulkheadMetrics.bindTo(meterRegistry);
    final List<String> metricNames = meterRegistry.getMeters().stream().map(Meter::getId).map(Meter.Id::getName).collect(Collectors.toList());
    final List<String> expectedMetrics = newArrayList("resilience4j.bulkhead.testName.available_concurrent_calls");
    assertThat(metricNames).hasSameElementsAs(expectedMetrics);
}
Also used : Meter(io.micrometer.core.instrument.Meter) BulkheadRegistry(io.github.resilience4j.bulkhead.BulkheadRegistry) Test(org.junit.Test)

Aggregations

BulkheadRegistry (io.github.resilience4j.bulkhead.BulkheadRegistry)3 Test (org.junit.Test)3 Bulkhead (io.github.resilience4j.bulkhead.Bulkhead)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Meter (io.micrometer.core.instrument.Meter)1