Skip to content

Commit 0a24aef

Browse files
authored
[grid] Fix flaky SessionCleanup tests
1 parent 58a55ce commit 0a24aef

File tree

2 files changed

+40
-14
lines changed

2 files changed

+40
-14
lines changed

java/src/org/openqa/selenium/grid/distributor/GridModel.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ public void purgeDeadNodes() {
199199
NodeId id = node.getNodeId();
200200
if (nodeHealthCount.getOrDefault(id, 0) > UNHEALTHY_THRESHOLD) {
201201
toRemove.add(node);
202+
break;
202203
}
203204

204205
Instant now = Instant.now();

java/test/org/openqa/selenium/grid/router/SessionCleanUpTest.java

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import static java.net.HttpURLConnection.HTTP_OK;
2121
import static org.assertj.core.api.Assertions.assertThat;
22+
import static org.assertj.core.api.Assertions.fail;
2223
import static org.openqa.selenium.grid.data.Availability.DOWN;
2324
import static org.openqa.selenium.grid.data.Availability.UP;
2425
import static org.openqa.selenium.remote.Dialect.W3C;
@@ -42,7 +43,6 @@
4243
import org.openqa.selenium.grid.data.Availability;
4344
import org.openqa.selenium.grid.data.CreateSessionResponse;
4445
import org.openqa.selenium.grid.data.DefaultSlotMatcher;
45-
import org.openqa.selenium.grid.data.NodeRemovedEvent;
4646
import org.openqa.selenium.grid.data.NodeStatus;
4747
import org.openqa.selenium.grid.data.RequestId;
4848
import org.openqa.selenium.grid.data.Session;
@@ -85,7 +85,6 @@
8585
import java.util.Optional;
8686
import java.util.Set;
8787
import java.util.UUID;
88-
import java.util.concurrent.CountDownLatch;
8988
import java.util.concurrent.atomic.AtomicReference;
9089

9190
public class SessionCleanUpTest {
@@ -115,10 +114,15 @@ public void stopServer() {
115114
if (server != null) {
116115
server.stop();
117116
}
117+
118+
if (events != null) {
119+
events.close();
120+
}
118121
}
119122

120-
@Test(expected = NoSuchSessionException.class)
121-
public void shouldRemoveSessionAfterNodeIsShutDownGracefully() throws URISyntaxException {
123+
@Test
124+
public void shouldRemoveSessionAfterNodeIsShutDownGracefully()
125+
throws URISyntaxException {
122126
Capabilities capabilities = new ImmutableCapabilities("browserName", "cheese");
123127
CombinedHandler handler = new CombinedHandler();
124128
CombinedHandler nodeHandler = new CombinedHandler();
@@ -142,7 +146,7 @@ public void shouldRemoveSessionAfterNodeIsShutDownGracefully() throws URISyntaxE
142146
queue,
143147
new DefaultSlotSelector(),
144148
registrationSecret,
145-
Duration.ofSeconds(2),
149+
Duration.ofSeconds(1),
146150
false);
147151
handler.addHandler(distributor);
148152

@@ -205,46 +209,49 @@ public void shouldRemoveSessionAfterNodeIsShutDownGracefully() throws URISyntaxE
205209

206210
assertThat(session.getCapabilities()).isEqualTo(capabilities);
207211

208-
distributor.refresh();
209212
nodeServer.stop();
210213

211214
waitTillNodesAreRemoved(distributor);
212215

213-
sessions.get(id);
216+
try {
217+
waitTillSessionIsRemoved(sessions, id);
218+
} catch (Exception e) {
219+
fail("Session not removed");
220+
}
214221
}
215222

216-
@Test(expected = NoSuchSessionException.class)
223+
@Test
217224
public void shouldRemoveSessionAfterNodeIsDown() throws URISyntaxException {
218-
EventBus bus = new GuavaEventBus();
219225
CombinedHandler handler = new CombinedHandler();
220226
Capabilities capabilities = new ImmutableCapabilities("browserName", "cheese");
221227

222228
AtomicReference<Availability> availability = new AtomicReference<>(UP);
223229

224-
SessionMap sessions = new LocalSessionMap(tracer, bus);
230+
SessionMap sessions = new LocalSessionMap(tracer, events);
225231
handler.addHandler(sessions);
226232
NewSessionQueue queue = new LocalNewSessionQueue(
227233
tracer,
228-
bus,
234+
events,
229235
new DefaultSlotMatcher(),
230236
Duration.ofSeconds(2),
231237
Duration.ofSeconds(2),
232238
registrationSecret);
233239

234240
URI uri = new URI("http://localhost:" + PortProber.findFreePort());
235-
Node node = LocalNode.builder(tracer, bus, uri, uri, registrationSecret)
241+
Node node = LocalNode.builder(tracer, events, uri, uri, registrationSecret)
236242
.add(
237243
capabilities,
238244
new TestSessionFactory(
239245
(id, caps) -> new Session(id, uri, capabilities, caps, Instant.now())))
246+
.heartbeatPeriod(Duration.ofSeconds(500000))
240247
.advanced()
241248
.healthCheck(() -> new HealthCheck.Result(availability.get(), "TL;DR"))
242249
.build();
243250
handler.addHandler(node);
244251

245252
LocalDistributor distributor = new LocalDistributor(
246253
tracer,
247-
bus,
254+
events,
248255
new PassthroughHttpClient.Factory(handler),
249256
sessions,
250257
queue,
@@ -276,7 +283,11 @@ public void shouldRemoveSessionAfterNodeIsDown() throws URISyntaxException {
276283

277284
waitTillNodesAreRemoved(distributor);
278285

279-
sessions.get(id);
286+
try {
287+
waitTillSessionIsRemoved(sessions, id);
288+
} catch (Exception e) {
289+
fail("Session not removed");
290+
}
280291
}
281292

282293
private void waitToHaveCapacity(Distributor distributor) {
@@ -295,4 +306,18 @@ private void waitTillNodesAreRemoved(Distributor distributor) {
295306
return nodes.isEmpty();
296307
});
297308
}
309+
310+
private void waitTillSessionIsRemoved(SessionMap sessionMap, SessionId id) {
311+
new FluentWait<>(sessionMap)
312+
.withTimeout(Duration.ofSeconds(15))
313+
.pollingEvery(Duration.ofMillis(100))
314+
.until(s -> {
315+
try {
316+
s.get(id);
317+
} catch (NoSuchSessionException e) {
318+
return true;
319+
}
320+
return false;
321+
});
322+
}
298323
}

0 commit comments

Comments
 (0)