Skip to content

Commit 39e7cff

Browse files
author
YuI
authored
fix: UpstreamCheckUtilsTest will generate a port conflict(fix:#1003) (#1004)
1 parent 29bf9e9 commit 39e7cff

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

‎soul-common/src/test/java/org/dromara/soul/common/utils/UpstreamCheckUtilsTest.java‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
*/
3636
public final class UpstreamCheckUtilsTest {
3737

38+
private volatile int port = -1;
39+
3840
@Test
3941
public void testBlank() {
4042
assertFalse(UpstreamCheckUtils.checkUrl(""));
@@ -56,17 +58,22 @@ public void testSocketConnect() {
5658
Runnable runnable = () -> {
5759
ServerSocket serverSocket;
5860
try {
59-
serverSocket = new ServerSocket(13098);
61+
serverSocket = new ServerSocket(0);
62+
port = serverSocket.getLocalPort();
6063
Socket socket = serverSocket.accept();
6164
socket.close();
6265
} catch (IOException e) {
6366
e.printStackTrace();
6467
}
6568
};
6669
new Thread(runnable).start();
67-
Thread.sleep(100);
68-
assertTrue(UpstreamCheckUtils.checkUrl("127.0.0.1:13098"));
69-
assertFalse(UpstreamCheckUtils.checkUrl("http://127.0.0.1:13099"));
70-
assertTrue(UpstreamCheckUtils.checkUrl("http://127.0.0.1:13098"));
70+
71+
while (port == -1) {
72+
Thread.yield();
73+
}
74+
75+
assertTrue(UpstreamCheckUtils.checkUrl("127.0.0.1:" + port));
76+
assertFalse(UpstreamCheckUtils.checkUrl("http://127.0.0.1:" + (port == 0 ? port + 1 : port - 1)));
77+
assertTrue(UpstreamCheckUtils.checkUrl("http://127.0.0.1:" + port));
7178
}
7279
}

0 commit comments

Comments
 (0)