Skip to content

Commit 8a8bacd

Browse files
committed
Use CountDownLatch
1 parent 0cce180 commit 8a8bacd

File tree

2 files changed

+13
-20
lines changed

2 files changed

+13
-20
lines changed

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

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import java.net.Socket;
3232
import java.net.URI;
3333
import java.util.Scanner;
34+
import java.util.concurrent.CountDownLatch;
35+
3436
import org.java_websocket.client.WebSocketClient;
3537
import org.java_websocket.framing.CloseFrame;
3638
import org.java_websocket.handshake.ServerHandshake;
@@ -46,7 +48,6 @@
4648
public class OpeningHandshakeRejectionTest {
4749

4850
private static final String additionalHandshake = "Upgrade: websocket\r\nConnection: Upgrade\r\n\r\n";
49-
private static int counter = 0;
5051
private static Thread thread;
5152
private static ServerSocket serverSocket;
5253

@@ -144,12 +145,9 @@ public void run() {
144145
}
145146

146147
@AfterAll
147-
public static void successTests() throws InterruptedException, IOException {
148+
public static void successTests() throws IOException {
148149
serverSocket.close();
149150
thread.interrupt();
150-
if (debugPrintouts) {
151-
System.out.println(counter + " successful tests");
152-
}
153151
}
154152

155153
@Test()
@@ -226,7 +224,7 @@ public void testHandshakeRejectionTestCase11() throws Exception {
226224

227225
private void testHandshakeRejection(int i) throws Exception {
228226
final int finalI = i;
229-
final boolean[] threadReturned = {false};
227+
final CountDownLatch countDownLatch = new CountDownLatch(1);
230228
WebSocketClient webSocketClient = new WebSocketClient(
231229
new URI("ws://localhost:" + port + "/" + finalI)) {
232230
@Override
@@ -249,8 +247,7 @@ public void onClose(int code, String reason, boolean remote) {
249247
if (debugPrintouts) {
250248
System.out.println("Protocol error for test case: " + finalI);
251249
}
252-
threadReturned[0] = true;
253-
counter++;
250+
countDownLatch.countDown();
254251
} else {
255252
fail("The reason should be included!");
256253
}
@@ -262,8 +259,7 @@ public void onClose(int code, String reason, boolean remote) {
262259
if (debugPrintouts) {
263260
System.out.println("Refuses handshake error for test case: " + finalI);
264261
}
265-
counter++;
266-
threadReturned[0] = true;
262+
countDownLatch.countDown();
267263
}
268264
}
269265
}
@@ -276,9 +272,6 @@ public void onError(Exception ex) {
276272
Thread finalThread = new Thread(webSocketClient);
277273
finalThread.start();
278274
finalThread.join();
279-
280-
if (!threadReturned[0]) {
281-
fail("Error. Thread did not yet return");
282-
}
275+
countDownLatch.await();
283276
}
284277
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import java.util.ArrayList;
3636
import java.util.Collections;
3737
import java.util.Scanner;
38+
import java.util.concurrent.CountDownLatch;
39+
3840
import org.java_websocket.client.WebSocketClient;
3941
import org.java_websocket.drafts.Draft_6455;
4042
import org.java_websocket.extensions.IExtension;
@@ -490,7 +492,7 @@ public void testHandshakeRejectionTestCase29() throws Exception {
490492

491493
private void testProtocolRejection(int i, Draft_6455 draft) throws Exception {
492494
final int finalI = i;
493-
final boolean[] threadReturned = {false};
495+
final CountDownLatch countDownLatch = new CountDownLatch(1);
494496
final WebSocketClient webSocketClient = new WebSocketClient(
495497
new URI("ws://localhost:" + port + "/" + finalI), draft) {
496498
@Override
@@ -516,7 +518,7 @@ public void onOpen(ServerHandshake handshakedata) {
516518
case 24:
517519
case 25:
518520
case 26:
519-
threadReturned[0] = true;
521+
countDownLatch.countDown();
520522
closeConnection(CloseFrame.ABNORMAL_CLOSE, "Bye");
521523
break;
522524
default:
@@ -605,7 +607,7 @@ public void onClose(int code, String reason, boolean remote) {
605607
if (code != CloseFrame.PROTOCOL_ERROR) {
606608
fail("There should be a protocol error! " + finalI + " " + code);
607609
} else if (reason.endsWith("refuses handshake")) {
608-
threadReturned[0] = true;
610+
countDownLatch.countDown();
609611
} else {
610612
fail("The reason should be included!");
611613
}
@@ -635,9 +637,7 @@ public void run() {
635637
throw exc[0];
636638
}
637639

638-
if (!threadReturned[0]) {
639-
fail("Error");
640-
}
640+
countDownLatch.await();
641641

642642
}
643643

0 commit comments

Comments
 (0)