Skip to content

Commit 1b70be0

Browse files
authored
test: fail the StatementTimeoutTest if any of the tests are slow (#3686)
Adds a global timeout that is applied to each of the tests in StatementTimeoutTest. This ensures that if any of the tests are slow, the test case will fail. Also cleans up some warnings and removes some old-style assertions.
1 parent d782ae2 commit 1b70be0

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/StatementTimeoutTest.java

+21-16
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616

1717
package com.google.cloud.spanner.connection;
1818

19-
import static org.hamcrest.CoreMatchers.equalTo;
20-
import static org.hamcrest.CoreMatchers.is;
21-
import static org.hamcrest.MatcherAssert.assertThat;
2219
import static org.junit.Assert.assertEquals;
2320
import static org.junit.Assert.assertNotNull;
2421
import static org.junit.Assert.assertThrows;
@@ -59,7 +56,9 @@
5956
import java.util.concurrent.TimeoutException;
6057
import org.junit.After;
6158
import org.junit.Before;
59+
import org.junit.Rule;
6260
import org.junit.Test;
61+
import org.junit.rules.Timeout;
6362
import org.junit.runner.RunWith;
6463
import org.junit.runners.Parameterized;
6564
import org.junit.runners.Parameterized.Parameter;
@@ -90,12 +89,18 @@ public class StatementTimeoutTest extends AbstractMockServerTest {
9089
*/
9190
private static final int TIMEOUT_FOR_SLOW_STATEMENTS = 50;
9291

92+
// Set a global timeout to ensure that tests that freeze the mock serer fail within a reasonable
93+
// amount of time if they misbehave.
94+
@Rule public Timeout globalTimeout = Timeout.seconds(10);
95+
9396
@Parameters(name = "statementExecutorType = {0}")
9497
public static Object[] parameters() {
9598
return StatementExecutorType.values();
9699
}
97100

98-
@Parameter public StatementExecutorType statementExecutorType;
101+
@SuppressWarnings("ClassEscapesDefinedScope")
102+
@Parameter
103+
public StatementExecutorType statementExecutorType;
99104

100105
protected ITConnection createConnection() {
101106
ConnectionOptions options =
@@ -423,7 +428,7 @@ public void testTimeoutExceptionReadWriteTransactionalSlowCommit() {
423428
}
424429

425430
connection.setStatementTimeout(TIMEOUT_FOR_SLOW_STATEMENTS, TimeUnit.MILLISECONDS);
426-
SpannerException e = assertThrows(SpannerException.class, () -> connection.commit());
431+
SpannerException e = assertThrows(SpannerException.class, connection::commit);
427432
assertEquals(ErrorCode.DEADLINE_EXCEEDED, e.getErrorCode());
428433
}
429434
}
@@ -613,10 +618,10 @@ private void waitForDdlRequestOnServer() {
613618
try {
614619
Stopwatch watch = Stopwatch.createStarted();
615620
while (Collections2.filter(
616-
mockDatabaseAdmin.getRequests(),
617-
input -> input.getClass().equals(UpdateDatabaseDdlRequest.class))
618-
.size()
619-
== 0) {
621+
mockDatabaseAdmin.getRequests(),
622+
input -> input.getClass().equals(UpdateDatabaseDdlRequest.class))
623+
.isEmpty()) {
624+
//noinspection BusyWait
620625
Thread.sleep(1L);
621626
if (watch.elapsed(TimeUnit.MILLISECONDS) > EXECUTION_TIME_SLOW_STATEMENT) {
622627
throw new TimeoutException("Timeout while waiting for DDL request");
@@ -680,10 +685,10 @@ public void testCancelReadOnlyAutocommitMultipleStatements() {
680685
connection.cancel();
681686
});
682687

683-
SpannerException e =
688+
SpannerException exception =
684689
assertThrows(
685690
SpannerException.class, () -> connection.executeQuery(SELECT_RANDOM_STATEMENT));
686-
assertThat(e.getErrorCode(), is(equalTo(ErrorCode.CANCELLED)));
691+
assertEquals(ErrorCode.CANCELLED, exception.getErrorCode());
687692

688693
mockSpanner.removeAllExecutionTimes();
689694
connection.setStatementTimeout(TIMEOUT_FOR_FAST_STATEMENTS, TimeUnit.MILLISECONDS);
@@ -1000,7 +1005,7 @@ public void testCancelDdlBatch() {
10001005
waitForDdlRequestOnServer();
10011006
connection.cancel();
10021007
});
1003-
SpannerException e = assertThrows(SpannerException.class, () -> connection.runBatch());
1008+
SpannerException e = assertThrows(SpannerException.class, connection::runBatch);
10041009
assertEquals(ErrorCode.CANCELLED, e.getErrorCode());
10051010
} finally {
10061011
executor.shutdownNow();
@@ -1078,10 +1083,10 @@ public void testTimeoutExceptionDdlBatch() {
10781083
connection.startBatchDdl();
10791084
connection.setStatementTimeout(TIMEOUT_FOR_SLOW_STATEMENTS, TimeUnit.MILLISECONDS);
10801085

1081-
// the following statement will NOT timeout as the statement is only buffered locally
1086+
// the following statement will NOT time out as the statement is only buffered locally
10821087
connection.execute(Statement.of(SLOW_DDL));
1083-
// the runBatch() statement sends the statement to the server and should timeout
1084-
SpannerException e = assertThrows(SpannerException.class, () -> connection.runBatch());
1088+
// the runBatch() statement sends the statement to the server and should time out
1089+
SpannerException e = assertThrows(SpannerException.class, connection::runBatch);
10851090
assertEquals(ErrorCode.DEADLINE_EXCEEDED, e.getErrorCode());
10861091
}
10871092
}
@@ -1098,7 +1103,7 @@ public void testTimeoutExceptionDdlBatchMultipleStatements() {
10981103
for (int i = 0; i < 2; i++) {
10991104
connection.startBatchDdl();
11001105
connection.execute(Statement.of(SLOW_DDL));
1101-
SpannerException e = assertThrows(SpannerException.class, () -> connection.runBatch());
1106+
SpannerException e = assertThrows(SpannerException.class, connection::runBatch);
11021107
assertEquals(ErrorCode.DEADLINE_EXCEEDED, e.getErrorCode());
11031108
}
11041109
// try to do a new DDL statement that is fast.

0 commit comments

Comments
 (0)