|
52 | 52 | import java.util.concurrent.BlockingQueue; |
53 | 53 | import java.util.concurrent.ConcurrentLinkedDeque; |
54 | 54 | import java.util.concurrent.CountDownLatch; |
| 55 | +import java.util.concurrent.ExecutionException; |
55 | 56 | import java.util.concurrent.ExecutorService; |
56 | 57 | import java.util.concurrent.Executors; |
57 | 58 | import java.util.concurrent.ScheduledThreadPoolExecutor; |
@@ -624,6 +625,87 @@ private void assertSchemaGettersWorkOnDone(Statement statement, List<String> exp |
624 | 625 | assertEquals(expectedValues, actualValues); |
625 | 626 | } |
626 | 627 |
|
| 628 | + @Test(timeout = 60_000) |
| 629 | + public void cancelAfterCursorStateDonePreservesSuccess() throws Exception { |
| 630 | + List<String> actualValues = Collections.synchronizedList(new ArrayList<>()); |
| 631 | + CountDownLatch doneObservedLatch = new CountDownLatch(1); |
| 632 | + CountDownLatch cancelCalledLatch = new CountDownLatch(1); |
| 633 | + try (AsyncResultSet resultSet = |
| 634 | + client.singleUse().executeQueryAsync(READ_MULTIPLE_KEY_VALUE_STATEMENT)) { |
| 635 | + ApiFuture<Void> finished = |
| 636 | + resultSet.setCallback( |
| 637 | + executor, |
| 638 | + cursor -> { |
| 639 | + while (true) { |
| 640 | + switch (cursor.tryNext()) { |
| 641 | + case OK: |
| 642 | + actualValues.add(cursor.getString("Value")); |
| 643 | + break; |
| 644 | + case NOT_READY: |
| 645 | + return CallbackResponse.CONTINUE; |
| 646 | + case DONE: |
| 647 | + doneObservedLatch.countDown(); |
| 648 | + try { |
| 649 | + assertTrue(cancelCalledLatch.await(30, TimeUnit.SECONDS)); |
| 650 | + } catch (InterruptedException interruptedException) { |
| 651 | + Thread.currentThread().interrupt(); |
| 652 | + throw new RuntimeException(interruptedException); |
| 653 | + } |
| 654 | + assertEquals(READ_TABLE_TYPE, cursor.getType()); |
| 655 | + return CallbackResponse.DONE; |
| 656 | + } |
| 657 | + } |
| 658 | + }); |
| 659 | + |
| 660 | + assertTrue(doneObservedLatch.await(30, TimeUnit.SECONDS)); |
| 661 | + resultSet.cancel(); |
| 662 | + cancelCalledLatch.countDown(); |
| 663 | + assertNull(finished.get(30, TimeUnit.SECONDS)); |
| 664 | + } finally { |
| 665 | + cancelCalledLatch.countDown(); |
| 666 | + } |
| 667 | + assertEquals(Arrays.asList("v1", "v2", "v3"), actualValues); |
| 668 | + } |
| 669 | + |
| 670 | + @Test(timeout = 60_000) |
| 671 | + public void cancelAfterCursorExceptionPreservesOriginalError() throws Exception { |
| 672 | + Statement failingStatement = Statement.of("SELECT * FROM MissingTable"); |
| 673 | + mockSpanner.putStatementResult( |
| 674 | + StatementResult.exception( |
| 675 | + failingStatement, |
| 676 | + Status.INVALID_ARGUMENT.withDescription("missing table").asRuntimeException())); |
| 677 | + CountDownLatch exceptionObservedLatch = new CountDownLatch(1); |
| 678 | + CountDownLatch cancelCalledLatch = new CountDownLatch(1); |
| 679 | + try (AsyncResultSet resultSet = client.singleUse().executeQueryAsync(failingStatement)) { |
| 680 | + ApiFuture<Void> finished = |
| 681 | + resultSet.setCallback( |
| 682 | + executor, |
| 683 | + cursor -> { |
| 684 | + SpannerException thrown = assertThrows(SpannerException.class, cursor::tryNext); |
| 685 | + assertEquals(ErrorCode.INVALID_ARGUMENT, thrown.getErrorCode()); |
| 686 | + exceptionObservedLatch.countDown(); |
| 687 | + try { |
| 688 | + assertTrue(cancelCalledLatch.await(30, TimeUnit.SECONDS)); |
| 689 | + } catch (InterruptedException interruptedException) { |
| 690 | + Thread.currentThread().interrupt(); |
| 691 | + throw new RuntimeException(interruptedException); |
| 692 | + } |
| 693 | + return CallbackResponse.DONE; |
| 694 | + }); |
| 695 | + |
| 696 | + assertTrue(exceptionObservedLatch.await(30, TimeUnit.SECONDS)); |
| 697 | + resultSet.cancel(); |
| 698 | + cancelCalledLatch.countDown(); |
| 699 | + ExecutionException executionException = |
| 700 | + assertThrows(ExecutionException.class, () -> finished.get(30, TimeUnit.SECONDS)); |
| 701 | + assertEquals( |
| 702 | + ErrorCode.INVALID_ARGUMENT, |
| 703 | + ((SpannerException) executionException.getCause()).getErrorCode()); |
| 704 | + } finally { |
| 705 | + cancelCalledLatch.countDown(); |
| 706 | + } |
| 707 | + } |
| 708 | + |
627 | 709 | private Spanner createSingleThreadSpanner( |
628 | 710 | SpannerOptions.CloseableExecutorProvider asyncExecutorProvider) { |
629 | 711 | return SpannerOptions.newBuilder() |
|
0 commit comments