File tree Expand file tree Collapse file tree 1 file changed +4
-16
lines changed
playwright/src/test/java/com/microsoft/playwright Expand file tree Collapse file tree 1 file changed +4
-16
lines changed Original file line number Diff line number Diff line change 3737import static org .junit .jupiter .api .Assertions .assertEquals ;
3838
3939public 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 ) {
You can’t perform that action at this time.
0 commit comments