Skip to content

Commit d7dfd8d

Browse files
pujaganibarancev
authored andcommitted
Update tests to check results of Either when calling newSession() method.
Signed-off-by: Alexei Barantsev <[email protected]>
1 parent 2ceda22 commit d7dfd8d

File tree

3 files changed

+255
-150
lines changed

3 files changed

+255
-150
lines changed

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

+111-51
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,15 @@ public void shouldBeAbleToAddANodeAndCreateASession() throws URISyntaxException
165165
MutableCapabilities sessionCaps = new MutableCapabilities(caps);
166166
sessionCaps.setCapability("sausages", "gravy");
167167
try (NewSessionPayload payload = NewSessionPayload.create(sessionCaps)) {
168-
Session session = distributor.newSession(createRequest(payload)).right().getSession();
169-
170-
assertThat(session.getCapabilities()).isEqualTo(sessionCaps);
171-
assertThat(session.getUri()).isEqualTo(routableUri);
168+
Either<SessionNotCreatedException, CreateSessionResponse> result =
169+
distributor.newSession(createRequest(payload));
170+
if (result.isRight()) {
171+
Session session = result.right().getSession();
172+
assertThat(session.getCapabilities()).isEqualTo(sessionCaps);
173+
assertThat(session.getUri()).isEqualTo(routableUri);
174+
} else {
175+
fail("Session creation failed", result.left());
176+
}
172177
}
173178
}
174179

@@ -201,11 +206,17 @@ public void creatingASessionAddsItToTheSessionMap() throws URISyntaxException {
201206
MutableCapabilities sessionCaps = new MutableCapabilities(caps);
202207
sessionCaps.setCapability("sausages", "gravy");
203208
try (NewSessionPayload payload = NewSessionPayload.create(sessionCaps)) {
204-
Session returned = distributor.newSession(createRequest(payload)).right().getSession();
209+
Either<SessionNotCreatedException, CreateSessionResponse> result =
210+
distributor.newSession(createRequest(payload));
205211

206-
Session session = sessions.get(returned.getId());
207-
assertThat(session.getCapabilities()).isEqualTo(sessionCaps);
208-
assertThat(session.getUri()).isEqualTo(routableUri);
212+
if (result.isRight()) {
213+
Session returned = result.right().getSession();
214+
Session session = sessions.get(returned.getId());
215+
assertThat(session.getCapabilities()).isEqualTo(sessionCaps);
216+
assertThat(session.getUri()).isEqualTo(routableUri);
217+
} else {
218+
fail("Session creation failed", result.left());
219+
}
209220
}
210221
}
211222

@@ -520,9 +531,15 @@ public void theMostLightlyLoadedNodeIsSelectedFirst() {
520531
wait.until(obj -> distributor.getStatus().hasCapacity());
521532

522533
try (NewSessionPayload payload = NewSessionPayload.create(caps)) {
523-
Session session = distributor.newSession(createRequest(payload)).right().getSession();
534+
Either<SessionNotCreatedException, CreateSessionResponse> result =
535+
distributor.newSession(createRequest(payload));
524536

525-
assertThat(session.getUri()).isEqualTo(lightest.getStatus().getUri());
537+
if (result.isRight()) {
538+
Session session = result.right().getSession();
539+
assertThat(session.getUri()).isEqualTo(lightest.getStatus().getUri());
540+
} else {
541+
fail("Session creation failed", result.left());
542+
}
526543
}
527544
}
528545

