Search in sources :

Example 1 with PooledSession

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();
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) PooledSession(com.google.cloud.spanner.SessionPool.PooledSession) CountDownLatch(java.util.concurrent.CountDownLatch) PooledSession(com.google.cloud.spanner.SessionPool.PooledSession) Test(org.junit.Test)

Example 2 with PooledSession

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();
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) PooledSession(com.google.cloud.spanner.SessionPool.PooledSession) CountDownLatch(java.util.concurrent.CountDownLatch) PooledSession(com.google.cloud.spanner.SessionPool.PooledSession) Test(org.junit.Test)

Example 3 with PooledSession

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);
}
Also used : PooledSession(com.google.cloud.spanner.SessionPool.PooledSession) PooledSession(com.google.cloud.spanner.SessionPool.PooledSession) Test(org.junit.Test)

Aggregations

PooledSession (com.google.cloud.spanner.SessionPool.PooledSession)3 Test (org.junit.Test)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2