From 5b69d517d6d9967d34d0b67155a5b7d675552488 Mon Sep 17 00:00:00 2001 From: Sakthivel Subramanian Date: Mon, 7 Sep 2026 15:30:10 +0000 Subject: [PATCH 1/2] test(spanner): deflake testCreateSessionDeadlineExceededWithNoSessionCreateWaitTime Freeze MockSpanner before initializing DatabaseClient to deterministically bind the transaction to the initial failing CreateSession attempt, and enclose ReadContext in try-with-resources to ensure proper transaction cleanup. --- ...edSessionDatabaseClientMockServerTest.java | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/java-spanner/google-cloud-spanner/src/test/java/com/google/cloud/spanner/MultiplexedSessionDatabaseClientMockServerTest.java b/java-spanner/google-cloud-spanner/src/test/java/com/google/cloud/spanner/MultiplexedSessionDatabaseClientMockServerTest.java index c4cd8ba611f0..47557855fc1b 100644 --- a/java-spanner/google-cloud-spanner/src/test/java/com/google/cloud/spanner/MultiplexedSessionDatabaseClientMockServerTest.java +++ b/java-spanner/google-cloud-spanner/src/test/java/com/google/cloud/spanner/MultiplexedSessionDatabaseClientMockServerTest.java @@ -106,7 +106,7 @@ public void testCreateSessionDeadlineExceededWithNoSessionCreateWaitTime() throw // Simulate a problem with the CreateSession RPC making it slow. mockSpanner.setCreateSessionExecutionTime( SimulatedExecutionTime.ofException(Status.DEADLINE_EXCEEDED.asRuntimeException())); - mockSpanner.freezeAfter(1); + mockSpanner.freeze(); Spanner testSpanner = SpannerOptions.newBuilder() @@ -119,17 +119,23 @@ public void testCreateSessionDeadlineExceededWithNoSessionCreateWaitTime() throw DatabaseClientImpl client = (DatabaseClientImpl) testSpanner.getDatabaseClient(DatabaseId.of("p", "i", "d")); - // The first attempt should lead to a DEADLINE_EXCEEDED error being propagated from the - // CreateSession attempt. - try (ResultSet resultSet = client.singleUse().executeQuery(STATEMENT)) { - SpannerException exception = assertThrows(SpannerException.class, resultSet::next); - assertEquals(ErrorCode.DEADLINE_EXCEEDED, exception.getErrorCode()); + // Wait until the initial CreateSession request has reached the mock server and is frozen. + mockSpanner.waitForRequestsToContain(CreateSessionRequest.class, 5000); + + // Acquire the transaction while the initial CreateSession request is in progress. + // This guarantees that this transaction binds to the initial (failing) attempt. + try (ReadContext readContext = client.singleUse()) { + mockSpanner.unfreeze(); + + // The first attempt should lead to a DEADLINE_EXCEEDED error being propagated from the + // CreateSession attempt. + try (ResultSet resultSet = readContext.executeQuery(STATEMENT)) { + SpannerException exception = assertThrows(SpannerException.class, resultSet::next); + assertEquals(ErrorCode.DEADLINE_EXCEEDED, exception.getErrorCode()); + } } // The next attempt should then succeed. - mockSpanner.unfreeze(); - assertNotNull(client.multiplexedSessionDatabaseClient.getCurrentSessionReference()); - try (ResultSet resultSet = client.singleUse().executeQuery(STATEMENT)) { //noinspection StatementWithEmptyBody while (resultSet.next()) {} From 014904221897099192c48297dcbd250954a5041b Mon Sep 17 00:00:00 2001 From: Sakthivel Subramanian Date: Mon, 7 Sep 2026 15:41:16 +0000 Subject: [PATCH 2/2] test(spanner): move unfreeze inside ResultSet try block before resultSet::next --- .../MultiplexedSessionDatabaseClientMockServerTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/java-spanner/google-cloud-spanner/src/test/java/com/google/cloud/spanner/MultiplexedSessionDatabaseClientMockServerTest.java b/java-spanner/google-cloud-spanner/src/test/java/com/google/cloud/spanner/MultiplexedSessionDatabaseClientMockServerTest.java index 47557855fc1b..33f64aa636c4 100644 --- a/java-spanner/google-cloud-spanner/src/test/java/com/google/cloud/spanner/MultiplexedSessionDatabaseClientMockServerTest.java +++ b/java-spanner/google-cloud-spanner/src/test/java/com/google/cloud/spanner/MultiplexedSessionDatabaseClientMockServerTest.java @@ -125,11 +125,10 @@ public void testCreateSessionDeadlineExceededWithNoSessionCreateWaitTime() throw // Acquire the transaction while the initial CreateSession request is in progress. // This guarantees that this transaction binds to the initial (failing) attempt. try (ReadContext readContext = client.singleUse()) { - mockSpanner.unfreeze(); - // The first attempt should lead to a DEADLINE_EXCEEDED error being propagated from the // CreateSession attempt. try (ResultSet resultSet = readContext.executeQuery(STATEMENT)) { + mockSpanner.unfreeze(); SpannerException exception = assertThrows(SpannerException.class, resultSet::next); assertEquals(ErrorCode.DEADLINE_EXCEEDED, exception.getErrorCode()); }