diff --git a/sdk-platform-java/gapic-generator-java-pom-parent/pom.xml b/sdk-platform-java/gapic-generator-java-pom-parent/pom.xml
index c8a8ee8554d9..bcf344855474 100644
--- a/sdk-platform-java/gapic-generator-java-pom-parent/pom.xml
+++ b/sdk-platform-java/gapic-generator-java-pom-parent/pom.xml
@@ -38,6 +38,7 @@
1.7.0
5.11.4
4.11.0
+ 4.3.0
2.0.16
true
diff --git a/sdk-platform-java/gax-java/dependencies.properties b/sdk-platform-java/gax-java/dependencies.properties
index 261ff0a9f8af..e35d7127e29b 100644
--- a/sdk-platform-java/gax-java/dependencies.properties
+++ b/sdk-platform-java/gax-java/dependencies.properties
@@ -96,3 +96,5 @@ maven.io_opentelemetry_opentelemetry_sdk=io.opentelemetry:opentelemetry-sdk:1.57
maven.io_opentelemetry_opentelemetry_sdk_common=io.opentelemetry:opentelemetry-sdk-common:1.57.0
maven.io_opentelemetry_opentelemetry_sdk_metrics=io.opentelemetry:opentelemetry-sdk-metrics:1.57.0
maven.com_google_guava_guava_testlib=com.google.guava:guava-testlib:32.1.3-jre
+maven.org_awaitility_awaitility=org.awaitility:awaitility:4.3.0
+
diff --git a/sdk-platform-java/gax-java/gax/BUILD.bazel b/sdk-platform-java/gax-java/gax/BUILD.bazel
index 7faa2d2597bb..773e9b9beb45 100644
--- a/sdk-platform-java/gax-java/gax/BUILD.bazel
+++ b/sdk-platform-java/gax-java/gax/BUILD.bazel
@@ -49,6 +49,7 @@ _TEST_COMPILE_DEPS = [
"@io_opentelemetry_opentelemetry_sdk_metrics//jar",
"@io_opentelemetry_opentelemetry_sdk_common//jar",
"@com_google_guava_guava_testlib//jar",
+ "@org_awaitility_awaitility//jar",
]
java_library(
diff --git a/sdk-platform-java/gax-java/gax/pom.xml b/sdk-platform-java/gax-java/gax/pom.xml
index 76688e04ef86..3316c766da89 100644
--- a/sdk-platform-java/gax-java/gax/pom.xml
+++ b/sdk-platform-java/gax-java/gax/pom.xml
@@ -111,6 +111,11 @@
${junit.version}
test
+
+ org.awaitility
+ awaitility
+ test
+
io.grpc
grpc-api
diff --git a/sdk-platform-java/gax-java/gax/src/test/java/com/google/api/gax/batching/ThresholdBatcherTest.java b/sdk-platform-java/gax-java/gax/src/test/java/com/google/api/gax/batching/ThresholdBatcherTest.java
index f87b772d3b4e..29b1628338a0 100644
--- a/sdk-platform-java/gax-java/gax/src/test/java/com/google/api/gax/batching/ThresholdBatcherTest.java
+++ b/sdk-platform-java/gax-java/gax/src/test/java/com/google/api/gax/batching/ThresholdBatcherTest.java
@@ -30,6 +30,7 @@
package com.google.api.gax.batching;
import static com.google.common.truth.Truth.assertThat;
+import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
@@ -44,6 +45,7 @@
import com.google.api.gax.batching.FlowController.FlowControlException;
import com.google.api.gax.batching.FlowController.LimitExceededBehavior;
import com.google.common.collect.ImmutableList;
+import java.time.Duration;
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.Arrays;
@@ -194,14 +196,16 @@ void testBatching() throws Exception {
batcher.add(SimpleBatch.fromInteger(3));
batcher.add(SimpleBatch.fromInteger(5));
// Give time for the executor to push the batch
- Thread.sleep(100);
- assertThat(receiver.getBatches()).hasSize(1);
+ await()
+ .atMost(Duration.ofSeconds(5))
+ .untilAsserted(() -> assertThat(receiver.getBatches()).hasSize(1));
batcher.add(SimpleBatch.fromInteger(7));
batcher.add(SimpleBatch.fromInteger(9));
// Give time for the executor to push the batch
- Thread.sleep(100);
- assertThat(receiver.getBatches()).hasSize(2);
+ await()
+ .atMost(Duration.ofSeconds(5))
+ .untilAsserted(() -> assertThat(receiver.getBatches()).hasSize(2));
batcher.add(SimpleBatch.fromInteger(11));
@@ -228,8 +232,9 @@ void testBatchingWithDelay() throws Exception {
batcher.add(SimpleBatch.fromInteger(3));
batcher.add(SimpleBatch.fromInteger(5));
// Give time for the delay to trigger and push the batch
- Thread.sleep(500);
- assertThat(receiver.getBatches()).hasSize(1);
+ await()
+ .atMost(Duration.ofSeconds(5))
+ .untilAsserted(() -> assertThat(receiver.getBatches()).hasSize(1));
batcher.add(SimpleBatch.fromInteger(11));
diff --git a/sdk-platform-java/gax-java/pom.xml b/sdk-platform-java/gax-java/pom.xml
index e46f1018a813..fc7cf36e5aed 100644
--- a/sdk-platform-java/gax-java/pom.xml
+++ b/sdk-platform-java/gax-java/pom.xml
@@ -175,6 +175,12 @@
pom
import
+
+ org.awaitility
+ awaitility
+ ${awaitility.version}
+ test
+