Skip to content

Commit

Permalink
Try to improve stability
Browse files Browse the repository at this point in the history
  • Loading branch information
marci4 committed Jan 1, 2025
1 parent 3be7aaf commit f6d97ea
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/test/java/org/java_websocket/issues/Issue962Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.util.concurrent.CountDownLatch;
import javax.net.SocketFactory;
import org.java_websocket.WebSocket;
import org.java_websocket.client.WebSocketClient;
Expand Down Expand Up @@ -112,6 +113,7 @@ public void onError(Exception ex) {
};

String bindingAddress = "127.0.0.1";
CountDownLatch serverStartedLatch = new CountDownLatch(1);

client.setSocketFactory(new TestSocketFactory(bindingAddress));

Expand All @@ -134,10 +136,12 @@ public void onError(WebSocket conn, Exception ex) {

@Override
public void onStart() {
serverStartedLatch.countDown();
}
};

server.start();
serverStartedLatch.await();
client.connectBlocking();
assertEquals(bindingAddress, client.getSocket().getLocalAddress().getHostAddress());
assertNotEquals(0, client.getSocket().getLocalPort());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
import org.java_websocket.util.SocketUtil;
import org.junit.jupiter.api.*;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

public class OpeningHandshakeRejectionTest {

Expand Down Expand Up @@ -224,7 +224,7 @@ public void testHandshakeRejectionTestCase11() throws Exception {

private void testHandshakeRejection(int i) throws Exception {
startServer();
assumeTrue(SocketUtil.waitForServerToStart(this.port));
assertTrue(SocketUtil.waitForServerToStart(this.port), "Server Start Status");
final int finalI = i;
final CountDownLatch countDownLatch = new CountDownLatch(1);
WebSocketClient webSocketClient = new WebSocketClient(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import org.junit.jupiter.api.*;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

public class ProtocolHandshakeRejectionTest {

Expand Down Expand Up @@ -494,7 +493,7 @@ public void testHandshakeRejectionTestCase29() throws Exception {

private void testProtocolRejection(int i, Draft_6455 draft) throws Exception {
startServer();
assumeTrue(SocketUtil.waitForServerToStart(this.port));
assertTrue(SocketUtil.waitForServerToStart(this.port), "Server Start Status");
final int finalI = i;
final CountDownLatch countDownLatch = new CountDownLatch(1);
final WebSocketClient webSocketClient = new WebSocketClient(
Expand Down
6 changes: 4 additions & 2 deletions src/test/java/org/java_websocket/server/DaemonThreadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public void test_AllCreatedThreadsAreDaemon() throws InterruptedException {

Set<Thread> threadSet1 = Thread.getAllStackTraces().keySet();
final CountDownLatch ready = new CountDownLatch(1);
final CountDownLatch serverStarted = new CountDownLatch(1);

WebSocketServer server = new WebSocketServer(new InetSocketAddress(SocketUtil.getAvailablePort())) {
@Override
Expand All @@ -32,12 +33,13 @@ public void onMessage(WebSocket conn, String message) {}
@Override
public void onError(WebSocket conn, Exception ex) {}
@Override
public void onStart() {}
public void onStart() {serverStarted.countDown();}
};
server.setDaemon(true);
server.setDaemon(false);
server.setDaemon(true);
server.start();
serverStarted.await();

WebSocketClient client = new WebSocketClient(URI.create("ws://localhost:" + server.getPort())) {
@Override
Expand All @@ -59,7 +61,7 @@ public void onError(Exception ex) {}
Set<Thread> threadSet2 = Thread.getAllStackTraces().keySet();
threadSet2.removeAll(threadSet1);

assertFalse(threadSet2.isEmpty(), "new threads created (no new threads indicates issue in test)");
assertFalse(threadSet2.isEmpty(), "new threads created (no new threads indicates issue in test)");

for (Thread t : threadSet2)
assertTrue(t.isDaemon(), t.getName());
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/java_websocket/util/SocketUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static int getAvailablePort() throws InterruptedException {
}
public static boolean waitForServerToStart(int port) throws InterruptedException {
Socket socket = null;
for (int i = 0; i < 10; i++) {
for (int i = 0; i < 50; i++) {
try {
socket = new Socket("localhost", port);
if (socket.isConnected()) {
Expand Down

0 comments on commit f6d97ea

Please sign in to comment.