|
57 | 57 | import java.util.concurrent.atomic.AtomicInteger; |
58 | 58 | import java.util.concurrent.atomic.AtomicLong; |
59 | 59 | import java.util.function.Supplier; |
| 60 | +import org.junit.After; |
60 | 61 | import org.junit.AfterClass; |
61 | 62 | import org.junit.Before; |
62 | 63 | import org.junit.BeforeClass; |
@@ -205,33 +206,39 @@ public static void stopServer() throws InterruptedException { |
205 | 206 |
|
206 | 207 | @Before |
207 | 208 | public void setUp() throws InterruptedException { |
| 209 | + // Reset the mock Spanner service to ensure a clean state for each test. |
208 | 210 | 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(); |
231 | 216 | } |
| 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. |
232 | 231 | invalidateSessionPool(client, spanner.getOptions().getSessionPoolOptions().getMinSessions()); |
233 | 232 | } |
234 | 233 |
|
| 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 | + |
235 | 242 | private static void invalidateSessionPool(DatabaseClient client, int minSessions) |
236 | 243 | throws InterruptedException { |
237 | 244 | // Wait for all sessions to have been created, and then delete them. |
|
0 commit comments