Skip to content

Commit c5d5386

Browse files
authored
test: BatcherImplTest.testThrottlingBlocking to check actual wait time (#2285)
* test: additional logs in BatcherImplTest * Replaced the assertion with actual wait time count
1 parent 79deccd commit c5d5386

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

gax-java/gax/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@
9393
</excludes>
9494
</configuration>
9595
</plugin>
96+
<plugin>
97+
<!-- Troubleshooting a flaky test in https://github.com/googleapis/sdk-platform-java/issues/1931 -->
98+
<groupId>org.apache.maven.plugins</groupId>
99+
<artifactId>maven-surefire-plugin</artifactId>
100+
<configuration>
101+
<argLine>-Djava.util.logging.SimpleFormatter.format="%1$tY %1$tl:%1$tM:%1$tS.%1$tL %2$s %4$s: %5$s%6$s%n"</argLine>
102+
</configuration>
103+
</plugin>
96104
</plugins>
97105
</build>
98106
</project>

gax-java/gax/src/test/java/com/google/api/gax/batching/BatcherImplTest.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@
8585
@RunWith(JUnit4.class)
8686
public class BatcherImplTest {
8787

88+
private static final Logger logger = Logger.getLogger(BatcherImplTest.class.getName());
89+
8890
private static final ScheduledExecutorService EXECUTOR =
8991
Executors.newSingleThreadScheduledExecutor();
9092

@@ -871,6 +873,7 @@ public void testThrottlingBlocking() throws Exception {
871873
public void run() {
872874
batcherAddThreadHolder.add(Thread.currentThread());
873875
batcher.add(1);
876+
logger.info("Called batcher.add(1)");
874877
}
875878
});
876879

@@ -885,20 +888,38 @@ public void run() {
885888
} while (batcherAddThreadHolder.isEmpty()
886889
|| batcherAddThreadHolder.get(0).getState() != Thread.State.WAITING);
887890

891+
long beforeGetCall = System.currentTimeMillis();
888892
executor.submit(
889893
() -> {
890894
try {
891895
Thread.sleep(throttledTime);
896+
logger.info("Calling flowController.release");
892897
flowController.release(1, 1);
898+
logger.info("Called flowController.release");
893899
} catch (InterruptedException e) {
894900
}
895901
});
896902

897903
try {
904+
logger.info("Calling future.get(10 ms)");
898905
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);
900920
} catch (TimeoutException e) {
901921
// expected
922+
logger.info("future.get(10 ms) timed out expectedly.");
902923
}
903924

904925
try {

0 commit comments

Comments
 (0)