Search in sources :

Example 16 with RedisProcess

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

the class RedissonRedLockTest method testTryLockLeasetime.

@Test
public void testTryLockLeasetime() 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");
    RedissonRedLock lock = new RedissonRedLock(lock1, lock2, lock3);
    ExecutorService executor = Executors.newFixedThreadPool(10);
    AtomicInteger counter = new AtomicInteger();
    for (int i = 0; i < 10; i++) {
        executor.submit(() -> {
            for (int j = 0; j < 5; j++) {
                try {
                    if (lock.tryLock(4, 2, TimeUnit.SECONDS)) {
                        int nextValue = counter.get() + 1;
                        Thread.sleep(1000);
                        counter.set(nextValue);
                        lock.unlock();
                    } else {
                        j--;
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    executor.shutdown();
    assertThat(executor.awaitTermination(2, TimeUnit.MINUTES)).isTrue();
    assertThat(counter.get()).isEqualTo(50);
    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) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) RLock(org.redisson.api.RLock) Test(org.junit.Test)

Example 17 with RedisProcess

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

the class RedissonRedLockTest method test.

//    @Test
public void test() throws IOException, InterruptedException {
    RedisProcess redis1 = redisTestMultilockInstance();
    RedisProcess redis2 = redisTestMultilockInstance();
    RedisProcess redis3 = redisTestMultilockInstance();
    NioEventLoopGroup group = new NioEventLoopGroup();
    RedissonClient client1 = createClient(group, redis1.getRedisServerAddressAndPort());
    RedissonClient client2 = createClient(group, redis2.getRedisServerAddressAndPort());
    RedissonClient client3 = createClient(group, redis3.getRedisServerAddressAndPort());
    final RLock lock1 = client1.getLock("lock1");
    final RLock lock2 = client2.getLock("lock2");
    final RLock lock3 = client3.getLock("lock3");
    RedissonMultiLock lock = new RedissonMultiLock(lock1, lock2, lock3);
    lock.lock();
    final AtomicBoolean executed = new AtomicBoolean();
    Thread t = new Thread() {

        @Override
        public void run() {
            RedissonMultiLock lock = new RedissonMultiLock(lock1, lock2, lock3);
            assertThat(lock.tryLock()).isFalse();
            assertThat(lock.tryLock()).isFalse();
            executed.set(true);
        }
    };
    t.start();
    t.join();
    await().atMost(5, TimeUnit.SECONDS).until(() -> assertThat(executed.get()).isTrue());
    lock.unlock();
    assertThat(redis1.stop()).isEqualTo(0);
    assertThat(redis2.stop()).isEqualTo(0);
    assertThat(redis3.stop()).isEqualTo(0);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RedissonClient(org.redisson.api.RedissonClient) RedisProcess(org.redisson.RedisRunner.RedisProcess) RLock(org.redisson.api.RLock) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 18 with RedisProcess

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

the class RedissonRedLockTest method testLockLeasetime.

@Test
public void testLockLeasetime() 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");
    RLock lock4 = client2.getLock("lock4");
    RLock lock5 = client2.getLock("lock5");
    RLock lock6 = client2.getLock("lock6");
    RLock lock7 = client2.getLock("lock7");
    RedissonRedLock lock = new RedissonRedLock(lock1, lock2, lock3, lock4, lock5, lock6, lock7);
    ExecutorService executor = Executors.newFixedThreadPool(10);
    AtomicInteger counter = new AtomicInteger();
    for (int i = 0; i < 10; i++) {
        executor.submit(() -> {
            for (int j = 0; j < 5; j++) {
                try {
                    lock.lock(2, TimeUnit.SECONDS);
                    int nextValue = counter.get() + 1;
                    Thread.sleep(1000);
                    counter.set(nextValue);
                    lock.unlock();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    executor.shutdown();
    assertThat(executor.awaitTermination(2, TimeUnit.MINUTES)).isTrue();
    assertThat(counter.get()).isEqualTo(50);
    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) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) RLock(org.redisson.api.RLock) Test(org.junit.Test)

Example 19 with RedisProcess

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

the class RedissonTest method testConnectionListener.

@Test
public void testConnectionListener() throws IOException, InterruptedException, TimeoutException {
    final RedisProcess p = redisTestConnection();
    final AtomicInteger connectCounter = new AtomicInteger();
    final AtomicInteger disconnectCounter = new AtomicInteger();
    Config config = new Config();
    config.useSingleServer().setAddress(p.getRedisServerAddressAndPort());
    RedissonClient r = Redisson.create(config);
    int id = r.getNodesGroup().addConnectionListener(new ConnectionListener() {

        @Override
        public void onDisconnect(InetSocketAddress addr) {
            assertThat(addr).isEqualTo(new InetSocketAddress(p.getRedisServerBindAddress(), p.getRedisServerPort()));
            disconnectCounter.incrementAndGet();
        }

        @Override
        public void onConnect(InetSocketAddress addr) {
            assertThat(addr).isEqualTo(new InetSocketAddress(p.getRedisServerBindAddress(), p.getRedisServerPort()));
            connectCounter.incrementAndGet();
        }
    });
    assertThat(id).isNotZero();
    r.getBucket("1").get();
    Assert.assertEquals(0, p.stop());
    try {
        r.getBucket("1").get();
    } catch (Exception e) {
    }
    RedisProcess pp = new RedisRunner().nosave().port(p.getRedisServerPort()).randomDir().run();
    r.getBucket("1").get();
    r.shutdown();
    Assert.assertEquals(0, pp.stop());
    await().atMost(1, TimeUnit.SECONDS).until(() -> assertThat(connectCounter.get()).isEqualTo(1));
    await().until(() -> assertThat(disconnectCounter.get()).isEqualTo(1));
}
Also used : RedissonClient(org.redisson.api.RedissonClient) RedisProcess(org.redisson.RedisRunner.RedisProcess) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Config(org.redisson.config.Config) InetSocketAddress(java.net.InetSocketAddress) ConnectionListener(org.redisson.connection.ConnectionListener) RedisException(org.redisson.client.RedisException) TimeoutException(java.util.concurrent.TimeoutException) RedisConnectionException(org.redisson.client.RedisConnectionException) RedisOutOfMemoryException(org.redisson.client.RedisOutOfMemoryException) IOException(java.io.IOException) Test(org.junit.Test)

Example 20 with RedisProcess

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

the class RedissonBlockingQueueTest method testPollAsyncReattach.

@Test
public void testPollAsyncReattach() throws InterruptedException, IOException, ExecutionException, TimeoutException {
    RedisProcess runner = new RedisRunner().nosave().randomDir().randomPort().run();
    Config config = new Config();
    config.useSingleServer().setAddress(runner.getRedisServerAddressAndPort());
    RedissonClient redisson = Redisson.create(config);
    RBlockingQueue<Integer> queue1 = redisson.getBlockingQueue("queue:pollany");
    RFuture<Integer> f = queue1.pollAsync(10, TimeUnit.SECONDS);
    f.await(1, TimeUnit.SECONDS);
    runner.stop();
    runner = new RedisRunner().port(runner.getRedisServerPort()).nosave().randomDir().run();
    queue1.put(123);
    // check connection rotation
    for (int i = 0; i < 10; i++) {
        queue1.put(i);
    }
    assertThat(queue1.size()).isEqualTo(10);
    Integer result = f.get(1, TimeUnit.SECONDS);
    assertThat(result).isEqualTo(123);
    redisson.shutdown();
    runner.stop();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RedissonClient(org.redisson.api.RedissonClient) RedisProcess(org.redisson.RedisRunner.RedisProcess) Config(org.redisson.config.Config) 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