Search in sources :

Example 16 with RReadWriteLock

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

the class RedissonReadWriteLockTest method testForceUnlock.

@Test
public void testForceUnlock() {
    RReadWriteLock lock = redisson.getReadWriteLock("lock");
    RLock readLock = lock.readLock();
    readLock.lock();
    assertThat(readLock.isLocked()).isTrue();
    lock.writeLock().forceUnlock();
    assertThat(readLock.isLocked()).isTrue();
    lock.readLock().forceUnlock();
    assertThat(readLock.isLocked()).isFalse();
    RLock writeLock = lock.writeLock();
    writeLock.lock();
    assertThat(writeLock.isLocked()).isTrue();
    lock.readLock().forceUnlock();
    assertThat(writeLock.isLocked()).isTrue();
    lock.writeLock().forceUnlock();
    assertThat(writeLock.isLocked()).isFalse();
    lock = redisson.getReadWriteLock("lock");
    assertThat(lock.readLock().isLocked()).isFalse();
    assertThat(lock.writeLock().isLocked()).isFalse();
}
Also used : RLock(org.redisson.api.RLock) RReadWriteLock(org.redisson.api.RReadWriteLock) Test(org.junit.jupiter.api.Test)

Example 17 with RReadWriteLock

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

the class RedissonReadWriteLockTest method testReadLockExpiration.

@Test
public void testReadLockExpiration() throws Exception {
    Thread thread1 = new Thread(() -> {
        RReadWriteLock rReadWriteLock = redisson.getReadWriteLock("test");
        RLock readLock = rReadWriteLock.readLock();
        readLock.lock(10, TimeUnit.SECONDS);
        try {
            Thread.sleep(9100);
        } catch (Exception e) {
        }
        readLock.unlock();
    });
    Thread thread2 = new Thread(() -> {
        RReadWriteLock rReadWriteLock = redisson.getReadWriteLock("test");
        RLock readLock = rReadWriteLock.readLock();
        readLock.lock(3, TimeUnit.SECONDS);
        try {
            Thread.sleep(2800);
        } catch (Exception e) {
        }
        readLock.unlock();
    });
    AtomicBoolean flag = new AtomicBoolean();
    Thread thread3 = new Thread(() -> {
        RReadWriteLock rReadWriteLock = redisson.getReadWriteLock("test");
        RLock writeLock = rReadWriteLock.writeLock();
        writeLock.lock(10, TimeUnit.SECONDS);
        flag.set(true);
        writeLock.unlock();
    });
    thread1.start();
    thread1.join(300);
    thread2.start();
    thread2.join(300);
    thread3.start();
    thread3.join(300);
    Awaitility.await().between(8, TimeUnit.SECONDS, 10, TimeUnit.SECONDS).untilTrue(flag);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RLock(org.redisson.api.RLock) RReadWriteLock(org.redisson.api.RReadWriteLock) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test)

Example 18 with RReadWriteLock

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

the class RedissonReadWriteLockTest method testMultiRead.

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

        public void run() {
            RLock r = lock.readLock();
            Assertions.assertFalse(readLock1.isHeldByCurrentThread());
            Assertions.assertTrue(readLock1.isLocked());
            r.lock();
            readLock2.set(r);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            r.unlock();
        }
    };
    t.start();
    t.join(50);
    Assertions.assertTrue(readLock2.get().isLocked());
    readLock1.unlock();
    Assertions.assertFalse(lock.writeLock().tryLock());
    Assertions.assertFalse(readLock1.isHeldByCurrentThread());
    Thread.sleep(1000);
    Assertions.assertFalse(readLock2.get().isLocked());
    Assertions.assertTrue(lock.writeLock().tryLock());
    Assertions.assertTrue(lock.writeLock().isLocked());
    Assertions.assertTrue(lock.writeLock().isHeldByCurrentThread());
    lock.writeLock().unlock();
    Assertions.assertFalse(lock.writeLock().isLocked());
    Assertions.assertFalse(lock.writeLock().isHeldByCurrentThread());
    Assertions.assertTrue(lock.writeLock().tryLock());
    lock.writeLock().forceUnlock();
}
Also used : RLock(org.redisson.api.RLock) AtomicReference(java.util.concurrent.atomic.AtomicReference) RReadWriteLock(org.redisson.api.RReadWriteLock) Test(org.junit.jupiter.api.Test)

Example 19 with RReadWriteLock

use of org.redisson.api.RReadWriteLock 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();
    Assertions.assertTrue(lock.tryLock());
    Assertions.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();
            Assertions.assertTrue(lock.tryLock());
        }
    };
    thread1.start();
    thread1.join();
    lock.unlock();
}
Also used : RLock(org.redisson.api.RLock) RReadWriteLock(org.redisson.api.RReadWriteLock) Test(org.junit.jupiter.api.Test)

Example 20 with RReadWriteLock

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

the class RedissonReadWriteLockTest method testIsHeldByCurrentThread.

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

Aggregations

RReadWriteLock (org.redisson.api.RReadWriteLock)26 Test (org.junit.jupiter.api.Test)25 RLock (org.redisson.api.RLock)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 SecureRandom (java.security.SecureRandom)3 Random (java.util.Random)3 ExecutionException (java.util.concurrent.ExecutionException)2 ExecutorService (java.util.concurrent.ExecutorService)2 TimeoutException (java.util.concurrent.TimeoutException)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 ArrayList (java.util.ArrayList)1 Callable (java.util.concurrent.Callable)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Future (java.util.concurrent.Future)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Test (org.junit.Test)1 ClusterProcesses (org.redisson.ClusterRunner.ClusterProcesses)1 RedissonClient (org.redisson.api.RedissonClient)1 Config (org.redisson.config.Config)1