diff --git a/core/src/test/java/io/questdb/client/test/cutlass/qwp/websocket/TestWebSocketServer.java b/core/src/test/java/io/questdb/client/test/cutlass/qwp/websocket/TestWebSocketServer.java index 908e3bd5..87576a24 100644 --- a/core/src/test/java/io/questdb/client/test/cutlass/qwp/websocket/TestWebSocketServer.java +++ b/core/src/test/java/io/questdb/client/test/cutlass/qwp/websocket/TestWebSocketServer.java @@ -398,6 +398,7 @@ public void start() throws IOException { while (running.get()) { try { Socket clientSocket = serverSocket.accept(); + clientSocket.setTcpNoDelay(true); ClientHandler clientHandler = new ClientHandler(clientSocket); clients.add(clientHandler); clientHandler.start(); @@ -445,12 +446,22 @@ public class ClientHandler implements Closeable { @Override public void close() { running.set(false); + try { + // FIN after the frames already written, never an RST: closing with unread input + // resets the connection and purges anything still queued for the peer. + socket.shutdownOutput(); + if (readThread == Thread.currentThread()) { + drainInput(); + } + } catch (IOException e) { + // ignore + } try { socket.close(); } catch (IOException e) { // ignore } - if (readThread != null) { + if (readThread != null && readThread != Thread.currentThread()) { try { readThread.join(5000); } catch (InterruptedException e) { @@ -459,6 +470,21 @@ public void close() { } } + private void drainInput() throws IOException { + socket.setSoTimeout(20); + byte[] scratch = new byte[8192]; + long deadlineNanos = System.nanoTime() + 200_000_000L; + while (System.nanoTime() < deadlineNanos) { + try { + if (in.read(scratch) < 0) { + return; + } + } catch (SocketTimeoutException e) { + return; + } + } + } + public synchronized void sendBinary(byte[] data) throws IOException { writeFrame(WebSocketOpcode.BINARY, data, data.length); }