Search in sources :

Example 36 with RLock

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

the class RedissonSpinLockTest method testIsHeldByCurrentThreadOtherThread.

@Test
public void testIsHeldByCurrentThreadOtherThread() throws InterruptedException {
    RLock lock = redisson.getSpinLock("lock");
    lock.lock();
    Thread t = new Thread() {

        public void run() {
            RLock lock = redisson.getSpinLock("lock");
            Assertions.assertFalse(lock.isHeldByCurrentThread());
        }
    };
    t.start();
    t.join();
    lock.unlock();
    Thread t2 = new Thread() {

        public void run() {
            RLock lock = redisson.getSpinLock("lock");
            Assertions.assertFalse(lock.isHeldByCurrentThread());
        }
    };
    t2.start();
    t2.join();
}
Also used : RLock(org.redisson.api.RLock) Test(org.junit.jupiter.api.Test)

Example 37 with RLock

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

the class RedissonSpinLockTest method testIsHeldByCurrentThread.

@Test
public void testIsHeldByCurrentThread() {
    RLock lock = redisson.getSpinLock("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 38 with RLock

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

the class RedissonRedLockTest method testLockSuccess.

@Test
public void testLockSuccess() 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");
    testLock(lock1, lock2, lock3, lock1);
    testLock(lock1, lock2, lock3, lock2);
    testLock(lock1, lock2, lock3, lock3);
    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 39 with RLock

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

the class RedissonRedLockTest method testMultiThreads.

// @Test
public void testMultiThreads() throws IOException, InterruptedException {
    RedisProcess redis1 = redisTestMultilockInstance();
    Config config1 = new Config();
    config1.useSingleServer().setAddress(redis1.getRedisServerAddressAndPort());
    RedissonClient client = Redisson.create(config1);
    RLock lock1 = client.getLock("lock1");
    RLock lock2 = client.getLock("lock2");
    RLock lock3 = client.getLock("lock3");
    Thread t = new Thread() {

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

Example 40 with RLock

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

the class RedissonRedLockTest method testLockFailed.

@Test
public void testLockFailed() 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();
            lock3.lock();
        }
    };
    t1.start();
    t1.join();
    Thread t = new Thread() {

        public void run() {
            RedissonMultiLock lock = new RedissonRedLock(lock1, lock2, lock3);
            lock.lock();
        }
    };
    t.start();
    t.join(1000);
    RedissonMultiLock lock = new RedissonRedLock(lock1, lock2, lock3);
    Assertions.assertFalse(lock.tryLock());
    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)

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