Search in sources :

Example 26 with RedissonClient

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

the class RedissonRemoteServiceTest method testInvocationWithSerializationCodec.

@Test
public void testInvocationWithSerializationCodec() {
    RedissonClient server = Redisson.create(createConfig().setCodec(new SerializationCodec()));
    RedissonClient client = Redisson.create(createConfig().setCodec(new SerializationCodec()));
    try {
        server.getRemoteService().register(RemoteInterface.class, new RemoteImpl());
        RemoteInterface service = client.getRemoteService().get(RemoteInterface.class);
        try {
            assertThat(service.resultMethod(21L)).isEqualTo(42L);
        } catch (Exception e) {
            Assert.fail("Should be compatible with SerializationCodec");
        }
        try {
            assertThat(service.doSomethingWithSerializablePojo(new SerializablePojo("test")).getStringField()).isEqualTo("test");
        } catch (Exception e) {
            e.printStackTrace();
            Assert.fail("Should be compatible with SerializationCodec");
        }
        try {
            assertThat(service.doSomethingWithPojo(new Pojo("test")).getStringField()).isEqualTo("test");
            Assert.fail("SerializationCodec should not be able to serialize a not serializable class");
        } catch (Exception e) {
            e.printStackTrace();
            assertThat(e.getCause()).isInstanceOf(NotSerializableException.class);
            assertThat(e.getCause().getMessage()).contains("Pojo");
        }
    } finally {
        client.shutdown();
        server.shutdown();
    }
}
Also used : NotSerializableException(java.io.NotSerializableException) RedissonClient(org.redisson.api.RedissonClient) SerializationCodec(org.redisson.codec.SerializationCodec) 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 27 with RedissonClient

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

the class RedissonRemoteServiceTest method testInvocationWithServiceName.

@Test
public void testInvocationWithServiceName() {
    RedissonClient server = createInstance();
    RedissonClient client = createInstance();
    server.getRemoteService("MyServiceNamespace").register(RemoteInterface.class, new RemoteImpl());
    RemoteInterface serviceRemoteInterface = client.getRemoteService("MyServiceNamespace").get(RemoteInterface.class);
    RemoteInterface otherServiceRemoteInterface = client.getRemoteService("MyOtherServiceNamespace").get(RemoteInterface.class);
    RemoteInterface defaultServiceRemoteInterface = client.getRemoteService().get(RemoteInterface.class);
    assertThat(serviceRemoteInterface.resultMethod(21L)).isEqualTo(42L);
    try {
        otherServiceRemoteInterface.resultMethod(21L);
        Assert.fail("Invoking a service in an unregistered custom services namespace should throw");
    } catch (Exception e) {
        assertThat(e).isInstanceOf(RemoteServiceAckTimeoutException.class);
    }
    try {
        defaultServiceRemoteInterface.resultMethod(21L);
        Assert.fail("Invoking a service in the unregistered default services namespace should throw");
    } catch (Exception e) {
        assertThat(e).isInstanceOf(RemoteServiceAckTimeoutException.class);
    }
    client.shutdown();
    server.shutdown();
}
Also used : RedissonClient(org.redisson.api.RedissonClient) RemoteServiceAckTimeoutException(org.redisson.remote.RemoteServiceAckTimeoutException) 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 28 with RedissonClient

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

the class RedissonRemoteServiceTest method testExecutorsAmountConcurrency.

