Search in sources :

Example 21 with RequestLimitRule

use of es.moki.ratelimitj.core.limiter.request.RequestLimitRule in project ratelimitj by mokies.

the class HazelcastRequestRateLimiterInternalTest method shouldEventuallyCleanUpExpiredKeys.

@Test
void shouldEventuallyCleanUpExpiredKeys() throws Exception {
    ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(2, TimeUnit.SECONDS, 5));
    RequestRateLimiter requestRateLimiter = getRateLimiter(rules, timeBandit);
    String key = "ip:127.0.0.5";
    IntStream.rangeClosed(1, 5).forEach(value -> {
        timeBandit.addUnixTimeMilliSeconds(100L);
        assertThat(requestRateLimiter.overLimitWhenIncremented(key)).isFalse();
    });
    IMap<Object, Object> map = hz.getMap(key);
    while (map.size() != 0) {
        Thread.sleep(10);
    }
    assertThat(map.size()).isZero();
}
Also used : RequestRateLimiter(es.moki.ratelimitj.core.limiter.request.RequestRateLimiter) DistributedObject(com.hazelcast.core.DistributedObject) RequestLimitRule(es.moki.ratelimitj.core.limiter.request.RequestLimitRule) Test(org.junit.jupiter.api.Test)

Example 22 with RequestLimitRule

use of es.moki.ratelimitj.core.limiter.request.RequestLimitRule in project ratelimitj by mokies.

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

Example 23 with RequestLimitRule

use of es.moki.ratelimitj.core.limiter.request.RequestLimitRule in project ratelimitj by mokies.

the class RedisRateLimiterFactoryTest method shouldNotReturnTheSameInstanceForSameRules.

@Test
void shouldNotReturnTheSameInstanceForSameRules() {
    RequestLimitRule rule1 = RequestLimitRule.of(1, TimeUnit.MINUTES, 22);
    RequestRateLimiter rateLimiter1 = factory.getInstance(ImmutableSet.of(rule1));
    RequestLimitRule rule2 = RequestLimitRule.of(1, TimeUnit.MINUTES, 33);
    RequestRateLimiter rateLimiter2 = factory.getInstance(ImmutableSet.of(rule2));
    assertThat(rateLimiter1).isNotSameAs(rateLimiter2);
}
Also used : RequestRateLimiter(es.moki.ratelimitj.core.limiter.request.RequestRateLimiter) RequestLimitRule(es.moki.ratelimitj.core.limiter.request.RequestLimitRule) Test(org.junit.jupiter.api.Test)

Aggregations

RequestLimitRule (es.moki.ratelimitj.core.limiter.request.RequestLimitRule)23 Test (org.junit.jupiter.api.Test)21 RequestRateLimiter (es.moki.ratelimitj.core.limiter.request.RequestRateLimiter)16 TimeSupplier (es.moki.ratelimitj.core.time.TimeSupplier)5 Set (java.util.Set)5 Logger (org.slf4j.Logger)5 LoggerFactory (org.slf4j.LoggerFactory)5 ReactiveRequestRateLimiter (es.moki.ratelimitj.core.limiter.request.ReactiveRequestRateLimiter)4 TimeUnit (java.util.concurrent.TimeUnit)4 ImmutableSet (com.google.common.collect.ImmutableSet)3 AsyncRequestRateLimiter (es.moki.ratelimitj.core.limiter.request.AsyncRequestRateLimiter)3 TimeBanditSupplier (es.moki.ratelimitj.test.time.TimeBanditSupplier)3 RateLimitUtils.coalesce (es.moki.ratelimitj.core.RateLimitUtils.coalesce)2 SystemTimeSupplier (es.moki.ratelimitj.core.time.SystemTimeSupplier)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Objects.requireNonNull (java.util.Objects.requireNonNull)2 Queue (java.util.Queue)2 CompletionStage (java.util.concurrent.CompletionStage)2 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)2