21
21
import io .kubernetes .client .openapi .models .V1Pod ;
22
22
import io .kubernetes .client .openapi .models .V1PodList ;
23
23
import io .kubernetes .client .util .Watch ;
24
+ import java .time .Duration ;
24
25
import java .util .Arrays ;
25
- import java .util .concurrent .atomic .AtomicBoolean ;
26
26
import java .util .concurrent .atomic .AtomicInteger ;
27
+ import org .awaitility .Awaitility ;
28
+ import org .hamcrest .core .IsEqual ;
27
29
import org .junit .Test ;
28
30
29
31
public class ControllerTest {
30
32
31
33
@ Test
32
- public void testControllerProcessDeltas () throws InterruptedException {
34
+ public void testControllerProcessDeltas () {
33
35
34
36
AtomicInteger receivingDeltasCount = new AtomicInteger (0 );
35
37
V1Pod foo1 = new V1Pod ().metadata (new V1ObjectMeta ().name ("foo1" ).namespace ("default" ));
@@ -40,8 +42,6 @@ public void testControllerProcessDeltas() throws InterruptedException {
40
42
new V1PodList ().metadata (new V1ListMeta ()).items (Arrays .asList (foo1 , foo2 , foo3 ));
41
43
DeltaFIFO deltaFIFO = new DeltaFIFO (Caches ::deletionHandlingMetaNamespaceKeyFunc , new Cache ());
42
44
43
- AtomicBoolean runOnce = new AtomicBoolean (false );
44
-
45
45
ListerWatcher <V1Pod , V1PodList > listerWatcher =
46
46
new MockRunOnceListerWatcher <V1Pod , V1PodList >(
47
47
podList , new Watch .Response <V1Pod >(EventType .MODIFIED .name (), foo3 ));
@@ -58,12 +58,12 @@ public void testControllerProcessDeltas() throws InterruptedException {
58
58
controllerThread .setDaemon (true );
59
59
controllerThread .start ();
60
60
61
- // sleep 1s for processing all the deltas
62
- Thread .sleep (1000 );
63
-
64
61
try {
62
+ Awaitility .await ()
63
+ .pollInterval (Duration .ofSeconds (1 ))
64
+ .timeout (Duration .ofSeconds (5 ))
65
+ .untilAtomic (receivingDeltasCount , IsEqual .equalTo (4 ));
65
66
assertEquals (4 , receivingDeltasCount .get ());
66
-
67
67
} catch (Throwable t ) {
68
68
throw new RuntimeException (t );
69
69
} finally {
0 commit comments