From a24e55805510282475b2c1d62ee734d338f281ca Mon Sep 17 00:00:00 2001 From: oakkitten Date: Thu, 8 Nov 2018 11:29:43 +0200 Subject: [PATCH 1/2] add test for issue #347 based on oakkitten/Java-WebSocket/commit/b352aad06272b8 --- .../java_websocket/issues/AllIssueTests.java | 3 +- .../java_websocket/issues/Issue347Test.java | 116 ++++++++++++++++++ 2 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 src/test/java/org/java_websocket/issues/Issue347Test.java diff --git a/src/test/java/org/java_websocket/issues/AllIssueTests.java b/src/test/java/org/java_websocket/issues/AllIssueTests.java index f763d04e..bb1fc9cb 100644 --- a/src/test/java/org/java_websocket/issues/AllIssueTests.java +++ b/src/test/java/org/java_websocket/issues/AllIssueTests.java @@ -43,7 +43,8 @@ org.java_websocket.issues.Issue765Test.class, org.java_websocket.issues.Issue825Test.class, org.java_websocket.issues.Issue834Test.class, - org.java_websocket.issues.Issue962Test.class + org.java_websocket.issues.Issue962Test.class + org.java_websocket.issues.Issue347Test.class }) /** * Start all tests for issues diff --git a/src/test/java/org/java_websocket/issues/Issue347Test.java b/src/test/java/org/java_websocket/issues/Issue347Test.java new file mode 100644 index 00000000..8c2a7b07 --- /dev/null +++ b/src/test/java/org/java_websocket/issues/Issue347Test.java @@ -0,0 +1,116 @@ +package org.java_websocket.issues; + +import org.java_websocket.WebSocket; +import org.java_websocket.client.WebSocketClient; +import org.java_websocket.handshake.ClientHandshake; +import org.java_websocket.handshake.ServerHandshake; +import org.java_websocket.server.WebSocketServer; +import org.java_websocket.util.SocketUtil; +import org.java_websocket.util.ThreadCheck; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.net.SocketAddress; +import java.net.URI; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +// this tests whether the client gets properly closed while it is connecting. the method +// WebSocketClient.closeConnection(...) is called shortly after WebSocketClient.connect(); +// the client is using a socket that takes at least 100ms to connect. then we check for zombies. + +@RunWith(Parameterized.class) +public class Issue347Test { + + private static final int NUMBER_OF_TESTS = 20; + private static final int DELAY_MULTIPLIER = 10; + + private static WebSocketServer server; + private static int port; + + @Parameterized.Parameter + public int delay; + + @Rule + public ThreadCheck zombies = new ThreadCheck(); + + @BeforeClass + public static void startServer() throws Exception { + port = SocketUtil.getAvailablePort(); + server = new WebSocketServer(new InetSocketAddress(port) , 16) { + @Override + public void onOpen(WebSocket conn, ClientHandshake handshake) {} + + @Override + public void onClose(WebSocket conn, int code, String reason, boolean remote) {} + + @Override + public void onMessage(WebSocket conn, String message) {} + + @Override + public void onError(WebSocket conn, Exception ex) {} + + @Override + public void onStart() {} + }; + server.setConnectionLostTimeout(0); + server.start(); + } + + @AfterClass + public static void stopServer() throws InterruptedException, IOException { + server.stop(); + } + + @Test(timeout = 5000) + public void runTestScenario() throws Exception { + final WebSocketClient client = new WebSocketClient( new URI("ws://localhost:" + port)) { + @Override + public void onOpen(ServerHandshake handshakedata) {} + + @Override + public void onMessage( String message ) {} + + @Override + public void onClose( int code, String reason, boolean remote ) {} + + @Override + public void onError( Exception ex ) {} + }; + client.setSocket(new SlowSocket()); + client.connect(); + Thread.sleep(delay); + client.closeConnection(1000, "foo"); + } + + @Parameterized.Parameters + public static Collection data() { + List ret = new ArrayList(NUMBER_OF_TESTS); + for (int i = 0; i < NUMBER_OF_TESTS; i++) ret.add(new Integer[]{i * DELAY_MULTIPLIER}); + return ret; + } + + private static class SlowSocket extends Socket { + @Override public void connect(SocketAddress socketAddress, int i) throws IOException { + sleep(100); + super.connect(socketAddress, i); + } + } + + @SuppressWarnings("SameParameterValue") + private static void sleep(long interval) { + try { + Thread.sleep(interval); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } +} From 5cbe65d5527b9c505df3113aae33a69dd2f9dbbf Mon Sep 17 00:00:00 2001 From: chenguoping Date: Tue, 28 Jul 2020 16:28:53 +0800 Subject: [PATCH 2/2] fix the zombie thread : WebSocketWorker --- src/test/java/org/java_websocket/issues/Issue347Test.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/java_websocket/issues/Issue347Test.java b/src/test/java/org/java_websocket/issues/Issue347Test.java index 8c2a7b07..9a1be706 100644 --- a/src/test/java/org/java_websocket/issues/Issue347Test.java +++ b/src/test/java/org/java_websocket/issues/Issue347Test.java @@ -35,7 +35,8 @@ public class Issue347Test { private static WebSocketServer server; private static int port; - + static CountDownLatch countServerDownLatch = new CountDownLatch( 1 ); + @Parameterized.Parameter public int delay; @@ -59,10 +60,13 @@ public void onMessage(WebSocket conn, String message) {} public void onError(WebSocket conn, Exception ex) {} @Override - public void onStart() {} + public void onStart() { + countServerDownLatch.countDown(); + } }; server.setConnectionLostTimeout(0); server.start(); + countServerDownLatch.await(); } @AfterClass