Search in sources :

Example 11 with RateLimiterRegistry

use of io.github.resilience4j.ratelimiter.RateLimiterRegistry in project resilience4j by resilience4j.

the class RateLimiterMetricsTest method shouldUseCustomPrefix.

@Test
public void shouldUseCustomPrefix() throws Throwable {
    // Given
    RateLimiterRegistry rateLimiterRegistry = RateLimiterRegistry.ofDefaults();
    RateLimiter rateLimiter = rateLimiterRegistry.rateLimiter("testLimit");
    metricRegistry.registerAll(RateLimiterMetrics.ofIterable("testPre", rateLimiterRegistry.getAllRateLimiters()));
    // Given the HelloWorldService returns Hello world
    BDDMockito.given(helloWorldService.returnHelloWorld()).willReturn("Hello world");
    // When
    String value = rateLimiter.executeSupplier(helloWorldService::returnHelloWorld);
    // Then
    assertThat(value).isEqualTo("Hello world");
    // Then the helloWorldService should be invoked 1 time
    BDDMockito.then(helloWorldService).should(times(1)).returnHelloWorld();
    assertThat(metricRegistry.getMetrics()).hasSize(2);
    assertThat(metricRegistry.getGauges().get("testPre.testLimit.number_of_waiting_threads").getValue()).isEqualTo(0);
    assertThat(metricRegistry.getGauges().get("testPre.testLimit.available_permissions").getValue()).isIn(DEFAULT_LIMIT_FOR_PERIOD, DEFAULT_LIMIT_FOR_PERIOD - 1);
}
Also used : RateLimiterRegistry(io.github.resilience4j.ratelimiter.RateLimiterRegistry) RateLimiter(io.github.resilience4j.ratelimiter.RateLimiter) Test(org.junit.Test)

Example 12 with RateLimiterRegistry

use of io.github.resilience4j.ratelimiter.RateLimiterRegistry in project resilience4j by resilience4j.

the class InMemoryRateLimiterRegistryTest method rateLimiterNewWithNullNameAndConfigSupplier.

@Test
public void rateLimiterNewWithNullNameAndConfigSupplier() throws Exception {
    exception.expect(NullPointerException.class);
    exception.expectMessage(NAME_MUST_NOT_BE_NULL);
    RateLimiterRegistry registry = new InMemoryRateLimiterRegistry(config);
    registry.rateLimiter(null, () -> config);
}
Also used : RateLimiterRegistry(io.github.resilience4j.ratelimiter.RateLimiterRegistry) Test(org.junit.Test)

Example 13 with RateLimiterRegistry

use of io.github.resilience4j.ratelimiter.RateLimiterRegistry in project resilience4j by resilience4j.

the class InMemoryRateLimiterRegistryTest method rateLimiterNewWithNullNameAndNonDefaultConfig.

@Test
public void rateLimiterNewWithNullNameAndNonDefaultConfig() throws Exception {
    exception.expect(NullPointerException.class);
    exception.expectMessage(NAME_MUST_NOT_BE_NULL);
    RateLimiterRegistry registry = new InMemoryRateLimiterRegistry(config);
    registry.rateLimiter(null, config);
}
Also used : RateLimiterRegistry(io.github.resilience4j.ratelimiter.RateLimiterRegistry) Test(org.junit.Test)

Aggregations

RateLimiterRegistry (io.github.resilience4j.ratelimiter.RateLimiterRegistry)13 Test (org.junit.Test)12 RateLimiter (io.github.resilience4j.ratelimiter.RateLimiter)5 RateLimiterConfig (io.github.resilience4j.ratelimiter.RateLimiterConfig)3 InMemoryRateLimiterRegistry (io.github.resilience4j.ratelimiter.internal.InMemoryRateLimiterRegistry)2 Meter (io.micrometer.core.instrument.Meter)1 Bean (org.springframework.context.annotation.Bean)1