Skip to content

Commit

Permalink
[grid]: Make the end to end test parameterised
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Mar 19, 2019
1 parent 687d1a8 commit abffeac
Showing 1 changed file with 55 additions and 42 deletions.
97 changes: 55 additions & 42 deletions java/server/test/org/openqa/selenium/grid/router/EndToEndTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@
import static org.openqa.selenium.remote.http.HttpMethod.GET;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.SessionNotCreatedException;
Expand Down Expand Up @@ -63,27 +67,39 @@
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collection;
import java.util.Map;
import java.util.UUID;
import java.util.function.Function;

@RunWith(Parameterized.class)
public class EndToEndTest {

private final Capabilities driverCaps = new ImmutableCapabilities("browserName", "cheese");
private final DistributedTracer tracer = DistributedTracer.builder().build();
private HttpClient.Factory clientFactory;
private static final Capabilities CAPS = new ImmutableCapabilities("browserName", "cheese");

@Test
public void inMemory() throws URISyntaxException, MalformedURLException {
@Parameterized.Parameters(name = "End to End {0}")
public static Collection<Object[]> buildTracers()
throws MalformedURLException, URISyntaxException {
return ImmutableSet.of(createInMemory(), createRemotes());
}

@Parameter(value = 0)
public Server<?> server;

@Parameter(value = 1)
public HttpClient.Factory clientFactory;

public static Object[] createInMemory() throws URISyntaxException, MalformedURLException {
EventBus bus = ZeroMqEventBus.create(
new ZContext(),
"inproc://end-to-end-pub",
"inproc://end-to-end-sub",
true);

DistributedTracer tracer = DistributedTracer.builder().build();
URI nodeUri = new URI("http://localhost:4444");
CombinedHandler handler = new CombinedHandler();
clientFactory = new RoutableHttpClientFactory(
HttpClient.Factory clientFactory = new RoutableHttpClientFactory(
nodeUri.toURL(),
handler,
HttpClient.Factory.createDefault());
Expand All @@ -95,7 +111,7 @@ public void inMemory() throws URISyntaxException, MalformedURLException {
handler.addHandler(distributor);

LocalNode node = LocalNode.builder(tracer, bus, clientFactory, nodeUri)
.add(driverCaps, createFactory(nodeUri))
.add(CAPS, createFactory(nodeUri))
.build();
handler.addHandler(node);
distributor.add(node);
Expand All @@ -106,20 +122,20 @@ public void inMemory() throws URISyntaxException, MalformedURLException {
server.addRoute(Routes.matching(router).using(router));
server.start();

exerciseDriver(server);
return new Object[] { server, clientFactory };
}

@Test
public void withServers() throws URISyntaxException {
public static Object[] createRemotes() throws URISyntaxException {
EventBus bus = ZeroMqEventBus.create(
new ZContext(),
"tcp://localhost:" + PortProber.findFreePort(),
"tcp://localhost:" + PortProber.findFreePort(),
true);

DistributedTracer tracer = DistributedTracer.builder().build();
LocalSessionMap localSessions = new LocalSessionMap(tracer, bus);

clientFactory = HttpClient.Factory.createDefault();
HttpClient.Factory clientFactory = HttpClient.Factory.createDefault();

Server<?> sessionServer = createServer();
sessionServer.addRoute(
Expand All @@ -128,7 +144,8 @@ public void withServers() throws URISyntaxException {
.decorateWith(W3CCommandHandler.class));
sessionServer.start();

SessionMap sessions = new RemoteSessionMap(getClient(sessionServer));
HttpClient client = HttpClient.Factory.createDefault().createClient(sessionServer.getUrl());
SessionMap sessions = new RemoteSessionMap(client);

LocalDistributor localDistributor = new LocalDistributor(
tracer,
Expand All @@ -150,7 +167,7 @@ public void withServers() throws URISyntaxException {
int port = PortProber.findFreePort();
URI nodeUri = new URI("http://localhost:" + port);
LocalNode localNode = LocalNode.builder(tracer, bus, clientFactory, nodeUri)
.add(driverCaps, createFactory(nodeUri))
.add(CAPS, createFactory(nodeUri))
.build();
Server<?> nodeServer = new BaseServer<>(
new BaseServerOptions(
Expand All @@ -171,10 +188,33 @@ public void withServers() throws URISyntaxException {
.decorateWith(W3CCommandHandler.class));
routerServer.start();

exerciseDriver(routerServer);
return new Object[] { routerServer, clientFactory };
}

private static Server<?> createServer() {
int port = PortProber.findFreePort();
return new BaseServer<>(new BaseServerOptions(
new MapConfig(ImmutableMap.of("server", ImmutableMap.of("port", port)))));
}

private static Function<Capabilities, Session> createFactory(URI serverUri) {
class SpoofSession extends Session implements CommandHandler {

private SpoofSession(Capabilities capabilities) {
super(new SessionId(UUID.randomUUID()), serverUri, capabilities);
}

@Override
public void execute(HttpRequest req, HttpResponse resp) {

}
}

return caps -> new SpoofSession(caps);
}

private void exerciseDriver(Server<?> server) {
@Test
public void exerciseDriver() {
// The node added only has a single node. Make sure we can start and stop sessions.
Capabilities caps = new ImmutableCapabilities("browserName", "cheese", "type", "cheddar");
WebDriver driver = new RemoteWebDriver(server.getUrl(), caps);
Expand Down Expand Up @@ -209,31 +249,4 @@ private void exerciseDriver(Server<?> server) {
driver.get("http://www.google.com");
driver.quit();
}

private HttpClient getClient(Server<?> server) {
return HttpClient.Factory.createDefault().createClient(server.getUrl());
}

private Server<?> createServer() {
int port = PortProber.findFreePort();
return new BaseServer<>(new BaseServerOptions(
new MapConfig(ImmutableMap.of("server", ImmutableMap.of("port", port)))));
}

private Function<Capabilities, Session> createFactory(URI serverUri) {
class SpoofSession extends Session implements CommandHandler {

private SpoofSession(Capabilities capabilities) {
super(new SessionId(UUID.randomUUID()), serverUri, capabilities);
}

@Override
public void execute(HttpRequest req, HttpResponse resp) {

}
}

return caps -> new SpoofSession(caps);
}

}

0 comments on commit abffeac

Please sign in to comment.