Skip to content

Commit 3be7aaf

Browse files
committed
Check for server status
1 parent cd0829f commit 3be7aaf

File tree

3 files changed

+41
-11
lines changed

3 files changed

+41
-11
lines changed

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,14 @@
4141
import org.junit.jupiter.api.*;
4242

4343
import static org.junit.jupiter.api.Assertions.fail;
44+
import static org.junit.jupiter.api.Assumptions.assumeTrue;
4445

4546
public class OpeningHandshakeRejectionTest {
4647

4748
private int port = -1;
4849
private Thread thread;
4950
private ServerSocket serverSocket;
5051

51-
private final CountDownLatch serverStartCountDownLatch = new CountDownLatch(1);
52-
5352
private static final String additionalHandshake = "Upgrade: websocket\r\nConnection: Upgrade\r\n\r\n";
5453

5554
public void startServer() throws InterruptedException {
@@ -59,7 +58,6 @@ public void startServer() throws InterruptedException {
5958
try {
6059
serverSocket = new ServerSocket(port);
6160
serverSocket.setReuseAddress(true);
62-
serverStartCountDownLatch.countDown();
6361
while (true) {
6462
Socket client = null;
6563
try {
@@ -144,8 +142,12 @@ public void startServer() throws InterruptedException {
144142

145143
@AfterEach
146144
public void cleanUp() throws IOException {
147-
serverSocket.close();
148-
thread.interrupt();
145+
if (serverSocket != null) {
146+
serverSocket.close();
147+
}
148+
if (thread != null) {
149+
thread.interrupt();
150+
}
149151
}
150152

151153
@Test()
@@ -219,9 +221,10 @@ public void testHandshakeRejectionTestCase10() throws Exception {
219221
public void testHandshakeRejectionTestCase11() throws Exception {
220222
testHandshakeRejection(11);
221223
}
224+
222225
private void testHandshakeRejection(int i) throws Exception {
223226
startServer();
224-
this.serverStartCountDownLatch.await();
227+
assumeTrue(SocketUtil.waitForServerToStart(this.port));
225228
final int finalI = i;
226229
final CountDownLatch countDownLatch = new CountDownLatch(1);
227230
WebSocketClient webSocketClient = new WebSocketClient(

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@
4848
import org.junit.jupiter.api.*;
4949

5050
import static org.junit.jupiter.api.Assertions.*;
51+
import static org.junit.jupiter.api.Assumptions.assumeTrue;
5152

5253
public class ProtocolHandshakeRejectionTest {
5354

5455
private static final String additionalHandshake = "HTTP/1.1 101 Websocket Connection Upgrade\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n";
5556
private Thread thread;
5657
private ServerSocket serverSocket;
57-
private final CountDownLatch serverStartCountDownLatch = new CountDownLatch(1);
5858

5959
private int port = -1;
6060

@@ -65,7 +65,6 @@ public void startServer() throws InterruptedException {
6565
try {
6666
serverSocket = new ServerSocket(port);
6767
serverSocket.setReuseAddress(true);
68-
serverStartCountDownLatch.countDown();
6968
while (true) {
7069
Socket client = null;
7170
try {
@@ -268,8 +267,12 @@ private static String getSecKey(String seckey) {
268267

269268
@AfterEach
270269
public void successTests() throws IOException {
271-
serverSocket.close();
272-
thread.interrupt();
270+
if (serverSocket != null) {
271+
serverSocket.close();
272+
}
273+
if (thread != null) {
274+
thread.interrupt();
275+
}
273276
}
274277

275278
@Test()
@@ -491,7 +494,7 @@ public void testHandshakeRejectionTestCase29() throws Exception {
491494

492495
private void testProtocolRejection(int i, Draft_6455 draft) throws Exception {
493496
startServer();
494-
serverStartCountDownLatch.await();
497+
assumeTrue(SocketUtil.waitForServerToStart(this.port));
495498
final int finalI = i;
496499
final CountDownLatch countDownLatch = new CountDownLatch(1);
497500
final WebSocketClient webSocketClient = new WebSocketClient(

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import java.io.IOException;
2929
import java.net.ServerSocket;
30+
import java.net.Socket;
3031

3132
public class SocketUtil {
3233

@@ -37,6 +38,29 @@ public static int getAvailablePort() throws InterruptedException {
3738
} catch (IOException e) {
3839
// Retry
3940
}
41+
Thread.sleep(5);
4042
}
4143
}
44+
public static boolean waitForServerToStart(int port) throws InterruptedException {
45+
Socket socket = null;
46+
for (int i = 0; i < 10; i++) {
47+
try {
48+
socket = new Socket("localhost", port);
49+
if (socket.isConnected()) {
50+
return true;
51+
}
52+
} catch (IOException ignore) {
53+
// Ignore
54+
} finally {
55+
if (socket != null) {
56+
try {
57+
socket.close();
58+
} catch (IOException ignore) {
59+
}
60+
}
61+
}
62+
Thread.sleep(10);
63+
}
64+
return false;
65+
}
4266
}

0 commit comments

Comments
 (0)