Search in sources :

Example 11 with RequestRateLimiter

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

the class InMemoryRateLimiterFactoryTest method shouldReturnTheSameInstanceForSameRules.

@Test
void shouldReturnTheSameInstanceForSameRules() {
    RequestLimitRule rule1 = RequestLimitRule.of(1, TimeUnit.MINUTES, 10);
    RequestRateLimiter rateLimiter1 = factory.getInstance(ImmutableSet.of(rule1));
    RequestLimitRule rule2 = RequestLimitRule.of(1, TimeUnit.MINUTES, 10);
    RequestRateLimiter rateLimiter2 = factory.getInstance(ImmutableSet.of(rule2));
    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 12 with RequestRateLimiter

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

the class InMemoryRequestRateLimiterInternalTest method shouldEventuallyCleanUpExpiredKeys.

@Test
@Disabled
void shouldEventuallyCleanUpExpiredKeys() throws Exception {
    ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(1, 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();
    });
    while (expiryingKeyMap.size() != 0) {
        Thread.sleep(50);
    }
    assertThat(expiryingKeyMap.size()).isZero();
}
Also used : RequestRateLimiter(es.moki.ratelimitj.core.limiter.request.RequestRateLimiter) RequestLimitRule(es.moki.ratelimitj.core.limiter.request.RequestLimitRule) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 13 with RequestRateLimiter

use of es.moki.ratelimitj.core.limiter.request.RequestRateLimiter 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 14 with RequestRateLimiter

use of es.moki.ratelimitj.core.limiter.request.RequestRateLimiter 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 15 with RequestRateLimiter

use of es.moki.ratelimitj.core.limiter.request.RequestRateLimiter 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

RequestRateLimiter (es.moki.ratelimitj.core.limiter.request.RequestRateLimiter)15 RequestLimitRule (es.moki.ratelimitj.core.limiter.request.RequestLimitRule)14 Test (org.junit.jupiter.api.Test)14 Stopwatch (com.google.common.base.Stopwatch)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 DistributedObject (com.hazelcast.core.DistributedObject)1 RateLimited (es.moki.ratelimij.dropwizard.annotation.RateLimited)1 TimeSupplier (es.moki.ratelimitj.core.time.TimeSupplier)1 TimeBanditSupplier (es.moki.ratelimitj.test.time.TimeBanditSupplier)1 NumberFormat (java.text.NumberFormat)1 Locale (java.util.Locale)1 Random (java.util.Random)1 Set (java.util.Set)1 TimeUnit (java.util.concurrent.TimeUnit)1 IntStream (java.util.stream.IntStream)1 AnnotatedMethod (org.glassfish.jersey.server.model.AnnotatedMethod)1 Disabled (org.junit.jupiter.api.Disabled)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1