Search in sources :

Example 6 with RRateLimiter

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();
    });
}
Also used : RRateLimiter(org.redisson.api.RRateLimiter) Test(org.junit.jupiter.api.Test)

Example 7 with RRateLimiter

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();
}
Also used : RRateLimiter(org.redisson.api.RRateLimiter) Test(org.junit.jupiter.api.Test)

Example 8 with RRateLimiter

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();
}
Also used : RRateLimiter(org.redisson.api.RRateLimiter) Test(org.junit.jupiter.api.Test)

Example 9 with RRateLimiter

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);
}
Also used : RRateLimiter(org.redisson.api.RRateLimiter) Test(org.junit.jupiter.api.Test)

Example 10 with RRateLimiter

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();
}
Also used : RRateLimiter(org.redisson.api.RRateLimiter) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)14 RRateLimiter (org.redisson.api.RRateLimiter)14 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 Duration (java.time.Duration)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Queue (java.util.Queue)1 java.util.concurrent (java.util.concurrent)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Assertions (org.junit.jupiter.api.Assertions)1 RScoredSortedSet (org.redisson.api.RScoredSortedSet)1 RateIntervalUnit (org.redisson.api.RateIntervalUnit)1 RateType (org.redisson.api.RateType)1