Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #834 by starting WebSocketWorker of the WebSocketServer in the start function #850

Merged
merged 3 commits into from
Feb 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/main/java/org/java_websocket/server/WebSocketServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ public WebSocketServer( InetSocketAddress address , int decodercount , List<Draf
for( int i = 0 ; i < decodercount ; i++ ) {
WebSocketWorker ex = new WebSocketWorker();
decoders.add( ex );
ex.start();
}
}

Expand All @@ -229,6 +228,10 @@ 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
3 changes: 2 additions & 1 deletion src/test/java/org/java_websocket/issues/AllIssueTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
org.java_websocket.issues.Issue732Test.class,
org.java_websocket.issues.Issue764Test.class,
org.java_websocket.issues.Issue765Test.class,
org.java_websocket.issues.Issue825Test.class
org.java_websocket.issues.Issue825Test.class,
org.java_websocket.issues.Issue834Test.class
})
/**
* Start all tests for issues
Expand Down
50 changes: 50 additions & 0 deletions src/test/java/org/java_websocket/issues/Issue834Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.java_websocket.issues;

import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;
import org.java_websocket.util.SocketUtil;
import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Set;

public class Issue834Test {

@Test(timeout = 1000)
public void testNoNewThreads() throws IOException {

Set<Thread> threadSet1 = Thread.getAllStackTraces().keySet();

new WebSocketServer(new InetSocketAddress(SocketUtil.getAvailablePort())) {
@Override
public void onOpen(WebSocket conn, ClientHandshake handshake) {
}

@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() {
}
};

Set<Thread> threadSet2 = Thread.getAllStackTraces().keySet();

//checks that no threads are started in the constructor
Assert.assertEquals(threadSet1, threadSet2);

}

}