use of es.moki.ratelimitj.core.limiter.request.AsyncRequestRateLimiter in project ratelimitj by mokies.
the class AbstractAsyncRequestRateLimiterTest method shouldLimitSingleWindowAsync.
@Test
void shouldLimitSingleWindowAsync() throws Exception {
ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(10, TimeUnit.SECONDS, 5));
AsyncRequestRateLimiter rateLimiter = getAsyncRateLimiter(rules, timeBandit);
Queue<CompletionStage> stageAsserts = new ConcurrentLinkedQueue<>();
Stream.generate(() -> "ip:127.0.0.2").limit(5).forEach(key -> {
timeBandit.addUnixTimeMilliSeconds(1000L);
stageAsserts.add(rateLimiter.overLimitAsync(key).thenAccept(result -> assertThat(result).isFalse()));
});
for (CompletionStage stage : stageAsserts) {
stage.toCompletableFuture().get();
}
assertThat(rateLimiter.overLimitAsync("ip:127.0.0.2").toCompletableFuture().get()).isTrue();
}
use of es.moki.ratelimitj.core.limiter.request.AsyncRequestRateLimiter in project ratelimitj by mokies.
the class AbstractAsyncRequestRateLimiterTest method shouldResetLimit.
@Test
void shouldResetLimit() throws Exception {
ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(60, TimeUnit.SECONDS, 1));
AsyncRequestRateLimiter rateLimiter = getAsyncRateLimiter(rules, timeBandit);
String key = "ip:127.1.0.1";
assertThat(rateLimiter.overLimitAsync(key).toCompletableFuture().get()).isFalse();
assertThat(rateLimiter.overLimitAsync(key).toCompletableFuture().get()).isTrue();
assertThat(rateLimiter.resetLimitAsync(key).toCompletableFuture().get()).isTrue();
assertThat(rateLimiter.resetLimitAsync(key).toCompletableFuture().get()).isFalse();
assertThat(rateLimiter.overLimitAsync(key).toCompletableFuture().get()).isFalse();
}
use of es.moki.ratelimitj.core.limiter.request.AsyncRequestRateLimiter in project ratelimitj by mokies.
the class AbstractAsyncRequestRateLimiterTest method shouldLimitDualWindowAsync.
@Test
void shouldLimitDualWindowAsync() throws Exception {
ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(2, TimeUnit.SECONDS, 5), RequestLimitRule.of(10, TimeUnit.SECONDS, 20));
AsyncRequestRateLimiter rateLimiter = getAsyncRateLimiter(rules, timeBandit);
Queue<CompletionStage> stageAsserts = new ConcurrentLinkedQueue<>();
Stream.generate(() -> "ip:127.0.0.10").limit(5).forEach(key -> {
timeBandit.addUnixTimeMilliSeconds(200L);
log.debug("incrementing rate limiter");
stageAsserts.add(rateLimiter.overLimitAsync(key).thenAccept(result -> assertThat(result).isFalse()));
});
for (CompletionStage stage : stageAsserts) {
stage.toCompletableFuture().get();
}
assertThat(rateLimiter.overLimitAsync("ip:127.0.0.10").toCompletableFuture().get()).isTrue();
timeBandit.addUnixTimeMilliSeconds(1000L);
assertThat(rateLimiter.overLimitAsync("ip:127.0.0.10").toCompletableFuture().get()).isFalse();
}
Aggregations