|
20 | 20 | import static org.junit.Assert.assertArrayEquals; |
21 | 21 | import static org.junit.Assert.assertEquals; |
22 | 22 | import static org.junit.Assert.assertThrows; |
| 23 | +import static org.junit.Assert.assertTrue; |
23 | 24 | import static org.mockito.ArgumentMatchers.any; |
24 | 25 | import static org.mockito.ArgumentMatchers.eq; |
25 | 26 | import static org.mockito.Mockito.doThrow; |
|
29 | 30 | import static org.mockito.Mockito.verify; |
30 | 31 | import static org.mockito.Mockito.when; |
31 | 32 |
|
| 33 | +import com.google.api.core.ApiFuture; |
32 | 34 | import com.google.api.core.ApiFutures; |
| 35 | +import com.google.api.core.SettableApiFuture; |
33 | 36 | import com.google.cloud.grpc.GrpcTransportOptions; |
34 | 37 | import com.google.cloud.grpc.GrpcTransportOptions.ExecutorFactory; |
35 | 38 | import com.google.cloud.spanner.ErrorHandler.DefaultErrorHandler; |
@@ -195,6 +198,129 @@ public void testCommitWithClientContext() { |
195 | 198 | assertEquals(clientContext, capturedOptions.clientContext()); |
196 | 199 | } |
197 | 200 |
|
| 201 | + @Test |
| 202 | + public void commitCancelsInFlightRpcWhenCallingThreadInterrupted() { |
| 203 | + when(session.getName()).thenReturn("projects/p/instances/i/databases/d/sessions/s"); |
| 204 | + TransactionContextImpl transaction = |
| 205 | + TransactionContextImpl.newBuilder() |
| 206 | + .setSession(session) |
| 207 | + .setTransactionId(ByteString.copyFromUtf8("test-txn")) |
| 208 | + .setOptions(Options.fromTransactionOptions()) |
| 209 | + .setRpc(rpc) |
| 210 | + .setTracer(tracer) |
| 211 | + .setSpan(span) |
| 212 | + .build(); |
| 213 | + SettableApiFuture<CommitResponse> inFlightCommit = SettableApiFuture.create(); |
| 214 | + when(rpc.commitAsync(Mockito.any(CommitRequest.class), Mockito.anyMap())) |
| 215 | + .thenAnswer( |
| 216 | + invocation -> { |
| 217 | + Thread.currentThread().interrupt(); |
| 218 | + return inFlightCommit; |
| 219 | + }); |
| 220 | + |
| 221 | + try { |
| 222 | + SpannerException e = assertThrows(SpannerException.class, transaction::commit); |
| 223 | + assertEquals(ErrorCode.CANCELLED, e.getErrorCode()); |
| 224 | + assertTrue("in-flight Commit RPC was not cancelled", inFlightCommit.isCancelled()); |
| 225 | + } finally { |
| 226 | + // Clear the interrupt flag so it cannot leak into other tests. |
| 227 | + Thread.interrupted(); |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + @Test |
| 232 | + public void commitAsyncCancelsInFlightRpcWhenReturnedFutureIsCancelled() { |
| 233 | + when(session.getName()).thenReturn("projects/p/instances/i/databases/d/sessions/s"); |
| 234 | + TransactionContextImpl transaction = |
| 235 | + TransactionContextImpl.newBuilder() |
| 236 | + .setSession(session) |
| 237 | + .setTransactionId(ByteString.copyFromUtf8("test-txn")) |
| 238 | + .setOptions(Options.fromTransactionOptions()) |
| 239 | + .setRpc(rpc) |
| 240 | + .setTracer(tracer) |
| 241 | + .setSpan(span) |
| 242 | + .build(); |
| 243 | + SettableApiFuture<CommitResponse> inFlightCommit = SettableApiFuture.create(); |
| 244 | + when(rpc.commitAsync(Mockito.any(CommitRequest.class), Mockito.anyMap())) |
| 245 | + .thenReturn(inFlightCommit); |
| 246 | + |
| 247 | + ApiFuture<com.google.cloud.spanner.CommitResponse> commitFuture = transaction.commitAsync(); |
| 248 | + assertTrue(commitFuture.cancel(true)); |
| 249 | + |
| 250 | + assertTrue("in-flight Commit RPC was not cancelled", inFlightCommit.isCancelled()); |
| 251 | + } |
| 252 | + |
| 253 | + @Test |
| 254 | + public void commitAsyncSkipsCommitRpcWhenCancelledBeforeItIsSent() { |
| 255 | + SettableApiFuture<Transaction> beginTransaction = SettableApiFuture.create(); |
| 256 | + setUpTransactionThatCommitsAfterBeginTransaction(beginTransaction); |
| 257 | + TransactionContextImpl transaction = newTransactionWithoutTransactionId(); |
| 258 | + |
| 259 | + ApiFuture<com.google.cloud.spanner.CommitResponse> commitFuture = transaction.commitAsync(); |
| 260 | + // The Commit RPC is only sent once BeginTransaction has finished, so cancelling here means |
| 261 | + // that the commit is abandoned before there is any RPC to cancel. |
| 262 | + assertTrue(commitFuture.cancel(true)); |
| 263 | + verify(rpc, never()).commitAsync(Mockito.any(CommitRequest.class), Mockito.anyMap()); |
| 264 | + |
| 265 | + beginTransaction.set( |
| 266 | + Transaction.newBuilder().setId(ByteString.copyFromUtf8("test-txn")).build()); |
| 267 | + |
| 268 | + // The commit was already abandoned, so the Commit RPC should not be sent at all. |
| 269 | + verify(rpc, never()).commitAsync(Mockito.any(CommitRequest.class), Mockito.anyMap()); |
| 270 | + } |
| 271 | + |
| 272 | + @Test |
| 273 | + public void commitSkipsCommitRpcWhenCallingThreadInterruptedBeforeItIsSent() { |
| 274 | + SettableApiFuture<Transaction> beginTransaction = SettableApiFuture.create(); |
| 275 | + setUpTransactionThatCommitsAfterBeginTransaction(beginTransaction); |
| 276 | + TransactionContextImpl transaction = newTransactionWithoutTransactionId(); |
| 277 | + |
| 278 | + try { |
| 279 | + // Interrupting before commit() waits for the result means that it gives up while |
| 280 | + // BeginTransaction is still pending, and therefore before the Commit RPC has been sent. |
| 281 | + Thread.currentThread().interrupt(); |
| 282 | + SpannerException e = assertThrows(SpannerException.class, transaction::commit); |
| 283 | + assertEquals(ErrorCode.CANCELLED, e.getErrorCode()); |
| 284 | + verify(rpc, never()).commitAsync(Mockito.any(CommitRequest.class), Mockito.anyMap()); |
| 285 | + |
| 286 | + // commit() restores the interrupt flag, and BeginTransaction finishes on a gax thread that |
| 287 | + // does not have that flag. Clear it so the listeners below are not interrupted either. |
| 288 | + Thread.interrupted(); |
| 289 | + beginTransaction.set( |
| 290 | + Transaction.newBuilder().setId(ByteString.copyFromUtf8("test-txn")).build()); |
| 291 | + |
| 292 | + // The commit was already abandoned, so the Commit RPC should not be sent at all. |
| 293 | + verify(rpc, never()).commitAsync(Mockito.any(CommitRequest.class), Mockito.anyMap()); |
| 294 | + } finally { |
| 295 | + // Clear the interrupt flag so it cannot leak into other tests. |
| 296 | + Thread.interrupted(); |
| 297 | + } |
| 298 | + } |
| 299 | + |
| 300 | + private void setUpTransactionThatCommitsAfterBeginTransaction( |
| 301 | + SettableApiFuture<Transaction> beginTransaction) { |
| 302 | + when(session.getName()).thenReturn("projects/p/instances/i/databases/d/sessions/s"); |
| 303 | + when(session.beginTransactionAsync( |
| 304 | + Mockito.any(Options.class), |
| 305 | + Mockito.anyBoolean(), |
| 306 | + Mockito.anyMap(), |
| 307 | + Mockito.any(), |
| 308 | + Mockito.any())) |
| 309 | + .thenReturn(beginTransaction); |
| 310 | + when(rpc.commitAsync(Mockito.any(CommitRequest.class), Mockito.anyMap())) |
| 311 | + .thenReturn(SettableApiFuture.create()); |
| 312 | + } |
| 313 | + |
| 314 | + private TransactionContextImpl newTransactionWithoutTransactionId() { |
| 315 | + return TransactionContextImpl.newBuilder() |
| 316 | + .setSession(session) |
| 317 | + .setOptions(Options.fromTransactionOptions()) |
| 318 | + .setRpc(rpc) |
| 319 | + .setTracer(tracer) |
| 320 | + .setSpan(span) |
| 321 | + .build(); |
| 322 | + } |
| 323 | + |
198 | 324 | @SuppressWarnings("unchecked") |
199 | 325 | @Test |
200 | 326 | public void usesPreparedTransaction() { |
|
0 commit comments