1717
1818package org .apache .dolphinscheduler .server .master .runner ;
1919
20- import static org .mockito .ArgumentMatchers .any ;
20+ import static org .awaitility .Awaitility .await ;
21+ import static org .junit .jupiter .api .Assertions .assertEquals ;
2122import static org .mockito .Mockito .doThrow ;
22- import static org .mockito .Mockito .mock ;
2323import static org .mockito .Mockito .times ;
2424import static org .mockito .Mockito .verify ;
2525import static org .mockito .Mockito .when ;
2929import org .apache .dolphinscheduler .plugin .task .api .enums .TaskExecutionStatus ;
3030import org .apache .dolphinscheduler .server .master .engine .task .client .ITaskExecutorClient ;
3131import org .apache .dolphinscheduler .server .master .engine .task .runnable .ITaskExecutionRunnable ;
32- import org .apache .dolphinscheduler .server .master .exception .dispatch .TaskDispatchException ;
33- import org .apache .dolphinscheduler .server .master .runner .queue .PriorityAndDelayBasedTaskEntry ;
34- import org .apache .dolphinscheduler .server .master .runner .queue .PriorityDelayQueue ;
3532
33+ import java .time .Duration ;
34+
35+ import org .junit .jupiter .api .BeforeEach ;
3636import org .junit .jupiter .api .Test ;
3737import org .junit .jupiter .api .extension .ExtendWith ;
3838import org .mockito .InjectMocks ;
@@ -46,43 +46,86 @@ public class WorkerGroupTaskDispatcherTest {
4646 private ITaskExecutorClient taskExecutorClient ;
4747
4848 @ Mock
49- private PriorityDelayQueue < PriorityAndDelayBasedTaskEntry > workerGroupQueue ;
49+ private ITaskExecutionRunnable taskExecutionRunnable ;
5050
5151 @ InjectMocks
5252 private WorkerGroupTaskDispatcher workerGroupTaskDispatcher ;
5353
54- @ Test
55- public void dispatch_TaskStatusEligible_ShouldDispatchTask () throws TaskDispatchException {
56- ITaskExecutionRunnable taskExecutionRunnable = mock ( ITaskExecutionRunnable . class );
57- TaskInstance taskInstance = mock ( TaskInstance . class );
54+ @ BeforeEach
55+ public void setUp () {
56+ workerGroupTaskDispatcher = new WorkerGroupTaskDispatcher ( "testWorkerGroup" , taskExecutorClient );
57+ }
5858
59- when (workerGroupQueue .take ()).thenReturn (new PriorityAndDelayBasedTaskEntry <>(0 , taskExecutionRunnable ));
59+ @ Test
60+ public void testDispatch_Success () throws Exception {
61+ TaskInstance taskInstance = new TaskInstance ();
62+ taskInstance .setState (TaskExecutionStatus .SUBMITTED_SUCCESS );
6063 when (taskExecutionRunnable .getTaskInstance ()).thenReturn (taskInstance );
61- when (taskInstance .getState ()).thenReturn (TaskExecutionStatus .SUBMITTED_SUCCESS );
6264
63- workerGroupTaskDispatcher .dispatch ( );
65+ workerGroupTaskDispatcher .add ( taskExecutionRunnable , 0L );
6466
65- verify (workerGroupQueue , times (1 )).take ();
66- verify (taskExecutorClient , times (1 )).dispatch (taskExecutionRunnable );
67+ workerGroupTaskDispatcher .start ();
68+ await ().atMost (Duration .ofSeconds (2 )).untilAsserted (() -> {
69+ workerGroupTaskDispatcher .close ();
70+ verify (taskExecutorClient , times (1 )).dispatch (taskExecutionRunnable );
71+ });
6772 }
6873
6974 @ Test
70- public void dispatch_TaskDispatchFails_ShouldRetryTask () throws TaskDispatchException {
71- ITaskExecutionRunnable taskExecutionRunnable = mock (ITaskExecutionRunnable .class );
72- TaskInstance taskInstance = mock (TaskInstance .class );
75+ public void testDispatch_FailureAndRetry () throws Exception {
7376
74- when (workerGroupQueue .take ()).thenReturn (new PriorityAndDelayBasedTaskEntry <>(0 , taskExecutionRunnable ));
75- when (taskExecutionRunnable .getTaskInstance ()).thenReturn (taskInstance );
76- when (taskInstance .getState ()).thenReturn (TaskExecutionStatus .SUBMITTED_SUCCESS );
7777 TaskExecutionContext taskExecutionContext = new TaskExecutionContext ();
7878 taskExecutionContext .setDispatchFailTimes (1 );
79+ TaskInstance taskInstance = new TaskInstance ();
80+ taskInstance .setState (TaskExecutionStatus .SUBMITTED_SUCCESS );
81+ when (taskExecutionRunnable .getTaskInstance ()).thenReturn (taskInstance );
7982 when (taskExecutionRunnable .getTaskExecutionContext ()).thenReturn (taskExecutionContext );
8083 doThrow (new RuntimeException ("Dispatch failed" )).when (taskExecutorClient ).dispatch (taskExecutionRunnable );
8184
82- workerGroupTaskDispatcher .dispatch ();
85+ workerGroupTaskDispatcher .add (taskExecutionRunnable , 0L );
86+
87+ workerGroupTaskDispatcher .start ();
88+ await ().atMost (Duration .ofSeconds (5 )).untilAsserted (() -> {
89+ workerGroupTaskDispatcher .close ();
90+ verify (taskExecutorClient , times (2 )).dispatch (taskExecutionRunnable );
91+ });
8392
84- verify (workerGroupQueue , times (1 )).take ();
85- verify (taskExecutorClient , times (1 )).dispatch (taskExecutionRunnable );
86- verify (workerGroupQueue , times (1 )).add (any (PriorityAndDelayBasedTaskEntry .class ));
8793 }
94+
95+ @ Test
96+ public void testDispatch_TaskStatusCheck () throws Exception {
97+ TaskInstance taskInstance = new TaskInstance ();
98+ taskInstance .setState (TaskExecutionStatus .RUNNING_EXECUTION );
99+ when (taskExecutionRunnable .getTaskInstance ()).thenReturn (taskInstance );
100+
101+ workerGroupTaskDispatcher .add (taskExecutionRunnable , 0L );
102+
103+ workerGroupTaskDispatcher .start ();
104+ await ().atMost (Duration .ofSeconds (1 )).untilAsserted (() -> {
105+ workerGroupTaskDispatcher .close ();
106+ });
107+
108+ verify (taskExecutorClient , times (0 )).dispatch (taskExecutionRunnable );
109+ }
110+
111+ @ Test
112+ public void testClose_QueueEmpty () throws Exception {
113+ workerGroupTaskDispatcher .start ();
114+ workerGroupTaskDispatcher .close ();
115+ await ().atMost (Duration .ofSeconds (1 )).until (
116+ () -> workerGroupTaskDispatcher .getStatus ().equals (DispatchWorkerStatus .DELETE_SUCCESS ));
117+
118+ }
119+
120+ @ Test
121+ public void testClose_QueueNotEmpty () throws Exception {
122+ TaskInstance taskInstance = new TaskInstance ();
123+ taskInstance .setState (TaskExecutionStatus .SUBMITTED_SUCCESS );
124+ when (taskExecutionRunnable .getTaskInstance ()).thenReturn (taskInstance );
125+ workerGroupTaskDispatcher .add (taskExecutionRunnable , 1000 );
126+ workerGroupTaskDispatcher .start ();
127+ workerGroupTaskDispatcher .close ();
128+ assertEquals (DispatchWorkerStatus .DELETING , workerGroupTaskDispatcher .getStatus ());
129+ }
130+
88131}
0 commit comments