Search in sources :

Example 56 with RLock

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

the class RedissonReadWriteLockTest method testReadLockLeaseTimeoutDiffThreadsRRW.

@Test
public void testReadLockLeaseTimeoutDiffThreadsRRW() throws InterruptedException {
    new Thread(() -> {
        RLock readLock = redisson.getReadWriteLock("my_read_write_lock").readLock();
        try {
            Assert.assertTrue(readLock.tryLock(1, 10, TimeUnit.SECONDS));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }).start();
    Thread.sleep(5000);
    new Thread(() -> {
        RLock readLock2 = redisson.getReadWriteLock("my_read_write_lock").readLock();
        try {
            Assert.assertTrue(readLock2.tryLock(1, 10, TimeUnit.SECONDS));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        readLock2.unlock();
    }).start();
    final AtomicBoolean executed = new AtomicBoolean();
    new Thread(() -> {
        RLock writeLock = redisson.getReadWriteLock("my_read_write_lock").writeLock();
        try {
            boolean locked = writeLock.tryLock(10, 10, TimeUnit.SECONDS);
            executed.set(locked);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }).start();
    await().atMost(6, TimeUnit.SECONDS).untilTrue(executed);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RLock(org.redisson.api.RLock) Test(org.junit.Test)

Example 57 with RLock

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

the class RedissonReadWriteLockTest method testReentrancy.

@Test
public void testReentrancy() throws InterruptedException {
    RReadWriteLock rwlock = redisson.getReadWriteLock("lock");
    RLock lock = rwlock.readLock();
    Assert.assertTrue(lock.tryLock());
    Assert.assertTrue(lock.tryLock());
    lock.unlock();
    // next row  for test renew expiration tisk.
    //Thread.currentThread().sleep(TimeUnit.SECONDS.toMillis(RedissonLock.LOCK_EXPIRATION_INTERVAL_SECONDS*2));
    Thread thread1 = new Thread() {

        @Override
        public void run() {
            RReadWriteLock rwlock = redisson.getReadWriteLock("lock1");
            RLock lock = rwlock.readLock();
            Assert.assertTrue(lock.tryLock());
        }
    };
    thread1.start();
    thread1.join();
    lock.unlock();
}
Also used : RLock(org.redisson.api.RLock) RReadWriteLock(org.redisson.api.RReadWriteLock) Test(org.junit.Test)

Example 58 with RLock

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

the class RedissonReadWriteLockTest method testUnlockFail.

@Test(expected = IllegalMonitorStateException.class)
public void testUnlockFail() throws InterruptedException {
    RReadWriteLock rwlock = redisson.getReadWriteLock("lock");
    Thread t = new Thread() {

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

        ;
    };
    t.start();
    t.join();
    RLock lock = rwlock.readLock();
    try {
        lock.unlock();
    } finally {
        // clear scheduler
        lock.delete();
    }
}
Also used : RLock(org.redisson.api.RLock) RReadWriteLock(org.redisson.api.RReadWriteLock) Test(org.junit.Test)

Example 59 with RLock

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

the class RedissonReadWriteLockTest method testIsHeldByCurrentThread.

@Test
public void testIsHeldByCurrentThread() {
    RReadWriteLock rwlock = redisson.getReadWriteLock("lock");
    RLock lock = rwlock.readLock();
    Assert.assertFalse(lock.isHeldByCurrentThread());
    lock.lock();
    Assert.assertTrue(lock.isHeldByCurrentThread());
    lock.unlock();
    Assert.assertFalse(lock.isHeldByCurrentThread());
}
Also used : RLock(org.redisson.api.RLock) RReadWriteLock(org.redisson.api.RReadWriteLock) Test(org.junit.Test)

Example 60 with RLock

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

the class RedissonReadWriteLockTest method testConcurrency_SingleInstance.

@Test
public void testConcurrency_SingleInstance() throws InterruptedException {
    final AtomicInteger lockedCounter = new AtomicInteger();
    final Random r = new SecureRandom();
    int iterations = 15;
    testSingleInstanceConcurrency(iterations, rc -> {
        RReadWriteLock rwlock = rc.getReadWriteLock("testConcurrency_SingleInstance");
        RLock lock;
        if (r.nextBoolean()) {
            lock = rwlock.writeLock();
        } else {
            lock = rwlock.readLock();
        }
        lock.lock();
        lockedCounter.incrementAndGet();
        lock.unlock();
    });
    Assert.assertEquals(iterations, lockedCounter.get());
}
Also used : SecureRandom(java.security.SecureRandom) Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SecureRandom(java.security.SecureRandom) 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