Skip to content

Commit 17a4440

Browse files
authored
chore: unflake ITAsyncAPITest.invalidDatabase (#3257)
Fixes #3255
1 parent 7821b07 commit 17a4440

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITAsyncAPITest.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import java.util.concurrent.ExecutionException;
5959
import java.util.concurrent.ExecutorService;
6060
import java.util.concurrent.Executors;
61+
import java.util.concurrent.ThreadLocalRandom;
6162
import org.junit.AfterClass;
6263
import org.junit.Before;
6364
import org.junit.BeforeClass;
@@ -237,17 +238,24 @@ public void invalidDatabase() throws Exception {
237238
RemoteSpannerHelper helper = env.getTestHelper();
238239
DatabaseClient invalidClient =
239240
helper.getClient().getDatabaseClient(DatabaseId.of(helper.getInstanceId(), "invalid"));
240-
ApiFuture<Struct> row =
241-
invalidClient
242-
.singleUse(TimestampBound.strong())
243-
.readRowAsync(TABLE_NAME, Key.of("k99"), ALL_COLUMNS);
241+
Thread.sleep(ThreadLocalRandom.current().nextLong(100L));
244242
try {
243+
// The NOT_FOUND error can come from both the call to invalidClient.singleUse() as well as
244+
// from the call to row.get(), which is why both need to be inside the try block.
245+
ApiFuture<Struct> row =
246+
invalidClient
247+
.singleUse(TimestampBound.strong())
248+
.readRowAsync(TABLE_NAME, Key.of("k99"), ALL_COLUMNS);
245249
row.get();
246250
fail("missing expected exception");
247-
} catch (ExecutionException e) {
248-
assertThat(e.getCause()).isInstanceOf(SpannerException.class);
249-
SpannerException se = (SpannerException) e.getCause();
250-
assertThat(se.getErrorCode()).isEqualTo(ErrorCode.NOT_FOUND);
251+
} catch (ExecutionException | SpannerException thrownException) {
252+
SpannerException spannerException;
253+
if (thrownException instanceof ExecutionException) {
254+
spannerException = (SpannerException) thrownException.getCause();
255+
} else {
256+
spannerException = (SpannerException) thrownException;
257+
}
258+
assertEquals(ErrorCode.NOT_FOUND, spannerException.getErrorCode());
251259
}
252260
}
253261

0 commit comments

Comments
 (0)