Skip to content

Commit 2047167

Browse files
committed
Give all threads a custom name
1 parent b13c890 commit 2047167

File tree

6 files changed

+162
-5
lines changed

6 files changed

+162
-5
lines changed

src/main/java/org/java_websocket/AbstractWebSocket.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ protected void startConnectionLostTimer() {
128128
*/
129129
private void restartConnectionLostTimer() {
130130
cancelConnectionLostTimer();
131-
connectionLostTimer = new Timer();
131+
connectionLostTimer = new Timer("WebSocketTimer");
132132
connectionLostTimerTask = new TimerTask() {
133133

134134
/**

src/main/java/org/java_websocket/client/WebSocketClient.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ public void connect() {
266266
if( writeThread != null )
267267
throw new IllegalStateException( "WebSocketClient objects are not reuseable" );
268268
writeThread = new Thread( this );
269+
writeThread.setName( "WebSocketWriteThread-" + writeThread.getId() );
269270
writeThread.start();
270271
}
271272

@@ -621,7 +622,7 @@ public void onFragment( Framedata frame ) {
621622
private class WebsocketWriteThread implements Runnable {
622623
@Override
623624
public void run() {
624-
Thread.currentThread().setName( "WebsocketWriteThread" );
625+
Thread.currentThread().setName( "WebSocketWriteThread-" + Thread.currentThread().getId() );
625626
try {
626627
try {
627628
while( !Thread.interrupted() ) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ public void run() {
320320
return;
321321
}
322322
}
323-
selectorthread.setName( "WebsocketSelector" + selectorthread.getId() );
323+
selectorthread.setName( "WebSocketSelector-" + selectorthread.getId() );
324324
try {
325325
server = ServerSocketChannel.open();
326326
server.configureBlocking( false );

src/test/java/org/java_websocket/issues/AllIssueTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
org.java_websocket.issues.Issue621Test.class,
3535
org.java_websocket.issues.Issue580Test.class,
3636
org.java_websocket.issues.Issue256Test.class,
37-
org.java_websocket.issues.Issue661Test.class
37+
org.java_websocket.issues.Issue661Test.class,
38+
org.java_websocket.issues.Issue666Test.class
3839
})
3940
/**
4041
* Start all tests for issues
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/*
2+
* Copyright (c) 2010-2018 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+
package org.java_websocket.issues;
27+
28+
import org.java_websocket.WebSocket;
29+
import org.java_websocket.client.WebSocketClient;
30+
import org.java_websocket.handshake.ClientHandshake;
31+
import org.java_websocket.handshake.ServerHandshake;
32+
import org.java_websocket.server.WebSocketServer;
33+
import org.java_websocket.util.SocketUtil;
34+
import org.java_websocket.util.ThreadCheck;
35+
import org.junit.Assert;
36+
import org.junit.Test;
37+
38+
import java.net.InetSocketAddress;
39+
import java.net.URI;
40+
import java.util.Map;
41+
import java.util.concurrent.CountDownLatch;
42+
43+
public class Issue666Test {
44+
private CountDownLatch countServerDownLatch = new CountDownLatch( 1 );
45+
46+
@Test
47+
public void testServer() throws Exception {
48+
Map<Long, Thread> mapBefore = ThreadCheck.getThreadMap();
49+
int port = SocketUtil.getAvailablePort();
50+
WebSocketServer server = new WebSocketServer( new InetSocketAddress( port ) ) {
51+
@Override
52+
public void onOpen( WebSocket conn, ClientHandshake handshake ) {
53+
}
54+
55+
@Override
56+
public void onClose( WebSocket conn, int code, String reason, boolean remote ) {
57+
58+
}
59+
60+
@Override
61+
public void onMessage( WebSocket conn, String message ) {
62+
63+
}
64+
65+
@Override
66+
public void onError( WebSocket conn, Exception ex ) {
67+
68+
}
69+
70+
@Override
71+
public void onStart() {
72+
countServerDownLatch.countDown();
73+
}
74+
};
75+
server.start();
76+
countServerDownLatch.await();
77+
Map<Long, Thread> mapAfter = ThreadCheck.getThreadMap();
78+
for( long key : mapBefore.keySet() ) {
79+
mapAfter.remove( key );
80+
}
81+
for( Thread thread : mapAfter.values() ) {
82+
String name = thread.getName();
83+
if( !name.startsWith( "WebSocketSelector-" ) && !name.startsWith( "WebSocketWorker-" ) && !name.equals( "WebSocketTimer" ) ) {
84+
Assert.fail( "Thread not correctly named! Is: " + name );
85+
}
86+
}
87+
server.stop();
88+
}
89+
90+
@Test
91+
public void testClient() throws Exception {
92+
int port = SocketUtil.getAvailablePort();
93+
WebSocketClient client = new WebSocketClient( new URI( "ws://localhost:" + port ) ) {
94+
@Override
95+
public void onOpen( ServerHandshake handshakedata ) {
96+
97+
}
98+
99+
@Override
100+
public void onMessage( String message ) {
101+
102+
}
103+
104+
@Override
105+
public void onClose( int code, String reason, boolean remote ) {
106+
}
107+
108+
@Override
109+
public void onError( Exception ex ) {
110+
111+
}
112+
};
113+
WebSocketServer server = new WebSocketServer( new InetSocketAddress( port ) ) {
114+
@Override
115+
public void onOpen( WebSocket conn, ClientHandshake handshake ) {
116+
}
117+
118+
@Override
119+
public void onClose( WebSocket conn, int code, String reason, boolean remote ) {
120+
121+
}
122+
123+
@Override
124+
public void onMessage( WebSocket conn, String message ) {
125+
126+
}
127+
128+
@Override
129+
public void onError( WebSocket conn, Exception ex ) {
130+
131+
}
132+
133+
@Override
134+
public void onStart() {
135+
countServerDownLatch.countDown();
136+
}
137+
};
138+
server.start();
139+
countServerDownLatch.await();
140+
Map<Long, Thread> mapBefore = ThreadCheck.getThreadMap();
141+
client.connectBlocking();
142+
Map<Long, Thread> mapAfter = ThreadCheck.getThreadMap();
143+
for( long key : mapBefore.keySet() ) {
144+
mapAfter.remove( key );
145+
}
146+
for( Thread thread : mapAfter.values() ) {
147+
String name = thread.getName();
148+
if( !name.equals( "WebSocketTimer" ) && !name.startsWith( "WebSocketWriteThread-" ) ) {
149+
Assert.fail( "Thread not correctly named! Is: " + name );
150+
}
151+
}
152+
client.close();
153+
server.stop();
154+
}
155+
}

src/test/java/org/java_websocket/util/ThreadCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ private boolean checkZombies( boolean testOnly ) {
7171
return zombies > 0;
7272
}
7373

74-
private Map<Long,Thread> getThreadMap() {
74+
public static Map<Long,Thread> getThreadMap() {
7575
Map<Long,Thread> map = new HashMap<Long,Thread>();
7676
Thread[] threads = new Thread[ Thread.activeCount() * 2 ];
7777
int actualNb = Thread.enumerate( threads );

0 commit comments

Comments
 (0)