Skip to content

Commit baedf6b

Browse files
committed
[grid] Adjusting PortProber for Docker execution
We cannot check all interfaces when running inside Docker. [skip ci]
1 parent 101b42d commit baedf6b

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

java/src/org/openqa/selenium/net/PortProber.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class PortProber {
3535
public static final int START_OF_USER_PORTS = 1024;
3636
private static final Random random = new Random();
3737
private static final EphemeralPortRangeDetector ephemeralRangeDetector;
38+
private static final boolean inDocker = Boolean.parseBoolean(System.getenv("SE_DOCKER"));
3839

3940
static {
4041
final Platform current = Platform.getCurrent();
@@ -64,12 +65,12 @@ public static int findFreePort() {
6465
}
6566

6667
/**
67-
* Returns a port that is within a probable free range. <p/> Based on the ports in
68-
* http://en.wikipedia.org/wiki/Ephemeral_ports, this method stays away from all well-known
69-
* ephemeral port ranges, since they can arbitrarily race with the operating system in
70-
* allocations. Due to the port-greedy nature of selenium this happens fairly frequently.
71-
* Staying within the known safe range increases the probability tests will run green quite
72-
* significantly.
68+
* Returns a port that is within a probable free-range. <p/> Based on the ports in
69+
* <a href="http://en.wikipedia.org/wiki/Ephemeral_ports">...</a>, this method stays away
70+
* from all well-known ephemeral port ranges, since they can arbitrarily race with the
71+
* operating system in allocations. Due to the port-greedy nature of selenium this
72+
* happens fairly frequently. Staying within the known safe range increases the probability
73+
* tests will run green quite significantly.
7374
*
7475
* @return a random port number
7576
*/
@@ -125,7 +126,12 @@ private static boolean isFree(String bindHost, int port) {
125126
}
126127

127128
static int checkPortIsFree(int port) {
128-
if (isFree("localhost", port) && isFree("0.0.0.0", port) && isFree("::1", port)) {
129+
boolean localhostIsFree = isFree("localhost", port);
130+
// We cannot check against all interfaces if the Grid is running inside Docker.
131+
if (inDocker && localhostIsFree) {
132+
return port;
133+
}
134+
if (localhostIsFree && isFree("0.0.0.0", port) && isFree("::1", port)) {
129135
return port;
130136
}
131137
return -1;

0 commit comments

Comments
 (0)