use of org.redisson.api.RCountDownLatch in project redisson by redisson.
the class RedissonCountDownLatchConcurrentTest method testSingleCountDownAwait_SingleInstance.
@Test
public void testSingleCountDownAwait_SingleInstance() throws InterruptedException {
final int iterations = Runtime.getRuntime().availableProcessors() * 3;
RedissonClient redisson = BaseTest.createInstance();
final RCountDownLatch latch = redisson.getCountDownLatch("latch");
latch.trySetCount(iterations);
final AtomicInteger counter = new AtomicInteger();
ExecutorService executor = Executors.newScheduledThreadPool(iterations);
for (int i = 0; i < iterations; i++) {
executor.execute(() -> {
try {
latch.await();
Assertions.assertEquals(0, latch.getCount());
Assertions.assertEquals(iterations, counter.get());
} catch (InterruptedException e) {
Assertions.fail();
}
});
}
ExecutorService countDownExecutor = Executors.newFixedThreadPool(iterations);
for (int i = 0; i < iterations; i++) {
countDownExecutor.execute(() -> {
latch.countDown();
counter.incrementAndGet();
});
}
executor.shutdown();
Assertions.assertTrue(executor.awaitTermination(10, TimeUnit.SECONDS));
redisson.shutdown();
}
use of org.redisson.api.RCountDownLatch in project redisson by redisson.
the class RedissonCountDownLatchTest method testAwaitTimeoutFail.
@Test
public void testAwaitTimeoutFail() throws InterruptedException {
ExecutorService executor = Executors.newFixedThreadPool(2);
final RCountDownLatch latch = redisson.getCountDownLatch("latch1");
Assertions.assertTrue(latch.trySetCount(1));
executor.execute(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Assertions.fail();
}
latch.countDown();
});
executor.execute(() -> {
try {
Assertions.assertEquals(1, latch.getCount());
boolean res = latch.await(500, TimeUnit.MILLISECONDS);
Assertions.assertFalse(res);
} catch (InterruptedException e) {
Assertions.fail();
}
});
executor.shutdown();
Assertions.assertTrue(executor.awaitTermination(10, TimeUnit.SECONDS));
}
use of org.redisson.api.RCountDownLatch in project redisson by redisson.
the class RedissonCountDownLatchTest method testCount.
@Test
public void testCount() {
RCountDownLatch latch = redisson.getCountDownLatch("latch");
assertThat(latch.getCount()).isEqualTo(0);
}
use of org.redisson.api.RCountDownLatch in project redisson by redisson.
the class RedissonCountDownLatchTest method testAwaitTimeout.
@Test
public void testAwaitTimeout() throws InterruptedException {
ExecutorService executor = Executors.newFixedThreadPool(2);
final RCountDownLatch latch = redisson.getCountDownLatch("latch1");
Assertions.assertTrue(latch.trySetCount(1));
executor.execute(() -> {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
Assertions.fail();
}
latch.countDown();
});
executor.execute(() -> {
try {
Assertions.assertEquals(1, latch.getCount());
boolean res = latch.await(550, TimeUnit.MILLISECONDS);
Assertions.assertTrue(res);
} catch (InterruptedException e) {
Assertions.fail();
}
});
executor.shutdown();
Assertions.assertTrue(executor.awaitTermination(10, TimeUnit.SECONDS));
}
use of org.redisson.api.RCountDownLatch in project redisson by redisson.
the class RedissonTwoLockedThread method testCountDown.
@ParameterizedTest
@MethodSource("data")
public void testCountDown(Codec codec) throws InterruptedException {
Config config = BaseTest.createConfig();
config.setCodec(codec);
RedissonClient redisson = Redisson.create(config);
Assertions.assertTimeout(Duration.ofSeconds(3), () -> {
final String countDownName = getClass().getName() + ":countDown#1";
final CountDownLatch startSignal = new CountDownLatch(1);
final CountDownLatch testSignal = new CountDownLatch(1);
final CountDownLatch completeSignal = new CountDownLatch(2);
System.out.println("configure");
final long millis = System.currentTimeMillis();
new Thread() {
@Override
public void run() {
try {
startSignal.await();
RCountDownLatch countDownLatch = redisson.getCountDownLatch(countDownName);
System.out.println("1. getCountDownLatch " + countDownLatch.getName() + " - " + Thread.currentThread().getId());
countDownLatch.trySetCount(1);
System.out.println("1. trySetCount " + countDownLatch.getName() + " - " + Thread.currentThread().getId());
Thread.sleep(500);
testSignal.countDown();
Thread.sleep(500);
System.out.println("1. sleep " + countDownLatch.getName() + " - " + Thread.currentThread().getId());
countDownLatch.countDown();
System.out.println("1. countDown " + countDownLatch.getName() + " - " + Thread.currentThread().getId());
} catch (InterruptedException e) {
e.printStackTrace();
}
completeSignal.countDown();
}
}.start();
new Thread() {
@Override
public void run() {
try {
testSignal.await();
RCountDownLatch countDownLatch = redisson.getCountDownLatch(countDownName);
System.out.println("2. getCountDownLatch " + countDownLatch.getName() + " - " + Thread.currentThread().getId());
countDownLatch.await();
System.out.println("2. await " + countDownLatch.getName() + " - " + Thread.currentThread().getId());
long current = System.currentTimeMillis();
Assertions.assertTrue((current - millis) >= 1000, "current=" + current + ", millis=" + millis);
} catch (InterruptedException e) {
e.printStackTrace();
}
completeSignal.countDown();
}
}.start();
System.out.println("start");
startSignal.countDown();
completeSignal.await();
System.out.println("complete");
});
}
Aggregations