use of es.moki.ratelimitj.core.limiter.request.RequestLimitRule in project ratelimitj by mokies.
the class RedisRateLimiterFactoryTest 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);
}
use of es.moki.ratelimitj.core.limiter.request.RequestLimitRule 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.RequestLimitRule 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();
}
use of es.moki.ratelimitj.core.limiter.request.RequestLimitRule 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();
}
use of es.moki.ratelimitj.core.limiter.request.RequestLimitRule 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();
}
Aggregations