Skip to content

Commit 6958265

Browse files
committed
test: unflake StatementTimeoutTest
1 parent e62f5ab commit 6958265

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2302,6 +2302,7 @@ public void waitForRequestsToContain(Class<? extends AbstractMessage> type, long
23022302
throws InterruptedException, TimeoutException {
23032303
Stopwatch watch = Stopwatch.createStarted();
23042304
while (countRequestsOfType(type) == 0) {
2305+
//noinspection BusyWait
23052306
Thread.sleep(1L);
23062307
if (watch.elapsed(TimeUnit.MILLISECONDS) > timeoutMillis) {
23072308
throw new TimeoutException(

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import com.google.cloud.spanner.connection.ITAbstractSpannerTest.ITConnection;
3737
import com.google.cloud.spanner.connection.StatementExecutor.StatementExecutorType;
3838
import com.google.common.base.Stopwatch;
39-
import com.google.common.collect.Collections2;
4039
import com.google.longrunning.Operation;
4140
import com.google.protobuf.AbstractMessage;
4241
import com.google.protobuf.Any;
@@ -47,6 +46,9 @@
4746
import com.google.spanner.v1.ExecuteSqlRequest;
4847
import io.grpc.Status;
4948
import java.time.Duration;
49+
import java.util.ArrayList;
50+
import java.util.ConcurrentModificationException;
51+
import java.util.List;
5052
import java.util.concurrent.CountDownLatch;
5153
import java.util.concurrent.ExecutionException;
5254
import java.util.concurrent.ExecutorService;
@@ -617,20 +619,20 @@ static void waitForRequestsToContain(Class<? extends AbstractMessage> request) {
617619
private void waitForDdlRequestOnServer() {
618620
try {
619621
Stopwatch watch = Stopwatch.createStarted();
620-
while (Collections2.filter(
621-
mockDatabaseAdmin.getRequests(),
622-
input -> input.getClass().equals(UpdateDatabaseDdlRequest.class))
623-
.isEmpty()) {
622+
while (watch.elapsed(TimeUnit.MILLISECONDS) < EXECUTION_TIME_SLOW_STATEMENT) {
623+
try {
624+
List<AbstractMessage> requests = new ArrayList<>(mockDatabaseAdmin.getRequests());
625+
if (requests.stream().anyMatch(request -> request instanceof UpdateDatabaseDdlRequest)) {
626+
break;
627+
}
628+
} catch (ConcurrentModificationException ignore) {
629+
// Just ignore and retry.
630+
}
624631
//noinspection BusyWait
625632
Thread.sleep(1L);
626-
if (watch.elapsed(TimeUnit.MILLISECONDS) > EXECUTION_TIME_SLOW_STATEMENT) {
627-
throw new TimeoutException("Timeout while waiting for DDL request");
628-
}
629633
}
630634
} catch (InterruptedException e) {
631635
throw SpannerExceptionFactory.propagateInterrupt(e);
632-
} catch (TimeoutException e) {
633-
throw SpannerExceptionFactory.propagateTimeout(e);
634636
}
635637
}
636638

@@ -1010,6 +1012,7 @@ public void testCancelDdlBatch() {
10101012
} finally {
10111013
executor.shutdownNow();
10121014
}
1015+
connection.closeAsync();
10131016
}
10141017
}
10151018

@@ -1036,6 +1039,7 @@ public void testCancelDdlAutocommit() {
10361039
} finally {
10371040
executor.shutdownNow();
10381041
}
1042+
connection.closeAsync();
10391043
}
10401044
}
10411045

@@ -1049,6 +1053,8 @@ public void testTimeoutExceptionDdlAutocommit() {
10491053
SpannerException e =
10501054
assertThrows(SpannerException.class, () -> connection.execute(Statement.of(SLOW_DDL)));
10511055
assertEquals(ErrorCode.DEADLINE_EXCEEDED, e.getErrorCode());
1056+
1057+
connection.closeAsync();
10521058
}
10531059
}
10541060

0 commit comments

Comments
 (0)