|
60 | 60 | import java.util.concurrent.Executor;
|
61 | 61 | import java.util.concurrent.Executors;
|
62 | 62 | import java.util.concurrent.TimeUnit;
|
| 63 | +import java.util.concurrent.TimeoutException; |
63 | 64 | import java.util.concurrent.atomic.AtomicInteger;
|
64 | 65 | import org.junit.Test;
|
65 | 66 | import org.junit.runner.RunWith;
|
@@ -1112,4 +1113,48 @@ public ApiFuture<Void> apply(TransactionContext txn, Struct input)
|
1112 | 1113 | }
|
1113 | 1114 | }
|
1114 | 1115 | }
|
| 1116 | + |
| 1117 | + @Test |
| 1118 | + public void asyncTransactionManager_shouldPropagateStatementFailure() |
| 1119 | + throws ExecutionException, InterruptedException, TimeoutException { |
| 1120 | + DatabaseClient dbClient = client(); |
| 1121 | + try (AsyncTransactionManager transactionManager = dbClient.transactionManagerAsync()) { |
| 1122 | + TransactionContextFuture txnContextFuture = transactionManager.beginAsync(); |
| 1123 | + AsyncTransactionStep<Void, Long> updateFuture = |
| 1124 | + txnContextFuture.then( |
| 1125 | + new AsyncTransactionFunction<Void, Long>() { |
| 1126 | + @Override |
| 1127 | + public ApiFuture<Long> apply(TransactionContext txn, Void input) throws Exception { |
| 1128 | + return txn.executeUpdateAsync(INVALID_UPDATE_STATEMENT); |
| 1129 | + } |
| 1130 | + }, |
| 1131 | + executor); |
| 1132 | + final SettableApiFuture<Void> res = SettableApiFuture.create(); |
| 1133 | + ApiFutures.addCallback( |
| 1134 | + updateFuture, |
| 1135 | + new ApiFutureCallback<Long>() { |
| 1136 | + @Override |
| 1137 | + public void onFailure(Throwable throwable) { |
| 1138 | + // Check that we got the expected failure. |
| 1139 | + try { |
| 1140 | + assertThat(throwable).isInstanceOf(SpannerException.class); |
| 1141 | + SpannerException e = (SpannerException) throwable; |
| 1142 | + assertThat(e.getErrorCode()).isEqualTo(ErrorCode.INVALID_ARGUMENT); |
| 1143 | + assertThat(e.getMessage()).contains("invalid statement"); |
| 1144 | + res.set(null); |
| 1145 | + } catch (Throwable t) { |
| 1146 | + res.setException(t); |
| 1147 | + } |
| 1148 | + } |
| 1149 | + |
| 1150 | + @Override |
| 1151 | + public void onSuccess(Long aLong) { |
| 1152 | + res.setException(new AssertionError("Statement should not succeed.")); |
| 1153 | + } |
| 1154 | + }, |
| 1155 | + executor); |
| 1156 | + |
| 1157 | + assertThat(res.get(10L, TimeUnit.SECONDS)).isNull(); |
| 1158 | + } |
| 1159 | + } |
1115 | 1160 | }
|
0 commit comments