2626import static com .google .cloud .spanner .SpannerApiFutures .get ;
2727import static com .google .common .truth .Truth .assertThat ;
2828import static org .junit .Assert .assertEquals ;
29+ import static org .mockito .ArgumentMatchers .any ;
30+ import static org .mockito .ArgumentMatchers .eq ;
2931import static org .junit .Assert .assertFalse ;
3032import static org .junit .Assert .assertNotEquals ;
3133import static org .junit .Assert .assertNotNull ;
@@ -5653,25 +5655,32 @@ public void testdbIdFromClientId() {
56535655 @ Test
56545656 public void testrunWithSessionRetry_withRequestId () {
56555657 // Tests that DatabaseClientImpl.runWithSessionRetry correctly returns a XGoogSpannerRequestId
5656- // and correctly increases its nthRequest ordinal number and that attempts stay at 1.
5658+ // and correctly increases its nthRequest ordinal number and that attempts stay at 1, given
5659+ // a fresh session returned on SessionNotFoundException.
56575660 SessionPool pool = mock (SessionPool .class );
56585661 PooledSessionFuture sessionFut = mock (PooledSessionFuture .class );
56595662 when (pool .getSession ()).thenReturn (sessionFut );
5660- // TODO:(@olavloite) to kindly help with resolving this mocking that's failing.
5661- // when(pool.getPooledSessionReplacementHandler()).thenReturn(pool.new
5662- // PooledSessionReplacementHandler());
5663- TransactionOption option = mock (TransactionOption .class );
5663+ SessionPool .PooledSession pooledSession = mock (SessionPool .PooledSession .class );
5664+ when (sessionFut .get ()).thenReturn (pooledSession );
5665+ SessionPool .PooledSessionReplacementHandler sessionReplacementHandler = mock (SessionPool .PooledSessionReplacementHandler .class );
5666+ when (pool .getPooledSessionReplacementHandler ()).thenReturn (sessionReplacementHandler );
5667+ when (sessionReplacementHandler .replaceSession (any (), any ())).thenReturn (sessionFut );
56645668 DatabaseClientImpl client = new DatabaseClientImpl (pool , mock (TraceWrapper .class ));
56655669
5666- // 1. Run with no fail has a single attempt.
5670+ // 1. Run with no fail runs a single attempt.
5671+ final AtomicInteger nCalls = new AtomicInteger (0 );
56675672 client .runWithSessionRetry (
56685673 (session , reqId ) -> {
56695674 assertEquals (reqId .getAttempt (), 1 );
5675+ nCalls .incrementAndGet ();
56705676 return 1 ;
56715677 });
5678+ assertEquals (nCalls .get (), 1 );
5679+
5680+ // Reset the call counter.
5681+ nCalls .set (0 );
56725682
5673- // 2. Run with SessionNotFoundException.
5674- final AtomicInteger i = new AtomicInteger (0 );
5683+ // 2. Run with SessionNotFoundException and ensure that a fresh requestId is returned each time.
56755684 SessionNotFoundException excSessionNotFound =
56765685 SpannerExceptionFactoryTest .newSessionNotFoundException (
56775686 "projects/p/instances/i/databases/d/sessions/s" );
@@ -5687,11 +5696,13 @@ public void testrunWithSessionRetry_withRequestId() {
56875696 // a fresh requestId is generated.
56885697 assertEquals (reqId .getAttempt (), 1 );
56895698
5690- if (i .addAndGet (1 ) < 4 ) {
5699+ if (nCalls .addAndGet (1 ) < 4 ) {
56915700 throw excSessionNotFound ;
56925701 }
56935702
56945703 return 1 ;
56955704 });
5705+
5706+ assertEquals (nCalls .get (), 4 );
56965707 }
56975708}
0 commit comments