Skip to content

Commit

Permalink
Recreate spanner for tests individually
Browse files Browse the repository at this point in the history
  • Loading branch information
sakthivelmanii committed Jan 6, 2025
1 parent cfa1bc0 commit 8b3e467
Showing 1 changed file with 21 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -226,34 +226,28 @@ public static void stopServer() throws InterruptedException {
@Before
public void setUp() throws InterruptedException {
mockSpanner.reset();
if (spanner == null
|| spanner.getOptions().getSessionPoolOptions().isFailIfSessionNotFound()
!= failOnInvalidatedSession) {
System.out.println("Recreating spanner instance");
if (spanner != null) {
System.out.printf(
"Closing the spanner instance %s %s",
spanner.getOptions().getSessionPoolOptions().isFailIfSessionNotFound(),
failOnInvalidatedSession);
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]"));
System.out.println("Recreating spanner instance");
if (spanner != null) {
System.out.printf("Closing the spanner instance %s %s",
spanner.getOptions().getSessionPoolOptions().isFailIfSessionNotFound(), failOnInvalidatedSession);
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]"));
invalidateSessionPool(client, spanner.getOptions().getSessionPoolOptions().getMinSessions());
}

Expand Down

0 comments on commit 8b3e467

Please sign in to comment.