Search in sources :

Example 1 with RRateLimiter

use of org.redisson.api.RRateLimiter in project redisson by redisson.

the class RedissonRateLimiterTest method testAcquisitionInterval.

@Test
public void testAcquisitionInterval() throws InterruptedException {
    RRateLimiter rr = redisson.getRateLimiter("acquire");
    rr.trySetRate(RateType.OVERALL, 2, 5, RateIntervalUnit.SECONDS);
    assertThat(rr.tryAcquire()).isTrue();
    Thread.sleep(4000);
    assertThat(rr.tryAcquire()).isTrue();
    Thread.sleep(1050);
    assertThat(rr.tryAcquire()).isTrue();
    assertThat(rr.tryAcquire()).isFalse();
}
Also used : RRateLimiter(org.redisson.api.RRateLimiter) Test(org.junit.jupiter.api.Test)

Example 2 with RRateLimiter

use of org.redisson.api.RRateLimiter in project redisson by redisson.

the class RedissonRateLimiterTest method testRateValue.

@Test
public void testRateValue() throws InterruptedException {
    RRateLimiter rateLimiter = redisson.getRateLimiter("test1");
    int rate = 10_000;
    rateLimiter.setRate(RateType.OVERALL, rate, 10_000, RateIntervalUnit.MILLISECONDS);
    ExecutorService e = Executors.newFixedThreadPool(200);
    for (int i = 0; i < 200; i++) {
        e.execute(() -> {
            while (true) {
                rateLimiter.acquire();
            }
        });
    }
    RScoredSortedSet<Object> sortedSet = redisson.getScoredSortedSet("{test1}:permits");
    List<Integer> sizes = new ArrayList<>();
    for (int i = 0; i < 20; i++) {
        sizes.add(sortedSet.size());
        Thread.sleep(1000);
    }
    assertThat(sizes.stream().filter(s -> s == rate).count()).isGreaterThan(16);
    e.shutdownNow();
}
Also used : RScoredSortedSet(org.redisson.api.RScoredSortedSet) RateIntervalUnit(org.redisson.api.RateIntervalUnit) RateType(org.redisson.api.RateType) java.util.concurrent(java.util.concurrent) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Duration(java.time.Duration) Assertions(org.junit.jupiter.api.Assertions) RRateLimiter(org.redisson.api.RRateLimiter) Queue(java.util.Queue) ArrayList(java.util.ArrayList) RRateLimiter(org.redisson.api.RRateLimiter) Test(org.junit.jupiter.api.Test)

Example 3 with RRateLimiter

use of org.redisson.api.RRateLimiter in project redisson by redisson.

the class RedissonRateLimiterTest method testUpdateRateConfig.

@Test
public void testUpdateRateConfig() {
    RRateLimiter rr = redisson.getRateLimiter("acquire");
    assertThat(rr.trySetRate(RateType.OVERALL, 1, 5, RateIntervalUnit.SECONDS)).isTrue();
    rr.setRate(RateType.OVERALL, 2, 5, RateIntervalUnit.SECONDS);
    assertThat(rr.getConfig().getRate()).isEqualTo(2);
    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 4 with RRateLimiter

use of org.redisson.api.RRateLimiter in project redisson by redisson.

the class RedissonRateLimiterTest method testZeroTimeout.

@Test
public void testZeroTimeout() throws InterruptedException {
    RRateLimiter limiter = redisson.getRateLimiter("myLimiter");
    limiter.trySetRate(RateType.OVERALL, 5, 1, RateIntervalUnit.SECONDS);
    assertThat(limiter.availablePermits()).isEqualTo(5);
    assertThat(limiter.tryAcquire(1, 0, TimeUnit.SECONDS)).isTrue();
    assertThat(limiter.tryAcquire(1, 0, TimeUnit.SECONDS)).isTrue();
    assertThat(limiter.availablePermits()).isEqualTo(3);
    assertThat(limiter.tryAcquire(1, 0, TimeUnit.SECONDS)).isTrue();
    assertThat(limiter.tryAcquire(1, 0, TimeUnit.SECONDS)).isTrue();
    assertThat(limiter.availablePermits()).isEqualTo(1);
    assertThat(limiter.tryAcquire(1, 0, TimeUnit.SECONDS)).isTrue();
    assertThat(limiter.availablePermits()).isEqualTo(0);
    assertThat(limiter.tryAcquire(1, 0, TimeUnit.SECONDS)).isFalse();
    assertThat(limiter.tryAcquire(1, 0, TimeUnit.SECONDS)).isFalse();
    assertThat(limiter.tryAcquire(1, 0, TimeUnit.SECONDS)).isFalse();
    assertThat(limiter.tryAcquire(1, 0, TimeUnit.SECONDS)).isFalse();
    assertThat(limiter.tryAcquire(1, 0, TimeUnit.SECONDS)).isFalse();
    Thread.sleep(1000);
    assertThat(limiter.tryAcquire(1, 0, TimeUnit.SECONDS)).isTrue();
    assertThat(limiter.tryAcquire(1, 0, TimeUnit.SECONDS)).isTrue();
    assertThat(limiter.tryAcquire(1, 0, TimeUnit.SECONDS)).isTrue();
    assertThat(limiter.tryAcquire(1, 0, TimeUnit.SECONDS)).isTrue();
    assertThat(limiter.tryAcquire(1, 0, TimeUnit.SECONDS)).isTrue();
    assertThat(limiter.tryAcquire(1, 0, TimeUnit.SECONDS)).isFalse();
    assertThat(limiter.tryAcquire(1, 0, TimeUnit.SECONDS)).isFalse();
    assertThat(limiter.tryAcquire(1, 0, TimeUnit.SECONDS)).isFalse();
    assertThat(limiter.tryAcquire(1, 0, TimeUnit.SECONDS)).isFalse();
    assertThat(limiter.tryAcquire(1, 0, TimeUnit.SECONDS)).isFalse();
}
Also used : RRateLimiter(org.redisson.api.RRateLimiter) Test(org.junit.jupiter.api.Test)

Example 5 with RRateLimiter

use of org.redisson.api.RRateLimiter in project redisson by redisson.

the class RedissonRateLimiterTest method test.

@Test
public void test() throws InterruptedException {
    RRateLimiter rr = redisson.getRateLimiter("test");
    assertThat(rr.trySetRate(RateType.OVERALL, 10, 1, RateIntervalUnit.SECONDS)).isTrue();
    assertThat(rr.trySetRate(RateType.OVERALL, 20, 1, RateIntervalUnit.SECONDS)).isFalse();
    for (int j = 0; j < 3; j++) {
        for (int i = 0; i < 10; i++) {
            assertThat(rr.tryAcquire()).isTrue();
        }
        for (int i = 0; i < 10; i++) {
            assertThat(rr.tryAcquire()).isFalse();
        }
        Thread.sleep(1050);
    }
}
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