use of com.google.cloud.spanner.SessionPool.PooledSessionFuture in project java-spanner by googleapis.
the class DatabaseClientImplTest method testTransactionManager_usesOptions.
@Test
public void testTransactionManager_usesOptions() {
SessionPool pool = mock(SessionPool.class);
PooledSessionFuture session = mock(PooledSessionFuture.class);
when(pool.getSession()).thenReturn(session);
TransactionOption option = mock(TransactionOption.class);
DatabaseClientImpl client = new DatabaseClientImpl(pool);
client.transactionManager(option);
verify(session).transactionManager(option);
}
use of com.google.cloud.spanner.SessionPool.PooledSessionFuture in project java-spanner by googleapis.
the class DatabaseClientImplTest method testReadWriteTransaction_usesOptions.
@Test
public void testReadWriteTransaction_usesOptions() {
SessionPool pool = mock(SessionPool.class);
PooledSessionFuture session = mock(PooledSessionFuture.class);
when(pool.getSession()).thenReturn(session);
TransactionOption option = mock(TransactionOption.class);
DatabaseClientImpl client = new DatabaseClientImpl(pool);
client.readWriteTransaction(option);
verify(session).readWriteTransaction(option);
}
use of com.google.cloud.spanner.SessionPool.PooledSessionFuture in project java-spanner by googleapis.
the class ITSessionPoolIntegrationTest method sessionCreation.
@Test
public void sessionCreation() {
try (PooledSessionFuture session = pool.getSession()) {
assertThat(session.get()).isNotNull();
}
try (PooledSessionFuture session = pool.getSession();
PooledSessionFuture session2 = pool.getSession()) {
assertThat(session.get()).isNotNull();
assertThat(session2.get()).isNotNull();
}
}
use of com.google.cloud.spanner.SessionPool.PooledSessionFuture in project java-spanner by googleapis.
the class SessionPoolStressTest method stressTest.
@Test
public void stressTest() throws Exception {
int concurrentThreads = 10;
final int numOperationsPerThread = 1000;
final CountDownLatch releaseThreads = new CountDownLatch(1);
final CountDownLatch threadsDone = new CountDownLatch(concurrentThreads);
setupSpanner(db);
int minSessions = 2;
int maxSessions = concurrentThreads / 2;
SessionPoolOptions.Builder builder = SessionPoolOptions.newBuilder().setMinSessions(minSessions).setMaxSessions(maxSessions);
if (shouldBlock) {
builder.setBlockIfPoolExhausted();
} else {
builder.setFailIfPoolExhausted();
}
pool = SessionPool.createPool(builder.build(), new TestExecutorFactory(), mockSpanner.getSessionClient(db), clock);
pool.idleSessionRemovedListener = pooled -> {
String name = pooled.getName();
synchronized (lock) {
sessions.remove(name);
return null;
}
};
for (int i = 0; i < concurrentThreads; i++) {
new Thread(() -> {
Uninterruptibles.awaitUninterruptibly(releaseThreads);
for (int j = 0; j < numOperationsPerThread; j++) {
try {
PooledSessionFuture session = pool.getSession();
session.get();
Uninterruptibles.sleepUninterruptibly(random.nextInt(2), TimeUnit.MILLISECONDS);
resetTransaction(session.get().delegate);
session.close();
} catch (SpannerException e) {
if (e.getErrorCode() != ErrorCode.RESOURCE_EXHAUSTED || shouldBlock) {
setFailed(e);
}
} catch (Exception e) {
setFailed(e);
}
}
threadsDone.countDown();
}).start();
}
// Start maintenance threads in tight loop
final AtomicBoolean stopMaintenance = new AtomicBoolean(false);
new Thread(() -> {
while (!stopMaintenance.get()) {
runMaintenanceLoop(clock, pool, 1);
}
}).start();
releaseThreads.countDown();
threadsDone.await();
synchronized (lock) {
assertThat(maxAliveSessions).isAtMost(maxSessions);
}
stopMaintenance.set(true);
pool.closeAsync(new SpannerImpl.ClosedException()).get();
Exception e = getFailedError();
if (e != null) {
throw e;
}
}
use of com.google.cloud.spanner.SessionPool.PooledSessionFuture in project java-spanner by googleapis.
the class SessionPoolTest method poolClosureClosesLeakedSessions.
@Test
public void poolClosureClosesLeakedSessions() throws Exception {
SessionImpl mockSession1 = mockSession();
SessionImpl mockSession2 = mockSession();
final LinkedList<SessionImpl> sessions = new LinkedList<>(Arrays.asList(mockSession1, mockSession2));
doAnswer(invocation -> {
executor.submit(() -> {
SessionConsumerImpl consumer = invocation.getArgument(2, SessionConsumerImpl.class);
consumer.onSessionReady(sessions.pop());
});
return null;
}).when(sessionClient).asyncBatchCreateSessions(Mockito.eq(1), Mockito.anyBoolean(), any(SessionConsumer.class));
pool = createPool();
Session session1 = pool.getSession();
// Leaked sessions
PooledSessionFuture leakedSession = pool.getSession();
// Clear the leaked exception to suppress logging of expected exceptions.
leakedSession.clearLeakedException();
session1.close();
pool.closeAsync(new SpannerImpl.ClosedException()).get(5L, TimeUnit.SECONDS);
verify(mockSession1).asyncClose();
verify(mockSession2).asyncClose();
}
Aggregations