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