Skip to content

Commit 97db090

Browse files
test: cover pool close while a vRPC is queued in pendingRpcs
The existing awaitTerminated tests only close an empty pool, close after the vRPC has completed, or issue the vRPC against an already-closed pool — none of them exercise SessionPoolImpl.close()'s pendingRpcs cancellation loop with a non-empty toCancel list. Add closeCancelsInFlightVRpcAndDrains: park the session-open response in a DelayedClientInterceptor so the vRPC sits in pendingRpcs, then close the pool and verify the listener gets a single CANCELLED onClose with the "SessionPool closed:" description, awaitTerminated returns true via the OPENING session draining through sessions.close(req), and no phantom second onClose arrives once the delayed responses flush.
1 parent 77d93db commit 97db090

1 file changed

Lines changed: 84 additions & 0 deletions

File tree

  • java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/session

‎java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/session/SessionPoolImplTest.java‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
import io.grpc.ServerCall;
7474
import io.grpc.ServerCallHandler;
7575
import io.grpc.ServerInterceptor;
76+
import io.grpc.Status;
7677
import java.io.IOException;
7778
import java.time.Duration;
7879
import java.time.Instant;
@@ -319,6 +320,89 @@ void awaitTerminatedReturnsTrueAfterSessionsDrain()
319320
assertThat(sessionPool.awaitTerminated(Duration.ofSeconds(10))).isTrue();
320321
}
321322

323+
@Test
324+
void closeCancelsInFlightVRpcAndDrains() throws InterruptedException {
325+
// Park the session-open response in the client interceptor so the vRPC cannot drain to a
326+
// ready session and sits in pendingRpcs. Then close the pool and verify (a) the queued
327+
// vRPC's listener gets a CANCELLED onClose with the "SessionPool closed:" description from
328+
// SessionPoolImpl.close()'s cancellation loop, (b) awaitTerminated returns true (the
329+
// OPENING session is driven to CLOSED by sessions.close(req), not parked until the
330+
// watchdog tick), and (c) no second onClose arrives once the delayed responses finally
331+
// flush past the interceptor.
332+
SessionPool<OpenFakeSessionRequest> testSessionPool = null;
333+
try (ChannelPool delayedPool =
334+
new SingleChannelPool(
335+
Suppliers.ofInstance(
336+
Grpc.newChannelBuilderForAddress(
337+
"localhost", server.getPort(), InsecureChannelCredentials.create())
338+
.intercept(new DelayedClientInterceptor(Duration.ofMillis(100)))
339+
.build()))) {
340+
341+
delayedPool.start();
342+
343+
testSessionPool =
344+
new SessionPoolImpl<>(
345+
metrics,
346+
FeatureFlags.getDefaultInstance(),
347+
CLIENT_INFO,
348+
configManager,
349+
delayedPool,
350+
CallOptions.DEFAULT,
351+
FakeDescriptor.FAKE_SESSION,
352+
"fake-pool-in-flight",
353+
testTimer);
354+
355+
testSessionPool.start(OpenFakeSessionRequest.getDefaultInstance(), new Metadata());
356+
357+
VRpc<SessionFakeScriptedRequest, SessionFakeScriptedResponse> vrpc =
358+
testSessionPool.newCall(FakeDescriptor.SCRIPTED);
359+
CopyOnWriteArrayList<VRpcResult> closes = new CopyOnWriteArrayList<>();
360+
CountDownLatch firstClose = new CountDownLatch(1);
361+
Duration deadline = Duration.ofSeconds(10);
362+
vrpc.start(
363+
SessionFakeScriptedRequest.getDefaultInstance(),
364+
VRpcCallContext.create(
365+
Deadline.after(deadline.toMillis(), TimeUnit.MILLISECONDS), true, vrpcTracer),
366+
new VRpc.VRpcListener<SessionFakeScriptedResponse>() {
367+
@Override
368+
public void onMessage(SessionFakeScriptedResponse msg) {}
369+
370+
@Override
371+
public void onClose(VRpcResult result) {
372+
closes.add(result);
373+
firstClose.countDown();
374+
}
375+
});
376+
377+
// vRPC is now queued in pendingRpcs behind the parked session-open. Close the pool.
378+
testSessionPool.close(
379+
CloseSessionRequest.newBuilder()
380+
.setReason(CloseSessionRequest.CloseSessionReason.CLOSE_SESSION_REASON_USER)
381+
.setDescription("close while in flight")
382+
.build());
383+
384+
// Cancellation hops through the vRPC's per-op SerializingExecutor — not the gRPC
385+
// listener thread — so it should arrive promptly even while the interceptor is asleep.
386+
assertThat(firstClose.await(2, TimeUnit.SECONDS)).isTrue();
387+
assertThat(closes).hasSize(1);
388+
VRpcResult result = closes.get(0);
389+
assertThat(result).status().code().isEqualTo(Status.Code.CANCELLED);
390+
assertThat(result).status().description().contains("SessionPool closed:");
391+
392+
// OPENING session drains via sessions.close(req) once the delayed onMessages flush.
393+
assertThat(testSessionPool.awaitTerminated(Duration.ofSeconds(10))).isTrue();
394+
395+
// Regression guard: the delayed session-open responses arriving post-cancellation must
396+
// not produce a phantom second onClose on the cancelled vRPC's listener.
397+
Thread.sleep(200);
398+
assertThat(closes).hasSize(1);
399+
} finally {
400+
if (testSessionPool != null) {
401+
testSessionPool.close(CloseSessionRequest.getDefaultInstance());
402+
}
403+
}
404+
}
405+
322406
@Test
323407
void pendingVRpcOnClosedPoolDoesNotLeakDeadlineMonitor() throws InterruptedException {
324408
// Regression: PendingVRpc.start used to arm the deadline timer before the pool-state

0 commit comments

Comments
 (0)