Search in sources :

Example 66 with RedissonClient

use of org.redisson.api.RedissonClient 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 67 with RedissonClient

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

the class RedissonTest method testSer.

@Test(expected = RedisException.class)
public void testSer() {
    Config config = new Config();
    config.useSingleServer().setAddress(RedisRunner.getDefaultRedisServerBindAddressAndPort());
    config.setCodec(new SerializationCodec());
    RedissonClient r = Redisson.create(config);
    r.getMap("test").put("1", new Dummy());
}
Also used : RedissonClient(org.redisson.api.RedissonClient) Config(org.redisson.config.Config) SerializationCodec(org.redisson.codec.SerializationCodec) Test(org.junit.Test)

Example 68 with RedissonClient

use of org.redisson.api.RedissonClient 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 69 with RedissonClient

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

the class BaseConcurrentTest method testSingleInstanceConcurrency.

protected void testSingleInstanceConcurrency(int iterations, final RedissonRunnable runnable) throws InterruptedException {
    System.out.println("Single Instance Concurrent Job Interation: " + iterations);
    final RedissonClient r = BaseTest.createInstance();
    long watch = System.currentTimeMillis();
    ExecutorService pool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2);
    for (int i = 0; i < iterations; i++) {
        pool.execute(() -> {
            runnable.run(r);
        });
    }
    pool.shutdown();
    Assert.assertTrue(pool.awaitTermination(RedissonRuntimeEnvironment.isTravis ? 20 : 3, TimeUnit.MINUTES));
    System.out.println(System.currentTimeMillis() - watch);
    r.shutdown();
}
Also used : RedissonClient(org.redisson.api.RedissonClient) ExecutorService(java.util.concurrent.ExecutorService)

Example 70 with RedissonClient

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

the class CommandHandlersTest method testEncoder.

@Test(expected = RedisException.class)
public void testEncoder() throws InterruptedException {
    Config config = createConfig();
    config.setCodec(new ErrorsCodec());
    RedissonClient redisson = Redisson.create(config);
    redisson.getBucket("1234").set("1234");
}
Also used : RedissonClient(org.redisson.api.RedissonClient) Config(org.redisson.config.Config) Test(org.junit.Test)

Aggregations

RedissonClient (org.redisson.api.RedissonClient)95 Test (org.junit.Test)87 Config (org.redisson.config.Config)44 RedisProcess (org.redisson.RedisRunner.RedisProcess)23 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)22 CountDownLatch (java.util.concurrent.CountDownLatch)15 ExecutorService (java.util.concurrent.ExecutorService)12 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 RLock (org.redisson.api.RLock)10 IOException (java.io.IOException)9 ExecutionException (java.util.concurrent.ExecutionException)9 NotSerializableException (java.io.NotSerializableException)8 RemoteServiceAckTimeoutException (org.redisson.remote.RemoteServiceAckTimeoutException)8 RemoteServiceTimeoutException (org.redisson.remote.RemoteServiceTimeoutException)8 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 RemoteInvocationOptions (org.redisson.api.RemoteInvocationOptions)4 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)3 BasePatternStatusListener (org.redisson.api.listener.BasePatternStatusListener)3 SerializationCodec (org.redisson.codec.SerializationCodec)3