Search in sources :

Example 1 with AsyncRequestRateLimiter

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();
}
Also used : AsyncRequestRateLimiter(es.moki.ratelimitj.core.limiter.request.AsyncRequestRateLimiter) ImmutableSet(com.google.common.collect.ImmutableSet) Logger(org.slf4j.Logger) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) CompletionStage(java.util.concurrent.CompletionStage) Stream(java.util.stream.Stream) TimeSupplier(es.moki.ratelimitj.core.time.TimeSupplier) RequestLimitRule(es.moki.ratelimitj.core.limiter.request.RequestLimitRule) Queue(java.util.Queue) TimeBanditSupplier(es.moki.ratelimitj.test.time.TimeBanditSupplier) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) AsyncRequestRateLimiter(es.moki.ratelimitj.core.limiter.request.AsyncRequestRateLimiter) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) RequestLimitRule(es.moki.ratelimitj.core.limiter.request.RequestLimitRule) CompletionStage(java.util.concurrent.CompletionStage) Test(org.junit.jupiter.api.Test)

Example 2 with AsyncRequestRateLimiter

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();
}
Also used : AsyncRequestRateLimiter(es.moki.ratelimitj.core.limiter.request.AsyncRequestRateLimiter) RequestLimitRule(es.moki.ratelimitj.core.limiter.request.RequestLimitRule) Test(org.junit.jupiter.api.Test)

Example 3 with AsyncRequestRateLimiter

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();
}
Also used : AsyncRequestRateLimiter(es.moki.ratelimitj.core.limiter.request.AsyncRequestRateLimiter) ImmutableSet(com.google.common.collect.ImmutableSet) Logger(org.slf4j.Logger) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) CompletionStage(java.util.concurrent.CompletionStage) Stream(java.util.stream.Stream) TimeSupplier(es.moki.ratelimitj.core.time.TimeSupplier) RequestLimitRule(es.moki.ratelimitj.core.limiter.request.RequestLimitRule) Queue(java.util.Queue) TimeBanditSupplier(es.moki.ratelimitj.test.time.TimeBanditSupplier) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) AsyncRequestRateLimiter(es.moki.ratelimitj.core.limiter.request.AsyncRequestRateLimiter) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) RequestLimitRule(es.moki.ratelimitj.core.limiter.request.RequestLimitRule) CompletionStage(java.util.concurrent.CompletionStage) Test(org.junit.jupiter.api.Test)

Aggregations

AsyncRequestRateLimiter (es.moki.ratelimitj.core.limiter.request.AsyncRequestRateLimiter)3 RequestLimitRule (es.moki.ratelimitj.core.limiter.request.RequestLimitRule)3 Test (org.junit.jupiter.api.Test)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 TimeSupplier (es.moki.ratelimitj.core.time.TimeSupplier)2 TimeBanditSupplier (es.moki.ratelimitj.test.time.TimeBanditSupplier)2 Queue (java.util.Queue)2 Set (java.util.Set)2 CompletionStage (java.util.concurrent.CompletionStage)2 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)2 TimeUnit (java.util.concurrent.TimeUnit)2 Stream (java.util.stream.Stream)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2