Browse Source

Remove powermock in dolphinscheduler-master module (#12143)

* Remove powermock related code in dolphinscheduler-master module
* Remove redundant comments, useless code and unnecessary stubbings
3.2.0-release
Eric Gao 2 years ago committed by GitHub
parent
commit
cc82206c33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      dolphinscheduler-master/pom.xml
  2. 63
      dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/SubProcessTaskTest.java
  3. 5
      dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/processor/CacheProcessorTest.java
  4. 35
      dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/processor/TaskAckProcessorTest.java
  5. 4
      dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/processor/TaskKillResponseProcessorTest.java
  6. 24
      dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/registry/MasterRegistryClientTest.java
  7. 44
      dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/runner/MasterTaskExecThreadTest.java
  8. 49
      dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/runner/WorkflowExecuteRunnableTest.java
  9. 22
      dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/service/FailoverServiceTest.java

8
dolphinscheduler-master/pom.xml

@ -277,6 +277,14 @@
<artifactId>spring-cloud-starter-kubernetes-fabric8-config</artifactId> <artifactId>spring-cloud-starter-kubernetes-fabric8-config</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>3.12.4</version>
<!-- TODO: move this dependency to root pom after removing powermock in the whole project -->
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>

63
dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/SubProcessTaskTest.java

@ -18,11 +18,10 @@
package org.apache.dolphinscheduler.server.master; package org.apache.dolphinscheduler.server.master;
import org.apache.dolphinscheduler.common.enums.TimeoutFlag; import org.apache.dolphinscheduler.common.enums.TimeoutFlag;
import org.apache.dolphinscheduler.common.lifecycle.ServerLifeCycleManager;
import org.apache.dolphinscheduler.common.enums.WorkflowExecutionStatus; import org.apache.dolphinscheduler.common.enums.WorkflowExecutionStatus;
import org.apache.dolphinscheduler.common.lifecycle.ServerLifeCycleManager;
import org.apache.dolphinscheduler.common.model.TaskNode; import org.apache.dolphinscheduler.common.model.TaskNode;
import org.apache.dolphinscheduler.common.utils.JSONUtils; import org.apache.dolphinscheduler.common.utils.JSONUtils;
import org.apache.dolphinscheduler.dao.AlertDao;
import org.apache.dolphinscheduler.dao.entity.ProcessInstance; import org.apache.dolphinscheduler.dao.entity.ProcessInstance;
import org.apache.dolphinscheduler.dao.entity.TaskDefinition; import org.apache.dolphinscheduler.dao.entity.TaskDefinition;
import org.apache.dolphinscheduler.dao.entity.TaskInstance; import org.apache.dolphinscheduler.dao.entity.TaskInstance;
@ -35,22 +34,22 @@ import org.apache.dolphinscheduler.server.master.runner.task.SubTaskProcessor;
import org.apache.dolphinscheduler.server.master.runner.task.TaskAction; import org.apache.dolphinscheduler.server.master.runner.task.TaskAction;
import org.apache.dolphinscheduler.service.bean.SpringApplicationContext; import org.apache.dolphinscheduler.service.bean.SpringApplicationContext;
import org.apache.dolphinscheduler.service.process.ProcessService; import org.apache.dolphinscheduler.service.process.ProcessService;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.MockedStatic;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito; import org.mockito.junit.MockitoJUnitRunner;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import java.time.Duration; @RunWith(MockitoJUnitRunner.class)
import java.util.ArrayList;
import java.util.List;
@RunWith(PowerMockRunner.class)
@PrepareForTest({ServerLifeCycleManager.class})
public class SubProcessTaskTest { public class SubProcessTaskTest {
/** /**
@ -62,6 +61,8 @@ public class SubProcessTaskTest {
private ProcessInstance processInstance; private ProcessInstance processInstance;
private MockedStatic<ServerLifeCycleManager> mockedStaticServerLifeCycleManager;
@Before @Before
public void before() { public void before() {
ApplicationContext applicationContext = Mockito.mock(ApplicationContext.class); ApplicationContext applicationContext = Mockito.mock(ApplicationContext.class);
@ -73,38 +74,26 @@ public class SubProcessTaskTest {
config.setTaskCommitRetryTimes(3); config.setTaskCommitRetryTimes(3);
config.setTaskCommitInterval(Duration.ofSeconds(1)); config.setTaskCommitInterval(Duration.ofSeconds(1));
PowerMockito.mockStatic(ServerLifeCycleManager.class); mockedStaticServerLifeCycleManager = Mockito.mockStatic(ServerLifeCycleManager.class);
PowerMockito.when(ServerLifeCycleManager.isStopped()).thenReturn(false); Mockito.when(ServerLifeCycleManager.isStopped()).thenReturn(false);
processService = Mockito.mock(ProcessService.class); processService = Mockito.mock(ProcessService.class);
Mockito.when(applicationContext.getBean(ProcessService.class)).thenReturn(processService); Mockito.when(applicationContext.getBean(ProcessService.class)).thenReturn(processService);
AlertDao alertDao = Mockito.mock(AlertDao.class);
Mockito.when(applicationContext.getBean(AlertDao.class)).thenReturn(alertDao);
processInstance = getProcessInstance(); processInstance = getProcessInstance();
TaskInstance taskInstance = getTaskInstance();
Mockito.when(processService
.findProcessInstanceById(processInstance.getId()))
.thenReturn(processInstance);
// for SubProcessTaskExecThread.setTaskInstanceState
Mockito.when(processService Mockito.when(processService
.updateTaskInstance(Mockito.any())) .updateTaskInstance(Mockito.any()))
.thenReturn(true); .thenReturn(true);
// for MasterBaseTaskExecThread.submit
Mockito.when(processService
.submitTask(processInstance, taskInstance))
.thenAnswer(t -> t.getArgument(0));
TaskDefinition taskDefinition = new TaskDefinition(); TaskDefinition taskDefinition = new TaskDefinition();
taskDefinition.setTimeoutFlag(TimeoutFlag.OPEN); taskDefinition.setTimeoutFlag(TimeoutFlag.OPEN);
taskDefinition.setTimeoutNotifyStrategy(TaskTimeoutStrategy.WARN); taskDefinition.setTimeoutNotifyStrategy(TaskTimeoutStrategy.WARN);
taskDefinition.setTimeout(0); taskDefinition.setTimeout(0);
Mockito.when(processService.findTaskDefinition(1L, 1)) }
.thenReturn(taskDefinition);
@After
public void after() {
mockedStaticServerLifeCycleManager.close();
} }
private TaskInstance testBasicInit(WorkflowExecutionStatus expectResult) { private TaskInstance testBasicInit(WorkflowExecutionStatus expectResult) {
@ -112,10 +101,6 @@ public class SubProcessTaskTest {
ProcessInstance subProcessInstance = getSubProcessInstance(expectResult); ProcessInstance subProcessInstance = getSubProcessInstance(expectResult);
subProcessInstance.setVarPool(getProperty()); subProcessInstance.setVarPool(getProperty());
// for SubProcessTaskExecThread.waitTaskQuit
Mockito.when(processService
.findProcessInstanceById(subProcessInstance.getId()))
.thenReturn(subProcessInstance);
Mockito.when(processService Mockito.when(processService
.findSubProcessInstance(processInstance.getId(), taskInstance.getId())) .findSubProcessInstance(processInstance.getId(), taskInstance.getId()))
.thenReturn(subProcessInstance); .thenReturn(subProcessInstance);
@ -125,10 +110,7 @@ public class SubProcessTaskTest {
@Test @Test
public void testBasicSuccess() { public void testBasicSuccess() {
TaskInstance taskInstance = testBasicInit(WorkflowExecutionStatus.SUCCESS); testBasicInit(WorkflowExecutionStatus.SUCCESS);
// SubProcessTaskExecThread taskExecThread = new SubProcessTaskExecThread(taskInstance);
// taskExecThread.call();
// Assert.assertEquals(ExecutionStatus.SUCCESS, taskExecThread.getTaskInstance().getState());
} }
@Test @Test
@ -161,10 +143,7 @@ public class SubProcessTaskTest {
@Test @Test
public void testBasicFailure() { public void testBasicFailure() {
TaskInstance taskInstance = testBasicInit(WorkflowExecutionStatus.FAILURE); testBasicInit(WorkflowExecutionStatus.FAILURE);
// SubProcessTaskExecThread taskExecThread = new SubProcessTaskExecThread(taskInstance);
// taskExecThread.call();
// Assert.assertEquals(ExecutionStatus.FAILURE, taskExecThread.getTaskInstance().getState());
} }
private TaskNode getTaskNode() { private TaskNode getTaskNode() {

5
dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/processor/CacheProcessorTest.java

@ -28,7 +28,7 @@ import org.junit.runner.RunWith;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.powermock.modules.junit4.PowerMockRunner; import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.cache.Cache; import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager; import org.springframework.cache.CacheManager;
@ -37,8 +37,9 @@ import io.netty.channel.Channel;
/** /**
* task ack processor test * task ack processor test
*/ */
@RunWith(PowerMockRunner.class) @RunWith(MockitoJUnitRunner.class)
public class CacheProcessorTest { public class CacheProcessorTest {
@InjectMocks @InjectMocks
private CacheProcessor cacheProcessor = new CacheProcessor(); private CacheProcessor cacheProcessor = new CacheProcessor();

35
dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/processor/TaskAckProcessorTest.java

@ -29,17 +29,15 @@ import java.util.Date;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito; import org.mockito.Mockito;
import org.powermock.core.classloader.annotations.PrepareForTest; import org.mockito.junit.MockitoJUnitRunner;
import org.powermock.modules.junit4.PowerMockRunner;
import io.netty.channel.Channel; import io.netty.channel.Channel;
/** /**
* task ack processor test * task ack processor test
*/ */
@RunWith(PowerMockRunner.class) @RunWith(MockitoJUnitRunner.class)
@PrepareForTest({SpringApplicationContext.class, TaskEvent.class})
public class TaskAckProcessorTest { public class TaskAckProcessorTest {
private TaskExecuteRunningProcessor taskExecuteRunningProcessor; private TaskExecuteRunningProcessor taskExecuteRunningProcessor;
@ -51,18 +49,18 @@ public class TaskAckProcessorTest {
@Before @Before
public void before() { public void before() {
PowerMockito.mockStatic(SpringApplicationContext.class); Mockito.mockStatic(SpringApplicationContext.class);
taskEventService = PowerMockito.mock(TaskEventService.class); taskEventService = Mockito.mock(TaskEventService.class);
PowerMockito.when(SpringApplicationContext.getBean(TaskEventService.class)).thenReturn(taskEventService); Mockito.when(SpringApplicationContext.getBean(TaskEventService.class)).thenReturn(taskEventService);
processService = PowerMockito.mock(ProcessService.class); processService = Mockito.mock(ProcessService.class);
PowerMockito.when(SpringApplicationContext.getBean(ProcessService.class)).thenReturn(processService); Mockito.when(SpringApplicationContext.getBean(ProcessService.class)).thenReturn(processService);
taskExecuteRunningProcessor = new TaskExecuteRunningProcessor(); taskExecuteRunningProcessor = new TaskExecuteRunningProcessor();
channel = PowerMockito.mock(Channel.class); channel = Mockito.mock(Channel.class);
taskResponseEvent = PowerMockito.mock(TaskEvent.class); taskResponseEvent = Mockito.mock(TaskEvent.class);
taskExecuteRunningMessage = new TaskExecuteRunningCommand("127.0.0.1:5678", taskExecuteRunningMessage = new TaskExecuteRunningCommand("127.0.0.1:5678",
" 127.0.0.1:1234", " 127.0.0.1:1234",
@ -78,18 +76,5 @@ public class TaskAckProcessorTest {
@Test @Test
public void testProcess() { public void testProcess() {
// Command command = taskExecuteAckCommand.convert2Command();
// Assert.assertEquals(CommandType.TASK_EXECUTE_ACK,command.getType());
// InetSocketAddress socketAddress = new InetSocketAddress("localhost",12345);
// PowerMockito.when(channel.remoteAddress()).thenReturn(socketAddress);
// PowerMockito.mockStatic(TaskResponseEvent.class);
//
// PowerMockito.when(TaskResponseEvent.newAck(Mockito.any(), Mockito.any(), Mockito.anyString(),
// Mockito.anyString(), Mockito.anyString(), Mockito.anyInt(), channel))
// .thenReturn(taskResponseEvent);
// TaskInstance taskInstance = PowerMockito.mock(TaskInstance.class);
// PowerMockito.when(processService.findTaskInstanceById(Mockito.any())).thenReturn(taskInstance);
//
// taskAckProcessor.process(channel,command);
} }
} }

4
dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/processor/TaskKillResponseProcessorTest.java

@ -27,7 +27,7 @@ import java.util.ArrayList;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.powermock.api.mockito.PowerMockito; import org.mockito.Mockito;
import io.netty.channel.Channel; import io.netty.channel.Channel;
@ -45,7 +45,7 @@ public class TaskKillResponseProcessorTest {
@Before @Before
public void before() { public void before() {
taskKillResponseProcessor = new TaskKillResponseProcessor(); taskKillResponseProcessor = new TaskKillResponseProcessor();
channel = PowerMockito.mock(Channel.class); channel = Mockito.mock(Channel.class);
taskKillResponseCommand = new TaskKillResponseCommand(); taskKillResponseCommand = new TaskKillResponseCommand();
taskKillResponseCommand.setAppIds( taskKillResponseCommand.setAppIds(
new ArrayList<String>() { new ArrayList<String>() {

24
dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/registry/MasterRegistryClientTest.java

@ -18,7 +18,6 @@
package org.apache.dolphinscheduler.server.master.registry; package org.apache.dolphinscheduler.server.master.registry;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doNothing;
import org.apache.dolphinscheduler.common.enums.CommandType; import org.apache.dolphinscheduler.common.enums.CommandType;
import org.apache.dolphinscheduler.common.enums.NodeType; import org.apache.dolphinscheduler.common.enums.NodeType;
@ -30,9 +29,7 @@ import org.apache.dolphinscheduler.server.master.task.MasterHeartBeatTask;
import org.apache.dolphinscheduler.service.process.ProcessService; import org.apache.dolphinscheduler.service.process.ProcessService;
import org.apache.dolphinscheduler.service.registry.RegistryClient; import org.apache.dolphinscheduler.service.registry.RegistryClient;
import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.Optional;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -40,17 +37,13 @@ import org.junit.runner.RunWith;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.mockito.junit.MockitoJUnitRunner;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.springframework.test.util.ReflectionTestUtils; import org.springframework.test.util.ReflectionTestUtils;
/** /**
* MasterRegistryClientTest * MasterRegistryClientTest
*/ */
@RunWith(PowerMockRunner.class) @RunWith(MockitoJUnitRunner.class)
@PrepareForTest({RegistryClient.class})
@PowerMockIgnore({"javax.management.*"})
public class MasterRegistryClientTest { public class MasterRegistryClientTest {
@InjectMocks @InjectMocks
@ -70,12 +63,7 @@ public class MasterRegistryClientTest {
@Before @Before
public void before() throws Exception { public void before() throws Exception {
given(registryClient.getLock(Mockito.anyString())).willReturn(true);
given(registryClient.releaseLock(Mockito.anyString())).willReturn(true);
given(registryClient.getHostByEventDataPath(Mockito.anyString())).willReturn("127.0.0.1:8080"); given(registryClient.getHostByEventDataPath(Mockito.anyString())).willReturn("127.0.0.1:8080");
given(registryClient.getStoppable()).willReturn(cause -> {
});
ReflectionTestUtils.setField(masterRegistryClient, "registryClient", registryClient); ReflectionTestUtils.setField(masterRegistryClient, "registryClient", registryClient);
ReflectionTestUtils.setField(masterRegistryClient, "masterHeartBeatTask", masterHeartBeatTask); ReflectionTestUtils.setField(masterRegistryClient, "masterHeartBeatTask", masterHeartBeatTask);
@ -85,23 +73,15 @@ public class MasterRegistryClientTest {
processInstance.setRestartTime(new Date()); processInstance.setRestartTime(new Date());
processInstance.setHistoryCmd("xxx"); processInstance.setHistoryCmd("xxx");
processInstance.setCommandType(CommandType.STOP); processInstance.setCommandType(CommandType.STOP);
given(processService.queryNeedFailoverProcessInstances(Mockito.anyString()))
.willReturn(Arrays.asList(processInstance));
doNothing().when(processService).processNeedFailoverProcessInstances(Mockito.any(ProcessInstance.class));
TaskInstance taskInstance = new TaskInstance(); TaskInstance taskInstance = new TaskInstance();
taskInstance.setId(1); taskInstance.setId(1);
taskInstance.setStartTime(new Date()); taskInstance.setStartTime(new Date());
taskInstance.setHost("127.0.0.1:8080"); taskInstance.setHost("127.0.0.1:8080");
given(processService.queryNeedFailoverTaskInstances(Mockito.anyString()))
.willReturn(Arrays.asList(taskInstance));
given(processService.findProcessInstanceDetailById(Mockito.anyInt())).willReturn(Optional.of(processInstance));
given(registryClient.checkNodeExists(Mockito.anyString(), Mockito.any())).willReturn(true); given(registryClient.checkNodeExists(Mockito.anyString(), Mockito.any())).willReturn(true);
Server server = new Server(); Server server = new Server();
server.setHost("127.0.0.1"); server.setHost("127.0.0.1");
server.setPort(8080); server.setPort(8080);
server.setCreateTime(new Date()); server.setCreateTime(new Date());
given(registryClient.getServerList(NodeType.WORKER)).willReturn(Arrays.asList(server));
given(registryClient.getServerList(NodeType.MASTER)).willReturn(Arrays.asList(server));
} }
@Test @Test

44
dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/runner/MasterTaskExecThreadTest.java

@ -17,11 +17,11 @@
package org.apache.dolphinscheduler.server.master.runner; package org.apache.dolphinscheduler.server.master.runner;
import org.apache.dolphinscheduler.plugin.task.api.enums.TaskExecutionStatus;
import org.apache.dolphinscheduler.plugin.task.api.enums.TaskTimeoutStrategy;
import org.apache.dolphinscheduler.common.enums.TimeoutFlag; import org.apache.dolphinscheduler.common.enums.TimeoutFlag;
import org.apache.dolphinscheduler.dao.entity.TaskDefinition; import org.apache.dolphinscheduler.dao.entity.TaskDefinition;
import org.apache.dolphinscheduler.dao.entity.TaskInstance; import org.apache.dolphinscheduler.dao.entity.TaskInstance;
import org.apache.dolphinscheduler.plugin.task.api.enums.TaskExecutionStatus;
import org.apache.dolphinscheduler.plugin.task.api.enums.TaskTimeoutStrategy;
import org.apache.dolphinscheduler.service.bean.SpringApplicationContext; import org.apache.dolphinscheduler.service.bean.SpringApplicationContext;
import org.apache.dolphinscheduler.service.process.ProcessService; import org.apache.dolphinscheduler.service.process.ProcessService;
@ -34,10 +34,9 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
import org.powermock.api.mockito.PowerMockito;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
@RunWith(MockitoJUnitRunner.Silent.class) @RunWith(MockitoJUnitRunner.class)
@Ignore @Ignore
public class MasterTaskExecThreadTest { public class MasterTaskExecThreadTest {
@ -45,12 +44,9 @@ public class MasterTaskExecThreadTest {
@Before @Before
public void setUp() { public void setUp() {
ApplicationContext applicationContext = PowerMockito.mock(ApplicationContext.class); ApplicationContext applicationContext = Mockito.mock(ApplicationContext.class);
this.springApplicationContext = new SpringApplicationContext(); this.springApplicationContext = new SpringApplicationContext();
springApplicationContext.setApplicationContext(applicationContext); springApplicationContext.setApplicationContext(applicationContext);
// this.registryCenter = PowerMockito.mock(RegistryCenter.class);
// PowerMockito.when(SpringApplicationContext.getBean(RegistryCenter.class))
// .thenReturn(this.registryCenter);
ProcessService processService = Mockito.mock(ProcessService.class); ProcessService processService = Mockito.mock(ProcessService.class);
Mockito.when(SpringApplicationContext.getBean(ProcessService.class)) Mockito.when(SpringApplicationContext.getBean(ProcessService.class))
.thenReturn(processService); .thenReturn(processService);
@ -58,18 +54,10 @@ public class MasterTaskExecThreadTest {
taskDefinition.setTimeoutFlag(TimeoutFlag.OPEN); taskDefinition.setTimeoutFlag(TimeoutFlag.OPEN);
taskDefinition.setTimeoutNotifyStrategy(TaskTimeoutStrategy.WARN); taskDefinition.setTimeoutNotifyStrategy(TaskTimeoutStrategy.WARN);
taskDefinition.setTimeout(0); taskDefinition.setTimeout(0);
Mockito.when(processService.findTaskDefinition(1L, 1))
.thenReturn(taskDefinition);
// this.masterTaskExecThread = new MasterTaskExecThread(getTaskInstance());
} }
@Test @Test
public void testExistsValidWorkerGroup1() { public void testExistsValidWorkerGroup1() {
/*
* Mockito.when(registryCenter.getWorkerGroupDirectly()).thenReturn(Sets.newHashSet()); boolean b =
* masterTaskExecThread.existsValidWorkerGroup("default"); Assert.assertFalse(b);
*/
} }
@Test @Test
@ -77,22 +65,12 @@ public class MasterTaskExecThreadTest {
Set<String> workerGroups = new HashSet<>(); Set<String> workerGroups = new HashSet<>();
workerGroups.add("test1"); workerGroups.add("test1");
workerGroups.add("test2"); workerGroups.add("test2");
/*
* Mockito.when(registryCenter.getWorkerGroupDirectly()).thenReturn(workerGroups); boolean b =
* masterTaskExecThread.existsValidWorkerGroup("default"); Assert.assertFalse(b);
*/
} }
@Test @Test
public void testExistsValidWorkerGroup3() { public void testExistsValidWorkerGroup3() {
Set<String> workerGroups = new HashSet<>(); Set<String> workerGroups = new HashSet<>();
workerGroups.add("test1"); workerGroups.add("test1");
/*
* Mockito.when(registryCenter.getWorkerGroupDirectly()).thenReturn(workerGroups);
* Mockito.when(registryCenter.getWorkerGroupNodesDirectly("test1")).thenReturn(workerGroups); boolean b =
* masterTaskExecThread.existsValidWorkerGroup("test1"); Assert.assertTrue(b);
*/
} }
@Test @Test
@ -100,24 +78,10 @@ public class MasterTaskExecThreadTest {
ProcessService processService = Mockito.mock(ProcessService.class); ProcessService processService = Mockito.mock(ProcessService.class);
Mockito.when(SpringApplicationContext.getBean(ProcessService.class)) Mockito.when(SpringApplicationContext.getBean(ProcessService.class))
.thenReturn(processService); .thenReturn(processService);
TaskInstance taskInstance = getTaskInstance();
Mockito.when(processService.findTaskInstanceById(252612))
.thenReturn(taskInstance);
Mockito.when(processService.updateTaskInstance(taskInstance))
.thenReturn(true);
TaskDefinition taskDefinition = new TaskDefinition(); TaskDefinition taskDefinition = new TaskDefinition();
taskDefinition.setTimeoutFlag(TimeoutFlag.OPEN); taskDefinition.setTimeoutFlag(TimeoutFlag.OPEN);
taskDefinition.setTimeoutNotifyStrategy(TaskTimeoutStrategy.WARN); taskDefinition.setTimeoutNotifyStrategy(TaskTimeoutStrategy.WARN);
taskDefinition.setTimeout(0); taskDefinition.setTimeout(0);
Mockito.when(processService.findTaskDefinition(1L, 1))
.thenReturn(taskDefinition);
// MasterTaskExecThread masterTaskExecThread = new MasterTaskExecThread(taskInstance);
// masterTaskExecThread.pauseTask();
// org.junit.Assert.assertEquals(ExecutionStatus.PAUSE, taskInstance.getState());
} }
private TaskInstance getTaskInstance() { private TaskInstance getTaskInstance() {

49
dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/runner/WorkflowExecuteRunnableTest.java

@ -21,14 +21,10 @@ import static org.apache.dolphinscheduler.common.Constants.CMDPARAM_COMPLEMENT_D
import static org.apache.dolphinscheduler.common.Constants.CMDPARAM_COMPLEMENT_DATA_START_DATE; import static org.apache.dolphinscheduler.common.Constants.CMDPARAM_COMPLEMENT_DATA_START_DATE;
import static org.apache.dolphinscheduler.common.Constants.CMD_PARAM_RECOVERY_START_NODE_STRING; import static org.apache.dolphinscheduler.common.Constants.CMD_PARAM_RECOVERY_START_NODE_STRING;
import static org.apache.dolphinscheduler.common.Constants.CMD_PARAM_START_NODES; import static org.apache.dolphinscheduler.common.Constants.CMD_PARAM_START_NODES;
import static org.powermock.api.mockito.PowerMockito.mock;
import org.apache.dolphinscheduler.common.enums.CommandType;
import org.apache.dolphinscheduler.common.enums.Flag;
import org.apache.dolphinscheduler.common.enums.ProcessExecutionTypeEnum; import org.apache.dolphinscheduler.common.enums.ProcessExecutionTypeEnum;
import org.apache.dolphinscheduler.common.enums.WorkflowExecutionStatus; import org.apache.dolphinscheduler.common.enums.WorkflowExecutionStatus;
import org.apache.dolphinscheduler.common.graph.DAG; import org.apache.dolphinscheduler.common.graph.DAG;
import org.apache.dolphinscheduler.common.utils.DateUtils;
import org.apache.dolphinscheduler.common.utils.JSONUtils; import org.apache.dolphinscheduler.common.utils.JSONUtils;
import org.apache.dolphinscheduler.dao.entity.ProcessDefinition; import org.apache.dolphinscheduler.dao.entity.ProcessDefinition;
import org.apache.dolphinscheduler.dao.entity.ProcessInstance; import org.apache.dolphinscheduler.dao.entity.ProcessInstance;
@ -61,16 +57,10 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito; import org.mockito.junit.MockitoJUnitRunner;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
/** @RunWith(MockitoJUnitRunner.class)
* test for WorkflowExecuteThread
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({WorkflowExecuteRunnable.class})
public class WorkflowExecuteRunnableTest { public class WorkflowExecuteRunnableTest {
private WorkflowExecuteRunnable workflowExecuteThread; private WorkflowExecuteRunnable workflowExecuteThread;
@ -91,44 +81,32 @@ public class WorkflowExecuteRunnableTest {
@Before @Before
public void init() throws Exception { public void init() throws Exception {
applicationContext = mock(ApplicationContext.class); applicationContext = Mockito.mock(ApplicationContext.class);
SpringApplicationContext springApplicationContext = new SpringApplicationContext(); SpringApplicationContext springApplicationContext = new SpringApplicationContext();
springApplicationContext.setApplicationContext(applicationContext); springApplicationContext.setApplicationContext(applicationContext);
config = new MasterConfig(); config = new MasterConfig();
Mockito.when(applicationContext.getBean(MasterConfig.class)).thenReturn(config); processService = Mockito.mock(ProcessService.class);
processInstanceDao = Mockito.mock(ProcessInstanceDao.class);
processService = mock(ProcessService.class); processInstance = Mockito.mock(ProcessInstance.class);
Mockito.when(applicationContext.getBean(ProcessService.class)).thenReturn(processService);
processInstanceDao = mock(ProcessInstanceDao.class);
processInstance = mock(ProcessInstance.class);
Mockito.when(processInstance.getState()).thenReturn(WorkflowExecutionStatus.SUCCESS);
Mockito.when(processInstance.getHistoryCmd()).thenReturn(CommandType.COMPLEMENT_DATA.toString());
Mockito.when(processInstance.getIsSubProcess()).thenReturn(Flag.NO);
Mockito.when(processInstance.getScheduleTime()).thenReturn(DateUtils.stringToDate("2020-01-01 00:00:00"));
Map<String, String> cmdParam = new HashMap<>(); Map<String, String> cmdParam = new HashMap<>();
cmdParam.put(CMDPARAM_COMPLEMENT_DATA_START_DATE, "2020-01-01 00:00:00"); cmdParam.put(CMDPARAM_COMPLEMENT_DATA_START_DATE, "2020-01-01 00:00:00");
cmdParam.put(CMDPARAM_COMPLEMENT_DATA_END_DATE, "2020-01-20 23:00:00"); cmdParam.put(CMDPARAM_COMPLEMENT_DATA_END_DATE, "2020-01-20 23:00:00");
Mockito.when(processInstance.getCommandParam()).thenReturn(JSONUtils.toJsonString(cmdParam));
ProcessDefinition processDefinition = new ProcessDefinition(); ProcessDefinition processDefinition = new ProcessDefinition();
processDefinition.setGlobalParamMap(Collections.emptyMap()); processDefinition.setGlobalParamMap(Collections.emptyMap());
processDefinition.setGlobalParamList(Collections.emptyList()); processDefinition.setGlobalParamList(Collections.emptyList());
Mockito.when(processInstance.getProcessDefinition()).thenReturn(processDefinition); Mockito.when(processInstance.getProcessDefinition()).thenReturn(processDefinition);
stateWheelExecuteThread = mock(StateWheelExecuteThread.class); stateWheelExecuteThread = Mockito.mock(StateWheelExecuteThread.class);
curingGlobalParamsService = mock(CuringParamsService.class); curingGlobalParamsService = Mockito.mock(CuringParamsService.class);
NettyExecutorManager nettyExecutorManager = mock(NettyExecutorManager.class); NettyExecutorManager nettyExecutorManager = Mockito.mock(NettyExecutorManager.class);
ProcessAlertManager processAlertManager = mock(ProcessAlertManager.class); ProcessAlertManager processAlertManager = Mockito.mock(ProcessAlertManager.class);
workflowExecuteThread = PowerMockito.spy( workflowExecuteThread = Mockito.spy(
new WorkflowExecuteRunnable(processInstance, processService, processInstanceDao, nettyExecutorManager, new WorkflowExecuteRunnable(processInstance, processService, processInstanceDao, nettyExecutorManager,
processAlertManager, config, stateWheelExecuteThread, curingGlobalParamsService)); processAlertManager, config, stateWheelExecuteThread, curingGlobalParamsService));
// prepareProcess init dag
Field dag = WorkflowExecuteRunnable.class.getDeclaredField("dag"); Field dag = WorkflowExecuteRunnable.class.getDeclaredField("dag");
dag.setAccessible(true); dag.setAccessible(true);
dag.set(workflowExecuteThread, new DAG()); dag.set(workflowExecuteThread, new DAG());
PowerMockito.doNothing().when(workflowExecuteThread, "endProcess");
} }
@Test @Test
@ -136,7 +114,6 @@ public class WorkflowExecuteRunnableTest {
try { try {
Map<String, String> cmdParam = new HashMap<>(); Map<String, String> cmdParam = new HashMap<>();
cmdParam.put(CMD_PARAM_START_NODES, "1,2,3"); cmdParam.put(CMD_PARAM_START_NODES, "1,2,3");
Mockito.when(processInstance.getCommandParam()).thenReturn(JSONUtils.toJsonString(cmdParam));
Class<WorkflowExecuteRunnable> masterExecThreadClass = WorkflowExecuteRunnable.class; Class<WorkflowExecuteRunnable> masterExecThreadClass = WorkflowExecuteRunnable.class;
Method method = masterExecThreadClass.getDeclaredMethod("parseStartNodeName", String.class); Method method = masterExecThreadClass.getDeclaredMethod("parseStartNodeName", String.class);
method.setAccessible(true); method.setAccessible(true);
@ -249,19 +226,15 @@ public class WorkflowExecuteRunnableTest {
processDefinition1.setCode(11L); processDefinition1.setCode(11L);
processDefinition1.setExecutionType(ProcessExecutionTypeEnum.SERIAL_WAIT); processDefinition1.setExecutionType(ProcessExecutionTypeEnum.SERIAL_WAIT);
Mockito.when(processInstance.getId()).thenReturn(225); Mockito.when(processInstance.getId()).thenReturn(225);
Mockito.when(processService.findProcessInstanceById(225)).thenReturn(processInstance);
workflowExecuteThread.checkSerialProcess(processDefinition1); workflowExecuteThread.checkSerialProcess(processDefinition1);
Mockito.when(processInstance.getId()).thenReturn(225);
Mockito.when(processInstance.getNextProcessInstanceId()).thenReturn(222); Mockito.when(processInstance.getNextProcessInstanceId()).thenReturn(222);
ProcessInstance processInstance9 = new ProcessInstance(); ProcessInstance processInstance9 = new ProcessInstance();
processInstance9.setId(222); processInstance9.setId(222);
processInstance9.setProcessDefinitionCode(11L); processInstance9.setProcessDefinitionCode(11L);
processInstance9.setProcessDefinitionVersion(1); processInstance9.setProcessDefinitionVersion(1);
processInstance9.setState(WorkflowExecutionStatus.SERIAL_WAIT); processInstance9.setState(WorkflowExecutionStatus.SERIAL_WAIT);
Mockito.when(processService.findProcessInstanceById(225)).thenReturn(processInstance);
Mockito.when(processService.findProcessInstanceById(222)).thenReturn(processInstance9); Mockito.when(processService.findProcessInstanceById(222)).thenReturn(processInstance9);
workflowExecuteThread.checkSerialProcess(processDefinition1); workflowExecuteThread.checkSerialProcess(processDefinition1);
} catch (Exception e) { } catch (Exception e) {

22
dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/service/FailoverServiceTest.java

@ -44,7 +44,6 @@ import org.apache.dolphinscheduler.service.registry.RegistryClient;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.Optional;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
@ -52,19 +51,12 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.mockito.junit.MockitoJUnitRunner;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
/** @RunWith(MockitoJUnitRunner.class)
* MasterRegistryClientTest
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({RegistryClient.class})
@PowerMockIgnore({"javax.management.*"})
public class FailoverServiceTest { public class FailoverServiceTest {
private FailoverService failoverService; private FailoverService failoverService;
@ -104,7 +96,6 @@ public class FailoverServiceTest {
@Before @Before
public void before() throws Exception { public void before() throws Exception {
// init spring context
ApplicationContext applicationContext = Mockito.mock(ApplicationContext.class); ApplicationContext applicationContext = Mockito.mock(ApplicationContext.class);
SpringApplicationContext springApplicationContext = new SpringApplicationContext(); SpringApplicationContext springApplicationContext = new SpringApplicationContext();
springApplicationContext.setApplicationContext(applicationContext); springApplicationContext.setApplicationContext(applicationContext);
@ -132,10 +123,6 @@ public class FailoverServiceTest {
given(registryClient.getLock(Mockito.anyString())).willReturn(true); given(registryClient.getLock(Mockito.anyString())).willReturn(true);
given(registryClient.releaseLock(Mockito.anyString())).willReturn(true); given(registryClient.releaseLock(Mockito.anyString())).willReturn(true);
given(registryClient.getHostByEventDataPath(Mockito.anyString())).willReturn(testMasterHost);
given(registryClient.getStoppable()).willReturn(cause -> {
});
given(registryClient.checkNodeExists(Mockito.anyString(), Mockito.any())).willReturn(true);
processInstance = new ProcessInstance(); processInstance = new ProcessInstance();
processInstance.setId(1); processInstance.setId(1);
@ -157,16 +144,11 @@ public class FailoverServiceTest {
workerTaskInstance.setHost(testWorkerHost); workerTaskInstance.setHost(testWorkerHost);
workerTaskInstance.setTaskType(COMMON_TASK_TYPE); workerTaskInstance.setTaskType(COMMON_TASK_TYPE);
given(processService.queryNeedFailoverTaskInstances(Mockito.anyString()))
.willReturn(Arrays.asList(masterTaskInstance, workerTaskInstance));
given(processService.queryNeedFailoverProcessInstanceHost()).willReturn(Lists.newArrayList(testMasterHost));
given(processService.queryNeedFailoverProcessInstances(Mockito.anyString())) given(processService.queryNeedFailoverProcessInstances(Mockito.anyString()))
.willReturn(Arrays.asList(processInstance)); .willReturn(Arrays.asList(processInstance));
doNothing().when(processService).processNeedFailoverProcessInstances(Mockito.any(ProcessInstance.class)); doNothing().when(processService).processNeedFailoverProcessInstances(Mockito.any(ProcessInstance.class));
given(processService.findValidTaskListByProcessId(Mockito.anyInt(), Mockito.anyInt())) given(processService.findValidTaskListByProcessId(Mockito.anyInt(), Mockito.anyInt()))
.willReturn(Lists.newArrayList(masterTaskInstance, workerTaskInstance)); .willReturn(Lists.newArrayList(masterTaskInstance, workerTaskInstance));
given(processService.findProcessInstanceDetailById(Mockito.anyInt()))
.willReturn(Optional.ofNullable(processInstance));
Thread.sleep(1000); Thread.sleep(1000);
Server masterServer = new Server(); Server masterServer = new Server();

Loading…
Cancel
Save