Skip to content

Commit b5a4ebd

Browse files
authored
test: ignore if the mock server failed to return a row (#3335)
The test uses a trick in the mock server, where the mock server is requested to freeze after returning the first row. However, when the mock server adds the first row to the stream, it is not guaranteed to be readable for the client, which again causes the test to hang on the ResultSet#next() call. The gRPC libraries then execute keep-alive requests to keep the TCP connection alive while waiting for data from the mock server, which will never come. This caused the query to eventually fail with a RESOURCE_EXHAUSTED error. The tests work around this issue by just ignoring the case when the mock server fails to return the first row, as it is something that only very sporadically happens.
1 parent b3e2b0f commit b5a4ebd

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
import java.util.concurrent.TimeUnit;
4040
import java.util.stream.Collectors;
4141
import org.junit.After;
42+
import org.junit.AfterClass;
43+
import org.junit.BeforeClass;
4244
import org.junit.Test;
4345
import org.junit.runner.RunWith;
4446
import org.junit.runners.JUnit4;
@@ -59,6 +61,16 @@ Spanner createSpanner() {
5961
.getService();
6062
}
6163

64+
@BeforeClass
65+
public static void setWatchdogTimeout() {
66+
System.setProperty("com.google.cloud.spanner.watchdogTimeoutSeconds", "1");
67+
}
68+
69+
@AfterClass
70+
public static void clearWatchdogTimeout() {
71+
System.clearProperty("com.google.cloud.spanner.watchdogTimeoutSeconds");
72+
}
73+
6274
@After
6375
public void cleanup() {
6476
mockSpanner.unfreeze();
@@ -75,7 +87,13 @@ public void testBatchClient_closedSpannerWithOpenResultSet_streamsAreCancelled()
7587
client.batchReadOnlyTransaction(TimestampBound.strong());
7688
ResultSet resultSet = transaction.executeQuery(SELECT_RANDOM_STATEMENT)) {
7789
mockSpanner.freezeAfterReturningNumRows(1);
78-
assertTrue(resultSet.next());
90+
// This can sometimes fail, as the mock server may not always actually return the first row.
91+
try {
92+
assertTrue(resultSet.next());
93+
} catch (SpannerException exception) {
94+
assertEquals(ErrorCode.DEADLINE_EXCEEDED, exception.getErrorCode());
95+
return;
96+
}
7997
((SpannerImpl) spanner).close(1, TimeUnit.MILLISECONDS);
8098
// This should return an error as the stream is cancelled.
8199
SpannerException exception = assertThrows(SpannerException.class, resultSet::next);
@@ -93,7 +111,13 @@ public void testNormalDatabaseClient_closedSpannerWithOpenResultSet_sessionsAreD
93111
try (ReadOnlyTransaction transaction = client.readOnlyTransaction(TimestampBound.strong());
94112
ResultSet resultSet = transaction.executeQuery(SELECT_RANDOM_STATEMENT)) {
95113
mockSpanner.freezeAfterReturningNumRows(1);
96-
assertTrue(resultSet.next());
114+
// This can sometimes fail, as the mock server may not always actually return the first row.
115+
try {
116+
assertTrue(resultSet.next());
117+
} catch (SpannerException exception) {
118+
assertEquals(ErrorCode.DEADLINE_EXCEEDED, exception.getErrorCode());
119+
return;
120+
}
97121
List<ExecuteSqlRequest> executeSqlRequests =
98122
mockSpanner.getRequestsOfType(ExecuteSqlRequest.class).stream()
99123
.filter(request -> request.getSql().equals(SELECT_RANDOM_STATEMENT.getSql()))

0 commit comments

Comments
 (0)