19
19
20
20
import static java .net .HttpURLConnection .HTTP_OK ;
21
21
import static org .assertj .core .api .Assertions .assertThat ;
22
+ import static org .assertj .core .api .Assertions .fail ;
22
23
import static org .openqa .selenium .grid .data .Availability .DOWN ;
23
24
import static org .openqa .selenium .grid .data .Availability .UP ;
24
25
import static org .openqa .selenium .remote .Dialect .W3C ;
42
43
import org .openqa .selenium .grid .data .Availability ;
43
44
import org .openqa .selenium .grid .data .CreateSessionResponse ;
44
45
import org .openqa .selenium .grid .data .DefaultSlotMatcher ;
45
- import org .openqa .selenium .grid .data .NodeRemovedEvent ;
46
46
import org .openqa .selenium .grid .data .NodeStatus ;
47
47
import org .openqa .selenium .grid .data .RequestId ;
48
48
import org .openqa .selenium .grid .data .Session ;
85
85
import java .util .Optional ;
86
86
import java .util .Set ;
87
87
import java .util .UUID ;
88
- import java .util .concurrent .CountDownLatch ;
89
88
import java .util .concurrent .atomic .AtomicReference ;
90
89
91
90
public class SessionCleanUpTest {
@@ -115,10 +114,15 @@ public void stopServer() {
115
114
if (server != null ) {
116
115
server .stop ();
117
116
}
117
+
118
+ if (events != null ) {
119
+ events .close ();
120
+ }
118
121
}
119
122
120
- @ Test (expected = NoSuchSessionException .class )
121
- public void shouldRemoveSessionAfterNodeIsShutDownGracefully () throws URISyntaxException {
123
+ @ Test
124
+ public void shouldRemoveSessionAfterNodeIsShutDownGracefully ()
125
+ throws URISyntaxException {
122
126
Capabilities capabilities = new ImmutableCapabilities ("browserName" , "cheese" );
123
127
CombinedHandler handler = new CombinedHandler ();
124
128
CombinedHandler nodeHandler = new CombinedHandler ();
@@ -142,7 +146,7 @@ public void shouldRemoveSessionAfterNodeIsShutDownGracefully() throws URISyntaxE
142
146
queue ,
143
147
new DefaultSlotSelector (),
144
148
registrationSecret ,
145
- Duration .ofSeconds (2 ),
149
+ Duration .ofSeconds (1 ),
146
150
false );
147
151
handler .addHandler (distributor );
148
152
@@ -205,46 +209,49 @@ public void shouldRemoveSessionAfterNodeIsShutDownGracefully() throws URISyntaxE
205
209
206
210
assertThat (session .getCapabilities ()).isEqualTo (capabilities );
207
211
208
- distributor .refresh ();
209
212
nodeServer .stop ();
210
213
211
214
waitTillNodesAreRemoved (distributor );
212
215
213
- sessions .get (id );
216
+ try {
217
+ waitTillSessionIsRemoved (sessions , id );
218
+ } catch (Exception e ) {
219
+ fail ("Session not removed" );
220
+ }
214
221
}
215
222
216
- @ Test ( expected = NoSuchSessionException . class )
223
+ @ Test
217
224
public void shouldRemoveSessionAfterNodeIsDown () throws URISyntaxException {
218
- EventBus bus = new GuavaEventBus ();
219
225
CombinedHandler handler = new CombinedHandler ();
220
226
Capabilities capabilities = new ImmutableCapabilities ("browserName" , "cheese" );
221
227
222
228
AtomicReference <Availability > availability = new AtomicReference <>(UP );
223
229
224
- SessionMap sessions = new LocalSessionMap (tracer , bus );
230
+ SessionMap sessions = new LocalSessionMap (tracer , events );
225
231
handler .addHandler (sessions );
226
232
NewSessionQueue queue = new LocalNewSessionQueue (
227
233
tracer ,
228
- bus ,
234
+ events ,
229
235
new DefaultSlotMatcher (),
230
236
Duration .ofSeconds (2 ),
231
237
Duration .ofSeconds (2 ),
232
238
registrationSecret );
233
239
234
240
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 )
236
242
.add (
237
243
capabilities ,
238
244
new TestSessionFactory (
239
245
(id , caps ) -> new Session (id , uri , capabilities , caps , Instant .now ())))
246
+ .heartbeatPeriod (Duration .ofSeconds (500000 ))
240
247
.advanced ()
241
248
.healthCheck (() -> new HealthCheck .Result (availability .get (), "TL;DR" ))
242
249
.build ();
243
250
handler .addHandler (node );
244
251
245
252
LocalDistributor distributor = new LocalDistributor (
246
253
tracer ,
247
- bus ,
254
+ events ,
248
255
new PassthroughHttpClient .Factory (handler ),
249
256
sessions ,
250
257
queue ,
@@ -276,7 +283,11 @@ public void shouldRemoveSessionAfterNodeIsDown() throws URISyntaxException {
276
283
277
284
waitTillNodesAreRemoved (distributor );
278
285
279
- sessions .get (id );
286
+ try {
287
+ waitTillSessionIsRemoved (sessions , id );
288
+ } catch (Exception e ) {
289
+ fail ("Session not removed" );
290
+ }
280
291
}
281
292
282
293
private void waitToHaveCapacity (Distributor distributor ) {
@@ -295,4 +306,18 @@ private void waitTillNodesAreRemoved(Distributor distributor) {
295
306
return nodes .isEmpty ();
296
307
});
297
308
}
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
+ }
298
323
}
0 commit comments