Skip to content

Commit 1e9096a

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 1e9096a

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

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

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.google.cloud.spanner.AsyncResultSet.CursorState;
3434
import com.google.cloud.spanner.AsyncResultSet.ReadyCallback;
3535
import com.google.common.base.Function;
36+
import com.google.common.base.Stopwatch;
3637
import com.google.common.collect.Range;
3738
import com.google.protobuf.ByteString;
3839
import com.google.protobuf.Value;
@@ -50,7 +51,9 @@
5051
import java.util.concurrent.atomic.AtomicBoolean;
5152
import java.util.concurrent.atomic.AtomicInteger;
5253
import org.junit.Before;
54+
import org.junit.Rule;
5355
import org.junit.Test;
56+
import org.junit.rules.Timeout;
5457
import org.junit.runner.RunWith;
5558
import org.junit.runners.JUnit4;
5659
import org.mockito.Mockito;
@@ -59,6 +62,8 @@
5962

6063
@RunWith(JUnit4.class)
6164
public class AsyncResultSetImplTest {
65+
@Rule public final Timeout globalTimeout = Timeout.seconds(60);
66+
6267
private ExecutorProvider mockedProvider;
6368
private ExecutorProvider simpleProvider;
6469

@@ -198,7 +203,7 @@ public void withCallback() throws InterruptedException {
198203
return CallbackResponse.CONTINUE;
199204
});
200205
}
201-
finishedLatch.await();
206+
assertThat(finishedLatch.await(10, TimeUnit.SECONDS)).isTrue();
202207
// There should be between 1 and 5 callbacks, depending on the timing of the threads.
203208
// Normally, there should be just 1 callback.
204209
assertThat(callbackCounter.get()).isIn(Range.closed(1, 5));
@@ -228,7 +233,8 @@ public void callbackReceivesError() throws InterruptedException {
228233
return CallbackResponse.DONE;
229234
});
230235
}
231-
Exception e = receivedErr.take();
236+
Exception e = receivedErr.poll(10, TimeUnit.SECONDS);
237+
assertThat(e).isNotNull();
232238
assertThat(e).isInstanceOf(SpannerException.class);
233239
SpannerException se = (SpannerException) e;
234240
assertThat(se.getErrorCode()).isEqualTo(ErrorCode.INVALID_ARGUMENT);
@@ -263,7 +269,8 @@ public void callbackReceivesErrorHalfwayThrough() throws InterruptedException {
263269
return CallbackResponse.DONE;
264270
});
265271
}
266-
Exception e = receivedErr.take();
272+
Exception e = receivedErr.poll(10, TimeUnit.SECONDS);
273+
assertThat(e).isNotNull();
267274
assertThat(e).isInstanceOf(SpannerException.class);
268275
SpannerException se = (SpannerException) e;
269276
assertThat(se.getErrorCode()).isEqualTo(ErrorCode.INVALID_ARGUMENT);
@@ -300,8 +307,12 @@ public void pauseResume() throws InterruptedException {
300307
return CallbackResponse.DONE;
301308
});
302309
int rowCounter = 0;
310+
Stopwatch stopwatch = Stopwatch.createStarted();
303311
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);
305316
if (o != null) {
306317
rowCounter++;
307318
}
@@ -359,8 +370,12 @@ public Boolean answer(InvocationOnMock invocation) throws Throwable {
359370
}
360371
});
361372
int rowCounter = 0;
373+
Stopwatch stopwatch = Stopwatch.createStarted();
362374
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);
364379
if (o != null) {
365380
rowCounter++;
366381
}
@@ -453,8 +468,12 @@ public void cancel() throws InterruptedException {
453468
return CallbackResponse.DONE;
454469
});
455470
int rowCounter = 0;
471+
Stopwatch stopwatch = Stopwatch.createStarted();
456472
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);
458477
if (o != null) {
459478
rowCounter++;
460479
}

0 commit comments

Comments
 (0)