Search in sources :

Example 6 with RReadWriteLock

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

the class RedissonReadWriteLockTest method testWriteLock.

@Test
public void testWriteLock() throws InterruptedException {
    final RReadWriteLock lock = redisson.getReadWriteLock("lock");
    final RLock writeLock = lock.writeLock();
    writeLock.lock();
    Assertions.assertTrue(lock.writeLock().tryLock());
    Thread t = new Thread() {

        public void run() {
            Assertions.assertFalse(writeLock.isHeldByCurrentThread());
            Assertions.assertTrue(writeLock.isLocked());
            Assertions.assertFalse(lock.readLock().tryLock());
            Assertions.assertFalse(redisson.getReadWriteLock("lock").readLock().tryLock());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Assertions.assertTrue(lock.readLock().tryLock());
            Assertions.assertTrue(redisson.getReadWriteLock("lock").readLock().tryLock());
        }
    };
    t.start();
    t.join(50);
    writeLock.unlock();
    Assertions.assertTrue(lock.readLock().tryLock());
    Assertions.assertTrue(writeLock.isHeldByCurrentThread());
    writeLock.unlock();
    Thread.sleep(1000);
    Assertions.assertFalse(lock.writeLock().tryLock());
    Assertions.assertFalse(lock.writeLock().isLocked());
    Assertions.assertFalse(lock.writeLock().isHeldByCurrentThread());
    lock.writeLock().forceUnlock();
}
Also used : RLock(org.redisson.api.RLock) RReadWriteLock(org.redisson.api.RReadWriteLock) Test(org.junit.jupiter.api.Test)

Example 7 with RReadWriteLock

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

the class RedissonReadWriteLockTest method testIsLockedOtherThread.

@Test
public void testIsLockedOtherThread() 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();
            Assertions.assertTrue(lock.isLocked());
        }
    };
    t.start();
    t.join();
    lock.unlock();
    Thread t2 = new Thread() {

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

Example 8 with RReadWriteLock

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

the class RedissonReadWriteLockTest method testWriteReadReentrancy.

@Test
public void testWriteReadReentrancy() throws InterruptedException {
    RReadWriteLock readWriteLock = redisson.getReadWriteLock("TEST");
    readWriteLock.writeLock().lock();
    java.util.concurrent.locks.Lock rLock = readWriteLock.readLock();
    Assertions.assertTrue(rLock.tryLock());
    AtomicBoolean ref = new AtomicBoolean();
    Thread t1 = new Thread(() -> {
        boolean success = readWriteLock.readLock().tryLock();
        ref.set(success);
    });
    t1.start();
    t1.join();
    Assertions.assertFalse(ref.get());
    readWriteLock.writeLock().unlock();
    Assertions.assertFalse(readWriteLock.writeLock().tryLock());
    rLock.unlock();
    Assertions.assertTrue(readWriteLock.writeLock().tryLock());
    readWriteLock.writeLock().unlock();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RReadWriteLock(org.redisson.api.RReadWriteLock) Test(org.junit.jupiter.api.Test)

Example 9 with RReadWriteLock

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

the class RedissonReadWriteLockTest method testName.

@Test
public void testName() throws InterruptedException, ExecutionException, TimeoutException {
    ExecutorService service = Executors.newFixedThreadPool(10);
    RReadWriteLock rwlock = redisson.getReadWriteLock("{test}:abc:key");
    RLock rlock = rwlock.readLock();
    List<Callable<Void>> callables = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        callables.add(() -> {
            for (int j = 0; j < 10; j++) {
                rlock.lock();
                try {
                } finally {
                    rlock.unlock();
                }
            }
            return null;
        });
    }
    List<Future<Void>> futures = service.invokeAll(callables);
    for (Future<Void> future : futures) {
        assertThatCode(future::get).doesNotThrowAnyException();
    }
    service.shutdown();
    assertThat(service.awaitTermination(1, TimeUnit.MINUTES)).isTrue();
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) RLock(org.redisson.api.RLock) RReadWriteLock(org.redisson.api.RReadWriteLock) Callable(java.util.concurrent.Callable) Test(org.junit.jupiter.api.Test)

Example 10 with RReadWriteLock

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

the class RedissonReadWriteLockTest method testLockUnlock.

@Test
public void testLockUnlock() {
    RReadWriteLock rwlock = redisson.getReadWriteLock("lock");
    RLock lock = rwlock.readLock();
    lock.lock();
    lock.unlock();
    lock.lock();
    lock.unlock();
}
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