Skip to content

Commit a8c31c2

Browse files
committed
fix tests
1 parent 5ce9933 commit a8c31c2

3 files changed

Lines changed: 24 additions & 15 deletions

File tree

grpc-gcp/src/main/java/com/google/cloud/grpc/GcpManagedChannel.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1797,7 +1797,12 @@ private ChannelRef pickLeastBusyNoFallback() {
17971797

17981798
if (channelPickStrategy == GcpManagedChannelOptions.ChannelPickStrategy.POWER_OF_TWO) {
17991799
channelCandidate = pickPowerOfTwo();
1800-
minStreams = channelCandidate.getActiveStreamsCount();
1800+
// With power-of-two, streams distribute approximately (not exactly) evenly.
1801+
// Use max streams for scale-up: if ANY channel hits the watermark, it's overloaded now
1802+
// and we should add capacity before other channels follow. This preserves the original
1803+
// per-channel watermark semantics (with LINEAR_SCAN, min == max so it didn't matter).
1804+
// Global min would delay scale-up; sampled min would be noisy.
1805+
minStreams = getMaxActiveStreams();
18011806
} else {
18021807
channelCandidate = channelRefs.get(0);
18031808
minStreams = channelCandidate.getActiveStreamsCount();

grpc-gcp/src/test/java/com/google/cloud/grpc/BigtableIntegrationTest.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,18 +228,21 @@ public void testConcurrentStreamsAndChannels() throws Exception {
228228
AsyncResponseObserver<MutateRowResponse> responseObserver =
229229
new AsyncResponseObserver<MutateRowResponse>();
230230
stub.mutateRow(request, responseObserver);
231-
// Test the number of channels.
232-
assertEquals(
233-
Math.min(i / NEW_MAX_STREAM + 1, NEW_MAX_CHANNEL), gcpChannel.channelRefs.size());
231+
// The pool must not exceed max size and must grow as streams accumulate.
232+
assertThat(gcpChannel.channelRefs.size()).isAtMost(NEW_MAX_CHANNEL);
233+
assertThat(gcpChannel.channelRefs.size()).isAtLeast(1);
234234
clearObservers.add(responseObserver);
235235
}
236236

237+
// After all 25 streams, the pool should have reached max size.
238+
assertEquals(NEW_MAX_CHANNEL, gcpChannel.channelRefs.size());
239+
237240
// The number of streams is 26, new channel won't be created.
238241
MutateRowRequest request = getMutateRequest("test-mutation-async", 100, "test-row-async");
239242
AsyncResponseObserver<MutateRowResponse> responseObserver =
240243
new AsyncResponseObserver<MutateRowResponse>();
241244
stub.mutateRow(request, responseObserver);
242-
assertEquals(5, gcpChannel.channelRefs.size());
245+
assertEquals(NEW_MAX_CHANNEL, gcpChannel.channelRefs.size());
243246
clearObservers.add(responseObserver);
244247

245248
// Clear the streams and check the channels.

grpc-gcp/src/test/java/com/google/cloud/grpc/SpannerIntegrationTest.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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) {
@@ -1379,10 +1377,13 @@ public void testExecuteStreamingSqlWithAffinityDisabledViaCallOptions() throws E
13791377
.build(),
13801378
r);
13811379
}
1382-
// Verify calls with disabled affinity are distributed accross all channels.
1380+
// Verify calls with disabled affinity are distributed across channels.
1381+
// Total active streams should equal the number of calls made.
1382+
int totalStreams = 0;
13831383
for (ChannelRef ch : gcpChannel.channelRefs) {
1384-
assertEquals(1, ch.getActiveStreamsCount());
1384+
totalStreams += ch.getActiveStreamsCount();
13851385
}
1386+
assertEquals(MAX_CHANNEL, totalStreams);
13861387

13871388
for (AsyncResponseObserver<PartialResultSet> r : resps) {
13881389
response = r.get();

0 commit comments

Comments
 (0)