3333import static com .google .api .gax .rpc .testing .FakeBatchableApi .callLabeledIntSquarer ;
3434import static com .google .common .truth .Truth .assertThat ;
3535import static com .google .common .truth .Truth .assertWithMessage ;
36+ import static org .awaitility .Awaitility .await ;
3637import static org .junit .jupiter .api .Assertions .assertThrows ;
3738import static org .mockito .ArgumentMatchers .any ;
3839import static org .mockito .ArgumentMatchers .eq ;
@@ -599,15 +600,14 @@ void testPushCurrentBatchRunnable() throws Exception {
599600 // Batcher present inside runnable should be GCed after following loop.
600601 batcher .close ();
601602 batcher = null ;
602- for (int retry = 0 ; retry < 3 ; retry ++) {
603- System .gc ();
604- System .runFinalization ();
605- isExecutorCancelled = pushBatchRunnable .isCancelled ();
606- if (isExecutorCancelled ) {
607- break ;
608- }
609- Thread .sleep (DELAY_TIME * (1L << retry ));
610- }
603+ await ()
604+ .atMost (Duration .ofSeconds (5 ))
605+ .until (
606+ () -> {
607+ System .gc ();
608+ System .runFinalization ();
609+ return pushBatchRunnable .isCancelled ();
610+ });
611611 // ScheduledFuture should be isCancelled now.
612612 assertThat (pushBatchRunnable .isCancelled ()).isTrue ();
613613 }
@@ -735,15 +735,14 @@ public void splitResponse(
735735 void testUnclosedBatchersAreLogged () throws Exception {
736736 final long DELAY_TIME = 30L ;
737737 int actualRemaining = 0 ;
738- for (int retry = 0 ; retry < 3 ; retry ++) {
739- System .gc ();
740- System .runFinalization ();
741- actualRemaining = BatcherReference .cleanQueue ();
742- if (actualRemaining == 0 ) {
743- break ;
744- }
745- Thread .sleep (DELAY_TIME * (1L << retry ));
746- }
738+ await ()
739+ .atMost (Duration .ofSeconds (5 ))
740+ .until (
741+ () -> {
742+ System .gc ();
743+ System .runFinalization ();
744+ return BatcherReference .cleanQueue () == 0 ;
745+ });
747746 assertThat (actualRemaining ).isAtMost (0 );
748747 underTest = createDefaultBatcherImpl (batchingSettings , null );
749748 Batcher <Integer , Integer > extraBatcher = createDefaultBatcherImpl (batchingSettings , null );
@@ -771,20 +770,16 @@ public boolean isLoggable(LogRecord record) {
771770
772771 underTest = null ;
773772 // That *should* have been the last reference. Try to reclaim it.
774- boolean success = false ;
775- for (int retry = 0 ; retry < 3 ; retry ++) {
776- System .gc ();
777- System .runFinalization ();
778- int orphans = BatcherReference .cleanQueue ();
779- if (orphans == 1 ) {
780- success = true ;
781- break ;
782- }
783- // Validates that there are no other batcher instance present while GC cleanup.
784- assertWithMessage ("unexpected extra orphans" ).that (orphans ).isEqualTo (0 );
785- Thread .sleep (DELAY_TIME * (1L << retry ));
786- }
787- assertWithMessage ("Batcher was not garbage collected" ).that (success ).isTrue ();
773+ await ()
774+ .atMost (Duration .ofSeconds (5 ))
775+ .until (
776+ () -> {
777+ System .gc ();
778+ System .runFinalization ();
779+ int orphans = BatcherReference .cleanQueue ();
780+ assertWithMessage ("unexpected extra orphans" ).that (orphans ).isAtMost (1 );
781+ return orphans == 1 ;
782+ });
788783
789784 LogRecord lr ;
790785 synchronized (records ) {
@@ -809,15 +804,14 @@ void testClosedBatchersAreNotLogged() throws Exception {
809804 // Clean out the existing instances
810805 final long DELAY_TIME = 30L ;
811806 int actualRemaining = 0 ;
812- for (int retry = 0 ; retry < 3 ; retry ++) {
813- System .gc ();
814- System .runFinalization ();
815- actualRemaining = BatcherReference .cleanQueue ();
816- if (actualRemaining == 0 ) {
817- break ;
818- }
819- Thread .sleep (DELAY_TIME * (1L << retry ));
820- }
807+ await ()
808+ .atMost (Duration .ofSeconds (5 ))
809+ .until (
810+ () -> {
811+ System .gc ();
812+ System .runFinalization ();
813+ return BatcherReference .cleanQueue () == 0 ;
814+ });
821815 assertThat (actualRemaining ).isAtMost (0 );
822816
823817 // Capture logs
@@ -849,12 +843,17 @@ public boolean isLoggable(LogRecord record) {
849843 }
850844 }
851845 // Run GC a few times to give the batchers a chance to be collected
852- for (int retry = 0 ; retry < 100 ; retry ++) {
853- System .gc ();
854- System .runFinalization ();
855- BatcherReference .cleanQueue ();
856- Thread .sleep (10 );
857- }
846+ final AtomicInteger runs = new AtomicInteger (0 );
847+ await ()
848+ .pollInterval (Duration .ofMillis (10 ))
849+ .atMost (Duration .ofSeconds (2 ))
850+ .until (
851+ () -> {
852+ System .gc ();
853+ System .runFinalization ();
854+ BatcherReference .cleanQueue ();
855+ return runs .incrementAndGet () >= 20 ;
856+ });
858857
859858 synchronized (records ) {
860859 assertThat (records ).isEmpty ();
@@ -990,10 +989,12 @@ void testThrottlingBlocking() throws Exception {
990989 // resulting in a shorter total_throttled_time at the verification of throttledTime
991990 // at the end of the test.
992991 // https://github.com/googleapis/sdk-platform-java/issues/1193
993- do {
994- Thread .sleep (10 );
995- } while (batcherAddThreadHolder .isEmpty ()
996- || batcherAddThreadHolder .get (0 ).getState () != Thread .State .WAITING );
992+ await ()
993+ .atMost (Duration .ofSeconds (5 ))
994+ .until (
995+ () ->
996+ !batcherAddThreadHolder .isEmpty ()
997+ && batcherAddThreadHolder .get (0 ).getState () == Thread .State .WAITING );
997998
998999 long beforeGetCall = System .currentTimeMillis ();
9991000 executor .submit (
0 commit comments