Skip to content

Commit eb22fdb

Browse files
committed
let os assign ephemeral port
1 parent 6f99d9a commit eb22fdb

File tree

1 file changed

+4
-16
lines changed
  • playwright/src/test/java/com/microsoft/playwright

1 file changed

+4
-16
lines changed

playwright/src/test/java/com/microsoft/playwright/Utils.java

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,12 @@
3737
import static org.junit.jupiter.api.Assertions.assertEquals;
3838

3939
public class Utils {
40-
private static final AtomicInteger nextUnusedPort = new AtomicInteger(9000);
41-
42-
private static boolean available(int port) {
43-
try (ServerSocket ignored = new ServerSocket(port)) {
44-
return true;
45-
} catch (IOException ignored) {
46-
return false;
47-
}
48-
}
49-
5040
public static int nextFreePort() {
51-
for (int i = 0; i < 100; i++) {
52-
int port = nextUnusedPort.getAndIncrement();
53-
if (available(port)) {
54-
return port;
55-
}
41+
try (ServerSocket socket = new ServerSocket(0)) {
42+
return socket.getLocalPort();
43+
} catch (IOException e) {
44+
throw new RuntimeException("Cannot find free port", e);
5645
}
57-
throw new RuntimeException("Cannot find free port: " + nextUnusedPort.get());
5846
}
5947

6048
static void assertJsonEquals(Object expected, Object actual) {

0 commit comments

Comments
 (0)