Skip to content

Commit 31a9914

Browse files
krmahadevanlukeis
authored andcommitted
Query Selenium Server to retrieve actual running port.
Fixes #1299 Altered the registration logic as below : If port given was 0, then query server to find running port and update registration request with correct values for remoteHost. Signed-off-by: Luke Inman-Semerau <[email protected]>
1 parent c33471b commit 31a9914

File tree

7 files changed

+120
-2
lines changed

7 files changed

+120
-2
lines changed

java/server/src/org/openqa/grid/internal/utils/SelfRegisteringRemote.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@
3131
import org.openqa.grid.common.RegistrationRequest;
3232
import org.openqa.grid.common.exception.GridConfigurationException;
3333
import org.openqa.grid.common.exception.GridException;
34+
import org.openqa.grid.shared.GridNodeServer;
3435
import org.openqa.selenium.Platform;
3536
import org.openqa.selenium.remote.DesiredCapabilities;
3637
import org.openqa.selenium.remote.internal.HttpClientFactory;
3738
import org.openqa.selenium.remote.server.log.LoggingManager;
38-
import org.openqa.grid.shared.GridNodeServer;
3939

4040
import java.io.BufferedReader;
4141
import java.io.IOException;
@@ -216,6 +216,7 @@ private void registerToHub(boolean checkPresenceFirst) {
216216

217217
BasicHttpEntityEnclosingRequest r =
218218
new BasicHttpEntityEnclosingRequest("POST", registration.toExternalForm());
219+
updateConfigWithRealPort();
219220
String json = nodeConfig.toJSON();
220221
r.setEntity(new StringEntity(json,"UTF-8"));
221222

@@ -236,6 +237,19 @@ private void registerToHub(boolean checkPresenceFirst) {
236237

237238
}
238239

240+
void updateConfigWithRealPort() throws MalformedURLException {
241+
int port = Integer.parseInt(nodeConfig.getConfigAsString(RegistrationRequest.PORT));
242+
if (port != 0) {
243+
return;
244+
}
245+
port = server.getRealPort();
246+
Map<String, Object> config = nodeConfig.getConfiguration();
247+
config.put(RegistrationRequest.PORT, server.getRealPort());
248+
URL url = new URL(nodeConfig.getConfigAsString(RegistrationRequest.REMOTE_HOST));
249+
String newUrl = url.getProtocol() + "://" + url.getHost() + ":" + port;
250+
config.put(RegistrationRequest.REMOTE_HOST, newUrl);
251+
}
252+
239253
/**
240254
* uses the hub API to get some of its configuration.
241255
* @return json object of the current hub configuration

java/server/src/org/openqa/grid/shared/GridNodeServer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@
2020
public interface GridNodeServer {
2121
void boot() throws Exception;
2222
void stop();
23+
int getRealPort();
2324
}

java/server/src/org/openqa/selenium/remote/server/SeleniumServer.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.beust.jcommander.JCommander;
2121

2222
import org.openqa.grid.shared.GridNodeServer;
23+
import org.openqa.jetty.http.SocketListener;
2324
import org.openqa.selenium.remote.SessionId;
2425
import org.openqa.selenium.remote.server.handler.DeleteSession;
2526
import org.seleniumhq.jetty9.server.Connector;
@@ -58,6 +59,14 @@ public class SeleniumServer implements GridNodeServer {
5859
public SeleniumServer(int port) {
5960
this.port = port;
6061
}
62+
public int getRealPort() {
63+
if (server.isStarted()) {
64+
ServerConnector socket = (ServerConnector)server.getConnectors()[0];
65+
return socket.getPort();
66+
}
67+
return this.port;
68+
}
69+
6170

6271
private void addRcSupport(ServletContextHandler handler) {
6372
try {

java/server/src/org/openqa/selenium/server/SeleniumServer.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,14 @@ public int getPort() {
588588
return configuration.getPort();
589589
}
590590

591+
public int getRealPort() {
592+
if (server.isStarted()) {
593+
SocketListener socket = (SocketListener) server.getListeners()[0];
594+
return socket.getPort();
595+
}
596+
return getPort();
597+
}
598+
591599
/**
592600
* Exposes the internal Jetty server used by Selenium. This lets users add their own webapp to the
593601
* Selenium Server jetty instance. It is also a minor violation of encapsulation principles (what

java/server/test/org/openqa/grid/internal/GridInternalTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.openqa.grid.internal.listener.RegistrationListenerTest;
2424
import org.openqa.grid.internal.listener.SessionListenerTest;
2525
import org.openqa.grid.internal.utils.DefaultCapabilityMatcherTest;
26+
import org.openqa.grid.internal.utils.SelfRegisteringRemoteTest;
2627
import org.openqa.grid.plugin.RemoteProxyInheritanceTest;
2728

2829
@RunWith(Suite.class)
@@ -47,7 +48,8 @@
4748
StatusServletTests.class,
4849
Grid1ConfigurationLoaderTest.class,
4950
UserDefinedCapabilityMatcherTests.class,
50-
GridShutdownTest.class
51+
GridShutdownTest.class,
52+
SelfRegisteringRemoteTest.class
5153
})
5254
public class GridInternalTests {
5355
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.openqa.grid.internal.utils;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
import org.openqa.grid.common.GridRole;
7+
import org.openqa.grid.common.RegistrationRequest;
8+
import org.openqa.grid.shared.GridNodeServer;
9+
10+
import java.net.MalformedURLException;
11+
12+
public class SelfRegisteringRemoteTest {
13+
14+
@Test
15+
public void testHubRegistrationWhenPortExplicitlyZeroedOut() throws MalformedURLException {
16+
GridNodeServer server = new GridNodeServer() {
17+
@Override
18+
public void boot() throws Exception {}
19+
20+
@Override
21+
public void stop() {}
22+
23+
@Override
24+
public int getRealPort() {
25+
return 1234;
26+
}
27+
};
28+
RegistrationRequest config = new RegistrationRequest();
29+
config.setRole(GridRole.NODE);
30+
config.getConfiguration().put(RegistrationRequest.HUB_HOST, "localhost");
31+
config.getConfiguration().put(RegistrationRequest.HUB_PORT, 4444);
32+
config.getConfiguration().put(RegistrationRequest.PORT, 0);
33+
config.getConfiguration().put(RegistrationRequest.REMOTE_HOST, "http://localhost:0/");
34+
SelfRegisteringRemote remote = new SelfRegisteringRemote(config);
35+
remote.setRemoteServer(server);
36+
remote.updateConfigWithRealPort();
37+
String host = (String) remote.getConfiguration().get(RegistrationRequest.REMOTE_HOST);
38+
assertEquals("Ensure that the remote host is updated properly",
39+
"http://localhost:" + server.getRealPort(), host);
40+
41+
}
42+
43+
}

java/server/test/org/openqa/selenium/server/SeleniumServerUnitTest.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@
1818
package org.openqa.selenium.server;
1919

2020
import static org.junit.Assert.assertEquals;
21+
import static org.junit.Assert.assertTrue;
2122

2223
import org.junit.After;
2324
import org.junit.Test;
25+
import org.openqa.selenium.net.PortProber;
26+
27+
import java.io.IOException;
28+
import java.net.ServerSocket;
2429

2530
/**
2631
* Unit tests for SeleniumServer.
@@ -66,6 +71,42 @@ public void testJettyThreadsPositive() throws Exception {
6671
positiveJettyThreads, server.getJettyThreads());
6772
}
6873

74+
@Test
75+
public void testServerStartupWhenPortExplicitlyZeroed() throws Exception {
76+
RemoteControlConfiguration configuration = new RemoteControlConfiguration();
77+
configuration.setPort(0);
78+
SeleniumServer server = null;
79+
try {
80+
server = new SeleniumServer(configuration);
81+
server.start();
82+
assertTrue("Ensure that Jetty server spawns on a valid port", server.getRealPort() != 0);
83+
assertTrue("Ensure that Jetty server is listening on the port", isPortUsed(server.getRealPort()));
84+
} finally {
85+
if (server != null) {
86+
server.stop();
87+
}
88+
}
89+
}
90+
91+
private boolean isPortUsed(int port) {
92+
boolean used = false;
93+
ServerSocket socket = null;
94+
try {
95+
socket = new ServerSocket(port);
96+
} catch (IOException e) {
97+
used = true;
98+
} finally {
99+
if (socket != null) {
100+
try {
101+
socket.close();
102+
} catch (IOException e) {
103+
104+
}
105+
}
106+
}
107+
return used;
108+
}
109+
69110
// /**
70111
// * Test for a positive result when passing a positive argument for
71112
// * -jettyThreads.

0 commit comments

Comments
 (0)