Search in sources :

Example 1 with RemoteMap

use of net.morimekta.test.providence.thrift.map.RemoteMap in project providence by morimekta.

the class NonblockingSocketServerTest method testWithProvidenceClient.

@Test
public void testWithProvidenceClient() throws IOException, ExecutionException, InterruptedException, TimeoutException {
    try (NonblockingSocketClientHandler handler = new NonblockingSocketClientHandler(serializer, new InetSocketAddress("localhost", port))) {
        RemoteMap.Client client = new RemoteMap.Client(handler);
        Future<Boolean> a = executor.submit(() -> client.put("a", "1234"));
        Thread.sleep(3);
        Future<Boolean> b = executor.submit(() -> client.put("b", "2345"));
        Thread.sleep(3);
        Future<Boolean> c = executor.submit(() -> client.put("c", "3456"));
        Future<String> f = executor.submit(() -> client.get("f"));
        Thread.sleep(3);
        Future<Boolean> d = executor.submit(() -> client.put("d", "4567"));
        Thread.sleep(3);
        Future<Boolean> e = executor.submit(() -> client.put("e", "5678"));
        assertThat(a.get(1, TimeUnit.SECONDS), is(false));
        assertThat(b.get(1, TimeUnit.SECONDS), is(false));
        assertThat(c.get(1, TimeUnit.SECONDS), is(false));
        assertThat(d.get(1, TimeUnit.SECONDS), is(false));
        assertThat(e.get(1, TimeUnit.SECONDS), is(false));
        try {
            f.get(1, TimeUnit.SECONDS);
            fail("no exception");
        } catch (ExecutionException ee) {
            assertThat(ee.getCause(), is(instanceOf(NotFound.class)));
        }
        assertThat(remoteMap, is(ImmutableMap.of("a", "1234", "b", "2345", "c", "3456", "d", "4567", "e", "5678")));
    }
}
Also used : RemoteMap(net.morimekta.test.providence.thrift.map.RemoteMap) NonblockingSocketClientHandler(net.morimekta.providence.thrift.client.NonblockingSocketClientHandler) InetSocketAddress(java.net.InetSocketAddress) ExecutionException(java.util.concurrent.ExecutionException) NotFound(net.morimekta.test.providence.thrift.map.NotFound) Test(org.junit.Test)

Example 2 with RemoteMap

use of net.morimekta.test.providence.thrift.map.RemoteMap in project providence by morimekta.

the class NonblockingSocketServerTest method testWithPlainThriftClient.

@Test
public void testWithPlainThriftClient() throws IOException, TException, NotFound, InterruptedException {
    try (TSocket socket = new TSocket("localhost", port);
        TFramedTransport transport = new TFramedTransport(socket)) {
        socket.open();
        TProtocol protocol = factory.getProtocol(transport);
        net.morimekta.test.thrift.thrift.map.RemoteMap.Client client = new net.morimekta.test.thrift.thrift.map.RemoteMap.Client(protocol);
        client.put("a", "b");
        client.put("b", "");
        try {
            client.get("c");
            fail("no exception");
        } catch (net.morimekta.test.thrift.thrift.map.NotFound nfe) {
        // nothing to check.
        }
        verify(instrumentation, times(3)).onComplete(anyDouble(), any(PServiceCall.class), any(PServiceCall.class));
        verifyNoMoreInteractions(instrumentation);
        assertThat(remoteMap, is(ImmutableMap.of("a", "b", "b", "")));
    }
    reset(instrumentation);
    remoteMap.clear();
    try (TSocket socket = new TSocket("localhost", port);
        TFramedTransport transport = new TFramedTransport(socket)) {
        socket.open();
        TProtocol protocol = factory.getProtocol(transport);
        net.morimekta.test.thrift.thrift.map.RemoteMap.Client client = new net.morimekta.test.thrift.thrift.map.RemoteMap.Client(protocol);
        client.put("a", "b123");
        client.put("b", "a2345");
        try {
            client.get("c");
            fail("no exception");
        } catch (net.morimekta.test.thrift.thrift.map.NotFound nfe) {
        // nothing to check.
        }
        verify(instrumentation, times(3)).onComplete(anyDouble(), any(PServiceCall.class), any(PServiceCall.class));
        verifyNoMoreInteractions(instrumentation);
        assertThat(remoteMap, is(ImmutableMap.of("a", "b123", "b", "a2345")));
    }
}
Also used : RemoteMap(net.morimekta.test.providence.thrift.map.RemoteMap) TProtocol(org.apache.thrift.protocol.TProtocol) TFramedTransport(org.apache.thrift.transport.TFramedTransport) PServiceCall(net.morimekta.providence.PServiceCall) TSocket(org.apache.thrift.transport.TSocket) Test(org.junit.Test)

