3232import com .google .cloud .GrpcTransportOptions .ExecutorFactory ;
3333import com .google .cloud .spanner .SessionPool .Clock ;
3434import com .google .cloud .spanner .SessionPool .PooledSession ;
35- import com .google .common .util .concurrent .ListenableFuture ;
3635import com .google .common .util .concurrent .Uninterruptibles ;
3736import java .util .ArrayList ;
3837import java .util .Arrays ;
@@ -114,118 +113,7 @@ public void poolClosure() throws Exception {
114113 pool = createPool ();
115114 pool .closeAsync ().get ();
116115 }
117-
118- @ Test
119- public void poolClosureFailsPendingReadWaiters () throws Exception {
120- final CountDownLatch insideCreation = new CountDownLatch (1 );
121- final CountDownLatch releaseCreation = new CountDownLatch (1 );
122- when (client .createSession (db ))
123- .thenReturn (mock (Session .class ))
124- .thenAnswer (new Answer <Session >() {
125- @ Override
126- public Session answer (InvocationOnMock invocation ) throws Throwable {
127- insideCreation .countDown ();
128- releaseCreation .await ();
129- return mock (Session .class );
130- }
131- });
132- pool = createPool ();
133- pool .getReadSession ();
134- AtomicBoolean failed = new AtomicBoolean (false );
135- CountDownLatch latch = new CountDownLatch (1 );
136- getSessionAsync (latch , failed );
137- insideCreation .await ();
138- pool .closeAsync ();
139- releaseCreation .countDown ();
140- latch .await ();
141- assertThat (failed .get ()).isTrue ();
142- }
143-
144- @ Test
145- public void poolClosureFailsPendingWriteWaiters () throws Exception {
146- final CountDownLatch insideCreation = new CountDownLatch (1 );
147- final CountDownLatch releaseCreation = new CountDownLatch (1 );
148- when (client .createSession (db ))
149- .thenReturn (mock (Session .class ))
150- .thenAnswer (new Answer <Session >() {
151- @ Override
152- public Session answer (InvocationOnMock invocation ) throws Throwable {
153- insideCreation .countDown ();
154- releaseCreation .await ();
155- return mock (Session .class );
156- }
157- });
158- pool = createPool ();
159- pool .getReadSession ();
160- AtomicBoolean failed = new AtomicBoolean (false );
161- CountDownLatch latch = new CountDownLatch (1 );
162- getReadWriteSessionAsync (latch , failed );
163- insideCreation .await ();
164- pool .closeAsync ();
165- releaseCreation .countDown ();
166- latch .await ();
167- assertThat (failed .get ()).isTrue ();
168- }
169116
170- @ Test
171- public void poolClosesEvenIfCreationFails () throws Exception {
172- final CountDownLatch insideCreation = new CountDownLatch (1 );
173- final CountDownLatch releaseCreation = new CountDownLatch (1 );
174- when (client .createSession (db ))
175- .thenAnswer (new Answer <Session >() {
176- @ Override
177- public Session answer (InvocationOnMock invocation ) throws Throwable {
178- insideCreation .countDown ();
179- releaseCreation .await ();
180- throw SpannerExceptionFactory .newSpannerException (new RuntimeException ());
181- }
182- });
183- pool = createPool ();
184- AtomicBoolean failed = new AtomicBoolean (false );
185- CountDownLatch latch = new CountDownLatch (1 );
186- getSessionAsync (latch , failed );
187- insideCreation .await ();
188- ListenableFuture <Void > f = pool .closeAsync ();
189- releaseCreation .countDown ();
190- f .get ();
191- assertThat (f .isDone ()).isTrue ();
192- }
193-
194- @ Test
195- public void poolClosesEvenIfPreparationFails () throws Exception {
196- Session session = mock (Session .class );
197- when (client .createSession (db )).thenReturn (session );
198- final CountDownLatch insidePrepare = new CountDownLatch (1 );
199- final CountDownLatch releasePrepare = new CountDownLatch (1 );
200- doAnswer (new Answer <Session >() {
201- @ Override
202- public Session answer (InvocationOnMock invocation ) throws Throwable {
203- insidePrepare .countDown ();
204- releasePrepare .await ();
205- throw SpannerExceptionFactory .newSpannerException (new RuntimeException ());
206- }
207- }).when (session ).prepareReadWriteTransaction ();
208- pool = createPool ();
209- AtomicBoolean failed = new AtomicBoolean (false );
210- CountDownLatch latch = new CountDownLatch (1 );
211- getReadWriteSessionAsync (latch , failed );
212- insidePrepare .await ();
213- ListenableFuture <Void > f = pool .closeAsync ();
214- releasePrepare .countDown ();
215- f .get ();
216- assertThat (f .isDone ()).isTrue ();
217- }
218-
219- @ Test
220- public void poolClosureFailsNewRequests () throws Exception {
221- when (client .createSession (db )).thenReturn (mock (Session .class ));
222- pool = createPool ();
223- pool .getReadSession ();
224- pool .closeAsync ();
225- expectedException .expect (IllegalStateException .class );
226- pool .getReadSession ();
227- }
228-
229117 @ Test
230118 public void atMostMaxSessionsCreated () {
231119 AtomicBoolean failed = new AtomicBoolean (false );
@@ -354,35 +242,6 @@ public Void answer(InvocationOnMock arg0) throws Throwable {
354242 readSession .close ();
355243 writeSession .close ();
356244 }
357-
358- @ Test
359- public void getReadSessionFallsBackToWritePreparedSession () throws Exception {
360- Session mockSession1 = mock (Session .class );
361- final CountDownLatch prepareLatch = new CountDownLatch (2 );
362- doAnswer (
363- new Answer <Void >() {
364- @ Override
365- public Void answer (InvocationOnMock arg0 ) throws Throwable {
366- prepareLatch .countDown ();
367- return null ;
368- }
369- })
370- .when (mockSession1 )
371- .prepareReadWriteTransaction ();
372- when (client .createSession (db )).thenReturn (mockSession1 );
373- options =
374- SessionPoolOptions .newBuilder ()
375- .setMinSessions (minSessions )
376- .setMaxSessions (1 )
377- .setWriteSessionsFraction (1.0f )
378- .build ();
379- pool = createPool ();
380- pool .getReadWriteSession ().close ();
381- prepareLatch .await ();
382- // This session should also be write prepared.
383- PooledSession readSession = (PooledSession ) pool .getReadSession ();
384- verify (readSession .delegate , times (2 )).prepareReadWriteTransaction ();
385- }
386245
387246 @ Test
388247 public void failOnPoolExhaustion () {
@@ -403,18 +262,6 @@ public void failOnPoolExhaustion() {
403262 session1 .close ();
404263 }
405264
406- @ Test
407- public void poolWorksWhenSessionNotFound () {
408- Session mockSession1 = mock (Session .class );
409- Session mockSession2 = mock (Session .class );
410- doThrow (
411- SpannerExceptionFactory .newSpannerException (ErrorCode .NOT_FOUND , "Session not found" )).
412- when (mockSession1 ).prepareReadWriteTransaction ();
413- when (client .createSession (db )).thenReturn (mockSession1 ).thenReturn (mockSession2 );
414- pool = createPool ();
415- assertThat (((PooledSession ) pool .getReadWriteSession ()).delegate ).isEqualTo (mockSession2 );
416- }
417-
418265 @ Test
419266 public void idleSessionCleanup () throws Exception {
420267 options =
@@ -519,9 +366,6 @@ public void run() {
519366 try (Session session = pool .getReadSession ()) {
520367 failed .compareAndSet (false , session == null );
521368 Uninterruptibles .sleepUninterruptibly (10 , TimeUnit .MILLISECONDS );
522- } catch (SpannerException e ) {
523- failed .compareAndSet (false , true );
524- } finally {
525369 latch .countDown ();
526370 }
527371 }
@@ -537,9 +381,6 @@ public void run() {
537381 try (Session session = pool .getReadWriteSession ()) {
538382 failed .compareAndSet (false , session == null );
539383 Uninterruptibles .sleepUninterruptibly (2 , TimeUnit .MILLISECONDS );
540- } catch (SpannerException e ) {
541- failed .compareAndSet (false , true );
542- } finally {
543384 latch .countDown ();
544385 }
545386 }
0 commit comments