Skip to content

Commit b69d4cb

Browse files
authored
[grid] Ensure the RemoteNode can reach the LocalNode during tests
1 parent 59962fe commit b69d4cb

File tree

6 files changed

+274
-120
lines changed

6 files changed

+274
-120
lines changed

java/server/test/org/openqa/selenium/grid/distributor/AddingNodesTest.java

+70-23
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import org.openqa.selenium.grid.sessionmap.local.LocalSessionMap;
5050
import org.openqa.selenium.grid.sessionqueue.NewSessionQueue;
5151
import org.openqa.selenium.grid.sessionqueue.local.LocalNewSessionQueue;
52+
import org.openqa.selenium.grid.testing.PassthroughHttpClient;
5253
import org.openqa.selenium.grid.testing.TestSessionFactory;
5354
import org.openqa.selenium.grid.web.CombinedHandler;
5455
import org.openqa.selenium.grid.web.RoutableHttpClientFactory;
@@ -92,6 +93,8 @@ public class AddingNodesTest {
9293
private URL externalUrl;
9394
private CombinedHandler handler;
9495
private Capabilities stereotype;
96+
private LocalSessionMap sessions;
97+
private NewSessionQueue queue;
9598

9699
@Before
97100
public void setUpDistributor() throws MalformedURLException {
@@ -100,32 +103,15 @@ public void setUpDistributor() throws MalformedURLException {
100103

101104
handler = new CombinedHandler();
102105
externalUrl = new URL("http://example.com");
103-
HttpClient.Factory clientFactory = new RoutableHttpClientFactory(
104-
externalUrl,
105-
handler,
106-
HttpClient.Factory.createDefault());
107106

108-
LocalSessionMap sessions = new LocalSessionMap(tracer, bus);
109-
NewSessionQueue queue = new LocalNewSessionQueue(
107+
sessions = new LocalSessionMap(tracer, bus);
108+
queue = new LocalNewSessionQueue(
110109
tracer,
111110
bus,
112111
new DefaultSlotMatcher(),
113112
Duration.ofSeconds(2),
114113
Duration.ofSeconds(2),
115114
registrationSecret);
116-
Distributor local = new LocalDistributor(
117-
tracer,
118-
bus,
119-
clientFactory,
120-
sessions,
121-
queue,
122-
new DefaultSlotSelector(),
123-
registrationSecret,
124-
Duration.ofMinutes(5),
125-
false);
126-
127-
handler.addHandler(local);
128-
distributor = new RemoteDistributor(tracer, clientFactory, externalUrl, registrationSecret);
129115

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

@@ -143,7 +129,19 @@ public void shouldBeAbleToRegisterALocalNode() throws URISyntaxException {
143129
new TestSessionFactory(
144130
(id, caps) -> new Session(id, sessionUri, stereotype, caps, Instant.now())))
145131
.build();
146-
handler.addHandler(node);
132+
133+
Distributor local = new LocalDistributor(
134+
tracer,
135+
bus,
136+
new PassthroughHttpClient.Factory(node),
137+
sessions,
138+
queue,
139+
new DefaultSlotSelector(),
140+
registrationSecret,
141+
Duration.ofMinutes(5),
142+
false);
143+
144+
distributor = new RemoteDistributor(tracer, new PassthroughHttpClient.Factory(local), externalUrl, registrationSecret);
147145

148146
distributor.add(node);
149147

@@ -162,7 +160,19 @@ public void shouldBeAbleToRegisterACustomNode() throws URISyntaxException {
162160
externalUrl.toURI(),
163161
c -> new Session(
164162
new SessionId(UUID.randomUUID()), sessionUri, stereotype, c, Instant.now()));
165-
handler.addHandler(node);
163+
164+
Distributor local = new LocalDistributor(
165+
tracer,
166+
bus,
167+
new PassthroughHttpClient.Factory(node),
168+
sessions,
169+
queue,
170+
new DefaultSlotSelector(),
171+
registrationSecret,
172+
Duration.ofMinutes(5),
173+
false);
174+
175+
distributor = new RemoteDistributor(tracer, new PassthroughHttpClient.Factory(local), externalUrl, registrationSecret);
166176

167177
distributor.add(node);
168178

@@ -182,7 +192,19 @@ public void shouldBeAbleToRegisterNodesByListeningForEvents() throws URISyntaxEx
182192
new TestSessionFactory(
183193
(id, caps) -> new Session(id, sessionUri, stereotype, caps, Instant.now())))
184194
.build();
185-
handler.addHandler(node);
195+
196+
Distributor local = new LocalDistributor(
197+
tracer,
198+
bus,
199+
new PassthroughHttpClient.Factory(node),
200+
sessions,
201+
queue,
202+
new DefaultSlotSelector(),
203+
registrationSecret,
204+
Duration.ofMinutes(5),
205+
false);
206+
207+
distributor = new RemoteDistributor(tracer, new PassthroughHttpClient.Factory(local), externalUrl, registrationSecret);
186208

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

@@ -213,6 +235,19 @@ public void shouldKeepOnlyOneNodeWhenTwoRegistrationsHaveTheSameUriByListeningFo
213235
handler.addHandler(firstNode);
214236
handler.addHandler(secondNode);
215237

238+
Distributor local = new LocalDistributor(
239+
tracer,
240+
bus,
241+
new PassthroughHttpClient.Factory(handler),
242+
sessions,
243+
queue,
244+
new DefaultSlotSelector(),
245+
registrationSecret,
246+
Duration.ofMinutes(5),
247+
false);
248+
249+
distributor = new RemoteDistributor(tracer, new PassthroughHttpClient.Factory(local), externalUrl, registrationSecret);
250+
216251
bus.fire(new NodeStatusEvent(firstNode.getStatus()));
217252
bus.fire(new NodeStatusEvent(secondNode.getStatus()));
218253

@@ -234,7 +269,19 @@ public void distributorShouldUpdateStateOfExistingNodeWhenNodePublishesStateChan
234269
new TestSessionFactory(
235270
(id, caps) -> new Session(id, sessionUri, stereotype, caps, Instant.now())))
236271
.build();
237-
handler.addHandler(node);
272+
273+
Distributor local = new LocalDistributor(
274+
tracer,
275+
bus,
276+
new PassthroughHttpClient.Factory(node),
277+
sessions,
278+
queue,
279+
new DefaultSlotSelector(),
280+
registrationSecret,
281+
Duration.ofMinutes(5),
282+
false);
283+
284+
distributor = new RemoteDistributor(tracer, new PassthroughHttpClient.Factory(local), externalUrl, registrationSecret);
238285

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

java/server/test/org/openqa/selenium/grid/distributor/DistributorTest.java

+84-13
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292

9393
import static org.assertj.core.api.Assertions.assertThat;
9494
import static org.assertj.core.api.Assertions.fail;
95+
import static org.assertj.core.api.Assertions.from;
9596
import static org.junit.Assert.assertFalse;
9697
import static org.junit.Assert.assertTrue;
9798
import static org.openqa.selenium.grid.data.Availability.DOWN;
@@ -110,6 +111,8 @@ public class DistributorTest {
110111
private Capabilities caps;
111112
private URI nodeUri;
112113
private URI routableUri;
114+
private LocalSessionMap sessions;
115+
private NewSessionQueue queue;
113116

114117
private static <A, B> EitherAssert<A, B> assertThatEither(Either<A, B> either) {
115118
return new EitherAssert<>(either);
@@ -121,14 +124,21 @@ public void setUp() throws URISyntaxException {
121124
routableUri = createUri();
122125
tracer = DefaultTestTracer.createTracer();
123126
bus = new GuavaEventBus();
124-
LocalSessionMap sessions = new LocalSessionMap(tracer, bus);
125-
NewSessionQueue queue = new LocalNewSessionQueue(
127+
sessions = new LocalSessionMap(tracer, bus);
128+
queue = new LocalNewSessionQueue(
126129
tracer,
127130
bus,
128131
new DefaultSlotMatcher(),
129132
Duration.ofSeconds(2),
130133
Duration.ofSeconds(2),
131134
registrationSecret);
135+
136+
stereotype = new ImmutableCapabilities("browserName", "cheese");
137+
caps = new ImmutableCapabilities("browserName", "cheese");
138+
}
139+
140+
@Test
141+
public void creatingANewSessionWithoutANodeEndsInFailure() {
132142
local = new LocalDistributor(
133143
tracer,
134144
bus,
@@ -139,12 +149,6 @@ public void setUp() throws URISyntaxException {
139149
registrationSecret,
140150
Duration.ofMinutes(5),
141151
false);
142-
stereotype = new ImmutableCapabilities("browserName", "cheese");
143-
caps = new ImmutableCapabilities("browserName", "cheese");
144-
}
145-
146-
@Test
147-
public void creatingANewSessionWithoutANodeEndsInFailure() {
148152
Either<SessionNotCreatedException, CreateSessionResponse> result = local.newSession(createRequest(caps));
149153
assertThatEither(result).isLeft();
150154
}
@@ -509,6 +513,17 @@ public void registeringTheSameNodeMultipleTimesOnlyCountsTheFirstTime() {
509513
new TestSessionFactory((id, c) -> new Session(id, nodeUri, stereotype, c, Instant.now())))
510514
.build();
511515

516+
local = new LocalDistributor(
517+
tracer,
518+
bus,
519+
new PassthroughHttpClient.Factory(node),
520+
sessions,
521+
queue,
522+
new DefaultSlotSelector(),
523+
registrationSecret,
524+
Duration.ofMinutes(5),
525+
false);
526+
512527
local.add(node);
513528
local.add(node);
514529

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

1095+
local = new LocalDistributor(
1096+
tracer,
1097+
bus,
1098+
new PassthroughHttpClient.Factory(node),
1099+
sessions,
1100+
queue,
1101+
new DefaultSlotSelector(),
1102+
registrationSecret,
1103+
Duration.ofMinutes(5),
1104+
false);
1105+
10801106
local.add(node);
10811107

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

1127+
local = new LocalDistributor(
1128+
tracer,
1129+
bus,
1130+
new PassthroughHttpClient.Factory(node),
1131+
sessions,
1132+
queue,
1133+
new DefaultSlotSelector(),
1134+
registrationSecret,
1135+
Duration.ofMinutes(5),
1136+
false);
1137+
11011138
local.add(node);
11021139

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

11071144
@Test
11081145
public void shouldFallbackToSecondAvailableCapabilitiesIfFirstNotAvailable() {
1109-
local.add(createNode(new ImmutableCapabilities("browserName", "not cheese"), 1, 1));
1110-
local.add(createNode(new ImmutableCapabilities("browserName", "cheese"), 1, 0));
1146+
CombinedHandler handler = new CombinedHandler();
1147+
1148+
Node firstNode = createNode(new ImmutableCapabilities("browserName", "not cheese"), 1, 1);
1149+
Node secondNode = createNode(new ImmutableCapabilities("browserName", "cheese"), 1, 0);
1150+
1151+
handler.addHandler(firstNode);
1152+
handler.addHandler(secondNode);
1153+
1154+
local = new LocalDistributor(
1155+
tracer,
1156+
bus,
1157+
new PassthroughHttpClient.Factory(handler),
1158+
sessions,
1159+
queue,
1160+
new DefaultSlotSelector(),
1161+
registrationSecret,
1162+
Duration.ofMinutes(5),
1163+
false);
1164+
1165+
local.add(firstNode);
1166+
local.add(secondNode);
11111167
waitToHaveCapacity(local);
11121168

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

11331189
@Test
11341190
public void shouldFallbackToSecondAvailableCapabilitiesIfFirstThrowsOnCreation() {
1135-
local.add(createBrokenNode(new ImmutableCapabilities("browserName", "not cheese")));
1136-
local.add(createNode(new ImmutableCapabilities("browserName", "cheese"), 1, 0));
1191+
CombinedHandler handler = new CombinedHandler();
1192+
Node brokenNode = createBrokenNode(new ImmutableCapabilities("browserName", "not cheese"));
1193+
Node node = createNode(new ImmutableCapabilities("browserName", "cheese"), 1, 0);
1194+
handler.addHandler(brokenNode);
1195+
handler.addHandler(node);
1196+
local = new LocalDistributor(
1197+
tracer,
1198+
bus,
1199+
new PassthroughHttpClient.Factory(handler),
1200+
sessions,
1201+
queue,
1202+
new DefaultSlotSelector(),
1203+
registrationSecret,
1204+
Duration.ofMinutes(5),
1205+
false);
1206+
local.add(brokenNode);
1207+
local.add(node);
11371208
waitForAllNodesToHaveCapacity(local, 2);
11381209

11391210
SessionRequest sessionRequest = new SessionRequest(
@@ -1188,7 +1259,7 @@ private void waitForAllNodesToHaveCapacity(Distributor distributor, int nodeCoun
11881259
private void waitForAllNodesToMeetCondition(Distributor distributor, int nodeCount,
11891260
Availability availability) {
11901261
new FluentWait<>(distributor)
1191-
.withTimeout(Duration.ofSeconds(5))
1262+
.withTimeout(Duration.ofSeconds(10))
11921263
.pollingEvery(Duration.ofMillis(100))
11931264
.until(d -> {
11941265
Set<NodeStatus> nodes = d.getStatus().getNodes();

0 commit comments

Comments
 (0)