Search in sources :

Example 66 with RLock

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

the class RedissonReadWriteLockTest method testMultiRead.

@Test
public void testMultiRead() throws InterruptedException {
    final RReadWriteLock lock = redisson.getReadWriteLock("lock");
    Assert.assertFalse(lock.delete());
    final RLock readLock1 = lock.readLock();
    readLock1.lock();
    Assert.assertFalse(lock.writeLock().tryLock());
    final AtomicReference<RLock> readLock2 = new AtomicReference<RLock>();
    Thread t = new Thread() {

        public void run() {
            RLock r = lock.readLock();
            Assert.assertFalse(readLock1.isHeldByCurrentThread());
            Assert.assertTrue(readLock1.isLocked());
            r.lock();
            readLock2.set(r);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            r.unlock();
        }

        ;
    };
    t.start();
    t.join(50);
    Assert.assertTrue(readLock2.get().isLocked());
    readLock1.unlock();
    Assert.assertFalse(lock.writeLock().tryLock());
    Assert.assertFalse(readLock1.isHeldByCurrentThread());
    Thread.sleep(1000);
    Assert.assertFalse(readLock2.get().isLocked());
    Assert.assertTrue(lock.writeLock().tryLock());
    Assert.assertTrue(lock.writeLock().isLocked());
    Assert.assertTrue(lock.writeLock().isHeldByCurrentThread());
    lock.writeLock().unlock();
    Assert.assertFalse(lock.writeLock().isLocked());
    Assert.assertFalse(lock.writeLock().isHeldByCurrentThread());
    Assert.assertTrue(lock.writeLock().tryLock());
    lock.delete();
}
Also used : RLock(org.redisson.api.RLock) AtomicReference(java.util.concurrent.atomic.AtomicReference) RReadWriteLock(org.redisson.api.RReadWriteLock) Test(org.junit.Test)

Example 67 with RLock

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

the class RedissonReadWriteLockTest method testReadLockLeaseTimeout.

@Test
public void testReadLockLeaseTimeout() throws InterruptedException {
    RLock readLock = redisson.getReadWriteLock("my_read_write_lock").readLock();
    Assert.assertTrue(readLock.tryLock(1, 4, TimeUnit.SECONDS));
    Thread.sleep(3000);
    RLock readLock2 = redisson.getReadWriteLock("my_read_write_lock").readLock();
    Assert.assertTrue(readLock2.tryLock(1, 4, TimeUnit.SECONDS));
    readLock2.unlock();
    Thread.sleep(2000);
    RLock writeLock = redisson.getReadWriteLock("my_read_write_lock").writeLock();
    Assert.assertTrue(writeLock.tryLock());
}
Also used : RLock(org.redisson.api.RLock) Test(org.junit.Test)

Example 68 with RLock

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

the class RedissonReadWriteLockTest method testReadLockLeaseTimeoutDiffThreadsWRR.

@Test
public void testReadLockLeaseTimeoutDiffThreadsWRR() throws InterruptedException {
    RLock writeLock = redisson.getReadWriteLock("my_read_write_lock").writeLock();
    Assert.assertTrue(writeLock.tryLock(1, 10, TimeUnit.SECONDS));
    final AtomicInteger executed = new AtomicInteger();
    Thread t1 = new Thread(() -> {
        RLock readLock = redisson.getReadWriteLock("my_read_write_lock").readLock();
        readLock.lock();
        executed.incrementAndGet();
    });
    Thread t2 = new Thread(() -> {
        RLock readLock = redisson.getReadWriteLock("my_read_write_lock").readLock();
        readLock.lock();
        executed.incrementAndGet();
    });
    t1.start();
    t2.start();
    await().atMost(11, TimeUnit.SECONDS).until(() -> executed.get() == 2);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RLock(org.redisson.api.RLock) Test(org.junit.Test)

Example 69 with RLock

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

the class RedissonReadWriteLockTest method testIsHeldByCurrentThreadOtherThread.

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

        public void run() {
            RReadWriteLock rwlock = redisson.getReadWriteLock("lock");
            RLock lock = rwlock.readLock();
            Assert.assertFalse(lock.isHeldByCurrentThread());
        }

        ;
    };
    t.start();
    t.join();
    lock.unlock();
    Thread t2 = new Thread() {

        public void run() {
            RReadWriteLock rwlock = redisson.getReadWriteLock("lock");
            RLock lock = rwlock.readLock();
            Assert.assertFalse(lock.isHeldByCurrentThread());
        }

        ;
    };
    t2.start();
    t2.join();
}
Also used : RLock(org.redisson.api.RLock) RReadWriteLock(org.redisson.api.RReadWriteLock) Test(org.junit.Test)

Aggregations

RLock (org.redisson.api.RLock)69 Test (org.junit.Test)51 RReadWriteLock (org.redisson.api.RReadWriteLock)13 CacheLoaderException (javax.cache.integration.CacheLoaderException)10 CacheWriterException (javax.cache.integration.CacheWriterException)10 EntryProcessorException (javax.cache.processor.EntryProcessorException)10 RedissonClient (org.redisson.api.RedissonClient)10 RedisProcess (org.redisson.RedisRunner.RedisProcess)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 RedissonObject (org.redisson.RedissonObject)4 SecureRandom (java.security.SecureRandom)3 Random (java.util.Random)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)2 HashMap (java.util.HashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ExecutorService (java.util.concurrent.ExecutorService)2 Lock (java.util.concurrent.locks.Lock)2 RSemaphore (org.redisson.api.RSemaphore)2