use of com.google.cloud.spanner.SessionPool.SessionConsumerImpl in project java-spanner by googleapis.
the class SessionPoolTest method creationExceptionPropagatesToReadSession.
@Test
public void creationExceptionPropagatesToReadSession() {
doAnswer(invocation -> {
executor.submit(() -> {
SessionConsumerImpl consumer = invocation.getArgument(2, SessionConsumerImpl.class);
consumer.onSessionCreateFailure(SpannerExceptionFactory.newSpannerException(ErrorCode.INTERNAL, ""), 1);
return null;
});
return null;
}).when(sessionClient).asyncBatchCreateSessions(Mockito.eq(1), Mockito.anyBoolean(), any(SessionConsumer.class));
pool = createPool();
SpannerException e = assertThrows(SpannerException.class, () -> pool.getSession().get());
assertEquals(ErrorCode.INTERNAL, e.getErrorCode());
}
use of com.google.cloud.spanner.SessionPool.SessionConsumerImpl in project java-spanner by googleapis.
the class SessionPoolTest method failOnPoolExhaustion.
@Test
public void failOnPoolExhaustion() {
options = SessionPoolOptions.newBuilder().setMinSessions(1).setMaxSessions(1).setFailIfPoolExhausted().build();
doAnswer(invocation -> {
executor.submit(() -> {
SessionConsumerImpl consumer = invocation.getArgument(2, SessionConsumerImpl.class);
consumer.onSessionReady(mockSession());
});
return null;
}).when(sessionClient).asyncBatchCreateSessions(Mockito.eq(1), Mockito.anyBoolean(), any(SessionConsumer.class));
pool = createPool();
Session session1 = pool.getSession();
SpannerException e = assertThrows(SpannerException.class, () -> pool.getSession());
assertEquals(ErrorCode.RESOURCE_EXHAUSTED, e.getErrorCode());
session1.close();
session1 = pool.getSession();
assertThat(session1).isNotNull();
session1.close();
}
use of com.google.cloud.spanner.SessionPool.SessionConsumerImpl in project java-spanner by googleapis.
the class SessionPoolTest method testSessionNotFoundPartitionedUpdate.
@Test
public void testSessionNotFoundPartitionedUpdate() {
SpannerException sessionNotFound = SpannerExceptionFactoryTest.newSessionNotFoundException(sessionName);
Statement statement = Statement.of("UPDATE FOO SET BAR=1 WHERE 1=1");
final SessionImpl closedSession = mockSession();
when(closedSession.executePartitionedUpdate(statement)).thenThrow(sessionNotFound);
final SessionImpl openSession = mockSession();
when(openSession.executePartitionedUpdate(statement)).thenReturn(1L);
doAnswer(invocation -> {
executor.submit(() -> {
SessionConsumerImpl consumer = invocation.getArgument(2, SessionConsumerImpl.class);
consumer.onSessionReady(closedSession);
});
return null;
}).doAnswer(invocation -> {
executor.submit(() -> {
SessionConsumerImpl consumer = invocation.getArgument(2, SessionConsumerImpl.class);
consumer.onSessionReady(openSession);
});
return null;
}).when(sessionClient).asyncBatchCreateSessions(Mockito.eq(1), Mockito.anyBoolean(), any(SessionConsumer.class));
FakeClock clock = new FakeClock();
clock.currentTimeMillis = System.currentTimeMillis();
pool = createPool(clock);
DatabaseClientImpl impl = new DatabaseClientImpl(pool);
assertThat(impl.executePartitionedUpdate(statement)).isEqualTo(1L);
}
Aggregations