|
33 | 33 | import com.google.cloud.spanner.AsyncResultSet.CursorState; |
34 | 34 | import com.google.cloud.spanner.AsyncResultSet.ReadyCallback; |
35 | 35 | import com.google.common.base.Function; |
| 36 | +import com.google.common.base.Stopwatch; |
36 | 37 | import com.google.common.collect.Range; |
37 | 38 | import com.google.protobuf.ByteString; |
38 | 39 | import com.google.protobuf.Value; |
|
50 | 51 | import java.util.concurrent.atomic.AtomicBoolean; |
51 | 52 | import java.util.concurrent.atomic.AtomicInteger; |
52 | 53 | import org.junit.Before; |
| 54 | +import org.junit.Rule; |
53 | 55 | import org.junit.Test; |
| 56 | +import org.junit.rules.Timeout; |
54 | 57 | import org.junit.runner.RunWith; |
55 | 58 | import org.junit.runners.JUnit4; |
56 | 59 | import org.mockito.Mockito; |
|
59 | 62 |
|
60 | 63 | @RunWith(JUnit4.class) |
61 | 64 | public class AsyncResultSetImplTest { |
| 65 | + @Rule public final Timeout globalTimeout = Timeout.seconds(60); |
| 66 | + |
62 | 67 | private ExecutorProvider mockedProvider; |
63 | 68 | private ExecutorProvider simpleProvider; |
64 | 69 |
|
@@ -198,7 +203,7 @@ public void withCallback() throws InterruptedException { |
198 | 203 | return CallbackResponse.CONTINUE; |
199 | 204 | }); |
200 | 205 | } |
201 | | - finishedLatch.await(); |
| 206 | + assertThat(finishedLatch.await(10, TimeUnit.SECONDS)).isTrue(); |
202 | 207 | // There should be between 1 and 5 callbacks, depending on the timing of the threads. |
203 | 208 | // Normally, there should be just 1 callback. |
204 | 209 | assertThat(callbackCounter.get()).isIn(Range.closed(1, 5)); |
@@ -228,7 +233,8 @@ public void callbackReceivesError() throws InterruptedException { |
228 | 233 | return CallbackResponse.DONE; |
229 | 234 | }); |
230 | 235 | } |
231 | | - Exception e = receivedErr.take(); |
| 236 | + Exception e = receivedErr.poll(10, TimeUnit.SECONDS); |
| 237 | + assertThat(e).isNotNull(); |
232 | 238 | assertThat(e).isInstanceOf(SpannerException.class); |
233 | 239 | SpannerException se = (SpannerException) e; |
234 | 240 | assertThat(se.getErrorCode()).isEqualTo(ErrorCode.INVALID_ARGUMENT); |
@@ -263,7 +269,8 @@ public void callbackReceivesErrorHalfwayThrough() throws InterruptedException { |
263 | 269 | return CallbackResponse.DONE; |
264 | 270 | }); |
265 | 271 | } |
266 | | - Exception e = receivedErr.take(); |
| 272 | + Exception e = receivedErr.poll(10, TimeUnit.SECONDS); |
| 273 | + assertThat(e).isNotNull(); |
267 | 274 | assertThat(e).isInstanceOf(SpannerException.class); |
268 | 275 | SpannerException se = (SpannerException) e; |
269 | 276 | assertThat(se.getErrorCode()).isEqualTo(ErrorCode.INVALID_ARGUMENT); |
@@ -300,8 +307,12 @@ public void pauseResume() throws InterruptedException { |
300 | 307 | return CallbackResponse.DONE; |
301 | 308 | }); |
302 | 309 | int rowCounter = 0; |
| 310 | + Stopwatch stopwatch = Stopwatch.createStarted(); |
303 | 311 | while (!finished.get()) { |
304 | | - Object o = queue.poll(1L, TimeUnit.MILLISECONDS); |
| 312 | + if (stopwatch.elapsed(TimeUnit.SECONDS) > 10) { |
| 313 | + throw new RuntimeException("Test timed out waiting for finished"); |
| 314 | + } |
| 315 | + Object o = queue.poll(10L, TimeUnit.MILLISECONDS); |
305 | 316 | if (o != null) { |
306 | 317 | rowCounter++; |
307 | 318 | } |
@@ -359,8 +370,12 @@ public Boolean answer(InvocationOnMock invocation) throws Throwable { |
359 | 370 | } |
360 | 371 | }); |
361 | 372 | int rowCounter = 0; |
| 373 | + Stopwatch stopwatch = Stopwatch.createStarted(); |
362 | 374 | while (!callbackResult.isDone()) { |
363 | | - Object o = queue.poll(1L, TimeUnit.MILLISECONDS); |
| 375 | + if (stopwatch.elapsed(TimeUnit.SECONDS) > 10) { |
| 376 | + throw new RuntimeException("Test timed out waiting for callbackResult"); |
| 377 | + } |
| 378 | + Object o = queue.poll(10L, TimeUnit.MILLISECONDS); |
364 | 379 | if (o != null) { |
365 | 380 | rowCounter++; |
366 | 381 | } |
@@ -453,8 +468,12 @@ public void cancel() throws InterruptedException { |
453 | 468 | return CallbackResponse.DONE; |
454 | 469 | }); |
455 | 470 | int rowCounter = 0; |
| 471 | + Stopwatch stopwatch = Stopwatch.createStarted(); |
456 | 472 | while (!finished.get()) { |
457 | | - Object o = queue.poll(1L, TimeUnit.MILLISECONDS); |
| 473 | + if (stopwatch.elapsed(TimeUnit.SECONDS) > 10) { |
| 474 | + throw new RuntimeException("Test timed out waiting for finished"); |
| 475 | + } |
| 476 | + Object o = queue.poll(10L, TimeUnit.MILLISECONDS); |
458 | 477 | if (o != null) { |
459 | 478 | rowCounter++; |
460 | 479 | } |
|
0 commit comments