Search in sources :

Example 21 with RLock

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

the class RedissonLockTest method testExpire.

@Test
public void testExpire() throws InterruptedException {
    RLock lock = redisson.getLock("lock");
    lock.lock(2, TimeUnit.SECONDS);
    final long startTime = System.currentTimeMillis();
    Thread t = new Thread() {

        public void run() {
            RLock lock1 = redisson.getLock("lock");
            lock1.lock();
            long spendTime = System.currentTimeMillis() - startTime;
            Assertions.assertTrue(spendTime < 2020);
            lock1.unlock();
        }
    };
    t.start();
    t.join();
    assertThatThrownBy(() -> {
        lock.unlock();
    }).isInstanceOf(IllegalMonitorStateException.class);
}
Also used : RLock(org.redisson.api.RLock) Test(org.junit.jupiter.api.Test)

Example 22 with RLock

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

the class RedissonLockTest method testIsHeldByCurrentThread.

@Test
public void testIsHeldByCurrentThread() {
    RLock lock = redisson.getLock("lock");
    Assertions.assertFalse(lock.isHeldByCurrentThread());
    lock.lock();
    Assertions.assertTrue(lock.isHeldByCurrentThread());
    lock.unlock();
    Assertions.assertFalse(lock.isHeldByCurrentThread());
}
Also used : RLock(org.redisson.api.RLock) Test(org.junit.jupiter.api.Test)

Example 23 with RLock

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

the class RedissonLockTest method testReentrancy.

@Test
public void testReentrancy() throws InterruptedException {
    Lock lock = redisson.getLock("lock1");
    Assertions.assertTrue(lock.tryLock());
    Assertions.assertTrue(lock.tryLock());
    lock.unlock();
    // next row  for test renew expiration tisk.
    // Thread.currentThread().sleep(TimeUnit.SECONDS.toMillis(RedissonLock.LOCK_EXPIRATION_INTERVAL_SECONDS*2));
    Thread thread1 = new Thread() {

        @Override
        public void run() {
            RLock lock1 = redisson.getLock("lock1");
            Assertions.assertFalse(lock1.tryLock());
        }
    };
    thread1.start();
    thread1.join();
    lock.unlock();
}
Also used : RLock(org.redisson.api.RLock) Lock(java.util.concurrent.locks.Lock) RLock(org.redisson.api.RLock) Test(org.junit.jupiter.api.Test)

Example 24 with RLock

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

the class RedissonLockTest method testInCluster.

@Test
public void testInCluster() throws Exception {
    RedisRunner master1 = new RedisRunner().port(6890).randomDir().nosave();
    RedisRunner master2 = new RedisRunner().port(6891).randomDir().nosave();
    RedisRunner master3 = new RedisRunner().port(6892).randomDir().nosave();
    RedisRunner slave1 = new RedisRunner().port(6900).randomDir().nosave();
    RedisRunner slave2 = new RedisRunner().port(6901).randomDir().nosave();
    RedisRunner slave3 = new RedisRunner().port(6902).randomDir().nosave();
    ClusterRunner clusterRunner = new ClusterRunner().addNode(master1, slave1).addNode(master2, slave2).addNode(master3, slave3);
    ClusterRunner.ClusterProcesses process = clusterRunner.run();
    Thread.sleep(5000);
    Config config = new Config();
    config.useClusterServers().setLoadBalancer(new RandomLoadBalancer()).addNodeAddress(process.getNodes().stream().findAny().get().getRedisServerAddressAndPort());
    RedissonClient redisson = Redisson.create(config);
    RLock lock = redisson.getLock("myLock");
    lock.lock();
    assertThat(lock.isLocked()).isTrue();
    lock.unlock();
    assertThat(lock.isLocked()).isFalse();
    redisson.shutdown();
    process.shutdown();
}
Also used : RedissonClient(org.redisson.api.RedissonClient) Config(org.redisson.config.Config) RLock(org.redisson.api.RLock) RandomLoadBalancer(org.redisson.connection.balancer.RandomLoadBalancer) Test(org.junit.jupiter.api.Test)

Example 25 with RLock

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

the class RedissonLockTest method testTryLockWait.

@Test
public void testTryLockWait() throws InterruptedException {
    testSingleInstanceConcurrency(1, r -> {
        RLock lock = r.getLock("lock");
        lock.lock();
    });
    RLock lock = redisson.getLock("lock");
    long startTime = System.currentTimeMillis();
    lock.tryLock(3, TimeUnit.SECONDS);
    assertThat(System.currentTimeMillis() - startTime).isBetween(2990L, 3100L);
}
Also used : 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