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();
}
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();
}
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);
}
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();
}
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);
}
}
Aggregations