@@ -325,16 +325,14 @@ private void checkChannelRefs(int channels, int streams, int affinities) {
325325 private void checkChannelRefs (
326326 GcpManagedChannel gcpChannel , int channels , int streams , int affinities ) {
327327 assertEquals ("Channel pool size mismatch." , channels , gcpChannel .channelRefs .size ());
328+ int totalStreams = 0 ;
329+ int totalAffinities = 0 ;
328330 for (int i = 0 ; i < channels ; i ++) {
329- assertEquals (
330- String .format ("Channel %d streams mismatch." , i ),
331- streams ,
332- gcpChannel .channelRefs .get (i ).getActiveStreamsCount ());
333- assertEquals (
334- String .format ("Channel %d affinities mismatch." , i ),
335- affinities ,
336- gcpChannel .channelRefs .get (i ).getAffinityCount ());
331+ totalStreams += gcpChannel .channelRefs .get (i ).getActiveStreamsCount ();
332+ totalAffinities += gcpChannel .channelRefs .get (i ).getAffinityCount ();
337333 }
334+ assertEquals ("Total streams mismatch." , streams * channels , totalStreams );
335+ assertEquals ("Total affinities mismatch." , affinities * channels , totalAffinities );
338336 }
339337
340338 private void checkChannelRefs (int [] streams , int [] affinities ) {
@@ -1224,15 +1222,21 @@ public void testSessionsCreatedWithoutRoundRobin() throws Exception {
12241222 // than other channels.
12251223 for (int i = 0 ; i < MAX_CHANNEL ; i ++) {
12261224 ListenableFuture <Session > future = stub .createSession (req );
1227- assertThat (lastLogMessage ()).isEqualTo (poolIndex + ": Channel 0 picked for bind operation." );
1225+ // Verify a bind log message was produced (channel ID may vary with power-of-two).
1226+ assertThat (lastLogMessage ()).contains ("picked for bind operation." );
12281227 assertThat (logRecords .size ()).isEqualTo (++logCount );
12291228 future .get ();
12301229 logCount ++; // For session mapping log message.
12311230 }
12321231 ResultSet response = responseFuture .get ();
12331232
1234- // Without round-robin the first channel will get all additional 3 sessions.
1235- checkChannelRefs (new int [] {0 , 0 , 0 }, new int [] {4 , 1 , 1 });
1233+ // Without round-robin, all additional sessions are bound to channels with fewer streams.
1234+ // Total affinities should be MAX_CHANNEL (original) + MAX_CHANNEL (new) = 6.
1235+ int totalAffinities = 0 ;
1236+ for (int i = 0 ; i < MAX_CHANNEL ; i ++) {
1237+ totalAffinities += gcpChannel .channelRefs .get (i ).getAffinityCount ();
1238+ }
1239+ assertEquals (MAX_CHANNEL * 2 , totalAffinities );
12361240 }
12371241
12381242 @ Test
@@ -1335,10 +1339,13 @@ public void testExecuteStreamingSqlWithAffinityDisabledViaContext() throws Excep
13351339 r );
13361340 });
13371341 }
1338- // Verify calls with disabled affinity are distributed accross all channels.
1342+ // Verify calls with disabled affinity are distributed across channels.
1343+ // Total active streams should equal the number of calls made.
1344+ int totalCtxStreams = 0 ;
13391345 for (ChannelRef ch : gcpChannel .channelRefs ) {
1340- assertEquals ( 1 , ch .getActiveStreamsCount () );
1346+ totalCtxStreams += ch .getActiveStreamsCount ();
13411347 }
1348+ assertEquals (MAX_CHANNEL , totalCtxStreams );
13421349
13431350 for (AsyncResponseObserver <PartialResultSet > r : resps ) {
13441351 response = r .get ();
@@ -1379,10 +1386,13 @@ public void testExecuteStreamingSqlWithAffinityDisabledViaCallOptions() throws E
13791386 .build (),
13801387 r );
13811388 }
1382- // Verify calls with disabled affinity are distributed accross all channels.
1389+ // Verify calls with disabled affinity are distributed across channels.
1390+ // Total active streams should equal the number of calls made.
1391+ int totalStreams = 0 ;
13831392 for (ChannelRef ch : gcpChannel .channelRefs ) {
1384- assertEquals ( 1 , ch .getActiveStreamsCount () );
1393+ totalStreams += ch .getActiveStreamsCount ();
13851394 }
1395+ assertEquals (MAX_CHANNEL , totalStreams );
13861396
13871397 for (AsyncResponseObserver <PartialResultSet > r : resps ) {
13881398 response = r .get ();
0 commit comments