use of com.google.cloud.spanner.SessionPool.PooledSession in project google-cloud-java by GoogleCloudPlatform.
the class SessionPoolTest method sessionIsPrePrepared.
@Test
public void sessionIsPrePrepared() {
Session mockSession1 = mockSession();
Session mockSession2 = mockSession();
final CountDownLatch prepareLatch = new CountDownLatch(1);
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock arg0) throws Throwable {
prepareLatch.countDown();
return null;
}
}).when(mockSession1).prepareReadWriteTransaction();
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock arg0) throws Throwable {
prepareLatch.countDown();
return null;
}
}).when(mockSession2).prepareReadWriteTransaction();
when(client.createSession(db)).thenReturn(mockSession1).thenReturn(mockSession2);
options = SessionPoolOptions.newBuilder().setMinSessions(2).setMaxSessions(2).setWriteSessionsFraction(0.5f).build();
pool = createPool();
// One of the sessions would be pre prepared.
Uninterruptibles.awaitUninterruptibly(prepareLatch);
PooledSession readSession = (PooledSession) pool.getReadSession();
PooledSession writeSession = (PooledSession) pool.getReadWriteSession();
verify(writeSession.delegate, times(1)).prepareReadWriteTransaction();
verify(readSession.delegate, never()).prepareReadWriteTransaction();
readSession.close();
writeSession.close();
}
use of com.google.cloud.spanner.SessionPool.PooledSession in project google-cloud-java by GoogleCloudPlatform.
the class SessionPoolTest method getReadSessionFallsBackToWritePreparedSession.
@Test
public void getReadSessionFallsBackToWritePreparedSession() throws Exception {
Session mockSession1 = mockSession();
final CountDownLatch prepareLatch = new CountDownLatch(2);
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock arg0) throws Throwable {
prepareLatch.countDown();
return null;
}
}).when(mockSession1).prepareReadWriteTransaction();
when(client.createSession(db)).thenReturn(mockSession1);
options = SessionPoolOptions.newBuilder().setMinSessions(minSessions).setMaxSessions(1).setWriteSessionsFraction(1.0f).build();
pool = createPool();
pool.getReadWriteSession().close();
prepareLatch.await();
// This session should also be write prepared.
PooledSession readSession = (PooledSession) pool.getReadSession();
verify(readSession.delegate, times(2)).prepareReadWriteTransaction();
}
use of com.google.cloud.spanner.SessionPool.PooledSession in project google-cloud-java by GoogleCloudPlatform.
the class SessionPoolTest method poolWorksWhenSessionNotFound.
@Test
public void poolWorksWhenSessionNotFound() {
Session mockSession1 = mockSession();
Session mockSession2 = mockSession();
doThrow(SpannerExceptionFactory.newSpannerException(ErrorCode.NOT_FOUND, "Session not found")).when(mockSession1).prepareReadWriteTransaction();
when(client.createSession(db)).thenReturn(mockSession1).thenReturn(mockSession2);
pool = createPool();
assertThat(((PooledSession) pool.getReadWriteSession()).delegate).isEqualTo(mockSession2);
}
Aggregations