Skip to content

Commit 4cdbc3e

Browse files
committed
Use a dynamic port for tests
1 parent bfb72bc commit 4cdbc3e

File tree

4 files changed

+36
-9
lines changed

4 files changed

+36
-9
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.java_websocket.handshake.ClientHandshake;
3131
import org.java_websocket.handshake.ServerHandshake;
3232
import org.java_websocket.server.WebSocketServer;
33+
import org.java_websocket.util.SocketUtil;
3334
import org.junit.Test;
3435

3536
import java.net.InetSocketAddress;
@@ -47,7 +48,8 @@ public class Issue609Test {
4748

4849
@Test
4950
public void testIssue() throws Exception {
50-
WebSocketServer server = new WebSocketServer( new InetSocketAddress( 8887 ) ) {
51+
int port = SocketUtil.getAvailablePort();
52+
WebSocketServer server = new WebSocketServer( new InetSocketAddress( port ) ) {
5153
@Override
5254
public void onOpen( WebSocket conn, ClientHandshake handshake ) {
5355
}
@@ -73,7 +75,7 @@ public void onStart() {
7375
}
7476
};
7577
server.start();
76-
WebSocketClient webSocket = new WebSocketClient( new URI( "ws://localhost:8887" ) ) {
78+
WebSocketClient webSocket = new WebSocketClient( new URI( "ws://localhost:" + port ) ) {
7779
@Override
7880
public void onOpen( ServerHandshake handshakedata ) {
7981

src/test/java/org/java_websocket/misc/OpeningHandshakeRejectionTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.java_websocket.framing.CloseFrame;
3030
import org.java_websocket.handshake.ServerHandshake;
3131
import org.java_websocket.util.Charsetfunctions;
32+
import org.java_websocket.util.SocketUtil;
3233
import org.junit.AfterClass;
3334
import org.junit.BeforeClass;
3435
import org.junit.Test;
@@ -51,13 +52,16 @@ public class OpeningHandshakeRejectionTest {
5152

5253
private static boolean debugPrintouts = false;
5354

55+
private static int port;
56+
5457
@BeforeClass
55-
public static void startServer() {
58+
public static void startServer() throws Exception {
59+
port = SocketUtil.getAvailablePort();
5660
thread = new Thread(
5761
new Runnable() {
5862
public void run() {
5963
try {
60-
serverSocket = new ServerSocket( 8887 );
64+
serverSocket = new ServerSocket( port );
6165
serverSocket.setReuseAddress( true );
6266
while( true ) {
6367
Socket client = null;
@@ -198,7 +202,7 @@ public void testHandshakeRejectionTestCase11() throws Exception {
198202
private void testHandshakeRejection( int i ) throws Exception {
199203
final int finalI = i;
200204
final boolean[] threadReturned = { false };
201-
WebSocketClient webSocketClient = new WebSocketClient( new URI( "ws://localhost:8887/" + finalI ) ) {
205+
WebSocketClient webSocketClient = new WebSocketClient( new URI( "ws://localhost:"+ port + "/" + finalI ) ) {
202206
@Override
203207
public void onOpen( ServerHandshake handshakedata ) {
204208
fail( "There should not be a connection!" );

src/test/java/org/java_websocket/protocols/ProtoclHandshakeRejectionTest.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.java_websocket.handshake.ServerHandshake;
3333
import org.java_websocket.util.Base64;
3434
import org.java_websocket.util.Charsetfunctions;
35+
import org.java_websocket.util.SocketUtil;
3536
import org.junit.AfterClass;
3637
import org.junit.BeforeClass;
3738
import org.junit.Test;
@@ -58,13 +59,16 @@ public class ProtoclHandshakeRejectionTest {
5859

5960
private static boolean debugPrintouts = false;
6061

62+
private static int port;
63+
6164
@BeforeClass
62-
public static void startServer() {
65+
public static void startServer() throws Exception {
66+
port = SocketUtil.getAvailablePort();
6367
thread = new Thread(
6468
new Runnable() {
6569
public void run() {
6670
try {
67-
serverSocket = new ServerSocket( 8887 );
71+
serverSocket = new ServerSocket( port );
6872
serverSocket.setReuseAddress( true );
6973
int count = 1;
7074
while( true ) {
@@ -168,7 +172,7 @@ public void run() {
168172
}
169173
}
170174
} catch ( Exception e ) {
171-
e.printStackTrace( );
175+
e.printStackTrace();
172176
fail( "There should be no exception" );
173177
}
174178
}
@@ -294,7 +298,7 @@ public void testHandshakeRejectionTestCase17() throws Exception {
294298
private void testProtocolRejection( int i, Draft_6455 draft ) throws Exception {
295299
final int finalI = i;
296300
final boolean[] threadReturned = { false };
297-
WebSocketClient webSocketClient = new WebSocketClient( new URI( "ws://localhost:8887/" + finalI ), draft ) {
301+
WebSocketClient webSocketClient = new WebSocketClient( new URI( "ws://localhost:" + port + "/" + finalI ), draft ) {
298302
@Override
299303
public void onOpen( ServerHandshake handshakedata ) {
300304
switch(finalI) {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.java_websocket.util;
2+
import java.io.IOException;
3+
import java.net.ServerSocket;
4+
5+
public class SocketUtil {
6+
public static int getAvailablePort() throws IOException {
7+
ServerSocket srv = null;
8+
try {
9+
srv = new ServerSocket( 0 );
10+
return srv.getLocalPort();
11+
} finally {
12+
if( srv != null ) {
13+
srv.close();
14+
}
15+
}
16+
}
17+
}

0 commit comments

Comments
 (0)