Skip to content

Commit 142cb0a

Browse files
committed
test: unflake RetryOnInvalidatedSessionTest
1 parent 1af8e46 commit 142cb0a

File tree

1 file changed

+29
-22
lines changed

1 file changed

+29
-22
lines changed

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

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import java.util.concurrent.atomic.AtomicInteger;
5858
import java.util.concurrent.atomic.AtomicLong;
5959
import java.util.function.Supplier;
60+
import org.junit.After;
6061
import org.junit.AfterClass;
6162
import org.junit.Before;
6263
import org.junit.BeforeClass;
@@ -205,33 +206,39 @@ public static void stopServer() throws InterruptedException {
205206

206207
@Before
207208
public void setUp() throws InterruptedException {
209+
// Reset the mock Spanner service to ensure a clean state for each test.
208210
mockSpanner.reset();
209-
if (spanner == null
210-
|| spanner.getOptions().getSessionPoolOptions().isFailIfSessionNotFound()
211-
!= failOnInvalidatedSession) {
212-
if (spanner != null) {
213-
spanner.close();
214-
}
215-
SessionPoolOptions.Builder builder = SessionPoolOptions.newBuilder().setFailOnSessionLeak();
216-
if (failOnInvalidatedSession) {
217-
builder.setFailIfSessionNotFound();
218-
}
219-
// This prevents repeated retries for a large number of sessions in the pool.
220-
builder.setMinSessions(1);
221-
SessionPoolOptions sessionPoolOptions = builder.build();
222-
spanner =
223-
SpannerOptions.newBuilder()
224-
.setProjectId("[PROJECT]")
225-
.setChannelProvider(channelProvider)
226-
.setSessionPoolOption(sessionPoolOptions)
227-
.setCredentials(NoCredentials.getInstance())
228-
.build()
229-
.getService();
230-
client = spanner.getDatabaseClient(DatabaseId.of("[PROJECT]", "[INSTANCE]", "[DATABASE]"));
211+
212+
// Create a new Spanner instance for each test to ensure session isolation.
213+
SessionPoolOptions.Builder builder = SessionPoolOptions.newBuilder().setFailOnSessionLeak();
214+
if (failOnInvalidatedSession) {
215+
builder.setFailIfSessionNotFound();
231216
}
217+
builder.setMinSessions(1); // Ensure a minimum number of sessions are available.
218+
SessionPoolOptions sessionPoolOptions = builder.build();
219+
220+
spanner = SpannerOptions.newBuilder()
221+
.setProjectId("[PROJECT]")
222+
.setChannelProvider(channelProvider)
223+
.setSessionPoolOption(sessionPoolOptions)
224+
.setCredentials(NoCredentials.getInstance())
225+
.build()
226+
.getService();
227+
228+
client = spanner.getDatabaseClient(DatabaseId.of("[PROJECT]", "[INSTANCE]", "[DATABASE]"));
229+
230+
// Invalidate the session pool to ensure no sessions are reused.
232231
invalidateSessionPool(client, spanner.getOptions().getSessionPoolOptions().getMinSessions());
233232
}
234233

234+
@After
235+
public void tearDown() {
236+
// Close the Spanner instance to release all sessions.
237+
if (spanner != null) {
238+
spanner.close();
239+
}
240+
}
241+
235242
private static void invalidateSessionPool(DatabaseClient client, int minSessions)
236243
throws InterruptedException {
237244
// Wait for all sessions to have been created, and then delete them.

0 commit comments

Comments
 (0)