Skip to content

Commit

Permalink
Precalculate the port to use and extend the existing race a little
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Aug 7, 2018
1 parent 63c86bb commit 4965564
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions java/server/src/org/openqa/selenium/grid/server/BaseServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.net.MediaType;

import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.grid.web.CommandHandler;
import org.openqa.selenium.injector.Injector;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.net.NetworkUtils;
import org.openqa.selenium.net.PortProber;
import org.openqa.selenium.remote.http.HttpRequest;
import org.seleniumhq.jetty9.security.ConstraintMapping;
Expand All @@ -40,6 +42,8 @@
import org.seleniumhq.jetty9.util.security.Constraint;
import org.seleniumhq.jetty9.util.thread.QueuedThreadPool;

import java.io.UncheckedIOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.LinkedHashMap;
import java.util.Map;
Expand All @@ -52,14 +56,26 @@
public class BaseServer implements Server<BaseServer> {

private final org.seleniumhq.jetty9.server.Server server;
private final int port;
private final Map<Predicate<HttpRequest>, BiFunction<Injector, HttpRequest, CommandHandler>> handlers;
private final ServletContextHandler servletContextHandler;
private final Injector injector;
private URL url;
private final URL url;

public BaseServer(BaseServerOptions options) {
this.port = options.getPort();
int port = options.getPort() == 0 ? PortProber.findFreePort() : options.getPort();

String host;
try {
host = new NetworkUtils().getNonLoopbackAddressOfThisMachine();
} catch (WebDriverException ignored) {
host = "localhost";
}
try {
this.url = new URL("http", host, port, "");
} catch (MalformedURLException e) {
throw new UncheckedIOException(e);
}

this.server = new org.seleniumhq.jetty9.server.Server(
new QueuedThreadPool(options.getMaxServerThreads()));

Expand Down Expand Up @@ -107,6 +123,11 @@ public BaseServer(BaseServerOptions options) {

server.setHandler(servletContextHandler);

HttpConfiguration httpConfig = new HttpConfiguration();
ServerConnector http = new ServerConnector(server, new HttpConnectionFactory(httpConfig));
http.setPort(getUrl().getPort());
server.addConnector(http);

addServlet(new CommandHandlerServlet(injector, handlers), "/*");
}

Expand Down Expand Up @@ -136,19 +157,10 @@ public void addHandler(

@Override
public BaseServer start() {
int portToUse = port < 1 ? PortProber.findFreePort() : port;

HttpConfiguration httpConfig = new HttpConfiguration();
ServerConnector http = new ServerConnector(server, new HttpConnectionFactory(httpConfig));
http.setPort(portToUse);
server.addConnector(http);

try {
server.start();

url = server.getURI().toURL();

PortProber.waitForPortUp(portToUse, 10, SECONDS);
PortProber.waitForPortUp(getUrl().getPort(), 10, SECONDS);

return this;
} catch (RuntimeException e) {
Expand All @@ -172,9 +184,6 @@ public BaseServer stop() {

@Override
public URL getUrl() {
if (!server.isRunning()) {
throw new RuntimeException("Server is not running");
}
return url;
}
}

0 comments on commit 4965564

Please sign in to comment.