Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8755948

Browse files
authored
[grid] Improving DistributorTest (SeleniumHQ#9546)
1 parent 6f25a6f commit 8755948

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

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

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import org.openqa.selenium.MutableCapabilities;
3030
import org.openqa.selenium.NoSuchSessionException;
3131
import org.openqa.selenium.SessionNotCreatedException;
32-
import org.openqa.selenium.TimeoutException;
3332
import org.openqa.selenium.events.EventBus;
3433
import org.openqa.selenium.events.local.GuavaEventBus;
3534
import org.openqa.selenium.grid.data.Availability;
@@ -119,7 +118,7 @@ private static <A, B> EitherAssert<A, B> assertThatEither(Either<A, B> either) {
119118
@Before
120119
public void setUp() throws URISyntaxException {
121120
nodeUri = new URI("http://example:5678");
122-
routableUri = new URI(String.format("http://localhost:%s", PortProber.findFreePort()));
121+
routableUri = createUri();
123122
tracer = DefaultTestTracer.createTracer();
124123
bus = new GuavaEventBus();
125124
LocalSessionMap sessions = new LocalSessionMap(tracer, bus);
@@ -198,7 +197,7 @@ public void shouldStartHeartBeatOnNodeRegistration() {
198197
}
199198

200199
assertThat(eventFired).isTrue();
201-
assertThat(heartbeatStarted).isTrue();
200+
assertThat(heartbeatStarted.get()).isTrue();
202201
}
203202

204203
@Test
@@ -559,6 +558,8 @@ public void theMostLightlyLoadedNodeIsSelectedFirst() {
559558
.add(massive);
560559

561560
wait.until(obj -> distributor.getStatus().getNodes().size() == 4);
561+
wait.until(ignored -> distributor.getStatus().getNodes().stream().allMatch(
562+
node -> node.getAvailability() == UP && node.hasCapacity()));
562563
wait.until(obj -> distributor.getStatus().hasCapacity());
563564

564565
Either<SessionNotCreatedException, CreateSessionResponse> result =
@@ -684,10 +685,11 @@ public void shouldIncludeHostsThatAreUpInHostList() {
684685
queue,
685686
new DefaultSlotSelector(),
686687
registrationSecret,
687-
Duration.ofMinutes(5),
688+
Duration.ofSeconds(1),
688689
false);
689690
handler.addHandler(distributor);
690691
distributor.add(alwaysDown);
692+
waitForAllNodesToMeetCondition(distributor, 1, DOWN);
691693

692694
// Should be unable to create a session because the node is down.
693695
Either<SessionNotCreatedException, CreateSessionResponse> result =
@@ -904,10 +906,11 @@ public void shouldReturnNodesThatWereDownToPoolOfNodesOnceTheyMarkTheirHealthChe
904906
queue,
905907
new DefaultSlotSelector(),
906908
registrationSecret,
907-
Duration.ofMinutes(5),
909+
Duration.ofSeconds(1),
908910
false);
909911
handler.addHandler(distributor);
910912
distributor.add(node);
913+
waitForAllNodesToMeetCondition(distributor, 1, DOWN);
911914

912915
// Should be unable to create a session because the node is down.
913916
Either<SessionNotCreatedException, CreateSessionResponse> result =
@@ -1131,7 +1134,7 @@ public void shouldFallbackToSecondAvailableCapabilitiesIfFirstNotAvailable() {
11311134
public void shouldFallbackToSecondAvailableCapabilitiesIfFirstThrowsOnCreation() {
11321135
local.add(createBrokenNode(new ImmutableCapabilities("browserName", "not cheese")));
11331136
local.add(createNode(new ImmutableCapabilities("browserName", "cheese"), 1, 0));
1134-
waitToHaveCapacity(local);
1137+
waitForAllNodesToHaveCapacity(local, 2);
11351138

11361139
SessionRequest sessionRequest = new SessionRequest(
11371140
new RequestId(UUID.randomUUID()),
@@ -1179,21 +1182,19 @@ private void waitToHaveCapacity(Distributor distributor) {
11791182
}
11801183

11811184
private void waitForAllNodesToHaveCapacity(Distributor distributor, int nodeCount) {
1182-
try {
1183-
new FluentWait<>(distributor)
1184-
.withTimeout(Duration.ofSeconds(5))
1185-
.pollingEvery(Duration.ofMillis(100))
1186-
.until(d -> {
1187-
Set<NodeStatus> nodes = d.getStatus().getNodes();
1188-
return nodes.size() == nodeCount && nodes.stream().allMatch(
1189-
node -> node.getAvailability() == UP && node.hasCapacity());
1190-
});
1191-
} catch (TimeoutException ex) {
1192-
Set<NodeStatus> nodes = distributor.getStatus().getNodes();
1193-
System.out.println("*************");
1194-
System.out.println("" + nodes.size());
1195-
nodes.forEach(node -> System.out.println("" + node.hasCapacity()));
1196-
}
1185+
waitForAllNodesToMeetCondition(distributor, nodeCount, UP);
1186+
}
1187+
1188+
private void waitForAllNodesToMeetCondition(Distributor distributor, int nodeCount,
1189+
Availability availability) {
1190+
new FluentWait<>(distributor)
1191+
.withTimeout(Duration.ofSeconds(5))
1192+
.pollingEvery(Duration.ofMillis(100))
1193+
.until(d -> {
1194+
Set<NodeStatus> nodes = d.getStatus().getNodes();
1195+
return nodes.size() == nodeCount && nodes.stream().allMatch(
1196+
node -> node.getAvailability() == availability && node.hasCapacity());
1197+
});
11971198
}
11981199

11991200
class HandledSession extends Session implements HttpHandler {

0 commit comments

Comments
 (0)