@Override public void prepareReadWriteTransaction() { markUsed(); delegate.prepareReadWriteTransaction(); }
@Test public void getReadWriteSession() { Session mockSession = mockSession(); when(client.createSession(db)).thenReturn(mockSession); pool = createPool(); try (Session session = pool.getReadWriteSession()) { assertThat(session).isNotNull(); verify(mockSession).prepareReadWriteTransaction(); } }
@Test public void getMultipleReadWriteSessions() { Session mockSession1 = mockSession(); Session mockSession2 = mockSession(); when(client.createSession(db)).thenReturn(mockSession1).thenReturn(mockSession2); pool = createPool(); Session session1 = pool.getReadWriteSession(); Session session2 = pool.getReadWriteSession(); verify(mockSession1).prepareReadWriteTransaction(); verify(mockSession2).prepareReadWriteTransaction(); session1.close(); session2.close(); }
@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); }
@Test public void poolClosesEvenIfPreparationFails() throws Exception { Session session = mockSession(); when(client.createSession(db)).thenReturn(session); final CountDownLatch insidePrepare = new CountDownLatch(1); final CountDownLatch releasePrepare = new CountDownLatch(1); doAnswer( new Answer<Session>() { @Override public Session answer(InvocationOnMock invocation) throws Throwable { insidePrepare.countDown(); releasePrepare.await(); throw SpannerExceptionFactory.newSpannerException(new RuntimeException()); } }) .when(session) .prepareReadWriteTransaction(); pool = createPool(); AtomicBoolean failed = new AtomicBoolean(false); CountDownLatch latch = new CountDownLatch(1); getReadWriteSessionAsync(latch, failed); insidePrepare.await(); ListenableFuture<Void> f = pool.closeAsync(); releasePrepare.countDown(); f.get(); assertThat(f.isDone()).isTrue(); }
@Test public void prepareExceptionPropagatesToReadWriteSession() { Session session = mockSession(); when(client.createSession(db)).thenReturn(session); doThrow(SpannerExceptionFactory.newSpannerException(ErrorCode.INTERNAL, "")) .when(session) .prepareReadWriteTransaction(); pool = createPool(); expectedException.expect(isSpannerException(ErrorCode.INTERNAL)); pool.getReadWriteSession(); }
@Test public void prepareClosesOldSingleUseContext() { ReadContext ctx = session.singleUse(TimestampBound.strong()); Mockito.when(rpc.beginTransaction(Mockito.<BeginTransactionRequest>any(), Mockito.eq(options))) .thenReturn(Transaction.newBuilder().setId(ByteString.copyFromUtf8("t1")).build()); session.prepareReadWriteTransaction(); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("invalidated"); ctx.read("Dummy", KeySet.all(), Arrays.asList("C")); }
.prepareReadWriteTransaction(); doAnswer( new Answer<Void>() { .prepareReadWriteTransaction(); when(client.createSession(db)).thenReturn(mockSession1).thenReturn(mockSession2); 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();
@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(); }
@Override public void prepareReadWriteTransaction() { markUsed(); delegate.prepareReadWriteTransaction(); }