|
17 | 17 |
|
18 | 18 | package org.openqa.selenium.grid.router; |
19 | 19 |
|
| 20 | +import static org.assertj.core.api.Assertions.assertThat; |
| 21 | +import static org.assertj.core.api.Assertions.fail; |
20 | 22 | import static org.junit.Assert.assertEquals; |
21 | 23 | import static org.junit.Assert.assertFalse; |
22 | 24 | import static org.junit.Assert.assertNotNull; |
| 25 | +import static org.junit.Assert.assertTrue; |
23 | 26 | import static org.openqa.selenium.grid.data.Availability.DOWN; |
24 | 27 | import static org.openqa.selenium.grid.data.Availability.UP; |
25 | 28 | import static org.openqa.selenium.json.Json.MAP_TYPE; |
26 | 29 | import static org.openqa.selenium.remote.http.Contents.reader; |
| 30 | +import static org.openqa.selenium.remote.Dialect.OSS; |
| 31 | +import static org.openqa.selenium.remote.Dialect.W3C; |
27 | 32 | import static org.openqa.selenium.remote.http.HttpMethod.GET; |
28 | 33 |
|
| 34 | +import com.google.common.collect.ImmutableMap; |
| 35 | +import com.google.common.collect.ImmutableSet; |
| 36 | + |
29 | 37 | import org.junit.Before; |
30 | 38 | import org.junit.Test; |
31 | 39 | import org.openqa.selenium.Capabilities; |
32 | 40 | import org.openqa.selenium.ImmutableCapabilities; |
| 41 | +import org.openqa.selenium.SessionNotCreatedException; |
33 | 42 | import org.openqa.selenium.events.EventBus; |
34 | 43 | import org.openqa.selenium.events.local.GuavaEventBus; |
35 | 44 | import org.openqa.selenium.grid.data.Availability; |
| 45 | +import org.openqa.selenium.grid.data.CreateSessionResponse; |
36 | 46 | import org.openqa.selenium.grid.data.DefaultSlotMatcher; |
| 47 | +import org.openqa.selenium.grid.data.RequestId; |
37 | 48 | import org.openqa.selenium.grid.data.Session; |
| 49 | +import org.openqa.selenium.grid.data.SessionRequest; |
38 | 50 | import org.openqa.selenium.grid.distributor.Distributor; |
39 | 51 | import org.openqa.selenium.grid.distributor.local.LocalDistributor; |
40 | 52 | import org.openqa.selenium.grid.distributor.selector.DefaultSlotSelector; |
|
52 | 64 | import org.openqa.selenium.grid.web.Values; |
53 | 65 | import org.openqa.selenium.json.Json; |
54 | 66 | import org.openqa.selenium.json.JsonInput; |
| 67 | +import org.openqa.selenium.internal.Either; |
55 | 68 | import org.openqa.selenium.remote.http.HttpClient; |
56 | 69 | import org.openqa.selenium.remote.http.HttpRequest; |
57 | 70 | import org.openqa.selenium.remote.http.HttpResponse; |
|
65 | 78 | import java.net.URISyntaxException; |
66 | 79 | import java.time.Duration; |
67 | 80 | import java.time.Instant; |
| 81 | +import java.util.List; |
68 | 82 | import java.util.Map; |
| 83 | +import java.util.UUID; |
69 | 84 | import java.util.concurrent.atomic.AtomicReference; |
70 | 85 |
|
71 | 86 | public class RouterTest { |
@@ -214,13 +229,85 @@ public void aNodeThatIsUpAndHasSpareSessionsMeansTheGridIsReady() throws URISynt |
214 | 229 | } |
215 | 230 |
|
216 | 231 | @Test |
217 | | - public void shouldListAllNodesTheDistributorIsAwareOf() { |
| 232 | + public void shouldListAllNodesTheDistributorIsAwareOf() throws URISyntaxException, IOException { |
| 233 | + Capabilities chromeCapabilities = new ImmutableCapabilities("browser", "chrome"); |
| 234 | + Capabilities firefoxCapabilities = new ImmutableCapabilities("browser", "firefox"); |
| 235 | + URI firstNodeUri = new URI("http://example1.com"); |
| 236 | + URI secondNodeUri = new URI("http://example2.com"); |
| 237 | + |
| 238 | + AtomicReference<Availability> isUp = new AtomicReference<>(UP); |
| 239 | + |
| 240 | + Node firstNode = LocalNode.builder(tracer, bus, firstNodeUri, firstNodeUri, registrationSecret) |
| 241 | + .add(chromeCapabilities, new TestSessionFactory((id, caps) -> new Session(id, firstNodeUri, new ImmutableCapabilities(), caps, Instant.now()))) |
| 242 | + .advanced() |
| 243 | + .healthCheck(() -> new HealthCheck.Result(isUp.get(), "TL;DR")) |
| 244 | + .build(); |
| 245 | + |
| 246 | + Node secondNode = LocalNode.builder(tracer, bus, secondNodeUri, secondNodeUri, registrationSecret) |
| 247 | + .add(firefoxCapabilities, new TestSessionFactory((id, caps) -> new Session(id, secondNodeUri, new ImmutableCapabilities(), caps, Instant.now()))) |
| 248 | + .advanced() |
| 249 | + .healthCheck(() -> new HealthCheck.Result(isUp.get(), "TL;DR")) |
| 250 | + .build(); |
| 251 | + |
| 252 | + distributor.add(firstNode); |
| 253 | + distributor.add(secondNode); |
| 254 | + |
| 255 | + waitUntilReady(router, Duration.ofSeconds(5)); |
| 256 | + |
| 257 | + Map<String, Object> status = getStatus(router); |
| 258 | + List<Map<String,Object>> nodes = (List<Map<String, Object>>) status.get("nodes"); |
| 259 | + |
| 260 | + assertEquals(2, nodes.size()); |
218 | 261 |
|
| 262 | + String firstNodeId = (String) nodes.get(0).get("id"); |
| 263 | + String secondNodeId = (String) nodes.get(1).get("id"); |
| 264 | + |
| 265 | + assertFalse(firstNodeId.equals(secondNodeId)); |
219 | 266 | } |
220 | 267 |
|
221 | 268 | @Test |
222 | | - public void ifNodesHaveSpareSlotsButAlreadyHaveMaxSessionsGridIsNotReady() { |
| 269 | + public void ifNodesHaveSpareSlotsButAlreadyHaveMaxSessionsGridIsReady() |
| 270 | + throws URISyntaxException, IOException { |
| 271 | + Capabilities chromeCapabilities = new ImmutableCapabilities("browser", "chrome"); |
| 272 | + Capabilities firefoxCapabilities = new ImmutableCapabilities("browser", "firefox"); |
| 273 | + URI uri = new URI("http://example.com"); |
| 274 | + |
| 275 | + AtomicReference<Availability> isUp = new AtomicReference<>(UP); |
| 276 | + |
| 277 | + Node node = LocalNode.builder(tracer, bus, uri, uri, registrationSecret) |
| 278 | + .add(chromeCapabilities, new TestSessionFactory( |
| 279 | + (id, caps) -> new Session(id, uri, new ImmutableCapabilities(), caps, Instant.now()))) |
| 280 | + .add(firefoxCapabilities, new TestSessionFactory( |
| 281 | + (id, caps) -> new Session(id, uri, new ImmutableCapabilities(), caps, Instant.now()))) |
| 282 | + .maximumConcurrentSessions(1) |
| 283 | + .advanced() |
| 284 | + .healthCheck(() -> new HealthCheck.Result(isUp.get(), "TL;DR")) |
| 285 | + .build(); |
| 286 | + distributor.add(node); |
223 | 287 |
|
| 288 | + waitUntilReady(router, Duration.ofSeconds(5)); |
| 289 | + |
| 290 | + Map<String, Object> status = getStatus(router); |
| 291 | + assertTrue(status.toString(), (Boolean) status.get("ready")); |
| 292 | + |
| 293 | + SessionRequest sessionRequest = new SessionRequest( |
| 294 | + new RequestId(UUID.randomUUID()), |
| 295 | + Instant.now(), |
| 296 | + ImmutableSet.of(OSS, W3C), |
| 297 | + ImmutableSet.of(chromeCapabilities), |
| 298 | + ImmutableMap.of(), |
| 299 | + ImmutableMap.of()); |
| 300 | + |
| 301 | + Either<SessionNotCreatedException, CreateSessionResponse> response = |
| 302 | + distributor.newSession(sessionRequest); |
| 303 | + |
| 304 | + if (response.isRight()) { |
| 305 | + Session session = response.right().getSession(); |
| 306 | + assertThat(session).isNotNull(); |
| 307 | + assertTrue(status.toString(), (Boolean) status.get("ready")); |
| 308 | + } else { |
| 309 | + fail("Session creation failed", response.left()); |
| 310 | + } |
224 | 311 | } |
225 | 312 |
|
226 | 313 | private Map<String, Object> getStatus(Router router) throws IOException { |
|
0 commit comments