Search in sources :

Example 6 with RCountDownLatch

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

the class RedissonTwoLockedThread method testCountDown.

@Test(timeout = 3000)
public void testCountDown() throws InterruptedException {
    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();
                Assert.assertTrue("current=" + current + ", millis=" + millis, (current - millis) >= 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            completeSignal.countDown();
        }
    }.start();
    System.out.println("start");
    startSignal.countDown();
    completeSignal.await();
    System.out.println("complete");
}
Also used : RCountDownLatch(org.redisson.api.RCountDownLatch) CountDownLatch(java.util.concurrent.CountDownLatch) RCountDownLatch(org.redisson.api.RCountDownLatch) Test(org.junit.Test)

Example 7 with RCountDownLatch

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

the class RedissonCountDownLatchTest method testTrySetCount.

@Test
public void testTrySetCount() throws Exception {
    RCountDownLatch latch = redisson.getCountDownLatch("latch");
    assertThat(latch.trySetCount(1)).isTrue();
    assertThat(latch.trySetCount(2)).isFalse();
}
Also used : RCountDownLatch(org.redisson.api.RCountDownLatch) Test(org.junit.jupiter.api.Test)

Example 8 with RCountDownLatch

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

the class RedissonCountDownLatchTest method testCountDown.

@Test
public void testCountDown() throws InterruptedException {
    RCountDownLatch latch = redisson.getCountDownLatch("latch");
    latch.trySetCount(2);
    Assertions.assertEquals(2, latch.getCount());
    latch.countDown();
    Assertions.assertEquals(1, latch.getCount());
    latch.countDown();
    Assertions.assertEquals(0, latch.getCount());
    latch.await();
    latch.countDown();
    Assertions.assertEquals(0, latch.getCount());
    latch.await();
    latch.countDown();
    Assertions.assertEquals(0, latch.getCount());
    latch.await();
    RCountDownLatch latch1 = redisson.getCountDownLatch("latch1");
    latch1.trySetCount(1);
    latch1.countDown();
    Assertions.assertEquals(0, latch.getCount());
    latch1.countDown();
    Assertions.assertEquals(0, latch.getCount());
    latch1.await();
    RCountDownLatch latch2 = redisson.getCountDownLatch("latch2");
    latch2.trySetCount(1);
    latch2.countDown();
    latch2.await();
    latch2.await();
    RCountDownLatch latch3 = redisson.getCountDownLatch("latch3");
    Assertions.assertEquals(0, latch.getCount());
    latch3.await();
    RCountDownLatch latch4 = redisson.getCountDownLatch("latch4");
    Assertions.assertEquals(0, latch.getCount());
    latch4.countDown();
    Assertions.assertEquals(0, latch.getCount());
    latch4.await();
}
Also used : RCountDownLatch(org.redisson.api.RCountDownLatch) Test(org.junit.jupiter.api.Test)

Example 9 with RCountDownLatch

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

the class RedissonCountDownLatchTest method testDelete.

@Test
public void testDelete() throws Exception {
    RCountDownLatch latch = redisson.getCountDownLatch("latch");
    latch.trySetCount(1);
    Assertions.assertTrue(latch.delete());
}
Also used : RCountDownLatch(org.redisson.api.RCountDownLatch) Test(org.junit.jupiter.api.Test)

Example 10 with RCountDownLatch

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

the class RedissonCountDownLatchTest method testDeleteFailed.

@Test
public void testDeleteFailed() throws Exception {
    RCountDownLatch latch = redisson.getCountDownLatch("latch");
    Assertions.assertFalse(latch.delete());
}
Also used : RCountDownLatch(org.redisson.api.RCountDownLatch) Test(org.junit.jupiter.api.Test)

Aggregations

RCountDownLatch (org.redisson.api.RCountDownLatch)10 Test (org.junit.jupiter.api.Test)7 ExecutorService (java.util.concurrent.ExecutorService)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 RedissonClient (org.redisson.api.RedissonClient)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Test (org.junit.Test)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 MethodSource (org.junit.jupiter.params.provider.MethodSource)1 Config (org.redisson.config.Config)1