fix: update max_in_use_session at 10 mins interval (#3570) · googleapis/java-spanner@cc1753d (original) (raw)

`@@ -2247,6 +2247,54 @@ public void testWaitOnMinSessionsThrowsExceptionWhenTimeoutIsReached() {

`

2247

2247

`pool.maybeWaitOnMinSessions();

`

2248

2248

` }

`

2249

2249

``

``

2250

`+

@Test

`

``

2251

`+

public void reset_maxSessionsInUse() {

`

``

2252

`+

Clock clock = mock(Clock.class);

`

``

2253

`+

when(clock.instant()).thenReturn(Instant.now());

`

``

2254

`+

options =

`

``

2255

`+

SessionPoolOptions.newBuilder()

`

``

2256

`+

.setMinSessions(1)

`

``

2257

`+

.setMaxSessions(3)

`

``

2258

`+

.setIncStep(1)

`

``

2259

`+

.setMaxIdleSessions(0)

`

``

2260

`+

.setPoolMaintainerClock(clock)

`

``

2261

`+

.build();

`

``

2262

`+

setupForLongRunningTransactionsCleanup(options);

`

``

2263

+

``

2264

`+

pool = createPool(clock);

`

``

2265

`+

// Make sure pool has been initialized

`

``

2266

`+

pool.getSession().close();

`

``

2267

+

``

2268

`+

// All 3 sessions used. 100% of pool utilised.

`

``

2269

`+

PooledSessionFuture readSession1 = pool.getSession();

`

``

2270

`+

PooledSessionFuture readSession2 = pool.getSession();

`

``

2271

`+

PooledSessionFuture readSession3 = pool.getSession();

`

``

2272

+

``

2273

`+

// complete the async tasks

`

``

2274

`+

readSession1.get().setEligibleForLongRunning(false);

`

``

2275

`+

readSession2.get().setEligibleForLongRunning(false);

`

``

2276

`+

readSession3.get().setEligibleForLongRunning(true);

`

``

2277

+

``

2278

`+

assertEquals(3, pool.getMaxSessionsInUse());

`

``

2279

`+

assertEquals(3, pool.getNumberOfSessionsInUse());

`

``

2280

+

``

2281

`+

// Release 1 session

`

``

2282

`+

readSession1.get().close();

`

``

2283

+

``

2284

`+

// Verify that numSessionsInUse reduces to 2 while maxSessionsInUse remain 3

`

``

2285

`+

assertEquals(3, pool.getMaxSessionsInUse());

`

``

2286

`+

assertEquals(2, pool.getNumberOfSessionsInUse());

`

``

2287

+

``

2288

`+

// ensure that the lastResetTime for maxSessionsInUse > 10 minutes

`

``

2289

`+

when(clock.instant()).thenReturn(Instant.now().plus(11, ChronoUnit.MINUTES));

`

``

2290

+

``

2291

`+

pool.poolMaintainer.maintainPool();

`

``

2292

+

``

2293

`+

// Verify that maxSessionsInUse is reset to numSessionsInUse

`

``

2294

`+

assertEquals(2, pool.getMaxSessionsInUse());

`

``

2295

`+

assertEquals(2, pool.getNumberOfSessionsInUse());

`

``

2296

`+

}

`

``

2297

+

2250

2298

`private void mockKeepAlive(ReadContext context) {

`

2251

2299

`ResultSet resultSet = mock(ResultSet.class);

`

2252

2300

`when(resultSet.next()).thenReturn(true, false);

`