Skip to content

test: fail the StatementTimeoutTest if any of the tests are slow #3686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@

package com.google.cloud.spanner.connection;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;
Expand Down Expand Up @@ -59,7 +56,9 @@
import java.util.concurrent.TimeoutException;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
Expand Down Expand Up @@ -90,12 +89,18 @@ public class StatementTimeoutTest extends AbstractMockServerTest {
*/
private static final int TIMEOUT_FOR_SLOW_STATEMENTS = 50;

// Set a global timeout to ensure that tests that freeze the mock serer fail within a reasonable
// amount of time if they misbehave.
@Rule public Timeout globalTimeout = Timeout.seconds(10);

@Parameters(name = "statementExecutorType = {0}")
public static Object[] parameters() {
return StatementExecutorType.values();
}

@Parameter public StatementExecutorType statementExecutorType;
@SuppressWarnings("ClassEscapesDefinedScope")
@Parameter
public StatementExecutorType statementExecutorType;

protected ITConnection createConnection() {
ConnectionOptions options =
Expand Down Expand Up @@ -423,7 +428,7 @@ public void testTimeoutExceptionReadWriteTransactionalSlowCommit() {
}

connection.setStatementTimeout(TIMEOUT_FOR_SLOW_STATEMENTS, TimeUnit.MILLISECONDS);
SpannerException e = assertThrows(SpannerException.class, () -> connection.commit());
SpannerException e = assertThrows(SpannerException.class, connection::commit);
assertEquals(ErrorCode.DEADLINE_EXCEEDED, e.getErrorCode());
}
}
Expand Down Expand Up @@ -613,10 +618,10 @@ private void waitForDdlRequestOnServer() {
try {
Stopwatch watch = Stopwatch.createStarted();
while (Collections2.filter(
mockDatabaseAdmin.getRequests(),
input -> input.getClass().equals(UpdateDatabaseDdlRequest.class))
.size()
== 0) {
mockDatabaseAdmin.getRequests(),
input -> input.getClass().equals(UpdateDatabaseDdlRequest.class))
.isEmpty()) {
//noinspection BusyWait
Thread.sleep(1L);
if (watch.elapsed(TimeUnit.MILLISECONDS) > EXECUTION_TIME_SLOW_STATEMENT) {
throw new TimeoutException("Timeout while waiting for DDL request");
Expand Down Expand Up @@ -680,10 +685,10 @@ public void testCancelReadOnlyAutocommitMultipleStatements() {
connection.cancel();
});

SpannerException e =
SpannerException exception =
assertThrows(
SpannerException.class, () -> connection.executeQuery(SELECT_RANDOM_STATEMENT));
assertThat(e.getErrorCode(), is(equalTo(ErrorCode.CANCELLED)));
assertEquals(ErrorCode.CANCELLED, exception.getErrorCode());

mockSpanner.removeAllExecutionTimes();
connection.setStatementTimeout(TIMEOUT_FOR_FAST_STATEMENTS, TimeUnit.MILLISECONDS);
Expand Down Expand Up @@ -1000,7 +1005,7 @@ public void testCancelDdlBatch() {
waitForDdlRequestOnServer();
connection.cancel();
});
SpannerException e = assertThrows(SpannerException.class, () -> connection.runBatch());
SpannerException e = assertThrows(SpannerException.class, connection::runBatch);
assertEquals(ErrorCode.CANCELLED, e.getErrorCode());
} finally {
executor.shutdownNow();
Expand Down Expand Up @@ -1078,10 +1083,10 @@ public void testTimeoutExceptionDdlBatch() {
connection.startBatchDdl();
connection.setStatementTimeout(TIMEOUT_FOR_SLOW_STATEMENTS, TimeUnit.MILLISECONDS);

// the following statement will NOT timeout as the statement is only buffered locally
// the following statement will NOT time out as the statement is only buffered locally
connection.execute(Statement.of(SLOW_DDL));
// the runBatch() statement sends the statement to the server and should timeout
SpannerException e = assertThrows(SpannerException.class, () -> connection.runBatch());
// the runBatch() statement sends the statement to the server and should time out
SpannerException e = assertThrows(SpannerException.class, connection::runBatch);
assertEquals(ErrorCode.DEADLINE_EXCEEDED, e.getErrorCode());
}
}
Expand All @@ -1098,7 +1103,7 @@ public void testTimeoutExceptionDdlBatchMultipleStatements() {
for (int i = 0; i < 2; i++) {
connection.startBatchDdl();
connection.execute(Statement.of(SLOW_DDL));
SpannerException e = assertThrows(SpannerException.class, () -> connection.runBatch());
SpannerException e = assertThrows(SpannerException.class, connection::runBatch);
assertEquals(ErrorCode.DEADLINE_EXCEEDED, e.getErrorCode());
}
// try to do a new DDL statement that is fast.
Expand Down
Loading