Search in sources :

Example 96 with RLock

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

the class RedissonReadWriteLockTest method testConcurrency_MultiInstance.

@Test
public void testConcurrency_MultiInstance() throws InterruptedException {
    int iterations = 100;
    final AtomicInteger lockedCounter = new AtomicInteger();
    final Random r = new SecureRandom();
    testMultiInstanceConcurrency(iterations, rc -> {
        RReadWriteLock rwlock = rc.getReadWriteLock("testConcurrency_MultiInstance2");
        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 97 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();
    Assertions.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.jupiter.api.Test)

Example 98 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();
    Assertions.assertTrue(readLock.tryLock(1, 4, TimeUnit.SECONDS));
    Thread.sleep(3000);
    RLock readLock2 = redisson.getReadWriteLock("my_read_write_lock").readLock();
    Assertions.assertTrue(readLock2.tryLock(1, 4, TimeUnit.SECONDS));
    readLock2.unlock();
    Thread.sleep(2000);
    RLock writeLock = redisson.getReadWriteLock("my_read_write_lock").writeLock();
    Assertions.assertTrue(writeLock.tryLock());
}
Also used : RLock(org.redisson.api.RLock) Test(org.junit.jupiter.api.Test)

Example 99 with RLock

use of org.redisson.api.RLock 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 100 with RLock

use of org.redisson.api.RLock 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)

Aggregations

RLock (org.redisson.api.RLock)113 Test (org.junit.jupiter.api.Test)77 RedissonClient (org.redisson.api.RedissonClient)21 RReadWriteLock (org.redisson.api.RReadWriteLock)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)13 RedisProcess (org.redisson.RedisRunner.RedisProcess)12 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)11 CacheLoaderException (javax.cache.integration.CacheLoaderException)10 CacheWriterException (javax.cache.integration.CacheWriterException)10 EntryProcessorException (javax.cache.processor.EntryProcessorException)10 Config (org.redisson.config.Config)10 ExecutorService (java.util.concurrent.ExecutorService)8 Test (org.junit.Test)6 RedissonObject (org.redisson.RedissonObject)4 RSemaphore (org.redisson.api.RSemaphore)4 SecureRandom (java.security.SecureRandom)3 Date (java.util.Date)3 Random (java.util.Random)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Lock (java.util.concurrent.locks.Lock)3