2121import io .kubernetes .client .openapi .models .V1Pod ;
2222import io .kubernetes .client .openapi .models .V1PodList ;
2323import io .kubernetes .client .util .Watch ;
24+ import java .time .Duration ;
2425import java .util .Arrays ;
25- import java .util .concurrent .atomic .AtomicBoolean ;
2626import java .util .concurrent .atomic .AtomicInteger ;
27+ import org .awaitility .Awaitility ;
28+ import org .hamcrest .core .IsEqual ;
2729import org .junit .Test ;
2830
2931public class ControllerTest {
3032
3133 @ Test
32- public void testControllerProcessDeltas () throws InterruptedException {
34+ public void testControllerProcessDeltas () {
3335
3436 AtomicInteger receivingDeltasCount = new AtomicInteger (0 );
3537 V1Pod foo1 = new V1Pod ().metadata (new V1ObjectMeta ().name ("foo1" ).namespace ("default" ));
@@ -40,8 +42,6 @@ public void testControllerProcessDeltas() throws InterruptedException {
4042 new V1PodList ().metadata (new V1ListMeta ()).items (Arrays .asList (foo1 , foo2 , foo3 ));
4143 DeltaFIFO deltaFIFO = new DeltaFIFO (Caches ::deletionHandlingMetaNamespaceKeyFunc , new Cache ());
4244
43- AtomicBoolean runOnce = new AtomicBoolean (false );
44-
4545 ListerWatcher <V1Pod , V1PodList > listerWatcher =
4646 new MockRunOnceListerWatcher <V1Pod , V1PodList >(
4747 podList , new Watch .Response <V1Pod >(EventType .MODIFIED .name (), foo3 ));
@@ -58,12 +58,12 @@ public void testControllerProcessDeltas() throws InterruptedException {
5858 controllerThread .setDaemon (true );
5959 controllerThread .start ();
6060
61- // sleep 1s for processing all the deltas
62- Thread .sleep (1000 );
63-
6461 try {
62+ Awaitility .await ()
63+ .pollInterval (Duration .ofSeconds (1 ))
64+ .timeout (Duration .ofSeconds (5 ))
65+ .untilAtomic (receivingDeltasCount , IsEqual .equalTo (4 ));
6566 assertEquals (4 , receivingDeltasCount .get ());
66-
6767 } catch (Throwable t ) {
6868 throw new RuntimeException (t );
6969 } finally {
0 commit comments