@@ -97,7 +97,7 @@ public void testMaybeActivate() throws IOException, InterruptedException {
97
97
assertEquals ("node removed from jenkins" , j .jenkins .getNodes ().size (), 0 );
98
98
99
99
// watch was created
100
- assertTrue ( r . isWatchingCloud ( cloud . name ) );
100
+ assertShouldBeWatching ( r , cloud );
101
101
102
102
kubeClientRequests ()
103
103
// expect pod not running to be removed
@@ -129,7 +129,7 @@ public void testWatchFailOnActivate() throws IOException, InterruptedException {
129
129
kubeClientRequests ()
130
130
.assertRequestCountAtLeast ("/api/v1/namespaces/foo/pods?allowWatchBookmarks=true&watch=true" , 1 );
131
131
// watch failed to register
132
- assertFalse ( r . isWatchingCloud ( cloud . name ) );
132
+ assertShouldNotBeWatching ( r , cloud );
133
133
}
134
134
135
135
@ Test
@@ -151,15 +151,13 @@ public void testActivateOnNewComputer() throws IOException, InterruptedException
151
151
KubernetesComputer kc = new KubernetesComputer (n2 );
152
152
153
153
// should not be watching the newly created cloud at this point
154
- assertFalse ( "should not be watching cloud" , r . isWatchingCloud ( cloud . name ) );
154
+ assertShouldNotBeWatching ( r , cloud );
155
155
156
156
// fire compute on-line event
157
157
r .preLaunch (kc , tl );
158
158
159
159
// expect new cloud registered
160
- while (!r .isWatchingCloud (cloud .name )) {
161
- Thread .sleep (100 );
162
- }
160
+ assertShouldBeWatching (r , cloud );
163
161
kubeClientRequests ()
164
162
.assertRequestCountAtLeast ("/api/v1/namespaces/foo/pods?allowWatchBookmarks=true&watch=true" , 1 );
165
163
}
@@ -202,9 +200,7 @@ public void testReconnectOnNewComputer() throws InterruptedException, IOExceptio
202
200
203
201
// wait until watch is removed
204
202
System .out .println ("Waiting for watch to be removed" );
205
- while (r .isWatchingCloud (cloud .name )) {
206
- Thread .sleep (250L );
207
- }
203
+ assertShouldNotBeWatching (r , cloud );
208
204
System .out .println ("Watch removed" );
209
205
210
206
// launch computer
@@ -215,9 +211,7 @@ public void testReconnectOnNewComputer() throws InterruptedException, IOExceptio
215
211
216
212
// should have started new watch
217
213
System .out .println ("Waiting for a new watch to be started" );
218
- while (!r .isWatchingCloud (cloud .name )) {
219
- Thread .sleep (100 );
220
- }
214
+ assertShouldBeWatching (r , cloud );
221
215
System .out .println ("Watch started" );
222
216
}
223
217
@@ -244,7 +238,7 @@ public void testAddWatchWhenCloudAdded() throws InterruptedException, IOExceptio
244
238
j .jenkins .clouds .add (cloud );
245
239
246
240
// watch is added
247
- assertTrue ( "should be watching cloud" , r . isWatchingCloud ( cloud . name ) );
241
+ assertShouldBeWatching ( r , cloud );
248
242
kubeClientRequests ().assertRequestCountAtLeast (watchPodsPath , 1 );
249
243
}
250
244
@@ -264,13 +258,15 @@ public void testRemoveWatchWhenCloudRemoved() throws InterruptedException, IOExc
264
258
r .maybeActivate ();
265
259
266
260
// should not be watching the newly created cloud at this point
267
- assertTrue ( "should be watching cloud" , r . isWatchingCloud ( cloud . name ) );
261
+ assertShouldBeWatching ( r , cloud );
268
262
269
263
// invalidate client
270
264
j .jenkins .clouds .remove (cloud );
271
265
272
266
// watch is removed
273
- assertFalse ("should not be watching cloud" , r .isWatchingCloud (cloud .name ));
267
+ // org.csanchez.jenkins.plugins.kubernetes.pod.retention.Reaper.CloudPodWatcher.onClose() is called in a
268
+ // separate thread
269
+ assertShouldNotBeWatching (r , cloud );
274
270
}
275
271
276
272
@ Test (timeout = 10_000 )
@@ -313,7 +309,7 @@ public void testReplaceWatchWhenCloudUpdated() throws InterruptedException, IOEx
313
309
r .maybeActivate ();
314
310
315
311
// should not be watching the newly created cloud at this point
316
- assertTrue ( "should be watching cloud" , r . isWatchingCloud ( cloud . name ) );
312
+ assertShouldBeWatching ( r , cloud );
317
313
318
314
// invalidate client
319
315
cloud .setNamespace ("bar" );
@@ -322,7 +318,7 @@ public void testReplaceWatchWhenCloudUpdated() throws InterruptedException, IOEx
322
318
KubernetesSlave node = addNode (cloud , "node-123" , "node" );
323
319
324
320
// watch is still active
325
- assertTrue ( "should be watching cloud" , r . isWatchingCloud ( cloud . name ) );
321
+ assertShouldBeWatching ( r , cloud );
326
322
327
323
listener .waitForEvents ().expectEvent (Watcher .Action .MODIFIED , node );
328
324
kubeClientRequests ().assertRequestCountAtLeast (watchBarPodsPath , 1 );
@@ -358,10 +354,7 @@ public void testStopWatchingOnCloseException() throws InterruptedException {
358
354
listener .expectNoEvents ();
359
355
360
356
// watch is removed
361
- while (r .isWatchingCloud (cloud .name )) {
362
- Thread .sleep (250L );
363
- }
364
- assertFalse (r .isWatchingCloud (cloud .name ));
357
+ assertShouldNotBeWatching (r , cloud );
365
358
}
366
359
367
360
@ Test (timeout = 10_000 )
@@ -402,7 +395,7 @@ public void testKeepWatchingOnKubernetesApiServerError() throws InterruptedExcep
402
395
listener .expectNoEvents ();
403
396
404
397
// watch is still active
405
- assertTrue ( r . isWatchingCloud ( cloud . name ) );
398
+ assertShouldBeWatching ( r , cloud );
406
399
}
407
400
408
401
@ Test (timeout = 10_000 )
@@ -416,7 +409,11 @@ public void testKeepWatchingOnStatusWatchEvent() throws InterruptedException {
416
409
status .setCode (200 );
417
410
server .expect ()
418
411
.withPath (watchPodsPath )
419
- .andReturnChunked (200 , new WatchEvent (status , "ERROR" ))
412
+ .andUpgradeToWebSocket ()
413
+ .open ()
414
+ .immediately ()
415
+ .andEmit (new WatchEvent (status , "ERROR" ))
416
+ .done ()
420
417
.once ();
421
418
422
419
// activate reaper
@@ -429,7 +426,7 @@ public void testKeepWatchingOnStatusWatchEvent() throws InterruptedException {
429
426
listener .expectNoEvents ();
430
427
431
428
// watch is still active
432
- assertTrue ( r . isWatchingCloud ( cloud . name ) );
429
+ assertShouldBeWatching ( r , cloud );
433
430
}
434
431
435
432
@ Test
@@ -453,17 +450,13 @@ public void testCloseWatchersOnShutdown() throws InterruptedException {
453
450
r .maybeActivate ();
454
451
455
452
// watching cloud
456
- assertTrue (r .isWatchingCloud (cloud .name ));
457
- assertTrue (r .isWatchingCloud (cloud2 .name ));
458
- assertTrue (r .isWatchingCloud (cloud3 .name ));
453
+ assertShouldBeWatching (r , cloud , cloud2 , cloud3 );
459
454
460
455
// trigger shutdown listener
461
456
new Reaper .ReaperShutdownListener ().onBeforeShutdown ();
462
457
463
458
// watchers removed
464
- await ().until (() -> r .isWatchingCloud (cloud .name ), is (false ));
465
- await ().until (() -> r .isWatchingCloud (cloud2 .name ), is (false ));
466
- await ().until (() -> r .isWatchingCloud (cloud3 .name ), is (false ));
459
+ assertShouldNotBeWatching (r , cloud , cloud2 , cloud3 );
467
460
}
468
461
469
462
@ Test (timeout = 10_000 )
@@ -895,4 +888,16 @@ private static WatchEvent errorEvent() {
895
888
.endStatusObject ()
896
889
.build ();
897
890
}
891
+
892
+ private static void assertShouldBeWatching (Reaper r , KubernetesCloud ... clouds ) {
893
+ for (KubernetesCloud cloud : clouds ) {
894
+ await ("should be watching cloud " + cloud .name ).until (() -> r .isWatchingCloud (cloud .name ));
895
+ }
896
+ }
897
+
898
+ private static void assertShouldNotBeWatching (Reaper r , KubernetesCloud ... clouds ) {
899
+ for (KubernetesCloud cloud : clouds ) {
900
+ await ("should not be watching cloud " + cloud .name ).until (() -> !r .isWatchingCloud (cloud .name ));
901
+ }
902
+ }
898
903
}
0 commit comments