Example 3 with RemoteMap

use of net.morimekta.test.providence.thrift.map.RemoteMap in project providence by morimekta.

the class NonblockingSocketServerTest method testWithNonblockingThriftClient.

@Test
public void testWithNonblockingThriftClient() throws IOException, TException, ExecutionException, InterruptedException, TimeoutException {
    CompletableFuture<Boolean> a = new CompletableFuture<>();
    CompletableFuture<Boolean> b = new CompletableFuture<>();
    TAsyncClientManager manager = new TAsyncClientManager();
    try (TNonblockingSocket socket_a = new TNonblockingSocket("localhost", port);
        TNonblockingSocket socket_b = new TNonblockingSocket("localhost", port)) {
        socket_a.startConnect();
        socket_a.finishConnect();
        socket_b.startConnect();
        socket_b.finishConnect();
        net.morimekta.test.thrift.thrift.map.RemoteMap.AsyncClient client_a = new net.morimekta.test.thrift.thrift.map.RemoteMap.AsyncClient(factory, manager, socket_a);
        client_a.put("a", "bb", new AsyncMethodCallback<Boolean>() {

            @Override
            public void onComplete(Boolean response) {
                a.complete(response);
            }

            @Override
            public void onError(Exception exception) {
                a.completeExceptionally(exception);
            }
        });
        // Since thrift clients does not support handling multiple requests at
        // the same time (yes, even the AsyncClient does not support parallel
        // execution).
        net.morimekta.test.thrift.thrift.map.RemoteMap.AsyncClient client_b = new net.morimekta.test.thrift.thrift.map.RemoteMap.AsyncClient(factory, manager, socket_b);
        client_b.put("b", "aaa", new AsyncMethodCallback<Boolean>() {

            @Override
            public void onComplete(Boolean response) {
                b.complete(response);
            }

            @Override
            public void onError(Exception exception) {
                b.completeExceptionally(exception);
            }
        });
        assertThat(a.get(1, TimeUnit.SECONDS), is(Boolean.FALSE));
        assertThat(b.get(1, TimeUnit.SECONDS), is(Boolean.FALSE));
        assertThat(remoteMap, is(ImmutableMap.of("a", "bb", "b", "aaa")));
    }
}
Also used : TNonblockingSocket(org.apache.thrift.transport.TNonblockingSocket) TAsyncClientManager(org.apache.thrift.async.TAsyncClientManager) TimeoutException(java.util.concurrent.TimeoutException) TException(org.apache.thrift.TException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) CompletableFuture(java.util.concurrent.CompletableFuture) RemoteMap(net.morimekta.test.providence.thrift.map.RemoteMap) Test(org.junit.Test)

Aggregations

RemoteMap (net.morimekta.test.providence.thrift.map.RemoteMap)3 Test (org.junit.Test)3 ExecutionException (java.util.concurrent.ExecutionException)2 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 TimeoutException (java.util.concurrent.TimeoutException)1 PServiceCall (net.morimekta.providence.PServiceCall)1 NonblockingSocketClientHandler (net.morimekta.providence.thrift.client.NonblockingSocketClientHandler)1 NotFound (net.morimekta.test.providence.thrift.map.NotFound)1 TException (org.apache.thrift.TException)1 TAsyncClientManager (org.apache.thrift.async.TAsyncClientManager)1 TProtocol (org.apache.thrift.protocol.TProtocol)1 TFramedTransport (org.apache.thrift.transport.TFramedTransport)1 TNonblockingSocket (org.apache.thrift.transport.TNonblockingSocket)1 TSocket (org.apache.thrift.transport.TSocket)1