4949import java .util .concurrent .CountDownLatch ;
5050
5151public class Issue825Test {
52- private CountDownLatch countClientDownLatch = new CountDownLatch (3 );
53- private CountDownLatch countServerDownLatch = new CountDownLatch (1 );
52+
5453
5554 @ Test (timeout = 15000 )
5655 public void testIssue () throws IOException , URISyntaxException , KeyStoreException , NoSuchAlgorithmException , KeyManagementException , UnrecoverableKeyException , CertificateException , InterruptedException {
56+ final CountDownLatch countClientOpenLatch = new CountDownLatch (3 );
57+ final CountDownLatch countClientMessageLatch = new CountDownLatch (3 );
58+ final CountDownLatch countServerDownLatch = new CountDownLatch (1 );
5759 int port = SocketUtil .getAvailablePort ();
5860 final WebSocketClient webSocket = new WebSocketClient (new URI ("wss://localhost:" + port )) {
5961 @ Override
6062 public void onOpen (ServerHandshake handshakedata ) {
63+ countClientOpenLatch .countDown ();
6164 }
6265
6366 @ Override
@@ -72,7 +75,7 @@ public void onClose(int code, String reason, boolean remote) {
7275 public void onError (Exception ex ) {
7376 }
7477 };
75- WebSocketServer server = new MyWebSocketServer (port , countServerDownLatch , countClientDownLatch );
78+ WebSocketServer server = new MyWebSocketServer (port , countServerDownLatch , countClientMessageLatch );
7679
7780 // load up the key store
7881 String STORETYPE = "JKS" ;
@@ -109,23 +112,23 @@ public void onError(Exception ex) {
109112 webSocket .closeBlocking ();
110113 //Disconnect manually and reconnect
111114 webSocket .reconnect ();
112- Thread . sleep ( 100 );
115+ countClientOpenLatch . await ( );
113116 webSocket .send ( "me" );
114117 Thread .sleep ( 100 );
115118 webSocket .closeBlocking ();
116- countClientDownLatch .await ();
119+ countClientMessageLatch .await ();
117120 }
118121
119122
120123 private static class MyWebSocketServer extends WebSocketServer {
121- private final CountDownLatch countServerDownLatch ;
122- private final CountDownLatch countClientDownLatch ;
124+ private final CountDownLatch countServerLatch ;
125+ private final CountDownLatch countClientMessageLatch ;
123126
124127
125- public MyWebSocketServer (int port , CountDownLatch serverDownLatch , CountDownLatch countClientDownLatch ) {
128+ public MyWebSocketServer (int port , CountDownLatch serverDownLatch , CountDownLatch countClientMessageLatch ) {
126129 super (new InetSocketAddress (port ));
127- this .countServerDownLatch = serverDownLatch ;
128- this .countClientDownLatch = countClientDownLatch ;
130+ this .countServerLatch = serverDownLatch ;
131+ this .countClientMessageLatch = countClientMessageLatch ;
129132 }
130133
131134 @ Override
@@ -138,7 +141,7 @@ public void onClose(WebSocket conn, int code, String reason, boolean remote) {
138141
139142 @ Override
140143 public void onMessage (WebSocket conn , String message ) {
141- countClientDownLatch .countDown ();
144+ countClientMessageLatch .countDown ();
142145 }
143146
144147 @ Override
@@ -148,7 +151,7 @@ public void onError(WebSocket conn, Exception ex) {
148151
149152 @ Override
150153 public void onStart () {
151- countServerDownLatch .countDown ();
154+ countServerLatch .countDown ();
152155 }
153156 }
154157}
0 commit comments