Search in sources :

Example 46 with RedissonClient

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

the class RedissonTopicPatternTest method testUnsubscribe.

@Test
public void testUnsubscribe() throws InterruptedException {
    final CountDownLatch messageRecieved = new CountDownLatch(1);
    RedissonClient redisson = BaseTest.createInstance();
    RPatternTopic<Message> topic1 = redisson.getPatternTopic("topic1.*");
    int listenerId = topic1.addListener((pattern, channel, msg) -> {
        Assert.fail();
    });
    topic1.addListener((pattern, channel, msg) -> {
        Assert.assertEquals("topic1.*", pattern);
        Assert.assertEquals("topic1.t3", channel);
        Assert.assertEquals(new Message("123"), msg);
        messageRecieved.countDown();
    });
    topic1.removeListener(listenerId);
    //        topic1 = redisson.getPatternTopic("topic1.*");
    redisson.getTopic("topic1.t3").publish(new Message("123"));
    Assert.assertTrue(messageRecieved.await(5, TimeUnit.SECONDS));
    redisson.shutdown();
}
Also used : RedissonClient(org.redisson.api.RedissonClient) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 47 with RedissonClient

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

the class RedissonRemoteServiceTest method testCancelAsync.

@Test
public void testCancelAsync() throws InterruptedException {
    RedissonClient r1 = createInstance();
    AtomicInteger iterations = new AtomicInteger();
    ExecutorService executor = Executors.newSingleThreadExecutor();
    r1.getKeys().flushall();
    r1.getRemoteService().register(RemoteInterface.class, new RemoteImpl(iterations), 1, executor);
    RedissonClient r2 = createInstance();
    RemoteInterfaceAsync ri = r2.getRemoteService().get(RemoteInterfaceAsync.class);
    RFuture<Void> f = ri.cancelMethod();
    Thread.sleep(500);
    assertThat(f.cancel(true)).isTrue();
    executor.shutdown();
    r1.shutdown();
    r2.shutdown();
    assertThat(iterations.get()).isLessThan(Integer.MAX_VALUE / 2);
    assertThat(executor.awaitTermination(1, TimeUnit.SECONDS)).isTrue();
}
Also used : RedissonClient(org.redisson.api.RedissonClient) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Example 48 with RedissonClient

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

the class RedissonRemoteServiceTest method testExecutorAsync.

@Test
public void testExecutorAsync() throws InterruptedException {
    RedissonClient r1 = createInstance();
    ExecutorService executor = Executors.newSingleThreadExecutor();
    r1.getRemoteService().register(RemoteInterface.class, new RemoteImpl(), 1, executor);
    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();
    executor.shutdown();
    executor.awaitTermination(1, TimeUnit.MINUTES);
}
Also used : RedissonClient(org.redisson.api.RedissonClient) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Example 49 with RedissonClient

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

the class RedissonRemoteServiceTest method testNoAckWithoutResultInvocations.

@Test
public void testNoAckWithoutResultInvocations() throws InterruptedException {
    RedissonClient server = createInstance();
    RedissonClient client = createInstance();
    try {
        server.getRemoteService().register(RemoteInterface.class, new RemoteImpl());
        // no ack fire and forget
        RemoteInvocationOptions options = RemoteInvocationOptions.defaults().noAck().noResult();
        RemoteInterface service = client.getRemoteService().get(RemoteInterface.class, options);
        RemoteInterface invalidService = client.getRemoteService("Invalid").get(RemoteInterface.class, options);
        service.voidMethod("noAck/noResult", 100L);
        try {
            service.resultMethod(100L);
            Assert.fail();
        } catch (Exception e) {
            assertThat(e).isInstanceOf(IllegalArgumentException.class);
        }
        try {
            service.errorMethod();
        } catch (IOException e) {
            Assert.fail("noAck with noResult options should not throw server side exception");
        }
        try {
            service.errorMethodWithCause();
        } catch (Exception e) {
            Assert.fail("noAck with noResult options should not throw server side exception");
        }
        long time = System.currentTimeMillis();
        service.timeoutMethod();
        time = System.currentTimeMillis() - time;
        assertThat(time).describedAs("noAck with noResult options should not wait for the server to return a response").isLessThan(2000);
        try {
            invalidService.voidMethod("noAck/noResult", 21L);
        } catch (Exception e) {
            Assert.fail("noAck with noResult options should not throw any exception even while invoking a service in an unregistered services namespace");
        }
    } finally {
        client.shutdown();
        server.shutdown();
    }
}
Also used : RedissonClient(org.redisson.api.RedissonClient) IOException(java.io.IOException) RemoteServiceTimeoutException(org.redisson.remote.RemoteServiceTimeoutException) IOException(java.io.IOException) NotSerializableException(java.io.NotSerializableException) ExecutionException(java.util.concurrent.ExecutionException) RemoteServiceAckTimeoutException(org.redisson.remote.RemoteServiceAckTimeoutException) RemoteInvocationOptions(org.redisson.api.RemoteInvocationOptions) Test(org.junit.Test)

Example 50 with RedissonClient

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

the class RedissonRemoteServiceTest method testInvocations.

@Test
public void testInvocations() {
    RedissonClient r1 = createInstance();
    r1.getRemoteService().register(RemoteInterface.class, new RemoteImpl());
    RedissonClient r2 = createInstance();
    RemoteInterface ri = r2.getRemoteService().get(RemoteInterface.class);
    ri.voidMethod("someName", 100L);
    assertThat(ri.resultMethod(100L)).isEqualTo(200);
    try {
        ri.errorMethod();
        Assert.fail();
    } catch (IOException e) {
        assertThat(e.getMessage()).isEqualTo("Checking error throw");
    }
    try {
        ri.errorMethodWithCause();
        Assert.fail();
    } catch (Exception e) {
        assertThat(e.getCause()).isInstanceOf(ArithmeticException.class);
        assertThat(e.getCause().getMessage()).isEqualTo("/ by zero");
    }
    r1.shutdown();
    r2.shutdown();
}
Also used : RedissonClient(org.redisson.api.RedissonClient) IOException(java.io.IOException) 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)

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