|
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,141 @@ 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 commitAsyncCancelsCommitRpcThatIsSentAfterCancellation() { |
| 255 | + SettableApiFuture<Transaction> beginTransaction = SettableApiFuture.create(); |
| 256 | + SettableApiFuture<CommitResponse> inFlightCommit = |
| 257 | + setUpTransactionThatCommitsAfterBeginTransaction(beginTransaction); |
| 258 | + TransactionContextImpl transaction = newTransactionWithoutTransactionId(); |
| 259 | + |
| 260 | + ApiFuture<com.google.cloud.spanner.CommitResponse> commitFuture = transaction.commitAsync(); |
| 261 | + // The Commit RPC is only sent once BeginTransaction has finished, so cancelling here means |
| 262 | + // that the commit is abandoned before there is any RPC to cancel. |
| 263 | + assertTrue(commitFuture.cancel(true)); |
| 264 | + verify(rpc, never()).commitAsync(Mockito.any(CommitRequest.class), Mockito.anyMap()); |
| 265 | + |
| 266 | + beginTransaction.set( |
| 267 | + Transaction.newBuilder().setId(ByteString.copyFromUtf8("test-txn")).build()); |
| 268 | + |
| 269 | + assertTrue( |
| 270 | + "Commit RPC that was sent after the cancellation was not cancelled", |
| 271 | + inFlightCommit.isCancelled()); |
| 272 | + } |
| 273 | + |
| 274 | + @Test |
| 275 | + public void commitCancelsCommitRpcThatIsSentAfterCallingThreadInterrupted() { |
| 276 | + SettableApiFuture<Transaction> beginTransaction = SettableApiFuture.create(); |
| 277 | + SettableApiFuture<CommitResponse> inFlightCommit = |
| 278 | + setUpTransactionThatCommitsAfterBeginTransaction(beginTransaction); |
| 279 | + TransactionContextImpl transaction = newTransactionWithoutTransactionId(); |
| 280 | + |
| 281 | + try { |
| 282 | + // Interrupting before commit() waits for the result means that it gives up while |
| 283 | + // BeginTransaction is still pending, and therefore before the Commit RPC has been sent. |
| 284 | + Thread.currentThread().interrupt(); |
| 285 | + SpannerException e = assertThrows(SpannerException.class, transaction::commit); |
| 286 | + assertEquals(ErrorCode.CANCELLED, e.getErrorCode()); |
| 287 | + verify(rpc, never()).commitAsync(Mockito.any(CommitRequest.class), Mockito.anyMap()); |
| 288 | + |
| 289 | + // commit() restores the interrupt flag, and BeginTransaction finishes on a gax thread that |
| 290 | + // does not have that flag. Clear it so the listeners below are not interrupted either. |
| 291 | + Thread.interrupted(); |
| 292 | + beginTransaction.set( |
| 293 | + Transaction.newBuilder().setId(ByteString.copyFromUtf8("test-txn")).build()); |
| 294 | + |
| 295 | + verify(rpc, times(1)).commitAsync(Mockito.any(CommitRequest.class), Mockito.anyMap()); |
| 296 | + assertTrue( |
| 297 | + "Commit RPC that was sent after the interrupt was not cancelled", |
| 298 | + inFlightCommit.isCancelled()); |
| 299 | + } finally { |
| 300 | + // Clear the interrupt flag so it cannot leak into other tests. |
| 301 | + Thread.interrupted(); |
| 302 | + } |
| 303 | + } |
| 304 | + |
| 305 | + /** |
| 306 | + * Sets up a session that only finishes BeginTransaction once the given future is set, and returns |
| 307 | + * the future of the Commit RPC that is sent after that. |
| 308 | + */ |
| 309 | + private SettableApiFuture<CommitResponse> setUpTransactionThatCommitsAfterBeginTransaction( |
| 310 | + SettableApiFuture<Transaction> beginTransaction) { |
| 311 | + when(session.getName()).thenReturn("projects/p/instances/i/databases/d/sessions/s"); |
| 312 | + when(session.beginTransactionAsync( |
| 313 | + Mockito.any(Options.class), |
| 314 | + Mockito.anyBoolean(), |
| 315 | + Mockito.anyMap(), |
| 316 | + Mockito.any(), |
| 317 | + Mockito.any())) |
| 318 | + .thenReturn(beginTransaction); |
| 319 | + SettableApiFuture<CommitResponse> inFlightCommit = SettableApiFuture.create(); |
| 320 | + when(rpc.commitAsync(Mockito.any(CommitRequest.class), Mockito.anyMap())) |
| 321 | + .thenReturn(inFlightCommit); |
| 322 | + return inFlightCommit; |
| 323 | + } |
| 324 | + |
| 325 | + /** Returns a transaction that has to call BeginTransaction before it can commit. */ |
| 326 | + private TransactionContextImpl newTransactionWithoutTransactionId() { |
| 327 | + return TransactionContextImpl.newBuilder() |
| 328 | + .setSession(session) |
| 329 | + .setOptions(Options.fromTransactionOptions()) |
| 330 | + .setRpc(rpc) |
| 331 | + .setTracer(tracer) |
| 332 | + .setSpan(span) |
| 333 | + .build(); |
| 334 | + } |
| 335 | + |
198 | 336 | @SuppressWarnings("unchecked") |
199 | 337 | @Test |
200 | 338 | public void usesPreparedTransaction() { |
|
0 commit comments