Search in sources :

Example 21 with RReadWriteLock

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

the class RedissonReadWriteLockTest method testUnlockFail.

@Test
public void testUnlockFail() {
    Assertions.assertThrows(IllegalMonitorStateException.class, () -> {
        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.forceUnlock();
        }
    });
}
Also used : RLock(org.redisson.api.RLock) RReadWriteLock(org.redisson.api.RReadWriteLock) Test(org.junit.jupiter.api.Test)

Example 22 with RReadWriteLock

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

Example 23 with RReadWriteLock

use of org.redisson.api.RReadWriteLock 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();
    });
    Assertions.assertEquals(iterations, lockedCounter.get());
}
Also used : Random(java.util.Random) SecureRandom(java.security.SecureRandom) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SecureRandom(java.security.SecureRandom) RLock(org.redisson.api.RLock) RReadWriteLock(org.redisson.api.RReadWriteLock) Test(org.junit.jupiter.api.Test)

Example 24 with RReadWriteLock

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

the class RedissonReadWriteLockTest method testAutoExpire.

@Test
public void testAutoExpire() throws InterruptedException {
    testSingleInstanceConcurrency(1, r -> {
        RReadWriteLock lock1 = r.getReadWriteLock("lock");
        lock1.writeLock().lock();
    });
    RReadWriteLock lock1 = redisson.getReadWriteLock("lock");
    await().atMost(redisson.getConfig().getLockWatchdogTimeout() + 1000, TimeUnit.MILLISECONDS).until(() -> !lock1.writeLock().isLocked());
}
Also used : RReadWriteLock(org.redisson.api.RReadWriteLock) Test(org.junit.jupiter.api.Test)

Example 25 with RReadWriteLock

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

the class RedissonReadWriteLockTest method testReadLockExpirationRenewal.

@Test
public void testReadLockExpirationRenewal() throws InterruptedException {
    int threadCount = 50;
    ExecutorService executorService = Executors.newFixedThreadPool(threadCount / 5);
    AtomicInteger exceptions = new AtomicInteger();
    for (int i = 0; i < threadCount; i++) {
        executorService.submit(() -> {
            try {
                RReadWriteLock rw1 = redisson.getReadWriteLock("mytestlock");
                RLock readLock = rw1.readLock();
                readLock.lock();
                try {
                    Thread.sleep(redisson.getConfig().getLockWatchdogTimeout() + 5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                readLock.unlock();
            } catch (Exception e) {
                exceptions.incrementAndGet();
                e.printStackTrace();
            }
        });
    }
    executorService.shutdown();
    assertThat(executorService.awaitTermination(180, TimeUnit.SECONDS)).isTrue();
    assertThat(exceptions.get()).isZero();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) 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)

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