Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Supplier;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
Expand Down Expand Up @@ -205,33 +206,40 @@ public static void stopServer() throws InterruptedException {

@Before
public void setUp() throws InterruptedException {
// Reset the mock Spanner service to ensure a clean state for each test.
mockSpanner.reset();
if (spanner == null
|| spanner.getOptions().getSessionPoolOptions().isFailIfSessionNotFound()
!= failOnInvalidatedSession) {
if (spanner != null) {
spanner.close();
}
SessionPoolOptions.Builder builder = SessionPoolOptions.newBuilder().setFailOnSessionLeak();
if (failOnInvalidatedSession) {
builder.setFailIfSessionNotFound();
}
// This prevents repeated retries for a large number of sessions in the pool.
builder.setMinSessions(1);
SessionPoolOptions sessionPoolOptions = builder.build();
spanner =
SpannerOptions.newBuilder()
.setProjectId("[PROJECT]")
.setChannelProvider(channelProvider)
.setSessionPoolOption(sessionPoolOptions)
.setCredentials(NoCredentials.getInstance())
.build()
.getService();
client = spanner.getDatabaseClient(DatabaseId.of("[PROJECT]", "[INSTANCE]", "[DATABASE]"));

// Create a new Spanner instance for each test to ensure session isolation.
SessionPoolOptions.Builder builder = SessionPoolOptions.newBuilder().setFailOnSessionLeak();
if (failOnInvalidatedSession) {
builder.setFailIfSessionNotFound();
}
builder.setMinSessions(1); // Ensure a minimum number of sessions are available.
SessionPoolOptions sessionPoolOptions = builder.build();

spanner =
SpannerOptions.newBuilder()
.setProjectId("[PROJECT]")
.setChannelProvider(channelProvider)
.setSessionPoolOption(sessionPoolOptions)
.setCredentials(NoCredentials.getInstance())
.build()
.getService();

client = spanner.getDatabaseClient(DatabaseId.of("[PROJECT]", "[INSTANCE]", "[DATABASE]"));

// Invalidate the session pool to ensure no sessions are reused.
invalidateSessionPool(client, spanner.getOptions().getSessionPoolOptions().getMinSessions());
}

@After
public void tearDown() {
// Close the Spanner instance to release all sessions.
if (spanner != null) {
spanner.close();
}
}

private static void invalidateSessionPool(DatabaseClient client, int minSessions)
throws InterruptedException {
// Wait for all sessions to have been created, and then delete them.
Expand Down
Loading