3434import static org .junit .Assert .assertTrue ;
3535import static org .junit .Assume .assumeFalse ;
3636import static org .junit .Assume .assumeTrue ;
37+ import static org .mockito .ArgumentMatchers .any ;
3738import static org .mockito .Mockito .mock ;
3839import static org .mockito .Mockito .verify ;
3940import static org .mockito .Mockito .when ;
@@ -5653,25 +5654,33 @@ public void testdbIdFromClientId() {
56535654 @ Test
56545655 public void testrunWithSessionRetry_withRequestId () {
56555656 // Tests that DatabaseClientImpl.runWithSessionRetry correctly returns a XGoogSpannerRequestId
5656- // and correctly increases its nthRequest ordinal number and that attempts stay at 1.
5657+ // and correctly increases its nthRequest ordinal number and that attempts stay at 1, given
5658+ // a fresh session returned on SessionNotFoundException.
56575659 SessionPool pool = mock (SessionPool .class );
56585660 PooledSessionFuture sessionFut = mock (PooledSessionFuture .class );
56595661 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 );
5662+ SessionPool .PooledSession pooledSession = mock (SessionPool .PooledSession .class );
5663+ when (sessionFut .get ()).thenReturn (pooledSession );
5664+ SessionPool .PooledSessionReplacementHandler sessionReplacementHandler =
5665+ 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