@Test
public void testExecutorsAmountConcurrency() throws InterruptedException {
    // Redisson server and client
    final RedissonClient server = createInstance();
    final RedissonClient client = createInstance();
    final int serverAmount = 1;
    final int clientAmount = 10;
    // to store the current call concurrency count
    final AtomicInteger concurrency = new AtomicInteger(0);
    // a flag to indicate the the allowed concurrency was exceeded
    final AtomicBoolean concurrencyIsExceeded = new AtomicBoolean(false);
    // the server: register a service with an overrided timeoutMethod method that:
    // - incr the concurrency
    // - check if concurrency is greater than what was allowed, and if yes set the concurrencyOfOneIsExceeded flag
    // - wait 2s
    // - decr the concurrency
    server.getRemoteService().register(RemoteInterface.class, new RemoteImpl() {

        @Override
        public void timeoutMethod() throws InterruptedException {
            try {
                if (concurrency.incrementAndGet() > serverAmount) {
                    concurrencyIsExceeded.compareAndSet(false, true);
                }
                super.timeoutMethod();
            } finally {
                concurrency.decrementAndGet();
            }
        }
    }, serverAmount);
    // a latch to force the client threads to execute simultaneously
    // (as far as practicable, this is hard to predict)
    final CountDownLatch readyLatch = new CountDownLatch(1);
    // the client: starts a couple of threads that will:
    // - await for the ready latch
    // - then call timeoutMethod
    java.util.concurrent.Future[] clientFutures = new java.util.concurrent.Future[clientAmount];
    ExecutorService executor = Executors.newFixedThreadPool(clientAmount);
    for (int i = 0; i < clientAmount; i++) {
        clientFutures[i] = executor.submit(new Runnable() {

            @Override
            public void run() {
                try {
                    RemoteInterface ri = client.getRemoteService().get(RemoteInterface.class, clientAmount * 3, TimeUnit.SECONDS, clientAmount * 3, TimeUnit.SECONDS);
                    readyLatch.await();
                    ri.timeoutMethod();
                } catch (InterruptedException e) {
                // ignore
                }
            }
        });
    }
    // open the latch to wake the threads
    readyLatch.countDown();
    // await for the client threads to terminate
    for (java.util.concurrent.Future clientFuture : clientFutures) {
        try {
            clientFuture.get();
        } catch (ExecutionException e) {
        // ignore
        }
    }
    executor.shutdown();
    executor.awaitTermination(clientAmount * 3, TimeUnit.SECONDS);
    // shutdown the server and the client
    server.shutdown();
    client.shutdown();
    // do the concurrencyIsExceeded flag was set ?
    // if yes, that would indicate that the server exceeded its expected concurrency
    assertThat(concurrencyIsExceeded.get()).isEqualTo(false);
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RedissonClient(org.redisson.api.RedissonClient) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) RFuture(org.redisson.api.RFuture) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 29 with RedissonClient

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

the class RedissonRemoteServiceTest method testNoAckWithResultInvocations.

@Test
public void testNoAckWithResultInvocations() throws InterruptedException {
    RedissonClient server = createInstance();
    RedissonClient client = createInstance();
    try {
        server.getRemoteService().register(RemoteInterface.class, new RemoteImpl());
        // no ack but an execution timeout of 1 second
        RemoteInvocationOptions options = RemoteInvocationOptions.defaults().noAck().expectResultWithin(1, TimeUnit.SECONDS);
        RemoteInterface service = client.getRemoteService().get(RemoteInterface.class, options);
        service.voidMethod("noAck", 100L);
        assertThat(service.resultMethod(21L)).isEqualTo(42);
        try {
            service.errorMethod();
            Assert.fail();
        } catch (IOException e) {
            assertThat(e.getMessage()).isEqualTo("Checking error throw");
        }
        try {
            service.errorMethodWithCause();
            Assert.fail();
        } catch (Exception e) {
            assertThat(e.getCause()).isInstanceOf(ArithmeticException.class);
            assertThat(e.getCause().getMessage()).isEqualTo("/ by zero");
        }
        try {
            service.timeoutMethod();
            Assert.fail("noAck option should still wait for the server to return a response and throw if the execution timeout is exceeded");
        } catch (Exception e) {
            assertThat(e).isInstanceOf(RemoteServiceTimeoutException.class);
        }
    } finally {
        client.shutdown();
        server.shutdown();
    }
}
Also used : RedissonClient(org.redisson.api.RedissonClient) RemoteServiceTimeoutException(org.redisson.remote.RemoteServiceTimeoutException) 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 30 with RedissonClient

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

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