Skip to content

Commit e7809ee

Browse files
committed
test(spanner): add timeouts to AsyncResultSetImplTest
The AsyncResultSetImplTest seems to be hanging during some builds. This adds timeouts to some of the test methods, and a global timeout for the tests to prevent it from getting stuck, and instead throwing an error.
1 parent 8650bc6 commit e7809ee

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

java-spanner/google-cloud-spanner/src/test/java/com/google/cloud/spanner/AsyncResultSetImplTest.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@
5050
import java.util.concurrent.atomic.AtomicBoolean;
5151
import java.util.concurrent.atomic.AtomicInteger;
5252
import org.junit.Before;
53+
import org.junit.Rule;
5354
import org.junit.Test;
55+
import org.junit.rules.Timeout;
5456
import org.junit.runner.RunWith;
5557
import org.junit.runners.JUnit4;
5658
import org.mockito.Mockito;
@@ -59,6 +61,8 @@
5961

6062
@RunWith(JUnit4.class)
6163
public class AsyncResultSetImplTest {
64+
@Rule public final Timeout globalTimeout = Timeout.seconds(60);
65+
6266
private ExecutorProvider mockedProvider;
6367
private ExecutorProvider simpleProvider;
6468

@@ -198,7 +202,7 @@ public void withCallback() throws InterruptedException {
198202
return CallbackResponse.CONTINUE;
199203
});
200204
}
201-
finishedLatch.await();
205+
assertThat(finishedLatch.await(10, TimeUnit.SECONDS)).isTrue();
202206
// There should be between 1 and 5 callbacks, depending on the timing of the threads.
203207
// Normally, there should be just 1 callback.
204208
assertThat(callbackCounter.get()).isIn(Range.closed(1, 5));
@@ -228,7 +232,8 @@ public void callbackReceivesError() throws InterruptedException {
228232
return CallbackResponse.DONE;
229233
});
230234
}
231-
Exception e = receivedErr.take();
235+
Exception e = receivedErr.poll(10, TimeUnit.SECONDS);
236+
assertThat(e).isNotNull();
232237
assertThat(e).isInstanceOf(SpannerException.class);
233238
SpannerException se = (SpannerException) e;
234239
assertThat(se.getErrorCode()).isEqualTo(ErrorCode.INVALID_ARGUMENT);
@@ -263,7 +268,8 @@ public void callbackReceivesErrorHalfwayThrough() throws InterruptedException {
263268
return CallbackResponse.DONE;
264269
});
265270
}
266-
Exception e = receivedErr.take();
271+
Exception e = receivedErr.poll(10, TimeUnit.SECONDS);
272+
assertThat(e).isNotNull();
267273
assertThat(e).isInstanceOf(SpannerException.class);
268274
SpannerException se = (SpannerException) e;
269275
assertThat(se.getErrorCode()).isEqualTo(ErrorCode.INVALID_ARGUMENT);
@@ -300,8 +306,12 @@ public void pauseResume() throws InterruptedException {
300306
return CallbackResponse.DONE;
301307
});
302308
int rowCounter = 0;
309+
long deadline = System.currentTimeMillis() + 10000; // 10 seconds
303310
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);
305315
if (o != null) {
306316
rowCounter++;
307317
}
@@ -359,8 +369,12 @@ public Boolean answer(InvocationOnMock invocation) throws Throwable {
359369
}
360370
});
361371
int rowCounter = 0;
372+
long deadline = System.currentTimeMillis() + 10000; // 10 seconds
362373
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);
364378
if (o != null) {
365379
rowCounter++;
366380
}
@@ -453,8 +467,12 @@ public void cancel() throws InterruptedException {
453467
return CallbackResponse.DONE;
454468
});
455469
int rowCounter = 0;
470+
long deadline = System.currentTimeMillis() + 10000; // 10 seconds
456471
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);
458476
if (o != null) {
459477
rowCounter++;
460478
}

0 commit comments

Comments
 (0)