use of es.moki.ratelimitj.core.limiter.request.RequestLimitRule 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.RequestLimitRule 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.RequestLimitRule 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.RequestLimitRule 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();
}
use of es.moki.ratelimitj.core.limiter.request.RequestLimitRule in project ratelimitj by mokies.
the class AbstractSyncRequestRateLimiterTest method shouldGeLimitSingleWindowSync.
@Test
void shouldGeLimitSingleWindowSync() {
ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(10, TimeUnit.SECONDS, 5));
RequestRateLimiter requestRateLimiter = getRateLimiter(rules, timeBandit);
IntStream.rangeClosed(1, 4).forEach(value -> {
timeBandit.addUnixTimeMilliSeconds(1000L);
assertThat(requestRateLimiter.geLimitWhenIncremented("ip:127.0.1.2")).isFalse();
});
assertThat(requestRateLimiter.geLimitWhenIncremented("ip:127.0.1.2")).isTrue();
}
Aggregations