4040import org .java_websocket .util .SocketUtil ;
4141import org .junit .jupiter .api .*;
4242
43- import static org .junit .jupiter .api .Assertions .assertTrue ;
4443import static org .junit .jupiter .api .Assertions .fail ;
4544
4645public 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 \n Connection: 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!" );
0 commit comments