Skip to content

Commit

Permalink
[grid] Ensure the RemoteNode can reach the LocalNode during tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pujagani committed Jul 14, 2021
1 parent 59962fe commit b69d4cb
Show file tree
Hide file tree
Showing 6 changed files with 274 additions and 120 deletions.
Expand Up @@ -49,6 +49,7 @@
import org.openqa.selenium.grid.sessionmap.local.LocalSessionMap;
import org.openqa.selenium.grid.sessionqueue.NewSessionQueue;
import org.openqa.selenium.grid.sessionqueue.local.LocalNewSessionQueue;
import org.openqa.selenium.grid.testing.PassthroughHttpClient;
import org.openqa.selenium.grid.testing.TestSessionFactory;
import org.openqa.selenium.grid.web.CombinedHandler;
import org.openqa.selenium.grid.web.RoutableHttpClientFactory;
Expand Down Expand Up @@ -92,6 +93,8 @@ public class AddingNodesTest {
private URL externalUrl;
private CombinedHandler handler;
private Capabilities stereotype;
private LocalSessionMap sessions;
private NewSessionQueue queue;

@Before
public void setUpDistributor() throws MalformedURLException {
Expand All @@ -100,32 +103,15 @@ public void setUpDistributor() throws MalformedURLException {

handler = new CombinedHandler();
externalUrl = new URL("http://example.com");
HttpClient.Factory clientFactory = new RoutableHttpClientFactory(
externalUrl,
handler,
HttpClient.Factory.createDefault());

LocalSessionMap sessions = new LocalSessionMap(tracer, bus);
NewSessionQueue queue = new LocalNewSessionQueue(
sessions = new LocalSessionMap(tracer, bus);
queue = new LocalNewSessionQueue(
tracer,
bus,
new DefaultSlotMatcher(),
Duration.ofSeconds(2),
Duration.ofSeconds(2),
registrationSecret);
Distributor local = new LocalDistributor(
tracer,
bus,
clientFactory,
sessions,
queue,
new DefaultSlotSelector(),
registrationSecret,
Duration.ofMinutes(5),
false);

handler.addHandler(local);
distributor = new RemoteDistributor(tracer, clientFactory, externalUrl, registrationSecret);

stereotype = new ImmutableCapabilities("browserName", "gouda");

Expand All @@ -143,7 +129,19 @@ public void shouldBeAbleToRegisterALocalNode() throws URISyntaxException {
new TestSessionFactory(
(id, caps) -> new Session(id, sessionUri, stereotype, caps, Instant.now())))
.build();
handler.addHandler(node);

Distributor local = new LocalDistributor(
tracer,
bus,
new PassthroughHttpClient.Factory(node),
sessions,
queue,
new DefaultSlotSelector(),
registrationSecret,
Duration.ofMinutes(5),
false);

distributor = new RemoteDistributor(tracer, new PassthroughHttpClient.Factory(local), externalUrl, registrationSecret);

distributor.add(node);

Expand All @@ -162,7 +160,19 @@ public void shouldBeAbleToRegisterACustomNode() throws URISyntaxException {
externalUrl.toURI(),
c -> new Session(
new SessionId(UUID.randomUUID()), sessionUri, stereotype, c, Instant.now()));
handler.addHandler(node);

Distributor local = new LocalDistributor(
tracer,
bus,
new PassthroughHttpClient.Factory(node),
sessions,
queue,
new DefaultSlotSelector(),
registrationSecret,
Duration.ofMinutes(5),
false);

distributor = new RemoteDistributor(tracer, new PassthroughHttpClient.Factory(local), externalUrl, registrationSecret);

distributor.add(node);

Expand All @@ -182,7 +192,19 @@ public void shouldBeAbleToRegisterNodesByListeningForEvents() throws URISyntaxEx
new TestSessionFactory(
(id, caps) -> new Session(id, sessionUri, stereotype, caps, Instant.now())))
.build();
handler.addHandler(node);

Distributor local = new LocalDistributor(
tracer,
bus,
new PassthroughHttpClient.Factory(node),
sessions,
queue,
new DefaultSlotSelector(),
registrationSecret,
Duration.ofMinutes(5),
false);

distributor = new RemoteDistributor(tracer, new PassthroughHttpClient.Factory(local), externalUrl, registrationSecret);

bus.fire(new NodeStatusEvent(node.getStatus()));

Expand Down Expand Up @@ -213,6 +235,19 @@ public void shouldKeepOnlyOneNodeWhenTwoRegistrationsHaveTheSameUriByListeningFo
handler.addHandler(firstNode);
handler.addHandler(secondNode);

Distributor local = new LocalDistributor(
tracer,
bus,
new PassthroughHttpClient.Factory(handler),
sessions,
queue,
new DefaultSlotSelector(),
registrationSecret,
Duration.ofMinutes(5),
false);

distributor = new RemoteDistributor(tracer, new PassthroughHttpClient.Factory(local), externalUrl, registrationSecret);

bus.fire(new NodeStatusEvent(firstNode.getStatus()));
bus.fire(new NodeStatusEvent(secondNode.getStatus()));

Expand All @@ -234,7 +269,19 @@ public void distributorShouldUpdateStateOfExistingNodeWhenNodePublishesStateChan
new TestSessionFactory(
(id, caps) -> new Session(id, sessionUri, stereotype, caps, Instant.now())))
.build();
handler.addHandler(node);

Distributor local = new LocalDistributor(
tracer,
bus,
new PassthroughHttpClient.Factory(node),
sessions,
queue,
new DefaultSlotSelector(),
registrationSecret,
Duration.ofMinutes(5),
false);

distributor = new RemoteDistributor(tracer, new PassthroughHttpClient.Factory(local), externalUrl, registrationSecret);

bus.fire(new NodeStatusEvent(node.getStatus()));

Expand Down
Expand Up @@ -92,6 +92,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.assertj.core.api.Assertions.from;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.openqa.selenium.grid.data.Availability.DOWN;
Expand All @@ -110,6 +111,8 @@ public class DistributorTest {
private Capabilities caps;
private URI nodeUri;
private URI routableUri;
private LocalSessionMap sessions;
private NewSessionQueue queue;

private static <A, B> EitherAssert<A, B> assertThatEither(Either<A, B> either) {
return new EitherAssert<>(either);
Expand All @@ -121,14 +124,21 @@ public void setUp() throws URISyntaxException {
routableUri = createUri();
tracer = DefaultTestTracer.createTracer();
bus = new GuavaEventBus();
LocalSessionMap sessions = new LocalSessionMap(tracer, bus);
NewSessionQueue queue = new LocalNewSessionQueue(
sessions = new LocalSessionMap(tracer, bus);
queue = new LocalNewSessionQueue(
tracer,
bus,
new DefaultSlotMatcher(),
Duration.ofSeconds(2),
Duration.ofSeconds(2),
registrationSecret);

stereotype = new ImmutableCapabilities("browserName", "cheese");
caps = new ImmutableCapabilities("browserName", "cheese");
}

@Test
public void creatingANewSessionWithoutANodeEndsInFailure() {
local = new LocalDistributor(
tracer,
bus,
Expand All @@ -139,12 +149,6 @@ public void setUp() throws URISyntaxException {
registrationSecret,
Duration.ofMinutes(5),
false);
stereotype = new ImmutableCapabilities("browserName", "cheese");
caps = new ImmutableCapabilities("browserName", "cheese");
}

@Test
public void creatingANewSessionWithoutANodeEndsInFailure() {
Either<SessionNotCreatedException, CreateSessionResponse> result = local.newSession(createRequest(caps));
assertThatEither(result).isLeft();
}
Expand Down Expand Up @@ -509,6 +513,17 @@ public void registeringTheSameNodeMultipleTimesOnlyCountsTheFirstTime() {
new TestSessionFactory((id, c) -> new Session(id, nodeUri, stereotype, c, Instant.now())))
.build();

local = new LocalDistributor(
tracer,
bus,
new PassthroughHttpClient.Factory(node),
sessions,
queue,
new DefaultSlotSelector(),
registrationSecret,
Duration.ofMinutes(5),
false);

local.add(node);
local.add(node);

Expand Down Expand Up @@ -1077,6 +1092,17 @@ public void statusShouldIndicateThatDistributorIsNotAvailableIfNodesAreDown()
.healthCheck(() -> new HealthCheck.Result(DOWN, "TL;DR"))
.build();

local = new LocalDistributor(
tracer,
bus,
new PassthroughHttpClient.Factory(node),
sessions,
queue,
new DefaultSlotSelector(),
registrationSecret,
Duration.ofMinutes(5),
false);

local.add(node);

DistributorStatus status = local.getStatus();
Expand All @@ -1098,6 +1124,17 @@ public void disabledNodeShouldNotAcceptNewRequests()
.healthCheck(() -> new HealthCheck.Result(DOWN, "TL;DR"))
.build();

local = new LocalDistributor(
tracer,
bus,
new PassthroughHttpClient.Factory(node),
sessions,
queue,
new DefaultSlotSelector(),
registrationSecret,
Duration.ofMinutes(5),
false);

local.add(node);

DistributorStatus status = local.getStatus();
Expand All @@ -1106,8 +1143,27 @@ public void disabledNodeShouldNotAcceptNewRequests()

@Test
public void shouldFallbackToSecondAvailableCapabilitiesIfFirstNotAvailable() {
local.add(createNode(new ImmutableCapabilities("browserName", "not cheese"), 1, 1));
local.add(createNode(new ImmutableCapabilities("browserName", "cheese"), 1, 0));
CombinedHandler handler = new CombinedHandler();

Node firstNode = createNode(new ImmutableCapabilities("browserName", "not cheese"), 1, 1);
Node secondNode = createNode(new ImmutableCapabilities("browserName", "cheese"), 1, 0);

handler.addHandler(firstNode);
handler.addHandler(secondNode);

local = new LocalDistributor(
tracer,
bus,
new PassthroughHttpClient.Factory(handler),
sessions,
queue,
new DefaultSlotSelector(),
registrationSecret,
Duration.ofMinutes(5),
false);

local.add(firstNode);
local.add(secondNode);
waitToHaveCapacity(local);

SessionRequest sessionRequest = new SessionRequest(
Expand All @@ -1132,8 +1188,23 @@ public void shouldFallbackToSecondAvailableCapabilitiesIfFirstNotAvailable() {

@Test
public void shouldFallbackToSecondAvailableCapabilitiesIfFirstThrowsOnCreation() {
local.add(createBrokenNode(new ImmutableCapabilities("browserName", "not cheese")));
local.add(createNode(new ImmutableCapabilities("browserName", "cheese"), 1, 0));
CombinedHandler handler = new CombinedHandler();
Node brokenNode = createBrokenNode(new ImmutableCapabilities("browserName", "not cheese"));
Node node = createNode(new ImmutableCapabilities("browserName", "cheese"), 1, 0);
handler.addHandler(brokenNode);
handler.addHandler(node);
local = new LocalDistributor(
tracer,
bus,
new PassthroughHttpClient.Factory(handler),
sessions,
queue,
new DefaultSlotSelector(),
registrationSecret,
Duration.ofMinutes(5),
false);
local.add(brokenNode);
local.add(node);
waitForAllNodesToHaveCapacity(local, 2);

SessionRequest sessionRequest = new SessionRequest(
Expand Down Expand Up @@ -1188,7 +1259,7 @@ private void waitForAllNodesToHaveCapacity(Distributor distributor, int nodeCoun
private void waitForAllNodesToMeetCondition(Distributor distributor, int nodeCount,
Availability availability) {
new FluentWait<>(distributor)
.withTimeout(Duration.ofSeconds(5))
.withTimeout(Duration.ofSeconds(10))
.pollingEvery(Duration.ofMillis(100))
.until(d -> {
Set<NodeStatus> nodes = d.getStatus().getNodes();
Expand Down

0 comments on commit b69d4cb

Please sign in to comment.