yangyichao-mango commented on a change in pull request #3442:
URL: 
https://github.com/apache/incubator-dolphinscheduler/pull/3442#discussion_r468289587



##########
File path: 
dolphinscheduler-server/src/test/java/org/apache/dolphinscheduler/server/master/DependentTaskTest.java
##########
@@ -37,188 +45,426 @@
 import org.slf4j.LoggerFactory;
 import org.springframework.context.ApplicationContext;
 
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.List;
-
 @RunWith(MockitoJUnitRunner.Silent.class)
 public class DependentTaskTest {
 
     private static final Logger logger = 
LoggerFactory.getLogger(DependentTaskTest.class);
 
-    private ProcessService processService;
-    private ApplicationContext applicationContext;
+    /**
+     * TaskNode.runFlag : task can be run normally
+     */
+    public static final String FLOWNODE_RUN_FLAG_NORMAL = "NORMAL";
 
+    private ProcessService processService;
 
-    private MasterConfig config;
+    /**
+     * the dependent task to be tested
+     *   ProcessDefinition  id=1
+     *   Task               id=task-10, name=D
+     *   ProcessInstance    id=100
+     *   TaskInstance       id=1000
+     * notice: must be initialized by setupTaskInstance() on each test case
+     */
+    private ProcessInstance processInstance;
+    private TaskInstance taskInstance;
 
     @Before
-    public void before() throws Exception{
+    public void before() throws Exception {
+        ApplicationContext applicationContext = 
Mockito.mock(ApplicationContext.class);
+        SpringApplicationContext springApplicationContext = new 
SpringApplicationContext();
+        springApplicationContext.setApplicationContext(applicationContext);
 
-        config = new MasterConfig();
+        MasterConfig config = new MasterConfig();
         config.setMasterTaskCommitRetryTimes(3);
         config.setMasterTaskCommitInterval(1000);
-        processService = Mockito.mock(ProcessService.class);
-        DateInterval dateInterval =DependentDateUtils.getTodayInterval(new 
Date()).get(0);
-        Mockito.when(processService
-                .findLastRunningProcess(4, dateInterval.getStartTime(),
-                        dateInterval.getEndTime()))
-                .thenReturn(findLastProcessInterval());
+        
Mockito.when(applicationContext.getBean(MasterConfig.class)).thenReturn(config);
 
+        processService = Mockito.mock(ProcessService.class);
+        
Mockito.when(applicationContext.getBean(ProcessService.class)).thenReturn(processService);
 
+        ProcessDefinition processDefinition = getProcessDefinition(1);
+        processInstance = getProcessInstance(100, 1);
+        taskInstance = new TaskInstance();
+        taskInstance.setId(1000);
+        taskInstance.setProcessInstanceId(processInstance.getId());
+        
taskInstance.setProcessDefinitionId(processInstance.getProcessDefinitionId());
 
+        // for MasterBaseTaskExecThread.call
+        // for DependentTaskExecThread.waitTaskQuit
         Mockito.when(processService
-                .getTaskNodeListByDefinitionId(4))
-                .thenReturn(getTaskNodes());
-        Mockito.when(processService
-                .findValidTaskListByProcessId(11))
-                .thenReturn(getTaskInstances());
+                .findProcessInstanceById(100))
+                .thenAnswer(i -> processInstance);
 
+        // for MasterBaseTaskExecThread.submit
         Mockito.when(processService
-                .findTaskInstanceById(252612))
-                .thenReturn(getTaskInstance());
-
+                .submitTask(Mockito.argThat(taskInstance -> 
taskInstance.getId() == 1000)))
+                .thenAnswer(i -> taskInstance);
 
-        Mockito.when(processService.findProcessInstanceById(10111))
-                .thenReturn(getProcessInstance());
-        Mockito.when(processService.findProcessDefineById(0))
-                .thenReturn(getProcessDefinition());
-        Mockito.when(processService.saveTaskInstance(getTaskInstance()))
+        // for DependentTaskExecThread.initTaskParameters
+        Mockito.when(processService
+                .updateTaskInstance(Mockito.any()))
+                .thenReturn(true);
+        // for DependentTaskExecThread.updateTaskState
+        Mockito.when(processService
+                .saveTaskInstance(Mockito.any()))
                 .thenReturn(true);
 
-        applicationContext = Mockito.mock(ApplicationContext.class);
-        SpringApplicationContext springApplicationContext = new 
SpringApplicationContext();
-        springApplicationContext.setApplicationContext(applicationContext);
-        
Mockito.when(applicationContext.getBean(ProcessService.class)).thenReturn(processService);
-        
Mockito.when(applicationContext.getBean(MasterConfig.class)).thenReturn(config);
+        // for DependentTaskExecThread.waitTaskQuit
+        Mockito.when(processService
+                .findTaskInstanceById(1000))
+                .thenAnswer(i -> taskInstance);
     }
 
+    /**
+     * task basic function for DependentTask
+     * TODO:
+     */
     @Test
-    public void testDependAll() throws Exception{
-
-        TaskInstance taskInstance = getTaskInstance();
-        String dependString = 
"{\"dependTaskList\":[{\"dependItemList\":[{\"dateValue\":\"today\",\"depTasks\":\"ALL\",\"projectId\":1,\"definitionList\":[{\"label\":\"C\",\"value\":4},{\"label\":\"B\",\"value\":3},{\"label\":\"A\",\"value\":2}],\"cycle\":\"day\",\"definitionId\":4}],\"relation\":\"AND\"}],\"relation\":\"AND\"}";
-        taskInstance.setDependency(dependString);
-
-        Mockito.when(processService.submitTask(taskInstance))
-                .thenReturn(taskInstance);
-        DependentTaskExecThread dependentTask =
-                new DependentTaskExecThread(taskInstance);
-
-        dependentTask.call();
-
-        Assert.assertEquals(ExecutionStatus.SUCCESS, 
dependentTask.getTaskInstance().getState());
-
-        DateInterval dateInterval =DependentDateUtils.getTodayInterval(new 
Date()).get(0);
-
-
-        Mockito.when(processService
-                .findLastRunningProcess(4, dateInterval.getStartTime(),
-                        dateInterval.getEndTime()))
-                .thenReturn(findLastStopProcessInterval());
-        DependentTaskExecThread dependentFailure = new 
DependentTaskExecThread(taskInstance);
-        dependentFailure.call();
-        Assert.assertEquals(ExecutionStatus.FAILURE, 
dependentFailure.getTaskInstance().getState());
+    public void testBasic() throws Exception {

Review comment:
       Some tests are full of NPE, please mock enough and right beans to avoid 
this situation.
   Thx a lot~
   
![image](https://user-images.githubusercontent.com/29545877/89850451-f50fa800-dbbc-11ea-8b55-9da7940a5fb6.png)
   




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to