Skip to content

Commit d41630d

Browse files
committed
Use latches to wait for server setup
1 parent c36dea7 commit d41630d

File tree

2 files changed

+19
-21
lines changed

2 files changed

+19
-21
lines changed

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

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

43-
import static org.junit.jupiter.api.Assertions.assertTrue;
4443
import static org.junit.jupiter.api.Assertions.fail;
4544

4645
public class OpeningHandshakeRejectionTest {
@@ -49,28 +48,27 @@ public class OpeningHandshakeRejectionTest {
4948
private Thread thread;
5049
private ServerSocket serverSocket;
5150

52-
private boolean beforeeach_called;
51+
private final CountDownLatch serverStartCountDownLatch = new CountDownLatch(1);
5352

5453
private static final String additionalHandshake = "Upgrade: websocket\r\nConnection: Upgrade\r\n\r\n";
5554

5655
@BeforeEach
57-
public void startServer() {
58-
59-
try {
60-
port = SocketUtil.getAvailablePort();
61-
} catch (InterruptedException e) {
62-
throw new RuntimeException(e);
63-
}
64-
thread = new Thread(
56+
public void startServer() throws InterruptedException {
57+
this.port = SocketUtil.getAvailablePort();
58+
this.thread = new Thread(
6559
() -> {
6660
try {
6761
serverSocket = new ServerSocket(port);
6862
serverSocket.setReuseAddress(true);
63+
serverStartCountDownLatch.countDown();
6964
while (true) {
7065
Socket client = null;
7166
try {
7267
client = serverSocket.accept();
7368
Scanner in = new Scanner(client.getInputStream());
69+
if (!in.hasNextLine()) {
70+
continue;
71+
}
7472
String input = in.nextLine();
7573
String testCase = input.split(" ")[1];
7674
OutputStream os = client.getOutputStream();
@@ -142,8 +140,7 @@ public void startServer() {
142140
fail("There should not be an exception: " + e.getMessage() + " Port: " + port);
143141
}
144142
});
145-
thread.start();
146-
beforeeach_called = true;
143+
this.thread.start();
147144
}
148145

149146
@AfterEach
@@ -223,13 +220,12 @@ public void testHandshakeRejectionTestCase10() throws Exception {
223220
public void testHandshakeRejectionTestCase11() throws Exception {
224221
testHandshakeRejection(11);
225222
}
226-
227223
private void testHandshakeRejection(int i) throws Exception {
228-
assertTrue(beforeeach_called, "BeforeEach is called");
224+
this.serverStartCountDownLatch.await();
229225
final int finalI = i;
230226
final CountDownLatch countDownLatch = new CountDownLatch(1);
231227
WebSocketClient webSocketClient = new WebSocketClient(
232-
new URI("ws://localhost:" + port + "/" + finalI)) {
228+
new URI("ws://localhost:" + this.port + "/" + finalI)) {
233229
@Override
234230
public void onOpen(ServerHandshake handshakedata) {
235231
fail("There should not be a connection!");

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,26 +54,27 @@ public class ProtocolHandshakeRejectionTest {
5454
private static final String additionalHandshake = "HTTP/1.1 101 Websocket Connection Upgrade\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n";
5555
private Thread thread;
5656
private ServerSocket serverSocket;
57+
private final CountDownLatch serverStartCountDownLatch = new CountDownLatch(1);
5758

5859
private int port = -1;
5960

6061
@BeforeEach
61-
public void startServer() {
62-
try {
63-
port = SocketUtil.getAvailablePort();
64-
} catch (InterruptedException e) {
65-
throw new RuntimeException(e);
66-
}
62+
public void startServer() throws InterruptedException {
63+
port = SocketUtil.getAvailablePort();
6764
thread = new Thread(
6865
() -> {
6966
try {
7067
serverSocket = new ServerSocket(port);
7168
serverSocket.setReuseAddress(true);
69+
serverStartCountDownLatch.countDown();
7270
while (true) {
7371
Socket client = null;
7472
try {
7573
client = serverSocket.accept();
7674
Scanner in = new Scanner(client.getInputStream());
75+
if (!in.hasNextLine()) {
76+
continue;
77+
}
7778
String input = in.nextLine();
7879
String testCase = input.split(" ")[1];
7980
String seckey = "";
@@ -490,6 +491,7 @@ public void testHandshakeRejectionTestCase29() throws Exception {
490491
}
491492

492493
private void testProtocolRejection(int i, Draft_6455 draft) throws Exception {
494+
serverStartCountDownLatch.await();
493495
final int finalI = i;
494496
final CountDownLatch countDownLatch = new CountDownLatch(1);
495497
final WebSocketClient webSocketClient = new WebSocketClient(

0 commit comments

Comments
 (0)