use of es.moki.ratelimitj.core.limiter.request.RequestRateLimiter in project ratelimitj by mokies.
the class InMemoryRateLimiterFactoryTest method shouldReturnTheSameInstanceForSameSetOfRules.
@Test
void shouldReturnTheSameInstanceForSameSetOfRules() {
RequestLimitRule rule1a = RequestLimitRule.of(1, TimeUnit.MINUTES, 10);
RequestLimitRule rule1b = RequestLimitRule.of(1, TimeUnit.HOURS, 100);
RequestRateLimiter rateLimiter1 = factory.getInstance(ImmutableSet.of(rule1a, rule1b));
RequestLimitRule rule2a = RequestLimitRule.of(1, TimeUnit.MINUTES, 10);
RequestLimitRule rule2b = RequestLimitRule.of(1, TimeUnit.HOURS, 100);
RequestRateLimiter rateLimiter2 = factory.getInstance(ImmutableSet.of(rule2a, rule2b));
assertThat(rateLimiter1).isSameAs(rateLimiter2);
}
use of es.moki.ratelimitj.core.limiter.request.RequestRateLimiter in project ratelimitj by mokies.
the class AbstractSyncRequestRateLimiterTest method shouldLimitSingleWindowSync.
@Test
void shouldLimitSingleWindowSync() {
ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(10, TimeUnit.SECONDS, 5));
RequestRateLimiter requestRateLimiter = getRateLimiter(rules, timeBandit);
IntStream.rangeClosed(1, 5).forEach(value -> {
timeBandit.addUnixTimeMilliSeconds(1000L);
assertThat(requestRateLimiter.overLimitWhenIncremented("ip:127.0.1.1")).isFalse();
});
assertThat(requestRateLimiter.overLimitWhenIncremented("ip:127.0.1.1")).isTrue();
}
use of es.moki.ratelimitj.core.limiter.request.RequestRateLimiter in project ratelimitj by mokies.
the class AbstractSyncRequestRateLimiterTest method shouldLimitSingleWindowSyncWithMultipleKeys.
@Test
void shouldLimitSingleWindowSyncWithMultipleKeys() {
ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(10, TimeUnit.SECONDS, 5));
RequestRateLimiter requestRateLimiter = getRateLimiter(rules, timeBandit);
IntStream.rangeClosed(1, 5).forEach(value -> {
timeBandit.addUnixTimeMilliSeconds(1000L);
IntStream.rangeClosed(1, 10).forEach(keySuffix -> assertThat(requestRateLimiter.overLimitWhenIncremented("ip:127.0.0." + keySuffix)).isFalse());
});
IntStream.rangeClosed(1, 10).forEach(keySuffix -> assertThat(requestRateLimiter.overLimitWhenIncremented("ip:127.0.0." + keySuffix)).isTrue());
timeBandit.addUnixTimeMilliSeconds(5000L);
IntStream.rangeClosed(1, 10).forEach(keySuffix -> assertThat(requestRateLimiter.overLimitWhenIncremented("ip:127.0.0." + keySuffix)).isFalse());
}
use of es.moki.ratelimitj.core.limiter.request.RequestRateLimiter in project ratelimitj by mokies.
the class AbstractSyncRequestRateLimiterTest method shouldLimitWithWeightSingleWindowSync.
@Test
void shouldLimitWithWeightSingleWindowSync() {
ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(10, TimeUnit.SECONDS, 10));
RequestRateLimiter requestRateLimiter = getRateLimiter(rules, timeBandit);
IntStream.rangeClosed(1, 5).forEach(value -> {
timeBandit.addUnixTimeMilliSeconds(1000L);
assertThat(requestRateLimiter.overLimitWhenIncremented("ip:127.0.1.2", 2)).isFalse();
});
assertThat(requestRateLimiter.overLimitWhenIncremented("ip:127.0.1.2", 2)).isTrue();
}
use of es.moki.ratelimitj.core.limiter.request.RequestRateLimiter in project ratelimitj by mokies.
the class AbstractSyncRequestRateLimiterTest method shouldResetLimit.
@Test
void shouldResetLimit() {
ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(60, TimeUnit.SECONDS, 1));
RequestRateLimiter requestRateLimiter = getRateLimiter(rules, timeBandit);
String key = "ip:127.1.0.1";
assertThat(requestRateLimiter.overLimitWhenIncremented(key)).isFalse();
assertThat(requestRateLimiter.overLimitWhenIncremented(key)).isTrue();
assertThat(requestRateLimiter.resetLimit(key)).isTrue();
assertThat(requestRateLimiter.resetLimit(key)).isFalse();
assertThat(requestRateLimiter.overLimitWhenIncremented(key)).isFalse();
}
Aggregations