Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Move the startup of the WebSocketWorker inside of the run() method
  • Loading branch information
marci4 committed Feb 11, 2019
1 parent 8a89706 commit d417e04
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/main/java/org/java_websocket/server/WebSocketServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,6 @@ public void start() {
if( selectorthread != null )
throw new IllegalStateException( getClass().getName() + " can only be started once." );
new Thread( this ).start();

for( WebSocketWorker ex : decoders ){
ex.start();
}
}

/**
Expand Down Expand Up @@ -510,6 +506,9 @@ private boolean doSetupSelectorAndServerThread() {
selector = Selector.open();
server.register( selector, server.validOps() );
startConnectionLostTimer();
for( WebSocketWorker ex : decoders ){
ex.start();
}
onStart();
} catch ( IOException ex ) {
handleFatal( null, ex );
Expand Down
99 changes: 99 additions & 0 deletions src/test/java/org/java_websocket/issues/Issue855Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright (c) 2010-2019 Nathan Rajlich
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/

package org.java_websocket.issues;

import org.java_websocket.WebSocket;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.handshake.ServerHandshake;
import org.java_websocket.server.WebSocketServer;
import org.java_websocket.util.SocketUtil;
import org.junit.Test;

import java.net.InetSocketAddress;
import java.net.URI;
import java.util.concurrent.CountDownLatch;

public class Issue855Test {

CountDownLatch countServerDownLatch = new CountDownLatch(1);
CountDownLatch countDownLatch = new CountDownLatch(1);

@Test(timeout = 2000)
public void testIssue() throws Exception {
int port = SocketUtil.getAvailablePort();
WebSocketClient webSocket = new WebSocketClient(new URI("ws://localhost:" + port)) {
@Override
public void onOpen(ServerHandshake handshakedata) {
countDownLatch.countDown();
}

@Override
public void onMessage(String message) {

}

@Override
public void onClose(int code, String reason, boolean remote) {
}

@Override
public void onError(Exception ex) {

}
};
WebSocketServer server = new WebSocketServer(new InetSocketAddress(port)) {
@Override
public void onOpen(WebSocket conn, ClientHandshake handshake) {
conn.close();
}

@Override
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
}

@Override
public void onMessage(WebSocket conn, String message) {

}

@Override
public void onError(WebSocket conn, Exception ex) {

}

@Override
public void onStart() {
countServerDownLatch.countDown();
}
};
new Thread(server).start();
countServerDownLatch.await();
webSocket.connectBlocking();
server.stop();
}
}

0 comments on commit d417e04

Please sign in to comment.