Search in sources :

Example 11 with RRateLimiter

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

the class RedissonRateLimiterTest method testConcurrency2.

@Test
public void testConcurrency2() throws InterruptedException {
    RRateLimiter rr = redisson.getRateLimiter("test");
    rr.trySetRate(RateType.OVERALL, 18, 1, RateIntervalUnit.SECONDS);
    Queue<Long> queue = new ConcurrentLinkedQueue<Long>();
    AtomicLong counter = new AtomicLong();
    ExecutorService pool = Executors.newFixedThreadPool(8);
    for (int i = 0; i < 8; i++) {
        pool.execute(new Runnable() {

            @Override
            public void run() {
                try {
                    while (true) {
                        rr.acquire();
                        queue.add(System.currentTimeMillis());
                        if (counter.incrementAndGet() > 1000) {
                            break;
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    pool.shutdown();
    assertThat(pool.awaitTermination(2, TimeUnit.MINUTES)).isTrue();
    int count = 0;
    long start = 0;
    boolean skip = true;
    for (Long value : queue) {
        if (start == 0) {
            start = value;
        }
        count++;
        if (value - start >= 1000) {
            if (!skip) {
                assertThat(count).isLessThanOrEqualTo(18);
            } else {
                skip = false;
            }
            start = 0;
            count = 0;
        }
    }
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicLong(java.util.concurrent.atomic.AtomicLong) RRateLimiter(org.redisson.api.RRateLimiter) Test(org.junit.jupiter.api.Test)

Example 12 with RRateLimiter

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

the class RedissonRateLimiterTest method testAcquire.

@Test
public void testAcquire() {
    RRateLimiter rr = redisson.getRateLimiter("acquire");
    assertThat(rr.trySetRate(RateType.OVERALL, 1, 5, RateIntervalUnit.SECONDS)).isTrue();
    for (int i = 0; i < 10; i++) {
        rr.acquire(1);
    }
    assertThat(rr.tryAcquire()).isFalse();
}
Also used : RRateLimiter(org.redisson.api.RRateLimiter) Test(org.junit.jupiter.api.Test)

Example 13 with RRateLimiter

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

the class RedissonRateLimiterTest method testConcurrency.

@Test
public void testConcurrency() 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();
    Queue<Long> queue = new ConcurrentLinkedQueue<Long>();
    AtomicLong counter = new AtomicLong();
    ExecutorService pool = Executors.newFixedThreadPool(8);
    for (int i = 0; i < 8; i++) {
        pool.execute(new Runnable() {

            @Override
            public void run() {
                while (true) {
                    if (rr.tryAcquire()) {
                        if (counter.incrementAndGet() > 500) {
                            break;
                        }
                        queue.add(System.currentTimeMillis());
                    }
                    try {
                        Thread.sleep(ThreadLocalRandom.current().nextInt(10));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }
    pool.shutdown();
    assertThat(pool.awaitTermination(1, TimeUnit.MINUTES)).isTrue();
    int count = 0;
    long start = 0;
    for (Long value : queue) {
        if (count % 10 == 0) {
            if (start > 0) {
                assertThat(value - start).isGreaterThan(980);
            }
            start = value;
        }
        count++;
    }
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicLong(java.util.concurrent.atomic.AtomicLong) RRateLimiter(org.redisson.api.RRateLimiter) Test(org.junit.jupiter.api.Test)

Example 14 with RRateLimiter

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

the class RedissonRateLimiterTest method testAvailablePermits.

@Test
public void testAvailablePermits() throws InterruptedException {
    RRateLimiter rt = redisson.getRateLimiter("rt2");
    rt.trySetRate(RateType.OVERALL, 10, 5, RateIntervalUnit.SECONDS);
    assertThat(rt.availablePermits()).isEqualTo(10);
    rt.acquire(1);
    Thread.sleep(6000);
    assertThat(rt.availablePermits()).isEqualTo(10);
}
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