Skip to content
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
6 changes: 3 additions & 3 deletions src/main/java/org/java_websocket/AbstractWebSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ protected void startConnectionLostTimer() {
*/
private void restartConnectionLostTimer() {
cancelConnectionLostTimer();
connectionLostTimer = new Timer();
connectionLostTimer = new Timer("WebSocketTimer");
connectionLostTimerTask = new TimerTask() {

/**
Expand All @@ -138,7 +138,7 @@ private void restartConnectionLostTimer() {
@Override
public void run() {
connections.clear();
connections.addAll( connections() );
connections.addAll( getConnections() );
long current = (System.currentTimeMillis()-(connectionLostTimeout * 1500));
WebSocketImpl webSocketImpl;
for( WebSocket conn : connections ) {
Expand Down Expand Up @@ -169,7 +169,7 @@ public void run() {
* @return the currently available connections
* @since 1.3.4
*/
protected abstract Collection<WebSocket> connections();
protected abstract Collection<WebSocket> getConnections();

/**
* Cancel any running timer for the connection lost detection
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/java_websocket/client/WebSocketClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ public void connect() {
if( writeThread != null )
throw new IllegalStateException( "WebSocketClient objects are not reuseable" );
writeThread = new Thread( this );
writeThread.setName( "WebSocketWriteThread-" + writeThread.getId() );
writeThread.start();
}

Expand Down Expand Up @@ -329,7 +330,7 @@ public <T> void setAttachment(T attachment) {
}

@Override
protected Collection<WebSocket> connections() {
protected Collection<WebSocket> getConnections() {
return Collections.singletonList((WebSocket ) engine );
}

Expand Down Expand Up @@ -621,7 +622,7 @@ public void onFragment( Framedata frame ) {
private class WebsocketWriteThread implements Runnable {
@Override
public void run() {
Thread.currentThread().setName( "WebsocketWriteThread" );
Thread.currentThread().setName( "WebSocketWriteThread-" + Thread.currentThread().getId() );
try {
try {
while( !Thread.interrupted() ) {
Expand Down
19 changes: 17 additions & 2 deletions src/main/java/org/java_websocket/server/WebSocketServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,29 @@ public void stop() throws IOException , InterruptedException {
}

/**
* PLEASE use the method getConnections() in the future!
*
* Returns a WebSocket[] of currently connected clients.
* Its iterators will be failfast and its not judicious
* to modify it.
*
* @return The currently connected clients.
*
*/
@Deprecated
public Collection<WebSocket> connections() {
return this.connections;
return getConnections();
}

/**
* Returns all currently connected clients.
* This collection does not allow any modification e.g. removing a client.
*
* @return A unmodifiable collection of all currently connected clients
* @since 1.3.8
*/
public Collection<WebSocket> getConnections() {
return Collections.unmodifiableCollection( new ArrayList<WebSocket>(connections) );
}

public InetSocketAddress getAddress() {
Expand Down Expand Up @@ -305,7 +320,7 @@ public void run() {
return;
}
}
selectorthread.setName( "WebsocketSelector" + selectorthread.getId() );
selectorthread.setName( "WebSocketSelector-" + selectorthread.getId() );
try {
server = ServerSocketChannel.open();
server.configureBlocking( false );
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/org/java_websocket/issues/AllIssueTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
org.java_websocket.issues.Issue609Test.class,
org.java_websocket.issues.Issue621Test.class,
org.java_websocket.issues.Issue580Test.class,
org.java_websocket.issues.Issue256Test.class
org.java_websocket.issues.Issue256Test.class,
org.java_websocket.issues.Issue661Test.class,
org.java_websocket.issues.Issue666Test.class
})
/**
* Start all tests for issues
Expand Down
155 changes: 155 additions & 0 deletions src/test/java/org/java_websocket/issues/Issue666Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
* Copyright (c) 2010-2018 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.java_websocket.util.ThreadCheck;
import org.junit.Assert;
import org.junit.Test;

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

public class Issue666Test {
private CountDownLatch countServerDownLatch = new CountDownLatch( 1 );

@Test
public void testServer() throws Exception {
Map<Long, Thread> mapBefore = ThreadCheck.getThreadMap();
int port = SocketUtil.getAvailablePort();
WebSocketServer server = new WebSocketServer( new InetSocketAddress( port ) ) {
@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() {
countServerDownLatch.countDown();
}
};
server.start();
countServerDownLatch.await();
Map<Long, Thread> mapAfter = ThreadCheck.getThreadMap();
for( long key : mapBefore.keySet() ) {
mapAfter.remove( key );
}
for( Thread thread : mapAfter.values() ) {
String name = thread.getName();
if( !name.startsWith( "WebSocketSelector-" ) && !name.startsWith( "WebSocketWorker-" ) && !name.equals( "WebSocketTimer" ) ) {
Assert.fail( "Thread not correctly named! Is: " + name );
}
}
server.stop();
}

@Test
public void testClient() throws Exception {
int port = SocketUtil.getAvailablePort();
WebSocketClient client = new WebSocketClient( new URI( "ws://localhost:" + port ) ) {
@Override
public void onOpen( ServerHandshake handshakedata ) {

}

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

@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();
}
};
server.start();
countServerDownLatch.await();
Map<Long, Thread> mapBefore = ThreadCheck.getThreadMap();
client.connectBlocking();
Map<Long, Thread> mapAfter = ThreadCheck.getThreadMap();
for( long key : mapBefore.keySet() ) {
mapAfter.remove( key );
}
for( Thread thread : mapAfter.values() ) {
String name = thread.getName();
if( !name.equals( "WebSocketTimer" ) && !name.startsWith( "WebSocketWriteThread-" ) ) {
Assert.fail( "Thread not correctly named! Is: " + name );
}
}
client.close();
server.stop();
}
}
2 changes: 1 addition & 1 deletion src/test/java/org/java_websocket/util/ThreadCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private boolean checkZombies( boolean testOnly ) {
return zombies > 0;
}

private Map<Long,Thread> getThreadMap() {
public static Map<Long,Thread> getThreadMap() {
Map<Long,Thread> map = new HashMap<Long,Thread>();
Thread[] threads = new Thread[ Thread.activeCount() * 2 ];
int actualNb = Thread.enumerate( threads );
Expand Down