@Test public void testCancellation() { TestEndpoint endp = new TestEndpoint(); TestMessageConsumer consumer = new TestMessageConsumer(); RemoteEndpoint endpoint = new DebugRemoteEndpoint(consumer, endp); endpoint.consume(new RequestMessage() {{ setId("1"); setMethod("foo"); 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 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"); }
ResponseMessage response = (ResponseMessage) consumer.messages.get(0); assertNotNull("Check response has error", response.getError()); assertEquals(ResponseErrorCode.InternalError.getValue(), response.getError().getCode()); } finally { logMessages.unregister();
@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(); } }
assertEquals(ResponseErrorCode.InternalError.getValue(), response.getError().getCode()); String exception = (String) response.getError().getData(); String expected = "java.lang.RuntimeException: BAAZ\n\tat org.eclipse.lsp4j.jsonrpc.test.RemoteEndpointTest";