26
26
import static com .google .cloud .spanner .SpannerApiFutures .get ;
27
27
import static com .google .common .truth .Truth .assertThat ;
28
28
import static org .junit .Assert .assertEquals ;
29
+ import static org .mockito .ArgumentMatchers .any ;
30
+ import static org .mockito .ArgumentMatchers .eq ;
29
31
import static org .junit .Assert .assertFalse ;
30
32
import static org .junit .Assert .assertNotEquals ;
31
33
import static org .junit .Assert .assertNotNull ;
@@ -5653,25 +5655,32 @@ public void testdbIdFromClientId() {
5653
5655
@ Test
5654
5656
public void testrunWithSessionRetry_withRequestId () {
5655
5657
// 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.
5657
5660
SessionPool pool = mock (SessionPool .class );
5658
5661
PooledSessionFuture sessionFut = mock (PooledSessionFuture .class );
5659
5662
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 );
5664
5668
DatabaseClientImpl client = new DatabaseClientImpl (pool , mock (TraceWrapper .class ));
5665
5669
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 );
5667
5672
client .runWithSessionRetry (
5668
5673
(session , reqId ) -> {
5669
5674
assertEquals (reqId .getAttempt (), 1 );
5675
+ nCalls .incrementAndGet ();
5670
5676
return 1 ;
5671
5677
});
5678
+ assertEquals (nCalls .get (), 1 );
5679
+
5680
+ // Reset the call counter.
5681
+ nCalls .set (0 );
5672
5682
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.
5675
5684
SessionNotFoundException excSessionNotFound =
5676
5685
SpannerExceptionFactoryTest .newSessionNotFoundException (
5677
5686
"projects/p/instances/i/databases/d/sessions/s" );
@@ -5687,11 +5696,13 @@ public void testrunWithSessionRetry_withRequestId() {
5687
5696
// a fresh requestId is generated.
5688
5697
assertEquals (reqId .getAttempt (), 1 );
5689
5698
5690
- if (i .addAndGet (1 ) < 4 ) {
5699
+ if (nCalls .addAndGet (1 ) < 4 ) {
5691
5700
throw excSessionNotFound ;
5692
5701
}
5693
5702
5694
5703
return 1 ;
5695
5704
});
5705
+
5706
+ assertEquals (nCalls .get (), 4 );
5696
5707
}
5697
5708
}
0 commit comments