Skip to content

Commit b2ba1f3

Browse files
authored
chore(spanner): test fixes (#3639)
1 parent 52915cd commit b2ba1f3

File tree

2 files changed

+54
-7
lines changed

2 files changed

+54
-7
lines changed

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

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,51 @@ public void clearRequests() {
6060
@Test
6161
public void testAsyncRunner_doesNotReturnCommitTimestampBeforeCommit() {
6262
AsyncRunner runner = client().runAsync();
63-
IllegalStateException e =
64-
assertThrows(IllegalStateException.class, () -> runner.getCommitTimestamp());
65-
assertTrue(e.getMessage().contains("runAsync() has not yet been called"));
63+
if (isMultiplexedSessionsEnabledForRW()) {
64+
Throwable e = assertThrows(Throwable.class, () -> runner.getCommitTimestamp().get());
65+
// If the error occurs within the future, it gets wrapped in an ExecutionException.
66+
// This happens when DelayedAsyncRunner is invoked while the multiplexed session is not yet
67+
// created.
68+
// If the error occurs before the future is created, it may throw an IllegalStateException
69+
// instead.
70+
assertTrue(e instanceof ExecutionException || e instanceof IllegalStateException);
71+
if (e instanceof ExecutionException) {
72+
Throwable cause = e.getCause();
73+
assertTrue(cause instanceof IllegalStateException);
74+
assertTrue(cause.getMessage().contains("runAsync() has not yet been called"));
75+
} else {
76+
assertTrue(e.getMessage().contains("runAsync() has not yet been called"));
77+
}
78+
} else {
79+
IllegalStateException e =
80+
assertThrows(IllegalStateException.class, () -> runner.getCommitTimestamp());
81+
assertTrue(e.getMessage().contains("runAsync() has not yet been called"));
82+
}
6683
}
6784

6885
@Test
6986
public void testAsyncRunner_doesNotReturnCommitResponseBeforeCommit() {
7087
AsyncRunner runner = client().runAsync();
71-
IllegalStateException e =
72-
assertThrows(IllegalStateException.class, () -> runner.getCommitResponse());
73-
assertTrue(e.getMessage().contains("runAsync() has not yet been called"));
88+
if (isMultiplexedSessionsEnabledForRW()) {
89+
Throwable e = assertThrows(Throwable.class, () -> runner.getCommitResponse().get());
90+
// If the error occurs within the future, it gets wrapped in an ExecutionException.
91+
// This happens when DelayedAsyncRunner is invoked while the multiplexed session is not yet
92+
// created.
93+
// If the error occurs before the future is created, it may throw an IllegalStateException
94+
// instead.
95+
assertTrue(e instanceof ExecutionException || e instanceof IllegalStateException);
96+
if (e instanceof ExecutionException) {
97+
Throwable cause = e.getCause();
98+
assertTrue(cause instanceof IllegalStateException);
99+
assertTrue(cause.getMessage().contains("runAsync() has not yet been called"));
100+
} else {
101+
assertTrue(e.getMessage().contains("runAsync() has not yet been called"));
102+
}
103+
} else {
104+
IllegalStateException e =
105+
assertThrows(IllegalStateException.class, () -> runner.getCommitResponse());
106+
assertTrue(e.getMessage().contains("runAsync() has not yet been called"));
107+
}
74108
}
75109

76110
@Test
@@ -558,7 +592,9 @@ public void closeTransactionBeforeEndOfAsyncQuery() throws Exception {
558592
// Wait until at least one row has been fetched. At that moment there should be one session
559593
// checked out.
560594
dataReceived.await();
561-
assertThat(clientImpl.pool.getNumberOfSessionsInUse()).isEqualTo(1);
595+
if (!isMultiplexedSessionsEnabledForRW()) {
596+
assertThat(clientImpl.pool.getNumberOfSessionsInUse()).isEqualTo(1);
597+
}
562598
assertThat(res.isDone()).isFalse();
563599
dataChecked.countDown();
564600
// Get the data from the transaction.

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import static org.junit.Assert.assertNotNull;
2929
import static org.junit.Assert.assertThrows;
3030
import static org.junit.Assert.assertTrue;
31+
import static org.junit.Assume.assumeFalse;
3132

3233
import com.google.api.core.ApiFuture;
3334
import com.google.api.core.ApiFutureCallback;
@@ -250,6 +251,11 @@ public void asyncTransactionManagerUpdate() throws Exception {
250251

251252
@Test
252253
public void asyncTransactionManagerIsNonBlocking() throws Exception {
254+
// TODO: Remove this condition once DelayedAsyncTransactionManager is made non-blocking with
255+
// multiplexed sessions.
256+
assumeFalse(
257+
"DelayedAsyncTransactionManager is currently blocking with multiplexed sessions.",
258+
spanner.getOptions().getSessionPoolOptions().getUseMultiplexedSessionForRW());
253259
mockSpanner.freeze();
254260
try (AsyncTransactionManager manager = clientWithEmptySessionPool().transactionManagerAsync()) {
255261
TransactionContextFuture transactionContextFuture = manager.beginAsync();
@@ -633,6 +639,11 @@ public void asyncTransactionManagerBatchUpdate() throws Exception {
633639

634640
@Test
635641
public void asyncTransactionManagerIsNonBlockingWithBatchUpdate() throws Exception {
642+
// TODO: Remove this condition once DelayedAsyncTransactionManager is made non-blocking with
643+
// multiplexed sessions.
644+
assumeFalse(
645+
"DelayedAsyncTransactionManager is currently blocking with multiplexed sessions.",
646+
spanner.getOptions().getSessionPoolOptions().getUseMultiplexedSessionForRW());
636647
mockSpanner.freeze();
637648
try (AsyncTransactionManager manager = clientWithEmptySessionPool().transactionManagerAsync()) {
638649
TransactionContextFuture transactionContextFuture = manager.beginAsync();

0 commit comments

Comments
 (0)