Skip to content

Commit 83e0e8c

Browse files
authored
fix: fix flaky test (#798)
1 parent be81ef8 commit 83e0e8c

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/DynamicFlowControlCallableTest.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.util.Map;
4040
import java.util.concurrent.Future;
4141
import java.util.concurrent.TimeUnit;
42+
import java.util.concurrent.atomic.AtomicBoolean;
4243
import org.junit.Before;
4344
import org.junit.Rule;
4445
import org.junit.Test;
@@ -268,19 +269,30 @@ public ApiFuture<List<MutateRowsResponse>> futureCall(
268269

269270
private void createFlowControlEvent(final FlowController flowController) throws Exception {
270271
flowController.reserve(INITIAL_ELEMENT, 0);
272+
final AtomicBoolean threadStarted = new AtomicBoolean(false);
271273
Thread t =
272274
new Thread(
273275
new Runnable() {
274276
@Override
275277
public void run() {
276278
try {
279+
threadStarted.set(true);
277280
flowController.reserve(1, 0);
278281
} catch (Exception e) {
279282
}
280283
}
281284
});
282285
t.start();
283-
Thread.sleep(10);
286+
// Wait 5 seconds for the thread to start, and 50 milliseconds after it's started to make sure
287+
// flowController.reserve(1, 0) is blocked and creates a throttling event. It should never take
288+
// so long.
289+
for (int i = 0; i < 1000; i++) {
290+
if (threadStarted.get()) {
291+
break;
292+
}
293+
Thread.sleep(5);
294+
}
295+
Thread.sleep(50);
284296
flowController.release(INITIAL_ELEMENT, 0);
285297
t.join();
286298
flowController.release(1, 0);

0 commit comments

Comments
 (0)