Skip to content

Commit

Permalink
Ensure that a selected port is consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Oct 25, 2021
1 parent f8011bc commit d27b27a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
17 changes: 11 additions & 6 deletions java/src/org/openqa/selenium/grid/server/BaseServerOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class BaseServerOptions {

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

public BaseServerOptions(Config config) {
this.config = config;
Expand All @@ -53,13 +54,17 @@ public Optional<String> getHostname() {

@ManagedAttribute(name = "Port")
public int getPort() {
int port = config.getInt(SERVER_SECTION, "port")
.orElseGet(PortProber::findFreePort);
if (port == -1) {
return PortProber.findFreePort();
}
if (port < 0) {
throw new ConfigException("Port cannot be less than 0: " + port);
int newPort = config.getInt(SERVER_SECTION, "port")
.orElseGet(PortProber::findFreePort);
if (newPort == -1) {
newPort = PortProber.findFreePort();
}
if (newPort < 0) {
throw new ConfigException("Port cannot be less than 0: " + port);
}

port = newPort;
}
return port;
}
Expand Down
4 changes: 4 additions & 0 deletions java/test/org/openqa/selenium/grid/server/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ java_test_suite(
name = "MediumTests",
size = "medium",
srcs = glob(["*Test.java"]),
javacopts = [
"--release",
"11",
],
deps = [
"//java/src/org/openqa/selenium/grid/config",
"//java/src/org/openqa/selenium/grid/server",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.grid.server;

import org.junit.Test;
import org.openqa.selenium.grid.config.MapConfig;

import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

public class BaseServerOptionsTest {

@Test
public void readingThePortTwiceShouldGiveTheSameResult() {
BaseServerOptions options = new BaseServerOptions(new MapConfig(Map.of("server", Map.of("port", -1))));

int first = options.getPort();
int second = options.getPort();

assertThat(first).isEqualTo(second);
}

}

0 comments on commit d27b27a

Please sign in to comment.