Skip to content

Commit f6d97ea

Browse files
committed
Try to improve stability
1 parent 3be7aaf commit f6d97ea

File tree

5 files changed

+12
-7
lines changed

5 files changed

+12
-7
lines changed

src/test/java/org/java_websocket/issues/Issue962Test.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.net.URI;
3232
import java.net.URISyntaxException;
3333
import java.net.UnknownHostException;
34+
import java.util.concurrent.CountDownLatch;
3435
import javax.net.SocketFactory;
3536
import org.java_websocket.WebSocket;
3637
import org.java_websocket.client.WebSocketClient;
@@ -112,6 +113,7 @@ public void onError(Exception ex) {
112113
};
113114

114115
String bindingAddress = "127.0.0.1";
116+
CountDownLatch serverStartedLatch = new CountDownLatch(1);
115117

116118
client.setSocketFactory(new TestSocketFactory(bindingAddress));
117119

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

135137
@Override
136138
public void onStart() {
139+
serverStartedLatch.countDown();
137140
}
138141
};
139142

140143
server.start();
144+
serverStartedLatch.await();
141145
client.connectBlocking();
142146
assertEquals(bindingAddress, client.getSocket().getLocalAddress().getHostAddress());
143147
assertNotEquals(0, client.getSocket().getLocalPort());

src/test/java/org/java_websocket/misc/OpeningHandshakeRejectionTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040
import org.java_websocket.util.SocketUtil;
4141
import org.junit.jupiter.api.*;
4242

43+
import static org.junit.jupiter.api.Assertions.assertTrue;
4344
import static org.junit.jupiter.api.Assertions.fail;
44-
import static org.junit.jupiter.api.Assumptions.assumeTrue;
4545

4646
public class OpeningHandshakeRejectionTest {
4747

@@ -224,7 +224,7 @@ public void testHandshakeRejectionTestCase11() throws Exception {
224224

225225
private void testHandshakeRejection(int i) throws Exception {
226226
startServer();
227-
assumeTrue(SocketUtil.waitForServerToStart(this.port));
227+
assertTrue(SocketUtil.waitForServerToStart(this.port), "Server Start Status");
228228
final int finalI = i;
229229
final CountDownLatch countDownLatch = new CountDownLatch(1);
230230
WebSocketClient webSocketClient = new WebSocketClient(

src/test/java/org/java_websocket/protocols/ProtocolHandshakeRejectionTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
import org.junit.jupiter.api.*;
4949

5050
import static org.junit.jupiter.api.Assertions.*;
51-
import static org.junit.jupiter.api.Assumptions.assumeTrue;
5251

5352
public class ProtocolHandshakeRejectionTest {
5453

@@ -494,7 +493,7 @@ public void testHandshakeRejectionTestCase29() throws Exception {
494493

495494
private void testProtocolRejection(int i, Draft_6455 draft) throws Exception {
496495
startServer();
497-
assumeTrue(SocketUtil.waitForServerToStart(this.port));
496+
assertTrue(SocketUtil.waitForServerToStart(this.port), "Server Start Status");
498497
final int finalI = i;
499498
final CountDownLatch countDownLatch = new CountDownLatch(1);
500499
final WebSocketClient webSocketClient = new WebSocketClient(

src/test/java/org/java_websocket/server/DaemonThreadTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public void test_AllCreatedThreadsAreDaemon() throws InterruptedException {
2121

2222
Set<Thread> threadSet1 = Thread.getAllStackTraces().keySet();
2323
final CountDownLatch ready = new CountDownLatch(1);
24+
final CountDownLatch serverStarted = new CountDownLatch(1);
2425

2526
WebSocketServer server = new WebSocketServer(new InetSocketAddress(SocketUtil.getAvailablePort())) {
2627
@Override
@@ -32,12 +33,13 @@ public void onMessage(WebSocket conn, String message) {}
3233
@Override
3334
public void onError(WebSocket conn, Exception ex) {}
3435
@Override
35-
public void onStart() {}
36+
public void onStart() {serverStarted.countDown();}
3637
};
3738
server.setDaemon(true);
3839
server.setDaemon(false);
3940
server.setDaemon(true);
4041
server.start();
42+
serverStarted.await();
4143

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

62-
assertFalse(threadSet2.isEmpty(), "new threads created (no new threads indicates issue in test)");
64+
assertFalse(threadSet2.isEmpty(), "new threads created (no new threads indicates issue in test)");
6365

6466
for (Thread t : threadSet2)
6567
assertTrue(t.isDaemon(), t.getName());

src/test/java/org/java_websocket/util/SocketUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static int getAvailablePort() throws InterruptedException {
4343
}
4444
public static boolean waitForServerToStart(int port) throws InterruptedException {
4545
Socket socket = null;
46-
for (int i = 0; i < 10; i++) {
46+
for (int i = 0; i < 50; i++) {
4747
try {
4848
socket = new Socket("localhost", port);
4949
if (socket.isConnected()) {

0 commit comments

Comments
 (0)