4040import java .util .HashSet ;
4141import java .util .List ;
4242import java .util .UUID ;
43+ import java .util .concurrent .CountDownLatch ;
4344import java .util .concurrent .ExecutionException ;
4445import java .util .concurrent .Executors ;
4546import java .util .concurrent .Future ;
@@ -229,15 +230,27 @@ void testMultiStreamClosed_multiplexingEnabled() throws Exception {
229230 createConnectionWorkerPool (
230231 /* maxRequests= */ 3 , /* maxBytes= */ 1000 , java .time .Duration .ofSeconds (5 ));
231232
232- // Sets the sleep time to simulate requests stuck in connection.
233- testBigQueryWrite .setResponseSleep (Duration .ofMillis (50L ));
234233 StreamWriter writeStream1 = getTestStreamWriter (TEST_STREAM_1 );
235234 StreamWriter writeStream2 = getTestStreamWriter (TEST_STREAM_2 );
236235
237236 // Try append 20 requests, at the end we should have 2 requests per connection.
238237 long appendCount = 20 ;
238+ // Use a CountDownLatch to block mock server responses. This ensures all 20 requests
239+ // remain in-flight simultaneously, forcing the ConnectionWorkerPool to scale up to
240+ // 10 connections. Without this blocking, requests might finish early and prevent scaling.
241+ CountDownLatch latch = new CountDownLatch (1 );
239242 for (long i = 0 ; i < appendCount ; i ++) {
240- testBigQueryWrite .addResponse (createAppendResponse (i ));
243+ long offset = i ;
244+ testBigQueryWrite .addResponse (
245+ () -> {
246+ try {
247+ latch .await ();
248+ } catch (InterruptedException e ) {
249+ Thread .currentThread ().interrupt ();
250+ throw new RuntimeException (e );
251+ }
252+ return new FakeBigQueryWriteImpl .Response (createAppendResponse (offset ));
253+ });
241254 }
242255 List <ApiFuture <?>> futures = new ArrayList <>();
243256
@@ -253,12 +266,18 @@ void testMultiStreamClosed_multiplexingEnabled() throws Exception {
253266 writeStream , connectionWorkerPool , new String [] {String .valueOf (i )}, i ));
254267 }
255268
269+ // At the end we should scale up to 10 connections.
270+ try {
271+ assertThat (connectionWorkerPool .getCreateConnectionCount ()).isEqualTo (10 );
272+ assertThat (connectionWorkerPool .getTotalConnectionCount ()).isEqualTo (10 );
273+ } finally {
274+ // Release the latch to allow requests to complete.
275+ latch .countDown ();
276+ }
277+
256278 for (ApiFuture <?> future : futures ) {
257279 future .get ();
258280 }
259- // At the end we should scale up to 10 connections.
260- assertThat (connectionWorkerPool .getCreateConnectionCount ()).isEqualTo (10 );
261- assertThat (connectionWorkerPool .getTotalConnectionCount ()).isEqualTo (10 );
262281
263282 // Start testing calling close on each stream.
264283 // When we close the first stream, only the connection that only serve stream 1 will be closed.
0 commit comments