Search in sources :

Example 6 with RedisProcess

use of org.redisson.RedisRunner.RedisProcess 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);
    Assert.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.Test)

Example 7 with RedisProcess

use of org.redisson.RedisRunner.RedisProcess 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.Test)

Example 8 with RedisProcess

use of org.redisson.RedisRunner.RedisProcess 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");
    Thread t1 = new Thread() {

        public void run() {
            lock3.lock();
        }

        ;
    };
    t1.start();
    t1.join();
    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);
    lock3.delete();
    RedissonMultiLock lock = new RedissonRedLock(lock1, lock2, lock3);
    lock.lock();
    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.Test)

Example 9 with RedisProcess

use of org.redisson.RedisRunner.RedisProcess 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 10 with RedisProcess

use of org.redisson.RedisRunner.RedisProcess in project redisson by redisson.

the class RedissonMultiLockTest 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) Test(org.junit.Test)

Aggregations

RedisProcess (org.redisson.RedisRunner.RedisProcess)25 Test (org.junit.Test)23 RedissonClient (org.redisson.api.RedissonClient)23 Config (org.redisson.config.Config)17 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 RLock (org.redisson.api.RLock)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)2 ExecutorService (java.util.concurrent.ExecutorService)2 BaseTest (org.redisson.BaseTest)2 RedisRunner (org.redisson.RedisRunner)2 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 URI (java.net.URI)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 TimeoutException (java.util.concurrent.TimeoutException)1 MutableCacheEntryListenerConfiguration (javax.cache.configuration.MutableCacheEntryListenerConfiguration)1 MutableConfiguration (javax.cache.configuration.MutableConfiguration)1