Skip to content

Commit 6aa3b1f

Browse files
authored
Merge pull request TooTallNate#856 from marci4/Issue855
Move the startup of the WebSocketWorker inside of run()
2 parents 8a89706 + d417e04 commit 6aa3b1f

File tree

2 files changed

+102
-4
lines changed

2 files changed

+102
-4
lines changed

‎src/main/java/org/java_websocket/server/WebSocketServer.java‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,6 @@ public void start(){
228228
if( selectorthread != null )
229229
thrownewIllegalStateException( getClass().getName() + " can only be started once." );
230230
newThread( this ).start();
231-
232-
for( WebSocketWorkerex : decoders ){
233-
ex.start();
234-
}
235231
}
236232

237233
/**
@@ -510,6 +506,9 @@ private boolean doSetupSelectorAndServerThread(){
510506
selector = Selector.open();
511507
server.register( selector, server.validOps() );
512508
startConnectionLostTimer();
509+
for( WebSocketWorkerex : decoders ){
510+
ex.start();
511+
}
513512
onStart();
514513
} catch ( IOExceptionex ){
515514
handleFatal( null, ex );
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright (c) 2010-2019 Nathan Rajlich
3+
*
4+
* Permission is hereby granted, free of charge, to any person
5+
* obtaining a copy of this software and associated documentation
6+
* files (the "Software"), to deal in the Software without
7+
* restriction, including without limitation the rights to use,
8+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the
10+
* Software is furnished to do so, subject to the following
11+
* conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be
14+
* included in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
* OTHER DEALINGS IN THE SOFTWARE.
24+
*
25+
*/
26+
27+
packageorg.java_websocket.issues;
28+
29+
importorg.java_websocket.WebSocket;
30+
importorg.java_websocket.client.WebSocketClient;
31+
importorg.java_websocket.handshake.ClientHandshake;
32+
importorg.java_websocket.handshake.ServerHandshake;
33+
importorg.java_websocket.server.WebSocketServer;
34+
importorg.java_websocket.util.SocketUtil;
35+
importorg.junit.Test;
36+
37+
importjava.net.InetSocketAddress;
38+
importjava.net.URI;
39+
importjava.util.concurrent.CountDownLatch;
40+
41+
publicclassIssue855Test{
42+
43+
CountDownLatchcountServerDownLatch = newCountDownLatch(1);
44+
CountDownLatchcountDownLatch = newCountDownLatch(1);
45+
46+
@Test(timeout = 2000)
47+
publicvoidtestIssue() throwsException{
48+
intport = SocketUtil.getAvailablePort();
49+
WebSocketClientwebSocket = newWebSocketClient(newURI("ws://localhost:" + port)){
50+
@Override
51+
publicvoidonOpen(ServerHandshakehandshakedata){
52+
countDownLatch.countDown();
53+
}
54+
55+
@Override
56+
publicvoidonMessage(Stringmessage){
57+
58+
}
59+
60+
@Override
61+
publicvoidonClose(intcode, Stringreason, booleanremote){
62+
}
63+
64+
@Override
65+
publicvoidonError(Exceptionex){
66+
67+
}
68+
};
69+
WebSocketServerserver = newWebSocketServer(newInetSocketAddress(port)){
70+
@Override
71+
publicvoidonOpen(WebSocketconn, ClientHandshakehandshake){
72+
conn.close();
73+
}
74+
75+
@Override
76+
publicvoidonClose(WebSocketconn, intcode, Stringreason, booleanremote){
77+
}
78+
79+
@Override
80+
publicvoidonMessage(WebSocketconn, Stringmessage){
81+
82+
}
83+
84+
@Override
85+
publicvoidonError(WebSocketconn, Exceptionex){
86+
87+
}
88+
89+
@Override
90+
publicvoidonStart(){
91+
countServerDownLatch.countDown();
92+
}
93+
};
94+
newThread(server).start();
95+
countServerDownLatch.await();
96+
webSocket.connectBlocking();
97+
server.stop();
98+
}
99+
}

0 commit comments

Comments
(0)