3131import java .net .Socket ;
3232import java .net .URI ;
3333import java .util .Scanner ;
34+ import java .util .concurrent .CountDownLatch ;
35+
3436import org .java_websocket .client .WebSocketClient ;
3537import org .java_websocket .framing .CloseFrame ;
3638import org .java_websocket .handshake .ServerHandshake ;
4648public class OpeningHandshakeRejectionTest {
4749
4850 private static final String additionalHandshake = "Upgrade: websocket\r \n Connection: 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}
0 commit comments