use of io.github.resilience4j.ratelimiter.RateLimiter in project resilience4j by resilience4j.
the class InMemoryRateLimiterRegistryTest method rateLimiterPositiveWithSupplier.
@Test
@SuppressWarnings("unchecked")
public void rateLimiterPositiveWithSupplier() throws Exception {
RateLimiterRegistry registry = new InMemoryRateLimiterRegistry(config);
Supplier<RateLimiterConfig> rateLimiterConfigSupplier = mock(Supplier.class);
when(rateLimiterConfigSupplier.get()).thenReturn(config);
RateLimiter firstRateLimiter = registry.rateLimiter("test", rateLimiterConfigSupplier);
verify(rateLimiterConfigSupplier, times(1)).get();
RateLimiter sameAsFirst = registry.rateLimiter("test", rateLimiterConfigSupplier);
verify(rateLimiterConfigSupplier, times(1)).get();
RateLimiter anotherLimit = registry.rateLimiter("test1", rateLimiterConfigSupplier);
verify(rateLimiterConfigSupplier, times(2)).get();
then(firstRateLimiter).isEqualTo(sameAsFirst);
then(firstRateLimiter).isNotEqualTo(anotherLimit);
}
use of io.github.resilience4j.ratelimiter.RateLimiter in project resilience4j by resilience4j.
the class InMemoryRateLimiterRegistryTest method rateLimiterPositive.
@Test
public void rateLimiterPositive() throws Exception {
RateLimiterRegistry registry = RateLimiterRegistry.of(config);
RateLimiter firstRateLimiter = registry.rateLimiter("test");
RateLimiter anotherLimit = registry.rateLimiter("test1");
RateLimiter sameAsFirst = registry.rateLimiter("test");
then(firstRateLimiter).isEqualTo(sameAsFirst);
then(firstRateLimiter).isNotEqualTo(anotherLimit);
}
use of io.github.resilience4j.ratelimiter.RateLimiter in project resilience4j by resilience4j.
the class SemaphoreBasedRateLimiterImplTest method changeLimitForPeriod.
@Test
public void changeLimitForPeriod() throws Exception {
ScheduledExecutorService scheduledExecutorService = mock(ScheduledExecutorService.class);
RateLimiter rateLimiter = new SemaphoreBasedRateLimiter("some", config, scheduledExecutorService);
RateLimiterConfig rateLimiterConfig = rateLimiter.getRateLimiterConfig();
then(rateLimiterConfig.getTimeoutDuration()).isEqualTo(TIMEOUT);
then(rateLimiterConfig.getTimeoutDurationInNanos()).isEqualTo(TIMEOUT.toNanos());
then(rateLimiterConfig.getLimitForPeriod()).isEqualTo(LIMIT);
then(rateLimiterConfig.getLimitRefreshPeriod()).isEqualTo(REFRESH_PERIOD);
then(rateLimiterConfig.getLimitRefreshPeriodInNanos()).isEqualTo(REFRESH_PERIOD.toNanos());
rateLimiter.changeLimitForPeriod(LIMIT * 2);
then(rateLimiterConfig != rateLimiter.getRateLimiterConfig()).isTrue();
rateLimiterConfig = rateLimiter.getRateLimiterConfig();
then(rateLimiterConfig.getTimeoutDuration()).isEqualTo(TIMEOUT);
then(rateLimiterConfig.getTimeoutDurationInNanos()).isEqualTo(TIMEOUT.toNanos());
then(rateLimiterConfig.getLimitForPeriod()).isEqualTo(LIMIT * 2);
then(rateLimiterConfig.getLimitRefreshPeriod()).isEqualTo(REFRESH_PERIOD);
then(rateLimiterConfig.getLimitRefreshPeriodInNanos()).isEqualTo(REFRESH_PERIOD.toNanos());
}
use of io.github.resilience4j.ratelimiter.RateLimiter in project resilience4j by resilience4j.
the class RateLimiterExports method collect.
/**
* {@inheritDoc}
*/
@Override
public List<MetricFamilySamples> collect() {
final GaugeMetricFamily stats = new GaugeMetricFamily(name, "Rate Limiter Stats", asList("name", "param"));
for (RateLimiter rateLimiter : rateLimitersSupplier.get()) {
final RateLimiter.Metrics metrics = rateLimiter.getMetrics();
stats.addMetric(asList(rateLimiter.getName(), "available_permissions"), metrics.getAvailablePermissions());
stats.addMetric(asList(rateLimiter.getName(), "waiting_threads"), metrics.getNumberOfWaitingThreads());
}
return singletonList(stats);
}
use of io.github.resilience4j.ratelimiter.RateLimiter in project resilience4j by resilience4j.
the class RateLimiterMetricsTest method shouldRegisterMetrics.
@Test
public void shouldRegisterMetrics() throws Throwable {
// Given
RateLimiterRegistry rateLimiterRegistry = RateLimiterRegistry.ofDefaults();
RateLimiter rateLimiter = rateLimiterRegistry.rateLimiter("testLimit");
metricRegistry.registerAll(RateLimiterMetrics.ofRateLimiterRegistry(rateLimiterRegistry));
// 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("resilience4j.ratelimiter.testLimit.number_of_waiting_threads").getValue()).isEqualTo(0);
assertThat(metricRegistry.getGauges().get("resilience4j.ratelimiter.testLimit.available_permissions").getValue()).isIn(DEFAULT_LIMIT_FOR_PERIOD, DEFAULT_LIMIT_FOR_PERIOD - 1);
}
Aggregations