From 5f09b78a0fc6c4df46ddfe544bd3a4a4ea7a6530 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Knut=20Olav=20L=C3=B8ite?= Date: Mon, 10 Jun 2024 10:17:24 +0200 Subject: [PATCH] chore: add wait to test to prevent flakiness Adds a small wait to the test to ensure no flakiness, as it could be that sessions are re-used instead of just waiting for new sessions to be created. The test however always creates 4 sessions. Fixes #3106 --- .../cloud/spanner/DatabaseClientImplTest.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/DatabaseClientImplTest.java b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/DatabaseClientImplTest.java index 91189c70d9c..6443c904b86 100644 --- a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/DatabaseClientImplTest.java +++ b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/DatabaseClientImplTest.java @@ -5082,8 +5082,7 @@ public void testSessionPoolExhaustedError_containsStackTraces() { // Deliberately leak 4 sessions. for (int i = 0; i < 4; i++) { // Get a transaction manager without doing anything with it. This will reserve a session - // from - // the pool, but not increase the number of sessions marked as in use. + // from the pool, but not increase the number of sessions marked as in use. transactions.add(client.transactionManager()); } // Trying to get yet another transaction will fail. @@ -5111,6 +5110,19 @@ public void testSessionPoolExhaustedError_containsStackTraces() { for (TransactionManager transaction : transactions) { transaction.close(); } + // Wait up to 100 milliseconds for the sessions to actually all be in the pool, as there are + // two possible ways that the session pool handles the above: + // 1. The pool starts to create 4 sessions. + // 2. It then hands out whatever session has been created to one of the waiters. + // 3. The waiting process then executes its transaction, and when finished, the session is + // given to any other process waiting at that moment. + // The above means that although there will always be 4 sessions created, it could in theory + // be that not all of them are used, as it could be that a transaction finishes before the + // creation of session 2, 3, or 4 finished, and then the existing session is re-used. + Stopwatch watch = Stopwatch.createStarted(); + while (pool.getNumberOfSessionsInPool() < 4 && watch.elapsed(TimeUnit.MILLISECONDS) < 100) { + Thread.yield(); + } // Closing the transactions should return the sessions to the pool. assertEquals(4, pool.getNumberOfSessionsInPool()); }