|
5 | 5 | import static org.mockito.ArgumentMatchers.any; |
6 | 6 | import static org.mockito.Mockito.atMost; |
7 | 7 | import static org.mockito.Mockito.doThrow; |
| 8 | +import static org.mockito.Mockito.mock; |
8 | 9 | import static org.mockito.Mockito.never; |
9 | 10 | import static org.mockito.Mockito.only; |
10 | 11 | import static org.mockito.Mockito.spy; |
@@ -562,4 +563,203 @@ public void executeImplicitPreRead_ParallelImplicitPreReadEnabled_ShouldExecuteT |
562 | 563 | assertThatThrownBy(() -> parallelExecutor.executeImplicitPreRead(tasks, TX_ID)) |
563 | 564 | .isInstanceOf(CrudException.class); |
564 | 565 | } |
| 566 | + |
| 567 | + @Test |
| 568 | + public void executeTasks_SingleTaskAndNoWaitFalse_ShouldExecuteDirectly() |
| 569 | + throws ExecutionException, ValidationConflictException, CrudException { |
| 570 | + // Arrange |
| 571 | + List<ParallelExecutorTask> tasks = Collections.singletonList(task); |
| 572 | + boolean parallel = true; // Should be ignored |
| 573 | + boolean noWait = false; |
| 574 | + boolean stopOnError = true; |
| 575 | + |
| 576 | + // Act |
| 577 | + parallelExecutor.executeTasks(tasks, parallel, noWait, stopOnError, "test", TX_ID); |
| 578 | + |
| 579 | + // Assert |
| 580 | + verify(task).run(); |
| 581 | + verify(parallelExecutorService, never()).execute(any()); |
| 582 | + } |
| 583 | + |
| 584 | + @Test |
| 585 | + public void executeTasks_SingleTaskAndNoWaitTrue_ShouldUseParallelExecution() |
| 586 | + throws ExecutionException, ValidationConflictException, CrudException { |
| 587 | + // Arrange |
| 588 | + when(config.isParallelPreparationEnabled()).thenReturn(true); |
| 589 | + |
| 590 | + List<ParallelExecutorTask> tasks = Collections.singletonList(task); |
| 591 | + boolean parallel = true; |
| 592 | + boolean noWait = true; |
| 593 | + boolean stopOnError = false; |
| 594 | + |
| 595 | + // Act |
| 596 | + parallelExecutor.executeTasks(tasks, parallel, noWait, stopOnError, "test", TX_ID); |
| 597 | + |
| 598 | + // Assert |
| 599 | + verify(parallelExecutorService).execute(any()); |
| 600 | + } |
| 601 | + |
| 602 | + @Test |
| 603 | + public void executeTasks_ParallelTrue_ShouldExecuteTasksInParallel() |
| 604 | + throws ExecutionException, ValidationConflictException, CrudException { |
| 605 | + // Arrange |
| 606 | + boolean parallel = true; |
| 607 | + boolean noWait = false; |
| 608 | + boolean stopOnError = false; |
| 609 | + |
| 610 | + // Act |
| 611 | + parallelExecutor.executeTasks(tasks, parallel, noWait, stopOnError, "test", TX_ID); |
| 612 | + |
| 613 | + // Assert |
| 614 | + verify(parallelExecutorService, times(tasks.size())).execute(any()); |
| 615 | + } |
| 616 | + |
| 617 | + @Test |
| 618 | + public void executeTasks_ParallelFalse_ShouldExecuteTasksSerially() |
| 619 | + throws ExecutionException, ValidationConflictException, CrudException { |
| 620 | + // Arrange |
| 621 | + boolean parallel = false; |
| 622 | + boolean noWait = false; |
| 623 | + boolean stopOnError = false; |
| 624 | + |
| 625 | + // Act |
| 626 | + parallelExecutor.executeTasks(tasks, parallel, noWait, stopOnError, "test", TX_ID); |
| 627 | + |
| 628 | + // Assert |
| 629 | + verify(task, times(tasks.size())).run(); |
| 630 | + verify(parallelExecutorService, never()).execute(any()); |
| 631 | + } |
| 632 | + |
| 633 | + @Test |
| 634 | + public void executeTasks_ParallelTrueAndStopOnErrorTrue_ExceptionThrown_ShouldStopExecution() |
| 635 | + throws ExecutionException, ValidationConflictException, CrudException { |
| 636 | + // Arrange |
| 637 | + boolean parallel = true; |
| 638 | + boolean noWait = false; |
| 639 | + boolean stopOnError = true; |
| 640 | + |
| 641 | + doThrow(new ExecutionException("Test exception")).when(task).run(); |
| 642 | + |
| 643 | + // Act Assert |
| 644 | + assertThatThrownBy( |
| 645 | + () -> |
| 646 | + parallelExecutor.executeTasks(tasks, parallel, noWait, stopOnError, "test", TX_ID)) |
| 647 | + .isInstanceOf(ExecutionException.class) |
| 648 | + .hasMessage("Test exception"); |
| 649 | + |
| 650 | + verify(parallelExecutorService, times(tasks.size())).execute(any()); |
| 651 | + } |
| 652 | + |
| 653 | + @Test |
| 654 | + public void |
| 655 | + executeTasks_ParallelTrueAndStopOnErrorFalse_ExceptionThrown_ShouldContinueOtherTasks() |
| 656 | + throws ExecutionException, ValidationConflictException, CrudException { |
| 657 | + // Arrange |
| 658 | + boolean parallel = true; |
| 659 | + boolean noWait = false; |
| 660 | + boolean stopOnError = false; |
| 661 | + |
| 662 | + ParallelExecutorTask failingTask = mock(ParallelExecutorTask.class); |
| 663 | + doThrow(new ExecutionException("Test exception")).when(failingTask).run(); |
| 664 | + |
| 665 | + List<ParallelExecutorTask> mixedTasks = Arrays.asList(failingTask, task); |
| 666 | + |
| 667 | + // Act Assert |
| 668 | + assertThatThrownBy( |
| 669 | + () -> |
| 670 | + parallelExecutor.executeTasks( |
| 671 | + mixedTasks, parallel, noWait, stopOnError, "test", TX_ID)) |
| 672 | + .isInstanceOf(ExecutionException.class); |
| 673 | + |
| 674 | + verify(parallelExecutorService, times(mixedTasks.size())).execute(any()); |
| 675 | + } |
| 676 | + |
| 677 | + @Test |
| 678 | + public void |
| 679 | + executeTasks_ParallelTrueAndStopOnErrorFalse_ExceptionThrownByMultipleTasks_ShouldContinueOtherTasks() |
| 680 | + throws ExecutionException, ValidationConflictException, CrudException { |
| 681 | + // Arrange |
| 682 | + boolean parallel = true; |
| 683 | + boolean noWait = false; |
| 684 | + boolean stopOnError = false; |
| 685 | + |
| 686 | + ExecutionException executionException1 = new ExecutionException("Test exception1"); |
| 687 | + ParallelExecutorTask failingTask1 = mock(ParallelExecutorTask.class); |
| 688 | + doThrow(executionException1).when(failingTask1).run(); |
| 689 | + |
| 690 | + ExecutionException executionException2 = new ExecutionException("Test exception2"); |
| 691 | + ParallelExecutorTask failingTask2 = mock(ParallelExecutorTask.class); |
| 692 | + doThrow(executionException2).when(failingTask2).run(); |
| 693 | + |
| 694 | + List<ParallelExecutorTask> mixedTasks = Arrays.asList(failingTask1, failingTask2, task); |
| 695 | + |
| 696 | + // Act Assert |
| 697 | + assertThatThrownBy( |
| 698 | + () -> |
| 699 | + parallelExecutor.executeTasks( |
| 700 | + mixedTasks, parallel, noWait, stopOnError, "test", TX_ID)) |
| 701 | + .isEqualTo(executionException1) |
| 702 | + .hasSuppressedException(executionException2); |
| 703 | + |
| 704 | + verify(parallelExecutorService, times(mixedTasks.size())).execute(any()); |
| 705 | + } |
| 706 | + |
| 707 | + @Test |
| 708 | + public void |
| 709 | + executeTasks_ParallelFalseAndStopOnErrorFalse_ExceptionThrown_ShouldContinueOtherTasks() |
| 710 | + throws ExecutionException, ValidationConflictException, CrudException { |
| 711 | + // Arrange |
| 712 | + boolean parallel = false; |
| 713 | + boolean noWait = false; |
| 714 | + boolean stopOnError = false; |
| 715 | + |
| 716 | + ParallelExecutorTask failingTask = mock(ParallelExecutorTask.class); |
| 717 | + doThrow(new ExecutionException("Test exception")).when(failingTask).run(); |
| 718 | + |
| 719 | + List<ParallelExecutorTask> mixedTasks = Arrays.asList(failingTask, task); |
| 720 | + |
| 721 | + // Act Assert |
| 722 | + assertThatThrownBy( |
| 723 | + () -> |
| 724 | + parallelExecutor.executeTasks( |
| 725 | + mixedTasks, parallel, noWait, stopOnError, "test", TX_ID)) |
| 726 | + .isInstanceOf(ExecutionException.class); |
| 727 | + |
| 728 | + verify(failingTask, only()).run(); |
| 729 | + verify(task, only()).run(); |
| 730 | + verify(parallelExecutorService, never()).execute(any()); |
| 731 | + } |
| 732 | + |
| 733 | + @Test |
| 734 | + public void |
| 735 | + executeTasks_ParallelFalseAndStopOnErrorFalse_ExceptionThrownByMultipleTasks_ShouldContinueOtherTasks() |
| 736 | + throws ExecutionException, ValidationConflictException, CrudException { |
| 737 | + // Arrange |
| 738 | + boolean parallel = false; |
| 739 | + boolean noWait = false; |
| 740 | + boolean stopOnError = false; |
| 741 | + |
| 742 | + ExecutionException executionException1 = new ExecutionException("Test exception1"); |
| 743 | + ParallelExecutorTask failingTask1 = mock(ParallelExecutorTask.class); |
| 744 | + doThrow(executionException1).when(failingTask1).run(); |
| 745 | + |
| 746 | + ExecutionException executionException2 = new ExecutionException("Test exception2"); |
| 747 | + ParallelExecutorTask failingTask2 = mock(ParallelExecutorTask.class); |
| 748 | + doThrow(executionException2).when(failingTask2).run(); |
| 749 | + |
| 750 | + List<ParallelExecutorTask> mixedTasks = Arrays.asList(failingTask1, failingTask2, task); |
| 751 | + |
| 752 | + // Act Assert |
| 753 | + assertThatThrownBy( |
| 754 | + () -> |
| 755 | + parallelExecutor.executeTasks( |
| 756 | + mixedTasks, parallel, noWait, stopOnError, "test", TX_ID)) |
| 757 | + .isEqualTo(executionException1) |
| 758 | + .hasSuppressedException(executionException2); |
| 759 | + |
| 760 | + verify(failingTask1, only()).run(); |
| 761 | + verify(failingTask2, only()).run(); |
| 762 | + verify(task, only()).run(); |
| 763 | + verify(parallelExecutorService, never()).execute(any()); |
| 764 | + } |
565 | 765 | } |
0 commit comments