Skip to content

Commit 7941e12

Browse files
committed
[grid] Allowing -1 as port to use a random port
Fixes #9940
1 parent c69e336 commit 7941e12

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

java/src/org/openqa/selenium/grid/server/BaseServerOptions.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public class BaseServerOptions {
4141

4242
private static final Logger LOG = Logger.getLogger(BaseServerOptions.class.getName());
4343
private final Config config;
44-
private int port = -1;
4544

4645
public BaseServerOptions(Config config) {
4746
this.config = config;
@@ -54,17 +53,14 @@ public Optional<String> getHostname() {
5453

5554
@ManagedAttribute(name = "Port")
5655
public int getPort() {
56+
int port = config.getInt(SERVER_SECTION, "port")
57+
.orElseGet(PortProber::findFreePort);
5758
if (port == -1) {
58-
int newPort = config.getInt(SERVER_SECTION, "port")
59-
.orElseGet(PortProber::findFreePort);
60-
61-
if (newPort < 0) {
62-
throw new ConfigException("Port cannot be less than 0: " + newPort);
63-
}
64-
65-
port = newPort;
59+
return PortProber.findFreePort();
60+
}
61+
if (port < 0) {
62+
throw new ConfigException("Port cannot be less than 0: " + port);
6663
}
67-
6864
return port;
6965
}
7066

0 commit comments

Comments
 (0)