85
85
@ RunWith (JUnit4 .class )
86
86
public class BatcherImplTest {
87
87
88
+ private static final Logger logger = Logger .getLogger (BatcherImplTest .class .getName ());
89
+
88
90
private static final ScheduledExecutorService EXECUTOR =
89
91
Executors .newSingleThreadScheduledExecutor ();
90
92
@@ -871,6 +873,7 @@ public void testThrottlingBlocking() throws Exception {
871
873
public void run () {
872
874
batcherAddThreadHolder .add (Thread .currentThread ());
873
875
batcher .add (1 );
876
+ logger .info ("Called batcher.add(1)" );
874
877
}
875
878
});
876
879
@@ -885,20 +888,38 @@ public void run() {
885
888
} while (batcherAddThreadHolder .isEmpty ()
886
889
|| batcherAddThreadHolder .get (0 ).getState () != Thread .State .WAITING );
887
890
891
+ long beforeGetCall = System .currentTimeMillis ();
888
892
executor .submit (
889
893
() -> {
890
894
try {
891
895
Thread .sleep (throttledTime );
896
+ logger .info ("Calling flowController.release" );
892
897
flowController .release (1 , 1 );
898
+ logger .info ("Called flowController.release" );
893
899
} catch (InterruptedException e ) {
894
900
}
895
901
});
896
902
897
903
try {
904
+ logger .info ("Calling future.get(10 ms)" );
898
905
future .get (10 , TimeUnit .MILLISECONDS );
899
- assertWithMessage ("adding elements to batcher should be blocked by FlowControlled" ).fail ();
906
+ long afterGetCall = System .currentTimeMillis ();
907
+ long actualWaitTimeMs = afterGetCall - beforeGetCall ;
908
+
909
+ logger .info ("future.get(10 ms) unexpectedly returned. Wait time: " + actualWaitTimeMs );
910
+ // In a flaky test troubleshooting
911
+ // (https://github.com/googleapis/sdk-platform-java/issues/1931), we observed that
912
+ // "future.get" method did not throw TimeoutException in this multithreaded test.
913
+ // It's because the thread calling "future.get" was not being run (i.e. in the wait queue of
914
+ // CPUs) in a timely manner.
915
+ // To avoid the flakiness, as long as the "future.get" does not return before the specified
916
+ // timeout, this assertion is considered as good.
917
+ assertWithMessage ("adding elements to batcher should be blocked by FlowControlled" )
918
+ .that (actualWaitTimeMs )
919
+ .isAtLeast (10 );
900
920
} catch (TimeoutException e ) {
901
921
// expected
922
+ logger .info ("future.get(10 ms) timed out expectedly." );
902
923
}
903
924
904
925
try {
0 commit comments