Search in sources :

Example 1 with RReadWriteLock

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

the class RedissonReadWriteLockTest method testDelete.

@Test
public void testDelete() {
    RReadWriteLock lock = redisson.getReadWriteLock("lock");
    Assert.assertFalse(lock.delete());
    lock.readLock().lock();
    Assert.assertTrue(lock.delete());
}
Also used : RReadWriteLock(org.redisson.api.RReadWriteLock) Test(org.junit.Test)

Example 2 with RReadWriteLock

use of org.redisson.api.RReadWriteLock 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 3 with RReadWriteLock

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

the class RedissonReadWriteLockTest method testExpireRead.

@Test
public void testExpireRead() throws InterruptedException {
    RReadWriteLock lock = redisson.getReadWriteLock("lock");
    lock.readLock().lock(2, TimeUnit.SECONDS);
    final long startTime = System.currentTimeMillis();
    Thread t = new Thread() {

        public void run() {
            RReadWriteLock lock1 = redisson.getReadWriteLock("lock");
            lock1.readLock().lock();
            long spendTime = System.currentTimeMillis() - startTime;
            Assertions.assertTrue(spendTime < 2050);
            lock1.readLock().unlock();
        }
    };
    t.start();
    t.join();
    lock.readLock().unlock();
}
Also used : RReadWriteLock(org.redisson.api.RReadWriteLock) Test(org.junit.jupiter.api.Test)

Example 4 with RReadWriteLock

use of org.redisson.api.RReadWriteLock 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 5 with RReadWriteLock

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

the class RedissonReadWriteLockTest method testInCluster.

@Test
public void testInCluster() throws Exception {
    RedisRunner master1 = new RedisRunner().randomPort().randomDir().nosave();
    RedisRunner master2 = new RedisRunner().randomPort().randomDir().nosave();
    RedisRunner master3 = new RedisRunner().randomPort().randomDir().nosave();
    ClusterRunner clusterRunner = new ClusterRunner().addNode(master1).addNode(master2).addNode(master3);
    ClusterProcesses process = clusterRunner.run();
    Config config = new Config();
    config.useClusterServers().addNodeAddress(process.getNodes().stream().findAny().get().getRedisServerAddressAndPort());
    RedissonClient redisson = Redisson.create(config);
    RReadWriteLock s = redisson.getReadWriteLock("1234");
    s.writeLock().lock();
    s.readLock().lock();
    s.readLock().unlock();
    s.writeLock().unlock();
    redisson.shutdown();
    process.shutdown();
}
Also used : RedissonClient(org.redisson.api.RedissonClient) ClusterProcesses(org.redisson.ClusterRunner.ClusterProcesses) Config(org.redisson.config.Config) 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