use of io.github.resilience4j.ratelimiter.monitoring.model.RateLimiterEventsEndpointResponse 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);
}
Aggregations