Search in sources :

Example 1 with RCountDownLatch

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();
}
Also used : RCountDownLatch(org.redisson.api.RCountDownLatch) RedissonClient(org.redisson.api.RedissonClient) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService)

Example 2 with RCountDownLatch

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));
}
Also used : RCountDownLatch(org.redisson.api.RCountDownLatch) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.jupiter.api.Test)

Example 3 with RCountDownLatch

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);
}
Also used : RCountDownLatch(org.redisson.api.RCountDownLatch) Test(org.junit.jupiter.api.Test)

Example 4 with RCountDownLatch

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));
}
Also used : RCountDownLatch(org.redisson.api.RCountDownLatch) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.jupiter.api.Test)

Example 5 with RCountDownLatch

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");
    });
}
Also used : RCountDownLatch(org.redisson.api.RCountDownLatch) RedissonClient(org.redisson.api.RedissonClient) Config(org.redisson.config.Config) CountDownLatch(java.util.concurrent.CountDownLatch) RCountDownLatch(org.redisson.api.RCountDownLatch) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

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