Skip to content
This repository was archived by the owner on Sep 26, 2023. It is now read-only.

Commit e69393c

Browse files
authoredAug 22, 2022
fix(test): testThrottlingBlocking flakyness fix (#1775)
* fix(test): testThrottlingBlocking flakyness fix When BatcherImpl.sendOutstanding (method that calls callContext.withOption) is called from PushCurrentBatchRunnable, it uses the batcher's own executor, causing the mockito capture to fail due to be in a different thread from the one that originated add(). Other times, sendOutstanding will be called from the parent thread (successful test, in this case from our newFixedThreadPool). ALthough this is expected behavior, the argument captors do not work when used in different threads. Mockito.verify() is the recommended way of dealing with argument captors and happened to be the fix Internally, verify() relies on thread safe(r) notifiers for when the captors have been interacted with. When verifying flakiness, 100 runs in two threads were used with bazel test //gax:com.google.api.gax.batching.BatcherImplTest --runs_per_test=100 --test_filter Throttling --jobs 2 Mockito.verify() implementation: https://github.com/mockito/mockito/blob/2ded10ec704f2c93648d976031c2520a5a9b84aa/src/main/java/org/mockito/internal/MockitoCore.java#L183 * fix(test): remove unnecessary timeout in verify
1 parent f0fd491 commit e69393c

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed
 

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -887,10 +887,15 @@ public void run() {
887887
}
888888

889889
try {
890-
future.get(100, TimeUnit.MILLISECONDS);
890+
future.get(3, TimeUnit.SECONDS);
891891
} catch (TimeoutException e) {
892892
assertWithMessage("adding elements to batcher should not be blocked").fail();
893893
}
894+
895+
// Mockito recommends using verify() as the ONLY way to interact with Argument
896+
// captors - otherwise it may incur in unexpected behaviour
897+
Mockito.verify(callContext).withOption(key.capture(), value.capture());
898+
894899
// Verify that throttled time is recorded in ApiCallContext
895900
assertThat(key.getValue()).isSameInstanceAs(Batcher.THROTTLED_TIME_KEY);
896901
assertThat(value.getValue()).isAtLeast(throttledTime);

0 commit comments

Comments
 (0)
This repository has been archived.