use of org.redisson.api.RRateLimiter in project redisson by redisson.
the class RedissonRateLimiterTest method testTryAcquire.
@Test
public void testTryAcquire() {
Assertions.assertTimeout(Duration.ofMillis(1500), () -> {
RRateLimiter rr = redisson.getRateLimiter("acquire");
assertThat(rr.trySetRate(RateType.OVERALL, 1, 5, RateIntervalUnit.SECONDS)).isTrue();
assertThat(rr.tryAcquire(1, 1, TimeUnit.SECONDS)).isTrue();
assertThat(rr.tryAcquire(1, 1, TimeUnit.SECONDS)).isFalse();
assertThat(rr.tryAcquire()).isFalse();
});
}
use of org.redisson.api.RRateLimiter in project redisson by redisson.
the class RedissonRateLimiterTest method testPermitsExceeding.
@Test
public void testPermitsExceeding() throws InterruptedException {
RRateLimiter limiter = redisson.getRateLimiter("myLimiter");
limiter.trySetRate(RateType.PER_CLIENT, 1, 1, RateIntervalUnit.SECONDS);
org.assertj.core.api.Assertions.assertThatThrownBy(() -> limiter.tryAcquire(20)).hasMessageContaining("Requested permits amount could not exceed defined rate");
assertThat(limiter.tryAcquire()).isTrue();
}
use of org.redisson.api.RRateLimiter in project redisson by redisson.
the class RedissonRateLimiterTest method testRemove.
@Test
public void testRemove() {
RRateLimiter rateLimiter = redisson.getRateLimiter("test");
assertThat(rateLimiter.delete()).isFalse();
rateLimiter.trySetRate(RateType.OVERALL, 5L, 5L, RateIntervalUnit.MINUTES);
assertThat(redisson.getKeys().count()).isEqualTo(1);
rateLimiter.tryAcquire();
boolean deleted = rateLimiter.delete();
assertThat(redisson.getKeys().count()).isEqualTo(0);
assertThat(deleted).isTrue();
}
use of org.redisson.api.RRateLimiter in project redisson by redisson.
the class RedissonRateLimiterTest method testRateConfig.
@Test
public void testRateConfig() {
RRateLimiter rr = redisson.getRateLimiter("acquire");
assertThat(rr.trySetRate(RateType.OVERALL, 1, 5, RateIntervalUnit.SECONDS)).isTrue();
assertThat(rr.getConfig().getRate()).isEqualTo(1);
assertThat(rr.getConfig().getRateInterval()).isEqualTo(5000);
assertThat(rr.getConfig().getRateType()).isEqualTo(RateType.OVERALL);
}
use of org.redisson.api.RRateLimiter in project redisson by redisson.
the class RedissonRateLimiterTest method testExpire.
@Test
public void testExpire() throws InterruptedException {
RRateLimiter rr = redisson.getRateLimiter("limiter");
rr.trySetRate(RateType.OVERALL, 2, 5, RateIntervalUnit.SECONDS);
rr.tryAcquire();
rr.expire(Duration.ofSeconds(1));
Thread.sleep(1100);
assertThat(redisson.getKeys().count()).isZero();
}
Aggregations