Search in sources :

Example 86 with RLock

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

the class RedissonLockTest method testAutoExpire.

@Test
public void testAutoExpire() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    RedissonClient r = createInstance();
    Thread t = new Thread() {

        @Override
        public void run() {
            RLock lock = r.getLock("lock");
            lock.lock();
            latch.countDown();
            try {
                Thread.sleep(15000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };
    t.start();
    Assertions.assertTrue(latch.await(1, TimeUnit.SECONDS));
    RLock lock = redisson.getLock("lock");
    t.join();
    r.shutdown();
    await().atMost(redisson.getConfig().getLockWatchdogTimeout(), TimeUnit.MILLISECONDS).until(() -> !lock.isLocked());
}
Also used : RedissonClient(org.redisson.api.RedissonClient) RLock(org.redisson.api.RLock) Test(org.junit.jupiter.api.Test)

Example 87 with RLock

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

the class RedissonLockTest method testIsHeldByCurrentThreadOtherThread.

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

        public void run() {
            RLock lock = redisson.getLock("lock");
            Assertions.assertFalse(lock.isHeldByCurrentThread());
        }
    };
    t.start();
    t.join();
    lock.unlock();
    Thread t2 = new Thread() {

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

Example 88 with RLock

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

the class RedissonLockTest method testIsLocked.

@Test
public void testIsLocked() {
    RLock lock = redisson.getLock("lock");
    Assertions.assertFalse(lock.isLocked());
    lock.lock();
    Assertions.assertTrue(lock.isLocked());
    lock.unlock();
    Assertions.assertFalse(lock.isLocked());
}
Also used : RLock(org.redisson.api.RLock) Test(org.junit.jupiter.api.Test)

Example 89 with RLock

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

the class RedissonFairLockTest method testAutoExpire.

@Test
public void testAutoExpire() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    testSingleInstanceConcurrency(1, r -> {
        RLock lock = r.getFairLock("lock");
        lock.lock();
        latch.countDown();
    });
    Assertions.assertTrue(latch.await(1, TimeUnit.SECONDS));
    RLock lock = redisson.getFairLock("lock");
    await().atMost(redisson.getConfig().getLockWatchdogTimeout() + 1000, TimeUnit.MILLISECONDS).until(() -> !lock.isLocked());
}
Also used : RLock(org.redisson.api.RLock) Test(org.junit.jupiter.api.Test)

Example 90 with RLock

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

the class RedissonFairLockTest method testIsHeldByCurrentThreadOtherThread.

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

        public void run() {
            RLock lock = redisson.getFairLock("lock");
            Assertions.assertFalse(lock.isHeldByCurrentThread());
        }
    };
    t.start();
    t.join();
    lock.unlock();
    Thread t2 = new Thread() {

        public void run() {
            RLock lock = redisson.getFairLock("lock");
            Assertions.assertFalse(lock.isHeldByCurrentThread());
        }
    };
    t2.start();
    t2.join();
}
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