Search in sources :

Example 51 with RedissonClient

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

the class RedissonRemoteServiceTest method testInvocationWithFstCodec.

@Test
public void testInvocationWithFstCodec() {
    RedissonClient server = Redisson.create(createConfig().setCodec(new FstCodec()));
    RedissonClient client = Redisson.create(createConfig().setCodec(new FstCodec()));
    try {
        server.getRemoteService().register(RemoteInterface.class, new RemoteImpl());
        RemoteInterface service = client.getRemoteService().get(RemoteInterface.class);
        assertThat(service.resultMethod(21L)).as("Should be compatible with FstCodec").isEqualTo(42L);
        try {
            assertThat(service.doSomethingWithSerializablePojo(new SerializablePojo("test")).getStringField()).isEqualTo("test");
        } catch (Exception e) {
            Assert.fail("Should be compatible with FstCodec");
        }
        try {
            assertThat(service.doSomethingWithPojo(new Pojo("test")).getStringField()).isEqualTo("test");
            Assert.fail("FstCodec should not be able to serialize a not serializable class");
        } catch (Exception e) {
            assertThat(e.getCause()).isInstanceOf(RuntimeException.class);
            assertThat(e.getCause().getMessage()).contains("Pojo does not implement Serializable");
        }
    } finally {
        client.shutdown();
        server.shutdown();
    }
}
Also used : RedissonClient(org.redisson.api.RedissonClient) FstCodec(org.redisson.codec.FstCodec) RemoteServiceTimeoutException(org.redisson.remote.RemoteServiceTimeoutException) IOException(java.io.IOException) NotSerializableException(java.io.NotSerializableException) ExecutionException(java.util.concurrent.ExecutionException) RemoteServiceAckTimeoutException(org.redisson.remote.RemoteServiceAckTimeoutException) Test(org.junit.Test)

Example 52 with RedissonClient

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

the class RedissonRemoteServiceTest method testAsync.

@Test
public void testAsync() throws InterruptedException {
    RedissonClient r1 = createInstance();
    r1.getRemoteService().register(RemoteInterface.class, new RemoteImpl());
    RedissonClient r2 = createInstance();
    RemoteInterfaceAsync ri = r2.getRemoteService().get(RemoteInterfaceAsync.class);
    RFuture<Void> f = ri.voidMethod("someName", 100L);
    f.sync();
    RFuture<Long> resFuture = ri.resultMethod(100L);
    resFuture.sync();
    assertThat(resFuture.getNow()).isEqualTo(200);
    r1.shutdown();
    r2.shutdown();
}
Also used : RedissonClient(org.redisson.api.RedissonClient) Test(org.junit.Test)

Example 53 with RedissonClient

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

the class RedissonTopicPatternTest method testListenerRemove.

@Test
public void testListenerRemove() throws InterruptedException {
    RedissonClient redisson1 = BaseTest.createInstance();
    RPatternTopic<Message> topic1 = redisson1.getPatternTopic("topic.*");
    final CountDownLatch l = new CountDownLatch(1);
    topic1.addListener(new BasePatternStatusListener() {

        @Override
        public void onPUnsubscribe(String pattern) {
            Assert.assertEquals("topic.*", pattern);
            l.countDown();
        }
    });
    int id = topic1.addListener((pattern, channel, msg) -> {
        Assert.fail();
    });
    RedissonClient redisson2 = BaseTest.createInstance();
    RTopic<Message> topic2 = redisson2.getTopic("topic.t1");
    topic1.removeListener(id);
    topic2.publish(new Message("123"));
    redisson1.shutdown();
    redisson2.shutdown();
}
Also used : RedissonClient(org.redisson.api.RedissonClient) BasePatternStatusListener(org.redisson.api.listener.BasePatternStatusListener) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 54 with RedissonClient

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

the class RedissonTopicPatternTest method testReattach.

@Test
public void testReattach() 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);
    final AtomicBoolean executed = new AtomicBoolean();
    RPatternTopic<Integer> topic = redisson.getPatternTopic("topic*");
    topic.addListener(new PatternMessageListener<Integer>() {

        @Override
        public void onMessage(String pattern, String channel, Integer msg) {
            if (msg == 1) {
                executed.set(true);
            }
        }
    });
    runner.stop();
    runner = new RedisRunner().port(runner.getRedisServerPort()).nosave().randomDir().run();
    Thread.sleep(1000);
    redisson.getTopic("topic1").publish(1);
    await().atMost(5, TimeUnit.SECONDS).untilTrue(executed);
    redisson.shutdown();
    runner.stop();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RedissonClient(org.redisson.api.RedissonClient) RedisProcess(org.redisson.RedisRunner.RedisProcess) Config(org.redisson.config.Config) Test(org.junit.Test)

Example 55 with RedissonClient

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

the class RedissonTopicTest method testMultiTypeConnection.

@Test
public void testMultiTypeConnection() throws InterruptedException {
    RedissonClient redisson = BaseTest.createInstance();
    RTopic<String> stringTopic = redisson.getTopic("test1", StringCodec.INSTANCE);
    AtomicBoolean stringMessageReceived = new AtomicBoolean();
    stringTopic.addListener(new MessageListener<String>() {

        @Override
        public void onMessage(String channel, String msg) {
            assertThat(msg).isEqualTo("testmsg");
            stringMessageReceived.set(true);
        }
    });
    stringTopic.publish("testmsg");
    RTopic<Long> longTopic = redisson.getTopic("test2", LongCodec.INSTANCE);
    AtomicBoolean longMessageReceived = new AtomicBoolean();
    longTopic.addListener(new MessageListener<Long>() {

        @Override
        public void onMessage(String channel, Long msg) {
            assertThat(msg).isEqualTo(1L);
            longMessageReceived.set(true);
        }
    });
    longTopic.publish(1L);
    Awaitility.await().atMost(Duration.ONE_SECOND).untilTrue(stringMessageReceived);
    Awaitility.await().atMost(Duration.ONE_SECOND).untilTrue(longMessageReceived);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RedissonClient(org.redisson.api.RedissonClient) 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