Skip to content

Commit 1f07a11

Browse files
committed
fix tests
1 parent a6e28bf commit 1f07a11

1 file changed

Lines changed: 30 additions & 4 deletions

File tree

java-spanner/google-cloud-spanner/src/test/java/com/google/cloud/spanner/RetryOnDifferentGrpcChannelMockServerTest.java

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import io.grpc.ClientInterceptor;
3939
import io.grpc.Context;
4040
import io.grpc.Deadline;
41-
import io.grpc.ManagedChannelBuilder;
4241
import io.grpc.MethodDescriptor;
4342
import io.grpc.Status;
4443
import java.time.Duration;
@@ -64,6 +63,9 @@ public class RetryOnDifferentGrpcChannelMockServerTest extends AbstractMockServe
6463
/** Tracks the logical affinity keys before grpc-gcp routes the request. */
6564
private static final Map<String, Set<String>> LOGICAL_AFFINITY_KEYS = new HashMap<>();
6665

66+
/** Tracks the actual grpc-gcp channel IDs after grpc-gcp routes the request. */
67+
private static final Map<String, Set<Integer>> ACTUAL_CHANNEL_IDS = new HashMap<>();
68+
6769
@BeforeClass
6870
public static void setupAndStartServer() throws Exception {
6971
System.setProperty("spanner.retry_deadline_exceeded_on_different_channel", "true");
@@ -79,6 +81,7 @@ public static void removeSystemProperty() {
7981
@After
8082
public void clearRequests() {
8183
LOGICAL_AFFINITY_KEYS.clear();
84+
ACTUAL_CHANNEL_IDS.clear();
8285
mockSpanner.clearRequests();
8386
mockSpanner.removeAllExecutionTimes();
8487
}
@@ -122,7 +125,30 @@ SpannerOptions.Builder createSpannerOptionsBuilder() {
122125
return SpannerOptions.newBuilder()
123126
.setProjectId("my-project")
124127
.setHost(String.format("http://localhost:%d", getPort()))
125-
.setChannelConfigurator(ManagedChannelBuilder::usePlaintext)
128+
.setChannelConfigurator(
129+
builder ->
130+
builder
131+
.usePlaintext()
132+
.intercept(
133+
new ClientInterceptor() {
134+
@Override
135+
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
136+
MethodDescriptor<ReqT, RespT> method,
137+
CallOptions callOptions,
138+
Channel next) {
139+
Integer channelId =
140+
callOptions.getOption(GcpManagedChannel.CHANNEL_ID_KEY);
141+
if (channelId != null) {
142+
synchronized (ACTUAL_CHANNEL_IDS) {
143+
ACTUAL_CHANNEL_IDS
144+
.computeIfAbsent(
145+
method.getFullMethodName(), k -> new HashSet<>())
146+
.add(channelId);
147+
}
148+
}
149+
return next.newCall(method, callOptions);
150+
}
151+
}))
126152
.setCredentials(NoCredentials.getInstance())
127153
.setInterceptorProvider(createAffinityKeyInterceptorProvider());
128154
}
@@ -313,10 +339,10 @@ public void testSingleUseQuery_retriesOnNewChannel() {
313339
List<ExecuteSqlRequest> requests = mockSpanner.getRequestsOfType(ExecuteSqlRequest.class);
314340
// The requests use the same multiplexed session.
315341
assertEquals(requests.get(0).getSession(), requests.get(1).getSession());
316-
// Verify that the retry used 2 distinct logical affinity keys (before grpc-gcp routing).
342+
// Verify that the retry used 2 distinct actual grpc-gcp channels.
317343
assertEquals(
318344
2,
319-
LOGICAL_AFFINITY_KEYS
345+
ACTUAL_CHANNEL_IDS
320346
.getOrDefault("google.spanner.v1.Spanner/ExecuteStreamingSql", new HashSet<>())
321347
.size());
322348
}

0 commit comments

Comments
 (0)