Skip to content
This repository was archived by the owner on May 8, 2026. It is now read-only.

Commit 47cdf61

Browse files
test: deflake context propagation test
Change-Id: I382135f034cb5bbaa212d569b7f490568386df57
1 parent 52d5ffe commit 47cdf61

1 file changed

Lines changed: 43 additions & 6 deletions

File tree

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

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import com.google.api.gax.grpc.GaxGrpcProperties;
4040
import com.google.api.gax.grpc.GrpcCallContext;
4141
import com.google.api.gax.grpc.GrpcTransportChannel;
42+
import com.google.api.gax.retrying.RetrySettings;
4243
import com.google.api.gax.rpc.FailedPreconditionException;
4344
import com.google.api.gax.rpc.FixedTransportChannelProvider;
4445
import com.google.api.gax.rpc.InstantiatingWatchdogProvider;
@@ -110,6 +111,7 @@
110111
import io.grpc.ManagedChannelBuilder;
111112
import io.grpc.Metadata;
112113
import io.grpc.Metadata.Key;
114+
import io.grpc.MethodDescriptor;
113115
import io.grpc.Server;
114116
import io.grpc.ServerCall;
115117
import io.grpc.ServerCall.Listener;
@@ -708,7 +710,14 @@ public void testCallContextPropagatedInMutationBatcher()
708710

709711
// Override the timeout
710712
GrpcCallContext clientCtx =
711-
GrpcCallContext.createDefault().withTimeout(Duration.ofMinutes(10));
713+
GrpcCallContext.createDefault()
714+
.withTimeout(Duration.ofMinutes(10))
715+
.withRetrySettings(
716+
RetrySettings.newBuilder()
717+
.setTotalTimeout(Duration.ofMinutes(10))
718+
.build()
719+
720+
);
712721

713722
// Send a batch
714723
try (Batcher<RowMutationEntry, Void> batcher =
@@ -717,7 +726,7 @@ public void testCallContextPropagatedInMutationBatcher()
717726
}
718727

719728
// Ensure that the server got the overriden deadline
720-
Context serverCtx = contextInterceptor.contexts.poll();
729+
Context serverCtx = contextInterceptor.pollContext(BigtableGrpc.getMutateRowsMethod());
721730
assertThat(serverCtx).isNotNull();
722731
assertThat(serverCtx.getDeadline()).isAtLeast(Deadline.after(8, TimeUnit.MINUTES));
723732
}
@@ -738,7 +747,14 @@ public void testCallContextPropagatedInReadBatcher()
738747

739748
// Override the timeout
740749
GrpcCallContext clientCtx =
741-
GrpcCallContext.createDefault().withTimeout(Duration.ofMinutes(10));
750+
GrpcCallContext.createDefault()
751+
.withTimeout(Duration.ofMinutes(10))
752+
.withRetrySettings(
753+
RetrySettings.newBuilder()
754+
.setTotalTimeout(Duration.ofMinutes(10))
755+
.build()
756+
757+
);
742758

743759
// Send a batch
744760
try (Batcher<ByteString, Row> batcher =
@@ -747,7 +763,7 @@ public void testCallContextPropagatedInReadBatcher()
747763
}
748764

749765
// Ensure that the server got the overriden deadline
750-
Context serverCtx = contextInterceptor.contexts.poll();
766+
Context serverCtx = contextInterceptor.pollContext(BigtableGrpc.getReadRowsMethod());
751767
assertThat(serverCtx).isNotNull();
752768
assertThat(serverCtx.getDeadline()).isAtLeast(Deadline.after(8, TimeUnit.MINUTES));
753769
}
@@ -970,16 +986,37 @@ public <ReqT, RespT> Listener<ReqT> interceptCall(
970986
}
971987

972988
private static class ContextInterceptor implements ServerInterceptor {
973-
final BlockingQueue<Context> contexts = Queues.newLinkedBlockingDeque();
989+
final BlockingQueue<MethodContext> contexts = Queues.newLinkedBlockingDeque();
990+
991+
static class MethodContext {
992+
final MethodDescriptor<?,?> method;
993+
final Context context;
994+
995+
MethodContext(MethodDescriptor<?, ?> method, Context context) {
996+
this.method = method;
997+
this.context = context;
998+
}
999+
}
9741000

9751001
@Override
9761002
public <ReqT, RespT> Listener<ReqT> interceptCall(
9771003
ServerCall<ReqT, RespT> serverCall,
9781004
Metadata metadata,
9791005
ServerCallHandler<ReqT, RespT> serverCallHandler) {
980-
contexts.add(Context.current());
1006+
contexts.add(new MethodContext(serverCall.getMethodDescriptor(), Context.current()));
9811007
return serverCallHandler.startCall(serverCall, metadata);
9821008
}
1009+
1010+
Context pollContext(MethodDescriptor<?, ?> method) {
1011+
ContextInterceptor.MethodContext methodContext = contexts.poll();
1012+
while (methodContext != null) {
1013+
if (method.equals(methodContext.method)) {
1014+
return methodContext.context;
1015+
}
1016+
methodContext = contexts.poll();
1017+
}
1018+
return null;
1019+
}
9831020
}
9841021

9851022
private static class FakeDataService extends BigtableGrpc.BigtableImplBase {

0 commit comments

Comments
 (0)