From f2a17a5cfc58578d62328e8aff16561252eba890 Mon Sep 17 00:00:00 2001 From: Mingyu Li Date: Tue, 19 Nov 2024 22:42:36 -0600 Subject: [PATCH 1/6] fix NOD flaky test --- .../java_websocket/issues/Issue256Test.java | 208 ++++++++---------- 1 file changed, 94 insertions(+), 114 deletions(-) diff --git a/src/test/java/org/java_websocket/issues/Issue256Test.java b/src/test/java/org/java_websocket/issues/Issue256Test.java index 4faf1fa14..81325067b 100644 --- a/src/test/java/org/java_websocket/issues/Issue256Test.java +++ b/src/test/java/org/java_websocket/issues/Issue256Test.java @@ -31,9 +31,6 @@ import java.io.IOException; import java.net.InetSocketAddress; import java.net.URI; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; import java.util.concurrent.CountDownLatch; import org.java_websocket.WebSocket; import org.java_websocket.client.WebSocketClient; @@ -42,124 +39,107 @@ 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; -@RunWith(Parameterized.class) public class Issue256Test { - private static final int NUMBER_OF_TESTS = 10; - private static WebSocketServer ws; - - private static int port; - static CountDownLatch countServerDownLatch = new CountDownLatch(1); - @Rule - public ThreadCheck zombies = new ThreadCheck(); - - @Parameterized.Parameter - public int count; - - @BeforeClass - public static void startServer() throws Exception { - port = SocketUtil.getAvailablePort(); - ws = 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) { - conn.send(message); - } - - @Override - public void onError(WebSocket conn, Exception ex) { - - ex.printStackTrace(); - assumeThat(true, is(false)); - System.out.println("There should be no exception!"); - } - - @Override - public void onStart() { - countServerDownLatch.countDown(); - } - }; - ws.setConnectionLostTimeout(0); - ws.start(); - countServerDownLatch.await(); - } - - private void runTestScenarioReconnect(boolean closeBlocking) throws Exception { - final CountDownLatch countDownLatch0 = new CountDownLatch(1); - final CountDownLatch countDownLatch1 = new CountDownLatch(2); - WebSocketClient clt = new WebSocketClient(new URI("ws://localhost:" + port)) { - @Override - public void onOpen(ServerHandshake handshakedata) { - countDownLatch1.countDown(); - } - - @Override - public void onMessage(String message) { - - } - - @Override - public void onClose(int code, String reason, boolean remote) { - countDownLatch0.countDown(); - } - - @Override - public void onError(Exception ex) { - ex.printStackTrace(); - assumeThat(true, is(false)); - System.out.println("There should be no exception!"); - } - }; - clt.connectBlocking(); - if (closeBlocking) { - clt.closeBlocking(); - } else { - clt.getSocket().close(); + @Rule + public ThreadCheck zombies = new ThreadCheck(); + + private void runTestScenarioReconnect(boolean closeBlocking) throws Exception { + final CountDownLatch countServerDownLatch = new CountDownLatch(1); + int port = SocketUtil.getAvailablePort(); + WebSocketServer ws = 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) { + conn.send(message); + } + + @Override + public void onError(WebSocket conn, Exception ex) { + + ex.printStackTrace(); + assumeThat(true, is(false)); + System.out.println("There should be no exception!"); + } + + @Override + public void onStart() { + countServerDownLatch.countDown(); + } + }; + ws.setConnectionLostTimeout(0); + ws.start(); + countServerDownLatch.await(); + + final CountDownLatch countDownLatch0 = new CountDownLatch(1); + final CountDownLatch countDownLatch1 = new CountDownLatch(2); + WebSocketClient clt = new WebSocketClient(new URI("ws://localhost:" + port)) { + @Override + public void onOpen(ServerHandshake handshakedata) { + countDownLatch1.countDown(); + } + + @Override + public void onMessage(String message) { + + } + + @Override + public void onClose(int code, String reason, boolean remote) { + countDownLatch0.countDown(); + } + + @Override + public void onError(Exception ex) { + ex.printStackTrace(); + assumeThat(true, is(false)); + System.out.println("There should be no exception!"); + } + }; + clt.connectBlocking(); + if (closeBlocking) { + clt.closeBlocking(); + } else { + clt.getSocket().close(); + } + countDownLatch0.await(); + clt.reconnectBlocking(); + clt.closeBlocking(); + + // **Teardown Phase** + if (ws != null) { + ws.stop(1000); // Wait up to 1 second for the server to stop + ws = null; + } + // Allow time for resources to be released + Thread.sleep(100); } - countDownLatch0.await(); - clt.reconnectBlocking(); - clt.closeBlocking(); - } - - @AfterClass - public static void successTests() throws InterruptedException, IOException { - ws.stop(); - } - - @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}); + + @Test(timeout = 5000 * 10) + public void runReconnectSocketClose() throws Exception { + for (int i = 0; i < 10; i++) { + runTestScenarioReconnect(false); + } + } + + @Test(timeout = 5000 * 10) + public void runReconnectCloseBlocking() throws Exception { + for (int i = 0; i < 10; i++) { + runTestScenarioReconnect(true); + } } - return ret; - } - - @Test(timeout = 5000) - public void runReconnectSocketClose() throws Exception { - runTestScenarioReconnect(false); - } - - @Test(timeout = 5000) - public void runReconnectCloseBlocking() throws Exception { - runTestScenarioReconnect(true); - } } From 8b4f602b610e74e9e9974c29594a0c9e2a180f22 Mon Sep 17 00:00:00 2001 From: Mingyu Li Date: Wed, 20 Nov 2024 01:27:42 -0600 Subject: [PATCH 2/6] format updated --- .../java_websocket/issues/Issue256Test.java | 185 +++++++++--------- 1 file changed, 92 insertions(+), 93 deletions(-) diff --git a/src/test/java/org/java_websocket/issues/Issue256Test.java b/src/test/java/org/java_websocket/issues/Issue256Test.java index 81325067b..53f079114 100644 --- a/src/test/java/org/java_websocket/issues/Issue256Test.java +++ b/src/test/java/org/java_websocket/issues/Issue256Test.java @@ -44,102 +44,101 @@ public class Issue256Test { - @Rule - public ThreadCheck zombies = new ThreadCheck(); - - private void runTestScenarioReconnect(boolean closeBlocking) throws Exception { - final CountDownLatch countServerDownLatch = new CountDownLatch(1); - int port = SocketUtil.getAvailablePort(); - WebSocketServer ws = 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) { - conn.send(message); - } - - @Override - public void onError(WebSocket conn, Exception ex) { - - ex.printStackTrace(); - assumeThat(true, is(false)); - System.out.println("There should be no exception!"); - } - - @Override - public void onStart() { - countServerDownLatch.countDown(); - } - }; - ws.setConnectionLostTimeout(0); - ws.start(); - countServerDownLatch.await(); - - final CountDownLatch countDownLatch0 = new CountDownLatch(1); - final CountDownLatch countDownLatch1 = new CountDownLatch(2); - WebSocketClient clt = new WebSocketClient(new URI("ws://localhost:" + port)) { - @Override - public void onOpen(ServerHandshake handshakedata) { - countDownLatch1.countDown(); - } - - @Override - public void onMessage(String message) { - - } - - @Override - public void onClose(int code, String reason, boolean remote) { - countDownLatch0.countDown(); - } - - @Override - public void onError(Exception ex) { - ex.printStackTrace(); - assumeThat(true, is(false)); - System.out.println("There should be no exception!"); - } - }; - clt.connectBlocking(); - if (closeBlocking) { - clt.closeBlocking(); - } else { - clt.getSocket().close(); - } - countDownLatch0.await(); - clt.reconnectBlocking(); - clt.closeBlocking(); - - // **Teardown Phase** - if (ws != null) { - ws.stop(1000); // Wait up to 1 second for the server to stop - ws = null; - } - // Allow time for resources to be released - Thread.sleep(100); + @Rule + public ThreadCheck zombies = new ThreadCheck(); + + private void runTestScenarioReconnect(boolean closeBlocking) throws Exception { + final CountDownLatch countServerDownLatch = new CountDownLatch(1); + int port = SocketUtil.getAvailablePort(); + WebSocketServer ws = 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) { + conn.send(message); + } + + @Override + public void onError(WebSocket conn, Exception ex) { + + ex.printStackTrace(); + assumeThat(true, is(false)); + System.out.println("There should be no exception!"); + } + + @Override + public void onStart() { + countServerDownLatch.countDown(); + } + }; + ws.setConnectionLostTimeout(0); + ws.start(); + countServerDownLatch.await(); + + final CountDownLatch countDownLatch0 = new CountDownLatch(1); + final CountDownLatch countDownLatch1 = new CountDownLatch(2); + WebSocketClient clt = new WebSocketClient(new URI("ws://localhost:" + port)) { + @Override + public void onOpen(ServerHandshake handshakedata) { + countDownLatch1.countDown(); + } + + @Override + public void onMessage(String message) { + + } + + @Override + public void onClose(int code, String reason, boolean remote) { + countDownLatch0.countDown(); + } + + @Override + public void onError(Exception ex) { + ex.printStackTrace(); + assumeThat(true, is(false)); + System.out.println("There should be no exception!"); + } + }; + clt.connectBlocking(); + if (closeBlocking) { + clt.closeBlocking(); + } else { + clt.getSocket().close(); } - - @Test(timeout = 5000 * 10) - public void runReconnectSocketClose() throws Exception { - for (int i = 0; i < 10; i++) { - runTestScenarioReconnect(false); - } + countDownLatch0.await(); + clt.reconnectBlocking(); + clt.closeBlocking(); + + // **Teardown Phase** + if (ws != null) { + ws.stop(1000); // Wait up to 1 second for the server to stop + ws = null; } - - @Test(timeout = 5000 * 10) - public void runReconnectCloseBlocking() throws Exception { - for (int i = 0; i < 10; i++) { - runTestScenarioReconnect(true); - } + // Allow time for resources to be released + Thread.sleep(100); + } + + @Test(timeout = 5000 * 10) + public void runReconnectSocketClose() throws Exception { + for (int i = 0; i < 10; i++) { + runTestScenarioReconnect(false); } + } + @Test(timeout = 5000 * 10) + public void runReconnectCloseBlocking() throws Exception { + for (int i = 0; i < 10; i++) { + runTestScenarioReconnect(true); + } + } } From 4e65ca56bf93af1c7c94e68c1f6eeda93d2844e7 Mon Sep 17 00:00:00 2001 From: Mingyu Li Date: Wed, 20 Nov 2024 01:43:42 -0600 Subject: [PATCH 3/6] update --- .../java/org/java_websocket/issues/Issue256Test.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/test/java/org/java_websocket/issues/Issue256Test.java b/src/test/java/org/java_websocket/issues/Issue256Test.java index 53f079114..7ad757276 100644 --- a/src/test/java/org/java_websocket/issues/Issue256Test.java +++ b/src/test/java/org/java_websocket/issues/Issue256Test.java @@ -44,6 +44,7 @@ public class Issue256Test { + private static final int NUMBER_OF_TESTS = 10; @Rule public ThreadCheck zombies = new ThreadCheck(); @@ -127,18 +128,19 @@ public void onError(Exception ex) { Thread.sleep(100); } - @Test(timeout = 5000 * 10) + @Test(timeout = 5000 * NUMBER_OF_TESTS) public void runReconnectSocketClose() throws Exception { - for (int i = 0; i < 10; i++) { + for (int i = 0; i < NUMBER_OF_TESTS; i++) { runTestScenarioReconnect(false); } } - @Test(timeout = 5000 * 10) + @Test(timeout = 5000 * NUMBER_OF_TESTS) public void runReconnectCloseBlocking() throws Exception { - for (int i = 0; i < 10; i++) { + for (int i = 0; i < NUMBER_OF_TESTS; i++) { runTestScenarioReconnect(true); } } + } From 2038b46d2bca43386ae508bd52e01bcb9ad27ab0 Mon Sep 17 00:00:00 2001 From: Mingyu Li Date: Wed, 20 Nov 2024 02:37:47 -0600 Subject: [PATCH 4/6] trivial --- src/test/java/org/java_websocket/issues/Issue256Test.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/test/java/org/java_websocket/issues/Issue256Test.java b/src/test/java/org/java_websocket/issues/Issue256Test.java index 7ad757276..2df984f8a 100644 --- a/src/test/java/org/java_websocket/issues/Issue256Test.java +++ b/src/test/java/org/java_websocket/issues/Issue256Test.java @@ -119,13 +119,11 @@ public void onError(Exception ex) { clt.reconnectBlocking(); clt.closeBlocking(); - // **Teardown Phase** if (ws != null) { ws.stop(1000); // Wait up to 1 second for the server to stop ws = null; } - // Allow time for resources to be released - Thread.sleep(100); + Thread.sleep(100); // Allow time for resources to be released } @Test(timeout = 5000 * NUMBER_OF_TESTS) From 93b83297a79902335f1de6a7c6b2131f7a743c32 Mon Sep 17 00:00:00 2001 From: Mingyu Li Date: Wed, 20 Nov 2024 02:39:29 -0600 Subject: [PATCH 5/6] space --- src/test/java/org/java_websocket/issues/Issue256Test.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/org/java_websocket/issues/Issue256Test.java b/src/test/java/org/java_websocket/issues/Issue256Test.java index 2df984f8a..838b7ba3d 100644 --- a/src/test/java/org/java_websocket/issues/Issue256Test.java +++ b/src/test/java/org/java_websocket/issues/Issue256Test.java @@ -118,7 +118,6 @@ public void onError(Exception ex) { countDownLatch0.await(); clt.reconnectBlocking(); clt.closeBlocking(); - if (ws != null) { ws.stop(1000); // Wait up to 1 second for the server to stop ws = null; From 5ec6ece2a79145040bf3bb6297c536a3630690be Mon Sep 17 00:00:00 2001 From: Mingyu Li Date: Wed, 27 Nov 2024 23:17:33 -0600 Subject: [PATCH 6/6] min diff --- .../java_websocket/issues/Issue256Test.java | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/test/java/org/java_websocket/issues/Issue256Test.java b/src/test/java/org/java_websocket/issues/Issue256Test.java index 838b7ba3d..0b730086d 100644 --- a/src/test/java/org/java_websocket/issues/Issue256Test.java +++ b/src/test/java/org/java_websocket/issues/Issue256Test.java @@ -39,19 +39,24 @@ 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; public class Issue256Test { private static final int NUMBER_OF_TESTS = 10; + private WebSocketServer ws; + private int port; + CountDownLatch countServerDownLatch = new CountDownLatch(1); @Rule public ThreadCheck zombies = new ThreadCheck(); - private void runTestScenarioReconnect(boolean closeBlocking) throws Exception { - final CountDownLatch countServerDownLatch = new CountDownLatch(1); - int port = SocketUtil.getAvailablePort(); - WebSocketServer ws = new WebSocketServer(new InetSocketAddress(port), 16) { + @BeforeClass + public void startServer() throws Exception { + port = SocketUtil.getAvailablePort(); + ws = new WebSocketServer(new InetSocketAddress(port), 16) { @Override public void onOpen(WebSocket conn, ClientHandshake handshake) { @@ -83,7 +88,9 @@ public void onStart() { ws.setConnectionLostTimeout(0); ws.start(); countServerDownLatch.await(); + } + private void runTestScenarioReconnect(boolean closeBlocking) throws Exception { final CountDownLatch countDownLatch0 = new CountDownLatch(1); final CountDownLatch countDownLatch1 = new CountDownLatch(2); WebSocketClient clt = new WebSocketClient(new URI("ws://localhost:" + port)) { @@ -118,11 +125,11 @@ public void onError(Exception ex) { countDownLatch0.await(); clt.reconnectBlocking(); clt.closeBlocking(); - if (ws != null) { - ws.stop(1000); // Wait up to 1 second for the server to stop - ws = null; - } - Thread.sleep(100); // Allow time for resources to be released + } + + @AfterClass + public void successTests() throws InterruptedException, IOException { + ws.stop(); } @Test(timeout = 5000 * NUMBER_OF_TESTS)