/** * Create the remote endpoint that communicates with the local services. */ protected RemoteEndpoint createRemoteEndpoint(MessageJsonHandler jsonHandler) { MessageConsumer outgoingMessageStream = new StreamMessageConsumer(output, jsonHandler); outgoingMessageStream = wrapMessageConsumer(outgoingMessageStream); RemoteEndpoint remoteEndpoint = new RemoteEndpoint(outgoingMessageStream, ServiceEndpoints.toEndpoint(localServices)); jsonHandler.setMethodProvider(remoteEndpoint); return remoteEndpoint; } }
/** * Create the remote endpoint that communicates with the local services. */ protected RemoteEndpoint createRemoteEndpoint(MessageJsonHandler jsonHandler) { MessageConsumer outgoingMessageStream = new StreamMessageConsumer(output, jsonHandler); outgoingMessageStream = wrapMessageConsumer(outgoingMessageStream); RemoteEndpoint remoteEndpoint = new RemoteEndpoint(outgoingMessageStream, ServiceEndpoints.toEndpoint(localServices)); jsonHandler.setMethodProvider(remoteEndpoint); return remoteEndpoint; } }
@Test public void testExceptionInConsumer() throws Exception { TestEndpoint endp = new TestEndpoint(); MessageConsumer consumer = message -> { throw new RuntimeException("BAAZ"); }; RemoteEndpoint endpoint = new RemoteEndpoint(consumer, endp); CompletableFuture<Object> future = endpoint.request("foo", "myparam"); future.whenComplete((result, exception) -> { assertNull(result); assertNotNull(exception); assertEquals("BAAZ", exception.getMessage()); }); try { future.get(TIMEOUT, TimeUnit.MILLISECONDS); Assert.fail("Expected an ExecutionException."); } catch (ExecutionException exception) { assertEquals("java.lang.RuntimeException: BAAZ", exception.getMessage()); } }
@Test public void testExceptionInCompletableFuture() throws Exception { TestEndpoint endp = new TestEndpoint(); TestMessageConsumer consumer = new TestMessageConsumer(); RemoteEndpoint endpoint = new RemoteEndpoint(consumer, endp); CompletableFuture<Object> future = endpoint.request("foo", "myparam"); CompletableFuture<Void> chained = future.thenAccept(result -> { throw new RuntimeException("BAAZ"); }); endpoint.consume(init(new ResponseMessage(), it -> { it.setId("1"); it.setResult("Bar"); })); try { chained.get(TIMEOUT, TimeUnit.MILLISECONDS); Assert.fail("Expected an ExecutionException."); } catch (ExecutionException exception) { assertEquals("java.lang.RuntimeException: BAAZ", exception.getMessage()); } }
@Test public void testRequest1() { TestEndpoint endp = new TestEndpoint(); TestMessageConsumer consumer = new TestMessageConsumer(); RemoteEndpoint endpoint = new RemoteEndpoint(consumer, endp); endpoint.consume(init(new RequestMessage(), it -> { it.setId("1"); it.setMethod("foo"); it.setParams("myparam"); })); Entry<RequestMessage, CompletableFuture<Object>> entry = endp.requests.entrySet().iterator().next(); entry.getValue().complete("success"); assertEquals("foo", entry.getKey().getMethod()); assertEquals("myparam", entry.getKey().getParams()); ResponseMessage responseMessage = (ResponseMessage) consumer.messages.get(0); assertEquals("success", responseMessage.getResult()); assertEquals(Either.forLeft("1"), responseMessage.getRawId()); }
@Test public void testRequest2() { TestEndpoint endp = new TestEndpoint(); TestMessageConsumer consumer = new TestMessageConsumer(); RemoteEndpoint endpoint = new RemoteEndpoint(consumer, endp); endpoint.consume(init(new RequestMessage(), it -> { it.setId(1); it.setMethod("foo"); it.setParams("myparam"); })); Entry<RequestMessage, CompletableFuture<Object>> entry = endp.requests.entrySet().iterator().next(); entry.getValue().complete("success"); assertEquals("foo", entry.getKey().getMethod()); assertEquals("myparam", entry.getKey().getParams()); ResponseMessage responseMessage = (ResponseMessage) consumer.messages.get(0); assertEquals("success", responseMessage.getResult()); assertEquals(Either.forRight(1), responseMessage.getRawId()); }
@Test public void testCancellation() { TestEndpoint endp = new TestEndpoint(); TestMessageConsumer consumer = new TestMessageConsumer(); RemoteEndpoint endpoint = new RemoteEndpoint(consumer, endp); endpoint.consume(init(new RequestMessage(), it -> { it.setId("1"); it.setMethod("foo"); it.setParams("myparam"); })); Entry<RequestMessage, CompletableFuture<Object>> entry = endp.requests.entrySet().iterator().next(); entry.getValue().cancel(true); ResponseMessage message = (ResponseMessage) consumer.messages.get(0); assertNotNull(message); ResponseError error = message.getError(); assertNotNull(error); assertEquals(error.getCode(), ResponseErrorCode.RequestCancelled.getValue()); assertEquals(error.getMessage(), "The request (id: 1, method: 'foo') has been cancelled"); }
@Test public void testNullResponse() throws InterruptedException, ExecutionException { LogMessageAccumulator logMessages = new LogMessageAccumulator(); try { logMessages.registerTo(GenericEndpoint.class); Endpoint endpoint = ServiceEndpoints.toEndpoint(this); Map<String, JsonRpcMethod> methods = ServiceEndpoints.getSupportedMethods(LanguageServer.class); MessageJsonHandler handler = new MessageJsonHandler(methods); List<Message> msgs = new ArrayList<>(); MessageConsumer consumer = (message) -> { msgs.add(message); }; RemoteEndpoint re = new RemoteEndpoint(consumer, endpoint); RequestMessage request = new RequestMessage(); request.setId("1"); request.setMethod("shutdown"); re.consume(request); Assert.assertEquals("{\"jsonrpc\":\"2.0\",\"id\":\"1\",\"result\":null}", handler.serialize(msgs.get(0))); msgs.clear(); shutdownReturn = new Object(); re.consume(request); Assert.assertEquals("{\"jsonrpc\":\"2.0\",\"id\":\"1\",\"result\":{}}", handler.serialize(msgs.get(0))); } finally { logMessages.unregister(); } }
@Test public void testNotification() { TestEndpoint endp = new TestEndpoint(); TestMessageConsumer consumer = new TestMessageConsumer(); RemoteEndpoint endpoint = new RemoteEndpoint(consumer, endp); endpoint.consume(init(new NotificationMessage(), it -> { it.setMethod("foo"); it.setParams("myparam"); })); NotificationMessage notificationMessage = endp.notifications.get(0); assertEquals("foo", notificationMessage.getMethod()); assertEquals("myparam", notificationMessage.getParams()); assertTrue(consumer.messages.isEmpty()); }
@Test public void testHandleRequestIssues() { TestEndpoint endp = new TestEndpoint(); TestMessageConsumer consumer = new TestMessageConsumer(); RemoteEndpoint endpoint = new RemoteEndpoint(consumer, endp); endpoint.handle(init(new RequestMessage(), it -> { it.setId("1"); it.setMethod("foo"); it.setParams("myparam"); }), Collections.singletonList(new MessageIssue("bar"))); ResponseMessage responseMessage = (ResponseMessage) consumer.messages.get(0); assertNotNull(responseMessage.getError()); assertEquals("bar", responseMessage.getError().getMessage()); }
@Test public void testExceptionInOutputStream() throws Exception { LogMessageAccumulator logMessages = new LogMessageAccumulator(); try { logMessages.registerTo(RemoteEndpoint.class); TestEndpoint endp = new TestEndpoint(); MessageConsumer consumer = new MessageConsumer() { @Override public void consume(Message message) throws JsonRpcException { throw new JsonRpcException(new SocketException("Permission denied: connect")); } }; RemoteEndpoint endpoint = new RemoteEndpoint(consumer, endp); endpoint.notify("foo", null); logMessages.await(Level.WARNING, "Failed to send notification message."); } finally { logMessages.unregister(); } }
@Test public void testOutputStreamClosed() throws Exception { LogMessageAccumulator logMessages = new LogMessageAccumulator(); try { logMessages.registerTo(RemoteEndpoint.class); TestEndpoint endp = new TestEndpoint(); MessageConsumer consumer = new MessageConsumer() { @Override public void consume(Message message) throws JsonRpcException { throw new JsonRpcException(new SocketException("Socket closed")); } }; RemoteEndpoint endpoint = new RemoteEndpoint(consumer, endp); endpoint.notify("foo", null); logMessages.await(Level.INFO, "Failed to send notification message."); } finally { logMessages.unregister(); } }
TestMessageConsumer2 consumer = new TestMessageConsumer2(); RemoteEndpoint endpoint = new RemoteEndpoint(consumer, endp, (e) -> null);
@Test public void testExceptionHandlerMisbehaving1() { LogMessageAccumulator logMessages = new LogMessageAccumulator(); try { // Don't show the exception in the test execution log logMessages.registerTo(RemoteEndpoint.class); TestEndpoint endp = new TestEndpoint() { @Override public CompletableFuture<Object> request(String method, Object parameter) { throw new RuntimeException("BAAZ"); } }; TestMessageConsumer consumer = new TestMessageConsumer(); // Misbehaving exception handler that returns null RemoteEndpoint endpoint = new RemoteEndpoint(consumer, endp, (e) -> null); endpoint.consume(init(new RequestMessage(), it -> { it.setId("1"); it.setMethod("foo"); it.setParams("myparam"); })); assertEquals("Check some response received", 1, consumer.messages.size()); ResponseMessage response = (ResponseMessage) consumer.messages.get(0); assertEquals(ResponseErrorCode.InternalError.getValue(), response.getError().getCode()); } finally { logMessages.unregister(); } }
RemoteEndpoint endpoint = new RemoteEndpoint(consumer, endp);