diff --git a/test/org/rascalmpl/test/rpc/IValueOverJsonTests.java b/test/org/rascalmpl/test/rpc/IValueOverJsonTests.java index 417dadbe87d..0bcf0df7d90 100644 --- a/test/org/rascalmpl/test/rpc/IValueOverJsonTests.java +++ b/test/org/rascalmpl/test/rpc/IValueOverJsonTests.java @@ -3,6 +3,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; +import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -15,12 +16,14 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Supplier; +import org.checkerframework.checker.nullness.qual.Nullable; import org.eclipse.lsp4j.jsonrpc.Launcher; import org.eclipse.lsp4j.jsonrpc.messages.ResponseError; import org.eclipse.lsp4j.jsonrpc.messages.ResponseErrorCode; @@ -74,6 +77,8 @@ public class IValueOverJsonTests { private final PipedInputStream is0, is1; private final PipedOutputStream os0, os1; private final ExecutorService exec = Executors.newCachedThreadPool(); + private final TestThread server; + private final TestClient client; @Parameters(name="{0}") public static Iterable modesAndConfig() { @@ -94,31 +99,42 @@ public IValueOverJsonTests(ComplexTypeMode complexTypeMode, Consumer { + try { + close(is0); + close(is1); + close(os0); + close(os1); + exec.shutdown(); + } + catch (IOException ignored) { + System.err.println("Closing the streams failed with: " + ignored.getMessage()); + ignored.printStackTrace(); + } + }); } - class TestClient { + class TestClient implements Closeable { + private final Future connection; public TestClient(InputStream is, OutputStream os, Consumer gsonConfig) { Launcher clientLauncher = new Launcher.Builder() .setLocalService(this) @@ -133,16 +149,23 @@ public TestClient(InputStream is, OutputStream os, Consumer gsonCon .setExecutorService(exec) .create(); - clientLauncher.startListening(); + connection = clientLauncher.startListening(); testServer = clientLauncher.getRemoteProxy(); } + @Override + public void close() throws IOException { + if (connection != null) { + connection.cancel(true); + } + } } - static class TestThread extends Thread { + static class TestThread extends Thread implements Closeable { private final InputStream is; private final OutputStream os; private final Consumer gsonConfig; private final ExecutorService exec; + private volatile Future socket; public TestThread(InputStream is, OutputStream os, Consumer gsonConfig, ExecutorService exec) { this.is = is; @@ -167,7 +190,14 @@ public void run() { .setExecutorService(exec) .create(); - serverLauncher.startListening(); + socket = serverLauncher.startListening(); + } + + @Override + public void close() throws IOException { + if (socket != null) { + socket.cancel(true); + } } }