@@ -559,20 +576,32 @@ public void shouldUseLastSessionCreatedTimeAsTieBreaker() {
559576
handler.addHandler(middle);
560577
distributor.add(middle);
561578
try (NewSessionPayload payload = NewSessionPayload.create(caps)) {
562-
Session session = distributor.newSession(createRequest(payload)).right().getSession();
579+
Either<SessionNotCreatedException, CreateSessionResponse> result =
580+
distributor.newSession(createRequest(payload));
563581

564-
// Least lightly loaded is middle
565-
assertThat(session.getUri()).isEqualTo(middle.getStatus().getUri());
582+
if (result.isRight()) {
583+
Session session = result.right().getSession();
584+
// Least lightly loaded is middle
585+
assertThat(session.getUri()).isEqualTo(middle.getStatus().getUri());
586+
} else {
587+
fail("Session creation failed", result.left());
588+
}
566589
}
567590

568591
Node mostRecent = createNode(caps, 5, 0);
569592
handler.addHandler(mostRecent);
570593
distributor.add(mostRecent);
571594
try (NewSessionPayload payload = NewSessionPayload.create(caps)) {
572-
Session session = distributor.newSession(createRequest(payload)).right().getSession();
595+
Either<SessionNotCreatedException, CreateSessionResponse> result =
596+
distributor.newSession(createRequest(payload));
597+
if (result.isRight()) {
598+
Session session = result.right().getSession();
573599

574-
// Least lightly loaded is most recent
575-
assertThat(session.getUri()).isEqualTo(mostRecent.getStatus().getUri());
600+
// Least lightly loaded is most recent
601+
assertThat(session.getUri()).isEqualTo(mostRecent.getStatus().getUri());
602+
} else {
603+
fail("Session creation failed", result.left());
604+
}
576605
}
577606

578607
// All the nodes should be equally loaded.
@@ -582,9 +611,15 @@ public void shouldUseLastSessionCreatedTimeAsTieBreaker() {
582611

583612
// All nodes are now equally loaded. We should be going in time order now
584613
try (NewSessionPayload payload = NewSessionPayload.create(caps)) {
585-
Session session = distributor.newSession(createRequest(payload)).right().getSession();
614+
Either<SessionNotCreatedException, CreateSessionResponse> result =
615+
distributor.newSession(createRequest(payload));
616+
if (result.isRight()) {
617+
Session session = result.right().getSession();
586618

587-
assertThat(session.getUri()).isEqualTo(leastRecent.getStatus().getUri());
619+
assertThat(session.getUri()).isEqualTo(leastRecent.getStatus().getUri());
620+
} else {
621+
fail("Session creation failed", result.left());
622+
}
588623
}
589624
}
590625

@@ -723,22 +758,27 @@ public void shouldReleaseSlotOnceSessionEnds() throws URISyntaxException {
723758
// Use up the one slot available
724759
Session session;
725760
try (NewSessionPayload payload = NewSessionPayload.create(caps)) {
726-
session = distributor.newSession(createRequest(payload)).right().getSession();
727-
}
728-
729-
// Make sure the session map has the session
730-
sessions.get(session.getId());
731-
732-
node.stop(session.getId());
733-
// Now wait for the session map to say the session is gone.
734-
wait.until(obj -> {
735-
try {
761+
Either<SessionNotCreatedException, CreateSessionResponse> result =
762+
distributor.newSession(createRequest(payload));
763+
if (result.isRight()) {
764+
session = result.right().getSession();
765+
// Make sure the session map has the session
736766
sessions.get(session.getId());
737-
return false;
738-
} catch (NoSuchSessionException e) {
739-
return true;
767+
768+
node.stop(session.getId());
769+
// Now wait for the session map to say the session is gone.
770+
wait.until(obj -> {
771+
try {
772+
sessions.get(session.getId());
773+
return false;
774+
} catch (NoSuchSessionException e) {
775+
return true;
776+
}
777+
});
778+
} else {
779+
fail("Session creation failed", result.left());
740780
}
741-
});
781+
}
742782

743783
wait.until(obj -> distributor.getStatus().hasCapacity());
744784

@@ -940,30 +980,50 @@ public void shouldPrioritizeHostsWithTheMostSlotsAvailableForASessionType() {
940980
try (NewSessionPayload chromePayload = NewSessionPayload.create(chromeCapabilities);
941981
NewSessionPayload firefoxPayload = NewSessionPayload.create(firefoxCapabilities)) {
942982

943-
Session chromeSession = distributor.newSession(createRequest(chromePayload)).right().getSession();
944-
945-
assertThat( //Ensure the Uri of the Session matches one of the Chrome Nodes, not the Edge Node
946-
chromeSession.getUri()).isIn(
947-
chromeNodes
948-
.stream().map(Node::getStatus).collect(Collectors.toList()) //List of getStatus() from the Set
949-
.stream().map(NodeStatus::getUri).collect(Collectors.toList()) //List of getUri() from the Set
950-
);
951-
952-
Session firefoxSession = distributor.newSession(createRequest(firefoxPayload)).right().getSession();
953-
LOG.info(String.format("Firefox Session %d assigned to %s", i, chromeSession.getUri()));
954-
955-
boolean inFirefoxNodes = firefoxNodes.stream().anyMatch(node -> node.getUri().equals(firefoxSession.getUri()));
956-
boolean inChromeNodes = chromeNodes.stream().anyMatch(node -> node.getUri().equals(chromeSession.getUri()));
957-
//This could be either, or, or both
958-
assertTrue(inFirefoxNodes || inChromeNodes);
983+
Either<SessionNotCreatedException, CreateSessionResponse> chromeResult =
984+
distributor.newSession(createRequest(chromePayload));
985+
986+
if (chromeResult.isRight()) {
987+
Session chromeSession = chromeResult.right().getSession();
988+
989+
assertThat( //Ensure the Uri of the Session matches one of the Chrome Nodes, not the Edge Node
990+
chromeSession.getUri()).isIn(
991+
chromeNodes
992+
.stream().map(Node::getStatus).collect(Collectors.toList()) //List of getStatus() from the Set
993+
.stream().map(NodeStatus::getUri).collect(Collectors.toList()) //List of getUri() from the Set
994+
);
995+
996+
Either<SessionNotCreatedException, CreateSessionResponse> firefoxResult =
997+
distributor.newSession(createRequest(firefoxPayload));
998+
999+
if (firefoxResult.isRight()) {
1000+
Session firefoxSession = firefoxResult.right().getSession();
1001+
LOG.info(String.format("Firefox Session %d assigned to %s", i, chromeSession.getUri()));
1002+
1003+
boolean inFirefoxNodes = firefoxNodes.stream().anyMatch(node -> node.getUri().equals(firefoxSession.getUri()));
1004+
boolean inChromeNodes = chromeNodes.stream().anyMatch(node -> node.getUri().equals(chromeSession.getUri()));
1005+
//This could be either, or, or both
1006+
assertTrue(inFirefoxNodes || inChromeNodes);
1007+
} else {
1008+
fail("Session creation failed", firefoxResult.left());
1009+
}
1010+
} else {
1011+
fail("Session creation failed", chromeResult.left());
1012+
}
9591013
}
9601014
}
9611015

9621016
//The Chrome Nodes should be full at this point, but Firefox isn't... so send an Edge session and make sure it routes to an Edge node
9631017
try (NewSessionPayload edgePayload = NewSessionPayload.create(edgeCapabilities)) {
964-
Session edgeSession = distributor.newSession(createRequest(edgePayload)).right().getSession();
965-
966-
assertTrue(edgeNodes.stream().anyMatch(node -> node.getUri().equals(edgeSession.getUri())));
1018+
Either<SessionNotCreatedException, CreateSessionResponse> edgeResult =
1019+
distributor.newSession(createRequest(edgePayload));
1020+
if (edgeResult.isRight()) {
1021+
Session edgeSession = edgeResult.right().getSession();
1022+
1023+
assertTrue(edgeNodes.stream().anyMatch(node -> node.getUri().equals(edgeSession.getUri())));
1024+
} else {
1025+
fail("Session creation failed", edgeResult.left());
1026+
}
9671027
}
9681028
}
9691029

java/server/test/org/openqa/selenium/grid/distributor/local/LocalDistributorTest.java

+13-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.junit.Test;
2323
import org.openqa.selenium.Capabilities;
2424
import org.openqa.selenium.ImmutableCapabilities;
25+
import org.openqa.selenium.SessionNotCreatedException;
2526
import org.openqa.selenium.events.EventBus;
2627
import org.openqa.selenium.events.local.GuavaEventBus;
2728
import org.openqa.selenium.grid.data.CreateSessionResponse;
@@ -37,6 +38,7 @@
3738
import org.openqa.selenium.grid.sessionqueue.local.LocalNewSessionQueue;
3839
import org.openqa.selenium.grid.sessionqueue.local.LocalNewSessionQueuer;
3940
import org.openqa.selenium.grid.testing.TestSessionFactory;
41+
import org.openqa.selenium.internal.Either;
4042
import org.openqa.selenium.remote.HttpSessionId;
4143
import org.openqa.selenium.remote.SessionId;
4244
import org.openqa.selenium.remote.http.Contents;
@@ -64,6 +66,7 @@
6466
import java.util.concurrent.TimeUnit;
6567

6668
import static org.assertj.core.api.Assertions.assertThat;
69+
import static org.assertj.core.api.Assertions.fail;
6770
import static org.openqa.selenium.grid.data.Availability.DRAINING;
6871
import static org.openqa.selenium.remote.http.HttpMethod.GET;
6972

@@ -244,9 +247,16 @@ public HttpResponse execute(HttpRequest req) {
244247
List<Callable<SessionId>> callables = new ArrayList<>();
245248
for (int i = 0; i < 3; i++) {
246249
callables.add(() -> {
247-
CreateSessionResponse res = distributor.newSession(req).right();
248-
assertThat(res.getSession().getCapabilities().getBrowserName()).isEqualTo("cheese");
249-
return res.getSession().getId();
250+
Either<SessionNotCreatedException, CreateSessionResponse> result =
251+
distributor.newSession(req);
252+
if (result.isRight()) {
253+
CreateSessionResponse res = result.right();
254+
assertThat(res.getSession().getCapabilities().getBrowserName()).isEqualTo("cheese");
255+
return res.getSession().getId();
256+
} else {
257+
fail("Session creation failed", result.left());
258+
}
259+
return null;
250260
});
251261
}
252262

0 commit comments

Comments
 (0)