Search in sources :

Example 1 with RRemoteService

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

the class RedissonRemoteServiceTest method testFreeWorkers.

@Test
public void testFreeWorkers() throws InterruptedException, ExecutionException {
    RedissonClient r1 = createInstance();
    ExecutorService executor = Executors.newSingleThreadExecutor();
    RRemoteService rs = r1.getRemoteService();
    rs.register(RemoteInterface.class, new RemoteImpl(), 1, executor);
    assertThat(rs.getFreeWorkers(RemoteInterface.class)).isEqualTo(1);
    RedissonClient r2 = createInstance();
    RemoteInterfaceAsync ri = r2.getRemoteService().get(RemoteInterfaceAsync.class);
    RFuture<Void> f = ri.timeoutMethod();
    Thread.sleep(100);
    assertThat(rs.getFreeWorkers(RemoteInterface.class)).isEqualTo(0);
    f.get();
    assertThat(rs.getFreeWorkers(RemoteInterface.class)).isEqualTo(1);
    r1.shutdown();
    r2.shutdown();
    executor.shutdown();
    executor.awaitTermination(1, TimeUnit.MINUTES);
}
Also used : RedissonClient(org.redisson.api.RedissonClient) ExecutorService(java.util.concurrent.ExecutorService) RRemoteService(org.redisson.api.RRemoteService) Test(org.junit.jupiter.api.Test)

Example 2 with RRemoteService

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

the class RedissonRemoteServiceTest method testOptional.

@Test
public void testOptional() {
    RRemoteService remoteService = redisson.getRemoteService();
    remoteService.register(RemoteInterface.class, new RemoteImpl());
    RemoteInterface service = redisson.getRemoteService().get(RemoteInterface.class);
    Optional<Integer> r1 = service.optionalResult(null);
    assertThat(r1.isPresent()).isFalse();
    Optional<Integer> r2 = service.optionalResult(2);
    assertThat(r2.get()).isEqualTo(2);
    remoteService.deregister(RemoteInterface.class);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RRemoteService(org.redisson.api.RRemoteService) Test(org.junit.jupiter.api.Test)

Example 3 with RRemoteService

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

the class RedissonRemoteServiceTest method testConcurrentInvocations.

@Test
public void testConcurrentInvocations() {
    ExecutorService executorService = Executors.newFixedThreadPool(2);
    RRemoteService remoteService = redisson.getRemoteService();
    remoteService.register(RemoteInterface.class, new RemoteImpl());
    RemoteInterface service = redisson.getRemoteService().get(RemoteInterface.class);
    List<Future<?>> futures = new ArrayList<>();
    int iterations = 1000;
    AtomicBoolean bool = new AtomicBoolean();
    for (int i = 0; i < iterations; i++) {
        futures.add(executorService.submit(() -> {
            try {
                if (ThreadLocalRandom.current().nextBoolean()) {
                    service.resultMethod(1L);
                } else {
                    service.methodOverload();
                }
            } catch (Exception e) {
                e.printStackTrace();
                bool.set(true);
            }
        }));
    }
    while (!futures.stream().allMatch(Future::isDone)) {
    }
    assertThat(bool.get()).isFalse();
    remoteService.deregister(RemoteInterface.class);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) RFuture(org.redisson.api.RFuture) Future(java.util.concurrent.Future) RRemoteService(org.redisson.api.RRemoteService) 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.jupiter.api.Test)

Example 4 with RRemoteService

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

the class RedissonRemoteServiceTest method testPendingInvocations.

@Test
public void testPendingInvocations() throws InterruptedException, ExecutionException {
    ExecutorService executor = Executors.newSingleThreadExecutor();
    RRemoteService rs = redisson.getRemoteService();
    rs.register(RemoteInterface.class, new RemoteImpl(), 1, executor);
    assertThat(rs.getPendingInvocations(RemoteInterface.class)).isEqualTo(0);
    RemoteInterfaceAsync ri = redisson.getRemoteService().get(RemoteInterfaceAsync.class);
    for (int i = 0; i < 5; i++) {
        ri.timeoutMethod();
    }
    Thread.sleep(1000);
    assertThat(rs.getPendingInvocations(RemoteInterface.class)).isEqualTo(4);
    Thread.sleep(9000);
    assertThat(rs.getPendingInvocations(RemoteInterface.class)).isEqualTo(0);
    executor.shutdown();
    executor.awaitTermination(1, TimeUnit.MINUTES);
    rs.deregister(RemoteInterface.class);
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) RRemoteService(org.redisson.api.RRemoteService) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)4 RRemoteService (org.redisson.api.RRemoteService)4 ExecutorService (java.util.concurrent.ExecutorService)3 IOException (java.io.IOException)1 NotSerializableException (java.io.NotSerializableException)1 ArrayList (java.util.ArrayList)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 RFuture (org.redisson.api.RFuture)1 RedissonClient (org.redisson.api.RedissonClient)1 RemoteServiceAckTimeoutException (org.redisson.remote.RemoteServiceAckTimeoutException)1 RemoteServiceTimeoutException (org.redisson.remote.RemoteServiceTimeoutException)1