Search in sources :

Example 26 with RLock

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

the class RedissonReadWriteLockTest method testIsLocked.

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

Example 27 with RLock

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

the class RedissonReadWriteLockTest method testWriteLockExpiration.

@Test
public void testWriteLockExpiration() throws InterruptedException {
    RReadWriteLock rw1 = redisson.getReadWriteLock("test2s3");
    RLock l1 = rw1.writeLock();
    assertThat(l1.tryLock(10000, 10000, TimeUnit.MILLISECONDS)).isTrue();
    RLock l2 = rw1.writeLock();
    assertThat(l2.tryLock(1000, 1000, TimeUnit.MILLISECONDS)).isTrue();
    await().atMost(Duration.ofSeconds(10)).until(() -> {
        RReadWriteLock rw2 = redisson.getReadWriteLock("test2s3");
        try {
            return !rw2.writeLock().tryLock(3000, 1000, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
            return false;
        }
    });
}
Also used : RLock(org.redisson.api.RLock) RReadWriteLock(org.redisson.api.RReadWriteLock) Test(org.junit.jupiter.api.Test)

Example 28 with RLock

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

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

the class RedissonRedLockTest method testLockSuccess2.

@Test
public void testLockSuccess2() throws IOException, InterruptedException {
    RedisProcess redis1 = redisTestMultilockInstance();
    RedisProcess redis2 = redisTestMultilockInstance();
    RedissonClient client1 = createClient(redis1.getRedisServerAddressAndPort());
    RedissonClient client2 = createClient(redis2.getRedisServerAddressAndPort());
    RLock lock1 = client1.getLock("lock1");
    RLock lock2 = client1.getLock("lock2");
    RLock lock3 = client2.getLock("lock3");
    Thread t1 = new Thread() {

        public void run() {
            lock2.lock();
        }
    };
    t1.start();
    t1.join();
    RedissonMultiLock lock = new RedissonRedLock(lock1, lock2, lock3);
    assertThat(lock.tryLock(500, 5000, TimeUnit.MILLISECONDS)).isTrue();
    Thread.sleep(3000);
    lock.unlock();
    client1.shutdown();
    client2.shutdown();
    assertThat(redis1.stop()).isEqualTo(0);
    assertThat(redis2.stop()).isEqualTo(0);
}
Also used : RedissonClient(org.redisson.api.RedissonClient) RedisProcess(org.redisson.RedisRunner.RedisProcess) RLock(org.redisson.api.RLock) Test(org.junit.jupiter.api.Test)

Example 30 with RLock

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

the class RedissonRedLockTest method testConnectionFailed.

@Test
public void testConnectionFailed() throws IOException, InterruptedException {
    RedisProcess redis1 = redisTestMultilockInstance();
    RedisProcess redis2 = redisTestMultilockInstance();
    RedissonClient client1 = createClient(redis1.getRedisServerAddressAndPort());
    RedissonClient client2 = createClient(redis2.getRedisServerAddressAndPort());
    RLock lock1 = client1.getLock("lock1");
    RLock lock2 = client1.getLock("lock2");
    assertThat(redis2.stop()).isEqualTo(0);
    RLock lock3 = client2.getLock("lock3");
    Thread t = new Thread() {

        public void run() {
            RedissonMultiLock lock = new RedissonRedLock(lock1, lock2, lock3);
            lock.lock();
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
            }
            lock.unlock();
        }
    };
    t.start();
    t.join(1000);
    RedissonMultiLock lock = new RedissonRedLock(lock1, lock2, lock3);
    lock.lock();
    lock.unlock();
    client1.shutdown();
    client2.shutdown();
    assertThat(redis1.stop()).isEqualTo(0);
}
Also used : RedissonClient(org.redisson.api.RedissonClient) RedisProcess(org.redisson.RedisRunner.RedisProcess) RLock(org.redisson.api.RLock) Test(org.junit.jupiter.api.Test)

Aggregations

RLock (org.redisson.api.RLock)111 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