testMultiAwait added

pull/5361/head
Nikita Koksharov 1 year ago
parent d3a18b3831
commit c49dbfdc0e

@ -3,10 +3,14 @@ package org.redisson;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.redisson.api.RCountDownLatch;
import org.testcontainers.shaded.org.awaitility.Awaitility;
import java.time.Duration;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import static org.assertj.core.api.Assertions.assertThat;
@ -75,6 +79,40 @@ public class RedissonCountDownLatchTest extends BaseTest {
Assertions.assertTrue(executor.awaitTermination(10, TimeUnit.SECONDS));
}
@Test
public void testMultiAwait() throws InterruptedException {
RCountDownLatch latch = redisson.getCountDownLatch("latch");
latch.trySetCount(5);
AtomicInteger counter = new AtomicInteger();
for (int i = 0; i < 5; i++) {
Thread t = new Thread() {
@Override
public void run() {
RCountDownLatch latch2 = redisson.getCountDownLatch("latch");
latch2.awaitAsync(10L, TimeUnit.SECONDS).thenAccept(r -> {
if (r) {
counter.incrementAndGet();
}
});
}
};
t.start();
}
ScheduledExecutorService ee = Executors.newScheduledThreadPool(1);
for (int i = 0; i < 5; i++) {
ee.schedule(() -> {
RCountDownLatch latch2 = redisson.getCountDownLatch("latch");
latch2.countDown();
}, 1, TimeUnit.SECONDS);
}
Awaitility.await().atMost(Duration.ofSeconds(7)).until(() -> {
return latch.getCount() == 0;
});
assertThat(counter.get()).isEqualTo(5);
}
@Test
public void testCountDown() throws InterruptedException {
RCountDownLatch latch = redisson.getCountDownLatch("latch");

Loading…
Cancel
Save