|
3 | 3 | import static org.assertj.core.api.Assertions.assertThat;
|
4 | 4 | import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
5 | 5 | import static org.mockito.ArgumentMatchers.any;
|
| 6 | +import static org.mockito.ArgumentMatchers.anyList; |
| 7 | +import static org.mockito.ArgumentMatchers.anyLong; |
| 8 | +import static org.mockito.ArgumentMatchers.anyString; |
| 9 | +import static org.mockito.ArgumentMatchers.eq; |
6 | 10 | import static org.mockito.Mockito.doNothing;
|
7 | 11 | import static org.mockito.Mockito.doReturn;
|
8 | 12 | import static org.mockito.Mockito.doThrow;
|
9 | 13 | import static org.mockito.Mockito.mock;
|
| 14 | +import static org.mockito.Mockito.never; |
10 | 15 | import static org.mockito.Mockito.spy;
|
11 | 16 | import static org.mockito.Mockito.times;
|
12 | 17 | import static org.mockito.Mockito.verify;
|
@@ -672,4 +677,185 @@ public void putStateForGroupCommit_FullIdGiven_ShouldThrowAssertionError(
|
672 | 677 | parentId, fullIds, transactionState, current))
|
673 | 678 | .isInstanceOf(IllegalArgumentException.class);
|
674 | 679 | }
|
| 680 | + |
| 681 | + @Test |
| 682 | + void putStateForLazyRecoveryRollback_NormalIdGiven_ShouldCallPutState() |
| 683 | + throws CoordinatorException { |
| 684 | + // Arrange |
| 685 | + Coordinator spiedCoordinator = spy(coordinator); |
| 686 | + |
| 687 | + // Act |
| 688 | + spiedCoordinator.putStateForLazyRecoveryRollback(ANY_ID_1); |
| 689 | + |
| 690 | + // Assert |
| 691 | + verify(spiedCoordinator).putState(new State(ANY_ID_1, TransactionState.ABORTED)); |
| 692 | + } |
| 693 | + |
| 694 | + @Test |
| 695 | + void |
| 696 | + putStateForLazyRecoveryRollback_FullIdGivenWhenTransactionIsInGroupCommitWhenGroupCommitIsNotCommitted_ShouldInsertTwoRecordsWithParentIdAndFullId() |
| 697 | + throws CoordinatorException { |
| 698 | + // Arrange |
| 699 | + Coordinator spiedCoordinator = spy(coordinator); |
| 700 | + CoordinatorGroupCommitKeyManipulator keyManipulator = |
| 701 | + new CoordinatorGroupCommitKeyManipulator(); |
| 702 | + String parentId = keyManipulator.generateParentKey(); |
| 703 | + String fullId = keyManipulator.fullKey(parentId, ANY_ID_1); |
| 704 | + |
| 705 | + // Act |
| 706 | + spiedCoordinator.putStateForLazyRecoveryRollback(fullId); |
| 707 | + |
| 708 | + // Assert |
| 709 | + verify(spiedCoordinator) |
| 710 | + .putStateForGroupCommit( |
| 711 | + eq(parentId), eq(Collections.emptyList()), eq(TransactionState.ABORTED), anyLong()); |
| 712 | + verify(spiedCoordinator).putState(new State(fullId, TransactionState.ABORTED)); |
| 713 | + } |
| 714 | + |
| 715 | + @Test |
| 716 | + void |
| 717 | + putStateForLazyRecoveryRollback_FullIdGivenWhenTransactionIsInGroupCommitWhenGroupCommitIsCommitted_ShouldThrowCoordinatorConflictException() |
| 718 | + throws CoordinatorException { |
| 719 | + // Arrange |
| 720 | + Coordinator spiedCoordinator = spy(coordinator); |
| 721 | + CoordinatorGroupCommitKeyManipulator keyManipulator = |
| 722 | + new CoordinatorGroupCommitKeyManipulator(); |
| 723 | + String parentId = keyManipulator.generateParentKey(); |
| 724 | + String fullId = keyManipulator.fullKey(parentId, ANY_ID_1); |
| 725 | + |
| 726 | + doThrow(CoordinatorConflictException.class) |
| 727 | + .when(spiedCoordinator) |
| 728 | + .putStateForGroupCommit(anyString(), anyList(), any(), anyLong()); |
| 729 | + doReturn( |
| 730 | + Optional.of( |
| 731 | + new State( |
| 732 | + parentId, |
| 733 | + Collections.singletonList(ANY_ID_1), |
| 734 | + TransactionState.COMMITTED, |
| 735 | + System.currentTimeMillis()))) |
| 736 | + .when(spiedCoordinator) |
| 737 | + .getState(parentId); |
| 738 | + |
| 739 | + // Act |
| 740 | + assertThatThrownBy(() -> spiedCoordinator.putStateForLazyRecoveryRollback(fullId)) |
| 741 | + .isInstanceOf(CoordinatorConflictException.class); |
| 742 | + |
| 743 | + // Assert |
| 744 | + verify(spiedCoordinator) |
| 745 | + .putStateForGroupCommit( |
| 746 | + eq(parentId), eq(Collections.emptyList()), eq(TransactionState.ABORTED), anyLong()); |
| 747 | + verify(spiedCoordinator, never()).putState(new State(fullId, TransactionState.ABORTED)); |
| 748 | + } |
| 749 | + |
| 750 | + @Test |
| 751 | + void |
| 752 | + putStateForLazyRecoveryRollback_FullIdGivenWhenTransactionIsInGroupCommitWhenGroupCommitIsAbort_ShouldDoNothing() |
| 753 | + throws CoordinatorException { |
| 754 | + // Arrange |
| 755 | + Coordinator spiedCoordinator = spy(coordinator); |
| 756 | + CoordinatorGroupCommitKeyManipulator keyManipulator = |
| 757 | + new CoordinatorGroupCommitKeyManipulator(); |
| 758 | + String parentId = keyManipulator.generateParentKey(); |
| 759 | + String fullId = keyManipulator.fullKey(parentId, ANY_ID_1); |
| 760 | + |
| 761 | + doThrow(CoordinatorConflictException.class) |
| 762 | + .when(spiedCoordinator) |
| 763 | + .putStateForGroupCommit(anyString(), anyList(), any(), anyLong()); |
| 764 | + doReturn( |
| 765 | + Optional.of( |
| 766 | + new State( |
| 767 | + parentId, |
| 768 | + Collections.singletonList(ANY_ID_1), |
| 769 | + TransactionState.ABORTED, |
| 770 | + System.currentTimeMillis()))) |
| 771 | + .when(spiedCoordinator) |
| 772 | + .getState(parentId); |
| 773 | + |
| 774 | + // Act |
| 775 | + spiedCoordinator.putStateForLazyRecoveryRollback(fullId); |
| 776 | + |
| 777 | + // Assert |
| 778 | + verify(spiedCoordinator) |
| 779 | + .putStateForGroupCommit( |
| 780 | + eq(parentId), eq(Collections.emptyList()), eq(TransactionState.ABORTED), anyLong()); |
| 781 | + verify(spiedCoordinator, never()).putState(new State(fullId, TransactionState.ABORTED)); |
| 782 | + } |
| 783 | + |
| 784 | + @ParameterizedTest |
| 785 | + @EnumSource( |
| 786 | + value = TransactionState.class, |
| 787 | + names = {"COMMITTED", "ABORTED"}) |
| 788 | + void |
| 789 | + putStateForLazyRecoveryRollback_FullIdGivenWhenTransactionIsInDelayedGroupCommitWhenGroupCommitFinished_ShouldInsertRecordWithFullId( |
| 790 | + TransactionState transactionState) throws CoordinatorException { |
| 791 | + // Arrange |
| 792 | + Coordinator spiedCoordinator = spy(coordinator); |
| 793 | + CoordinatorGroupCommitKeyManipulator keyManipulator = |
| 794 | + new CoordinatorGroupCommitKeyManipulator(); |
| 795 | + String parentId = keyManipulator.generateParentKey(); |
| 796 | + String fullId = keyManipulator.fullKey(parentId, ANY_ID_1); |
| 797 | + |
| 798 | + doThrow(CoordinatorConflictException.class) |
| 799 | + .when(spiedCoordinator) |
| 800 | + .putStateForGroupCommit(anyString(), anyList(), any(), anyLong()); |
| 801 | + doReturn( |
| 802 | + Optional.of( |
| 803 | + new State( |
| 804 | + parentId, |
| 805 | + Collections.singletonList("other-id"), |
| 806 | + transactionState, |
| 807 | + System.currentTimeMillis()))) |
| 808 | + .when(spiedCoordinator) |
| 809 | + .getState(parentId); |
| 810 | + |
| 811 | + // Act |
| 812 | + spiedCoordinator.putStateForLazyRecoveryRollback(fullId); |
| 813 | + |
| 814 | + // Assert |
| 815 | + verify(spiedCoordinator) |
| 816 | + .putStateForGroupCommit( |
| 817 | + eq(parentId), eq(Collections.emptyList()), eq(TransactionState.ABORTED), anyLong()); |
| 818 | + verify(spiedCoordinator).putState(new State(fullId, TransactionState.ABORTED)); |
| 819 | + } |
| 820 | + |
| 821 | + @ParameterizedTest |
| 822 | + @EnumSource( |
| 823 | + value = TransactionState.class, |
| 824 | + names = {"COMMITTED", "ABORTED"}) |
| 825 | + void |
| 826 | + putStateForLazyRecoveryRollback_FullIdGivenWhenTransactionIsInDelayedGroupCommitWhenGroupCommitAndDelayedGroupCommitFinished_ShouldCoordinatorConflictException( |
| 827 | + TransactionState transactionState) throws CoordinatorException { |
| 828 | + // Arrange |
| 829 | + Coordinator spiedCoordinator = spy(coordinator); |
| 830 | + CoordinatorGroupCommitKeyManipulator keyManipulator = |
| 831 | + new CoordinatorGroupCommitKeyManipulator(); |
| 832 | + String parentId = keyManipulator.generateParentKey(); |
| 833 | + String fullId = keyManipulator.fullKey(parentId, ANY_ID_1); |
| 834 | + |
| 835 | + doThrow(CoordinatorConflictException.class) |
| 836 | + .when(spiedCoordinator) |
| 837 | + .putStateForGroupCommit(anyString(), anyList(), any(), anyLong()); |
| 838 | + doReturn( |
| 839 | + Optional.of( |
| 840 | + new State( |
| 841 | + parentId, |
| 842 | + Collections.singletonList("other-id"), |
| 843 | + transactionState, |
| 844 | + System.currentTimeMillis()))) |
| 845 | + .when(spiedCoordinator) |
| 846 | + .getState(parentId); |
| 847 | + doThrow(CoordinatorConflictException.class) |
| 848 | + .when(spiedCoordinator) |
| 849 | + .putState(new State(fullId, TransactionState.ABORTED)); |
| 850 | + |
| 851 | + // Act |
| 852 | + assertThatThrownBy(() -> spiedCoordinator.putStateForLazyRecoveryRollback(fullId)) |
| 853 | + .isInstanceOf(CoordinatorConflictException.class); |
| 854 | + |
| 855 | + // Assert |
| 856 | + verify(spiedCoordinator) |
| 857 | + .putStateForGroupCommit( |
| 858 | + eq(parentId), eq(Collections.emptyList()), eq(TransactionState.ABORTED), anyLong()); |
| 859 | + verify(spiedCoordinator).putState(new State(fullId, TransactionState.ABORTED)); |
| 860 | + } |
675 | 861 | }
|
0 commit comments