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