Search in sources :

Example 1 with RateLimiterEndpointResponse

use of io.github.resilience4j.ratelimiter.monitoring.model.RateLimiterEndpointResponse in project resilience4j by resilience4j.

the class RateLimiterAutoConfigurationTest method testRateLimiterAutoConfiguration.

/**
 * The test verifies that a RateLimiter instance is created and configured properly when the DummyService is invoked and
 * that the RateLimiter records successful and failed calls.
 */
@Test
public void testRateLimiterAutoConfiguration() throws IOException {
    assertThat(rateLimiterRegistry).isNotNull();
    assertThat(rateLimiterProperties).isNotNull();
    RateLimiter rateLimiter = rateLimiterRegistry.rateLimiter(DummyService.BACKEND);
    assertThat(rateLimiter).isNotNull();
    rateLimiter.getPermission(Duration.ZERO);
    await().atMost(2, TimeUnit.SECONDS).until(() -> rateLimiter.getMetrics().getAvailablePermissions() == 10);
    try {
        dummyService.doSomething(true);
    } catch (IOException ex) {
    // Do nothing.
    }
    dummyService.doSomething(false);
    assertThat(rateLimiter.getMetrics().getAvailablePermissions()).isEqualTo(8);
    assertThat(rateLimiter.getMetrics().getNumberOfWaitingThreads()).isEqualTo(0);
    assertThat(rateLimiter.getRateLimiterConfig().getLimitForPeriod()).isEqualTo(10);
    assertThat(rateLimiter.getRateLimiterConfig().getLimitRefreshPeriod()).isEqualTo(Duration.ofSeconds(1));
    assertThat(rateLimiter.getRateLimiterConfig().getTimeoutDuration()).isEqualTo(Duration.ofSeconds(0));
    // Test Actuator endpoints
    ResponseEntity<RateLimiterEndpointResponse> rateLimiterList = restTemplate.getForEntity("/actuator/ratelimiters", RateLimiterEndpointResponse.class);
    assertThat(rateLimiterList.getBody().getRateLimitersNames()).hasSize(2).containsExactly("backendA", "backendB");
    try {
        for (int i = 0; i < 11; i++) {
            dummyService.doSomething(false);
        }
    } catch (RequestNotPermitted e) {
    // Do nothing
    }
    ResponseEntity<RateLimiterEventsEndpointResponse> rateLimiterEventList = restTemplate.getForEntity("/actuator/ratelimiter-events", RateLimiterEventsEndpointResponse.class);
    List<RateLimiterEventDTO> eventsList = rateLimiterEventList.getBody().getEventsList();
    assertThat(eventsList).isNotEmpty();
    RateLimiterEventDTO lastEvent = eventsList.get(eventsList.size() - 1);
    assertThat(lastEvent.getRateLimiterEventType()).isEqualTo(RateLimiterEvent.Type.FAILED_ACQUIRE);
    await().atMost(2, TimeUnit.SECONDS).until(() -> rateLimiter.getMetrics().getAvailablePermissions() == 10);
    assertThat(rateLimiterAspect.getOrder()).isEqualTo(401);
}
Also used : IOException(java.io.IOException) RateLimiterEndpointResponse(io.github.resilience4j.ratelimiter.monitoring.model.RateLimiterEndpointResponse) RateLimiterEventsEndpointResponse(io.github.resilience4j.ratelimiter.monitoring.model.RateLimiterEventsEndpointResponse) RateLimiterEventDTO(io.github.resilience4j.ratelimiter.monitoring.model.RateLimiterEventDTO) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

RateLimiterEndpointResponse (io.github.resilience4j.ratelimiter.monitoring.model.RateLimiterEndpointResponse)1 RateLimiterEventDTO (io.github.resilience4j.ratelimiter.monitoring.model.RateLimiterEventDTO)1 RateLimiterEventsEndpointResponse (io.github.resilience4j.ratelimiter.monitoring.model.RateLimiterEventsEndpointResponse)1 IOException (java.io.IOException)1 Test (org.junit.Test)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1