Skip to content

Commit 7c83868

Browse files
committed
Try to stabilize SocketUtil
1 parent 1c7162d commit 7c83868

File tree

8 files changed

+14
-9
lines changed

8 files changed

+14
-9
lines changed

src/test/java/org/java_websocket/client/ConnectBlockingTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public void test_ConnectBlockingCleanup() throws Throwable {
3434
public void run() {
3535
try {
3636
ServerSocket serverSocket = new ServerSocket(port);
37+
serverSocket.setReuseAddress(true);
3738
ready.countDown();
3839
Socket clientSocket = serverSocket.accept();
3940
accepted.countDown();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public class Issue713Test {
5050
CountDownLatch countDownLatchBytebuffer = new CountDownLatch(10);
5151

5252
@Test
53-
public void testIllegalArgument() throws IOException {
53+
public void testIllegalArgument() throws InterruptedException {
5454
WebSocketServer server = new WebSocketServer(
5555
new InetSocketAddress(SocketUtil.getAvailablePort())) {
5656
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class Issue834Test {
1717

1818
@Test()
1919
@Timeout(1000)
20-
public void testNoNewThreads() throws IOException {
20+
public void testNoNewThreads() throws InterruptedException {
2121

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

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class OpeningHandshakeRejectionTest {
5151
private static final String additionalHandshake = "Upgrade: websocket\r\nConnection: Upgrade\r\n\r\n";
5252

5353
@BeforeEach
54-
public void startServer() throws IOException {
54+
public void startServer() throws IOException, InterruptedException {
5555
port = SocketUtil.getAvailablePort();
5656
thread = new Thread(
5757
new Runnable() {
@@ -218,6 +218,9 @@ public void testHandshakeRejectionTestCase11() throws Exception {
218218
}
219219

220220
private void testHandshakeRejection(int i) throws Exception {
221+
do {
222+
Thread.sleep(100);
223+
} while(serverSocket == null || !serverSocket.isBound());
221224
final int finalI = i;
222225
final CountDownLatch countDownLatch = new CountDownLatch(1);
223226
WebSocketClient webSocketClient = new WebSocketClient(

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public class ProtocolHandshakeRejectionTest {
5858
private int port;
5959

6060
@BeforeEach
61-
public void startServer() {
61+
public void startServer() throws InterruptedException {
6262
port = SocketUtil.getAvailablePort();
6363
thread = new Thread(
6464
new Runnable() {
@@ -489,7 +489,7 @@ public void testHandshakeRejectionTestCase29() throws Exception {
489489

490490
private void testProtocolRejection(int i, Draft_6455 draft) throws Exception {
491491
do {
492-
Thread.sleep(10);
492+
Thread.sleep(100);
493493
} while(serverSocket == null || !serverSocket.isBound());
494494
final int finalI = i;
495495
final CountDownLatch countDownLatch = new CountDownLatch(1);

src/test/java/org/java_websocket/server/DaemonThreadTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class DaemonThreadTest {
1717

1818
@Test()
1919
@Timeout(1000)
20-
public void test_AllCreatedThreadsAreDaemon() throws Throwable {
20+
public void test_AllCreatedThreadsAreDaemon() throws InterruptedException {
2121

2222
Set<Thread> threadSet1 = Thread.getAllStackTraces().keySet();
2323
final CountDownLatch ready = new CountDownLatch(1);

src/test/java/org/java_websocket/server/WebSocketServerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public void testConstructor() {
110110

111111

112112
@Test
113-
public void testGetAddress() throws IOException {
113+
public void testGetAddress() throws InterruptedException {
114114
int port = SocketUtil.getAvailablePort();
115115
InetSocketAddress inetSocketAddress = new InetSocketAddress(port);
116116
MyWebSocketServer server = new MyWebSocketServer(port);

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@
3030

3131
public class SocketUtil {
3232

33-
public static int getAvailablePort() {
33+
public static int getAvailablePort() throws InterruptedException {
3434
while (true) {
3535
try (ServerSocket srv = new ServerSocket(0)) {
3636
return srv.getLocalPort();
3737
} catch (IOException e) {
38-
// Ignore
38+
// Retry
39+
Thread.sleep(50);
3940
}
4041
}
4142
}

0 commit comments

Comments
 (0)