Search in sources :

Example 71 with RLock

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

the class RedissonSpinLockTest method testTryLockAsyncFailed.

@Test
public void testTryLockAsyncFailed() throws InterruptedException {
    RLock lock = redisson.getSpinLock("lock");
    lock.lock();
    AtomicBoolean lockAsyncSucceed = new AtomicBoolean();
    Thread thread = new Thread(() -> {
        RFuture<Boolean> booleanRFuture = lock.tryLockAsync();
        booleanRFuture.onComplete((res, e) -> {
            if (e != null) {
                Assertions.fail("Lock aquire failed for some reason");
            }
            lockAsyncSucceed.set(res);
        });
    });
    thread.start();
    Thread.sleep(1_000);
    assertThat(lockAsyncSucceed.get()).isFalse();
    lock.unlock();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RLock(org.redisson.api.RLock) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.jupiter.api.Test)

Example 72 with RLock

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

the class RedissonSpinLockTest method testIsLockedOtherThread.

@Test
public void testIsLockedOtherThread() throws InterruptedException {
    RLock lock = redisson.getSpinLock("lock");
    lock.lock();
    Thread t = new Thread() {

        public void run() {
            RLock lock = redisson.getSpinLock("lock");
            Assertions.assertTrue(lock.isLocked());
        }
    };
    t.start();
    t.join();
    lock.unlock();
    Thread t2 = new Thread() {

        public void run() {
            RLock lock = redisson.getSpinLock("lock");
            Assertions.assertFalse(lock.isLocked());
        }
    };
    t2.start();
    t2.join();
}
Also used : RLock(org.redisson.api.RLock) Test(org.junit.jupiter.api.Test)

Example 73 with RLock

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

the class RedissonTwoLockedThread method testLock.

@ParameterizedTest
@MethodSource("data")
public void testLock(Codec codec) throws InterruptedException {
    Config config = BaseTest.createConfig();
    config.setCodec(codec);
    RedissonClient redisson = Redisson.create(config);
    Assertions.assertTimeout(Duration.ofSeconds(3), () -> {
        final String lockName = "lock1";
        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();
                    RLock lock = redisson.getLock(lockName);
                    System.out.println("1. getlock " + lock.getName() + " - " + Thread.currentThread().getId());
                    lock.lock();
                    System.out.println("1. lock " + lock.getName() + " - " + Thread.currentThread().getId());
                    testSignal.countDown();
                    Thread.sleep(500);
                    lock.unlock();
                    System.out.println("1. unlock " + lock.getName() + " - " + Thread.currentThread().getId());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                completeSignal.countDown();
            }
        }.start();
        new Thread() {

            @Override
            public void run() {
                try {
                    testSignal.await();
                    RLock lock = redisson.getLock(lockName);
                    System.out.println("2. getlock " + lock.getName() + " - " + Thread.currentThread().getId());
                    lock.lock();
                    System.out.println("2. lock " + lock.getName() + " - " + Thread.currentThread().getId());
                    long current = System.currentTimeMillis();
                    Assertions.assertTrue(current - millis >= 500, "current=" + current + ", millis=" + millis);
                    Thread.sleep(500);
                    lock.unlock();
                    System.out.println("2. unlock " + lock.getName() + " - " + Thread.currentThread().getId());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                completeSignal.countDown();
            }
        }.start();
        System.out.println("start");
        startSignal.countDown();
        completeSignal.await();
        System.out.println("complete");
    });
    redisson.shutdown();
}
Also used : RedissonClient(org.redisson.api.RedissonClient) Config(org.redisson.config.Config) RLock(org.redisson.api.RLock) CountDownLatch(java.util.concurrent.CountDownLatch) RCountDownLatch(org.redisson.api.RCountDownLatch) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 74 with RLock

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

the class RedissonLockTest method testGetHoldCount.

@Test
public void testGetHoldCount() {
    RLock lock = redisson.getLock("lock");
    Assertions.assertEquals(0, lock.getHoldCount());
    lock.lock();
    Assertions.assertEquals(1, lock.getHoldCount());
    lock.unlock();
    Assertions.assertEquals(0, lock.getHoldCount());
    lock.lock();
    lock.lock();
    Assertions.assertEquals(2, lock.getHoldCount());
    lock.unlock();
    Assertions.assertEquals(1, lock.getHoldCount());
    lock.unlock();
    Assertions.assertEquals(0, lock.getHoldCount());
}
Also used : RLock(org.redisson.api.RLock) Test(org.junit.jupiter.api.Test)

Example 75 with RLock

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

the class RedissonLockTest method testUnlockFail.

@Test
public void testUnlockFail() {
    Assertions.assertThrows(IllegalMonitorStateException.class, () -> {
        RLock lock = redisson.getLock("lock");
        Thread t = new Thread() {

            public void run() {
                RLock lock = redisson.getLock("lock");
                lock.lock();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                lock.unlock();
            }
        };
        t.start();
        t.join(400);
        try {
            lock.unlock();
        } catch (IllegalMonitorStateException e) {
            t.join();
            throw e;
        }
    });
}
Also used : RLock(org.redisson.api.RLock) Test(org.junit.jupiter.api.Test)

Aggregations

RLock (org.redisson.api.RLock)113 Test (org.junit.jupiter.api.Test)77 RedissonClient (org.redisson.api.RedissonClient)21 RReadWriteLock (org.redisson.api.RReadWriteLock)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)13 RedisProcess (org.redisson.RedisRunner.RedisProcess)12 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)11 CacheLoaderException (javax.cache.integration.CacheLoaderException)10 CacheWriterException (javax.cache.integration.CacheWriterException)10 EntryProcessorException (javax.cache.processor.EntryProcessorException)10 Config (org.redisson.config.Config)10 ExecutorService (java.util.concurrent.ExecutorService)8 Test (org.junit.Test)6 RedissonObject (org.redisson.RedissonObject)4 RSemaphore (org.redisson.api.RSemaphore)4 SecureRandom (java.security.SecureRandom)3 Date (java.util.Date)3 Random (java.util.Random)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Lock (java.util.concurrent.locks.Lock)3