Skip to content

Commit b25b8ef

Browse files
committed
[java] Starting NettyAppServer with port 0
1 parent 932ac99 commit b25b8ef

3 files changed

Lines changed: 52 additions & 35 deletions

File tree

java/src/org/openqa/selenium/netty/server/NettyServer.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,24 @@
4848
import java.util.Optional;
4949
import java.util.function.BiFunction;
5050
import java.util.function.Consumer;
51+
import java.util.logging.Level;
52+
import java.util.logging.Logger;
5153

5254
import javax.net.ssl.SSLException;
5355

5456
public class NettyServer implements Server<NettyServer> {
5557

58+
private static final Logger log = Logger.getLogger(NettyServer.class.getName());
5659
private final EventLoopGroup bossGroup;
5760
private final EventLoopGroup workerGroup;
5861
private final int port;
5962
private final String host;
6063
private final boolean bindHost;
61-
private final URL externalUrl;
6264
private final HttpHandler handler;
6365
private final BiFunction<String, Consumer<Message>, Optional<Consumer<Message>>> websocketHandler;
6466
private final SslContext sslCtx;
6567
private final boolean allowCors;
66-
68+
private URL externalUrl;
6769
private Channel channel;
6870

6971
public NettyServer(
@@ -171,6 +173,19 @@ public NettyServer start() {
171173
throw e;
172174
}
173175

176+
if (port == 0) {
177+
try {
178+
int systemPickedPort = ((InetSocketAddress) channel.localAddress()).getPort();
179+
externalUrl = new URL(
180+
externalUrl.getProtocol(),
181+
externalUrl.getHost(),
182+
systemPickedPort,
183+
externalUrl.getFile());
184+
} catch (MalformedURLException e) {
185+
log.log(Level.WARNING, "Could not retrieve system picked port to build external url", e);
186+
}
187+
}
188+
174189
return this;
175190
}
176191
}

java/test/org/openqa/selenium/environment/webserver/NettyAppServer.java

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@
1717

1818
package org.openqa.selenium.environment.webserver;
1919

20+
import static com.google.common.net.HttpHeaders.CONTENT_TYPE;
21+
import static java.util.Collections.singletonMap;
22+
import static org.openqa.selenium.json.Json.JSON_UTF_8;
23+
import static org.openqa.selenium.remote.http.Contents.string;
24+
2025
import com.google.common.collect.ImmutableMap;
26+
2127
import org.openqa.selenium.grid.config.CompoundConfig;
2228
import org.openqa.selenium.grid.config.Config;
2329
import org.openqa.selenium.grid.config.MapConfig;
@@ -26,7 +32,6 @@
2632
import org.openqa.selenium.grid.server.Server;
2733
import org.openqa.selenium.internal.Require;
2834
import org.openqa.selenium.io.TemporaryFilesystem;
29-
import org.openqa.selenium.net.PortProber;
3035
import org.openqa.selenium.netty.server.NettyServer;
3136
import org.openqa.selenium.remote.http.Contents;
3237
import org.openqa.selenium.remote.http.HttpClient;
@@ -41,11 +46,6 @@
4146
import java.net.MalformedURLException;
4247
import java.net.URL;
4348

44-
import static com.google.common.net.HttpHeaders.CONTENT_TYPE;
45-
import static java.util.Collections.singletonMap;
46-
import static org.openqa.selenium.json.Json.JSON_UTF_8;
47-
import static org.openqa.selenium.remote.http.Contents.string;
48-
4949
public class NettyAppServer implements AppServer {
5050

5151
private final static Config sslConfig = new MapConfig(
@@ -94,7 +94,24 @@ private NettyAppServer(Config config, HttpHandler handler) {
9494

9595
private static Config createDefaultConfig() {
9696
return new MemoizedConfig(new MapConfig(
97-
singletonMap("server", singletonMap("port", PortProber.findFreePort()))));
97+
singletonMap("server", singletonMap("port", 0))));
98+
}
99+
100+
public static void main(String[] args) {
101+
MemoizedConfig config = new MemoizedConfig(new MapConfig(singletonMap("server", singletonMap("port", 2310))));
102+
BaseServerOptions options = new BaseServerOptions(config);
103+
104+
HttpHandler handler = new HandlersForTests(
105+
options.getHostname().orElse("localhost"),
106+
options.getPort(),
107+
TemporaryFilesystem.getDefaultTmpFS().createTempDir("netty", "server").toPath());
108+
109+
NettyAppServer server = new NettyAppServer(
110+
config,
111+
handler);
112+
server.start();
113+
114+
System.out.printf("Server started. Root URL: %s%n", server.whereIs("/"));
98115
}
99116

100117
@Override
@@ -181,21 +198,4 @@ public String getHostName() {
181198
public String getAlternateHostName() {
182199
return AppServer.detectAlternateHostname();
183200
}
184-
185-
public static void main(String[] args) {
186-
MemoizedConfig config = new MemoizedConfig(new MapConfig(singletonMap("server", singletonMap("port", 2310))));
187-
BaseServerOptions options = new BaseServerOptions(config);
188-
189-
HttpHandler handler = new HandlersForTests(
190-
options.getHostname().orElse("localhost"),
191-
options.getPort(),
192-
TemporaryFilesystem.getDefaultTmpFS().createTempDir("netty", "server").toPath());
193-
194-
NettyAppServer server = new NettyAppServer(
195-
config,
196-
handler);
197-
server.start();
198-
199-
System.out.printf("Server started. Root URL: %s%n", server.whereIs("/"));
200-
}
201201
}

java/test/org/openqa/selenium/net/UrlCheckerTest.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
// under the License.
1717
package org.openqa.selenium.net;
1818

19+
import static java.lang.System.currentTimeMillis;
20+
import static org.assertj.core.api.Assertions.assertThat;
21+
import static org.openqa.selenium.remote.http.Contents.utf8String;
22+
import static org.openqa.selenium.testing.Safely.safelyCall;
23+
1924
import org.junit.After;
2025
import org.junit.Before;
2126
import org.junit.Test;
@@ -28,11 +33,6 @@
2833
import java.util.concurrent.Executors;
2934
import java.util.concurrent.TimeUnit;
3035

31-
import static java.lang.System.currentTimeMillis;
32-
import static org.assertj.core.api.Assertions.assertThat;
33-
import static org.openqa.selenium.remote.http.Contents.utf8String;
34-
import static org.openqa.selenium.testing.Safely.safelyCall;
35-
3636
public class UrlCheckerTest {
3737

3838
private final UrlChecker urlChecker = new UrlChecker();
@@ -44,14 +44,16 @@ public class UrlCheckerTest {
4444
public void buildServer() throws MalformedURLException, UrlChecker.TimeoutException {
4545
// Warming NettyServer up
4646
final NettyAppServer server = createServer();
47-
executorService.submit(() -> {
48-
server.start();
49-
return null;
50-
});
47+
server.start();
48+
// executorService.submit(() -> {
49+
// server.start();
50+
// return null;
51+
// });
5152
urlChecker.waitUntilAvailable(10, TimeUnit.SECONDS, new URL(server.whereIs("/")));
5253
server.stop();
5354

5455
this.server = createServer();
56+
this.server.start();
5557
this.url = new URL(this.server.whereIs("/"));
5658
}
5759

0 commit comments

Comments
 (0)