Search in sources :

Example 1 with RemoteInstance

use of org.cryptomator.ui.util.SingleInstanceManager.RemoteInstance in project cryptomator by cryptomator.

the class SingleInstanceManagerTest method testOneMessage.

@Test
public void testOneMessage() throws Exception {
    ExecutorService exec = Executors.newCachedThreadPool();
    try {
        final LocalInstance server = SingleInstanceManager.startLocalInstance(appKey, exec);
        final Optional<RemoteInstance> r = SingleInstanceManager.getRemoteInstance(appKey);
        CountDownLatch latch = new CountDownLatch(1);
        final MessageListener listener = spy(new MessageListener() {

            @Override
            public void handleMessage(String message) {
                latch.countDown();
            }
        });
        server.registerListener(listener);
        assertTrue(r.isPresent());
        String message = "Is this thing on?";
        assertTrue(r.get().sendMessage(message, 1000));
        System.out.println("wrote message");
        latch.await(10, TimeUnit.SECONDS);
        verify(listener).handleMessage(message);
    } finally {
        exec.shutdownNow();
    }
}
Also used : LocalInstance(org.cryptomator.ui.util.SingleInstanceManager.LocalInstance) ExecutorService(java.util.concurrent.ExecutorService) MessageListener(org.cryptomator.ui.util.SingleInstanceManager.MessageListener) RemoteInstance(org.cryptomator.ui.util.SingleInstanceManager.RemoteInstance) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with RemoteInstance

use of org.cryptomator.ui.util.SingleInstanceManager.RemoteInstance in project cryptomator by cryptomator.

the class SingleInstanceManagerTest method testALotOfMessages.

@Test(timeout = 60000)
public void testALotOfMessages() throws Exception {
    final int connectors = 256;
    final int messagesPerConnector = 256;
    ExecutorService exec = Executors.newSingleThreadExecutor();
    ExecutorService exec2 = Executors.newFixedThreadPool(16);
    try (final LocalInstance server = SingleInstanceManager.startLocalInstance(appKey, exec)) {
        Set<String> sentMessages = new ConcurrentSkipListSet<>();
        Set<String> receivedMessages = new HashSet<>();
        CountDownLatch sendLatch = new CountDownLatch(connectors);
        CountDownLatch receiveLatch = new CountDownLatch(connectors * messagesPerConnector);
        server.registerListener(message -> {
            receivedMessages.add(message);
            receiveLatch.countDown();
        });
        Set<RemoteInstance> instances = Collections.synchronizedSet(new HashSet<>());
        for (int i = 0; i < connectors; i++) {
            exec2.submit(() -> {
                try {
                    final Optional<RemoteInstance> r = SingleInstanceManager.getRemoteInstance(appKey);
                    assertTrue(r.isPresent());
                    instances.add(r.get());
                    for (int j = 0; j < messagesPerConnector; j++) {
                        exec2.submit(() -> {
                            try {
                                for (; ; ) {
                                    final String message = UUID.randomUUID().toString();
                                    if (!sentMessages.add(message)) {
                                        continue;
                                    }
                                    r.get().sendMessage(message, 1000);
                                    break;
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        });
                    }
                    sendLatch.countDown();
                } catch (Throwable e) {
                    e.printStackTrace();
                }
            });
        }
        assertTrue(sendLatch.await(1, TimeUnit.MINUTES));
        exec2.shutdown();
        assertTrue(exec2.awaitTermination(1, TimeUnit.MINUTES));
        assertTrue(receiveLatch.await(1, TimeUnit.MINUTES));
        assertEquals(sentMessages, receivedMessages);
        for (RemoteInstance remoteInstance : instances) {
            try {
                remoteInstance.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    } finally {
        exec.shutdownNow();
        exec2.shutdownNow();
    }
}
Also used : LocalInstance(org.cryptomator.ui.util.SingleInstanceManager.LocalInstance) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutorService(java.util.concurrent.ExecutorService) RemoteInstance(org.cryptomator.ui.util.SingleInstanceManager.RemoteInstance) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

CountDownLatch (java.util.concurrent.CountDownLatch)2 ExecutorService (java.util.concurrent.ExecutorService)2 LocalInstance (org.cryptomator.ui.util.SingleInstanceManager.LocalInstance)2 RemoteInstance (org.cryptomator.ui.util.SingleInstanceManager.RemoteInstance)2 Test (org.junit.Test)2 HashSet (java.util.HashSet)1 ConcurrentSkipListSet (java.util.concurrent.ConcurrentSkipListSet)1 MessageListener (org.cryptomator.ui.util.SingleInstanceManager.MessageListener)1