Search in sources :

Example 1 with ReactiveRequestRateLimiter

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

the class AbstractReactiveRequestRateLimiterTest method shouldLimitSingleWindowReactive.

@Test
void shouldLimitSingleWindowReactive() {
    ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(10, TimeUnit.SECONDS, 5));
    ReactiveRequestRateLimiter rateLimiter = getRateLimiter(rules, timeBandit);
    Flux<Boolean> overLimitFlux = Flux.just("ip:127.0.1.5").repeat(5).flatMap(key -> {
        timeBandit.addUnixTimeMilliSeconds(100);
        return rateLimiter.overLimitWhenIncrementedReactive(key);
    });
    overLimitFlux.toStream().forEach(result -> assertThat(result).isFalse());
    assertThat(rateLimiter.overLimitWhenIncrementedReactive("ip:127.0.1.5").block()).isTrue();
}
Also used : ReactiveRequestRateLimiter(es.moki.ratelimitj.core.limiter.request.ReactiveRequestRateLimiter) RequestLimitRule(es.moki.ratelimitj.core.limiter.request.RequestLimitRule) Test(org.junit.jupiter.api.Test)

Example 2 with ReactiveRequestRateLimiter

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

the class AbstractReactiveRequestRateLimiterTest method shouldResetLimit.

@Test
void shouldResetLimit() {
    ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(60, TimeUnit.SECONDS, 1));
    ReactiveRequestRateLimiter rateLimiter = getRateLimiter(rules, timeBandit);
    String key = "ip:127.1.0.1";
    assertThat(rateLimiter.overLimitWhenIncrementedReactive(key).block()).isFalse();
    assertThat(rateLimiter.overLimitWhenIncrementedReactive(key).block()).isTrue();
    assertThat(rateLimiter.resetLimitReactive(key).block()).isTrue();
    assertThat(rateLimiter.resetLimitReactive(key).block()).isFalse();
    assertThat(rateLimiter.overLimitWhenIncrementedReactive(key).block()).isFalse();
}
Also used : ReactiveRequestRateLimiter(es.moki.ratelimitj.core.limiter.request.ReactiveRequestRateLimiter) RequestLimitRule(es.moki.ratelimitj.core.limiter.request.RequestLimitRule) Test(org.junit.jupiter.api.Test)

Example 3 with ReactiveRequestRateLimiter

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

the class AbstractReactiveRequestRateLimiterTest method shouldLimitDualWindowReactive.

@Test
void shouldLimitDualWindowReactive() {
    ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(1, TimeUnit.SECONDS, 5), RequestLimitRule.of(10, TimeUnit.SECONDS, 10));
    ReactiveRequestRateLimiter rateLimiter = getRateLimiter(rules, timeBandit);
    Flux.just("ip:127.0.1.6").repeat(5).flatMap(key -> {
        timeBandit.addUnixTimeMilliSeconds(100);
        return rateLimiter.overLimitWhenIncrementedReactive(key);
    }).toStream().forEach(result -> assertThat(result).isFalse());
    timeBandit.addUnixTimeMilliSeconds(1000L);
    Flux.just("ip:127.0.1.6").repeat(5).flatMap(key -> {
        timeBandit.addUnixTimeMilliSeconds(100);
        return rateLimiter.overLimitWhenIncrementedReactive(key);
    }).toStream().forEach(result -> assertThat(result).isFalse());
    assertThat(rateLimiter.overLimitWhenIncrementedReactive("ip:127.0.1.6").block()).isTrue();
}
Also used : ReactiveRequestRateLimiter(es.moki.ratelimitj.core.limiter.request.ReactiveRequestRateLimiter) RequestLimitRule(es.moki.ratelimitj.core.limiter.request.RequestLimitRule) Test(org.junit.jupiter.api.Test)

Example 4 with ReactiveRequestRateLimiter

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

the class AbstractReactiveRequestRateLimiterTest method shouldGeLimitSingleWindowReactive.

@Test
void shouldGeLimitSingleWindowReactive() {
    ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(10, TimeUnit.SECONDS, 5));
    ReactiveRequestRateLimiter rateLimiter = getRateLimiter(rules, timeBandit);
    Flux<Boolean> geLimitLimitFlux = Flux.just("ip:127.0.1.2").repeat(4).flatMap(key -> {
        timeBandit.addUnixTimeMilliSeconds(100);
        return rateLimiter.geLimitWhenIncrementedReactive(key);
    });
    geLimitLimitFlux.toStream().forEach(result -> assertThat(result).isFalse());
    assertThat(rateLimiter.geLimitWhenIncrementedReactive("ip:127.0.1.2").block()).isTrue();
}
Also used : ReactiveRequestRateLimiter(es.moki.ratelimitj.core.limiter.request.ReactiveRequestRateLimiter) RequestLimitRule(es.moki.ratelimitj.core.limiter.request.RequestLimitRule) Test(org.junit.jupiter.api.Test)

Aggregations

ReactiveRequestRateLimiter (es.moki.ratelimitj.core.limiter.request.ReactiveRequestRateLimiter)4 RequestLimitRule (es.moki.ratelimitj.core.limiter.request.RequestLimitRule)4 Test (org.junit.jupiter.api.Test)4