|
17 | 17 |
|
18 | 18 | package org.apache.dolphinscheduler.server.master.runner;
|
19 | 19 |
|
20 |
| -import static org.junit.jupiter.api.Assertions.assertEquals; |
21 |
| -import static org.junit.jupiter.api.Assertions.assertFalse; |
| 20 | +import static org.junit.Assert.assertTrue; |
| 21 | +import static org.mockito.Mockito.doNothing; |
| 22 | +import static org.mockito.Mockito.mock; |
| 23 | +import static org.mockito.Mockito.times; |
| 24 | +import static org.mockito.Mockito.verify; |
22 | 25 |
|
23 |
| -import org.apache.dolphinscheduler.dao.entity.TaskInstance; |
24 |
| -import org.apache.dolphinscheduler.dao.entity.WorkerGroup; |
| 26 | +import org.apache.dolphinscheduler.server.master.engine.task.client.ITaskExecutorClient; |
25 | 27 | import org.apache.dolphinscheduler.server.master.engine.task.runnable.ITaskExecutionRunnable;
|
26 | 28 |
|
27 |
| -import java.util.Arrays; |
28 |
| -import java.util.List; |
| 29 | +import java.util.concurrent.ConcurrentHashMap; |
29 | 30 |
|
| 31 | +import org.junit.jupiter.api.BeforeEach; |
30 | 32 | import org.junit.jupiter.api.Test;
|
31 |
| -import org.junit.jupiter.api.extension.ExtendWith; |
| 33 | +import org.junit.runner.RunWith; |
32 | 34 | import org.mockito.InjectMocks;
|
33 | 35 | import org.mockito.Mock;
|
34 |
| -import org.mockito.Mockito; |
35 |
| -import org.mockito.junit.jupiter.MockitoExtension; |
| 36 | +import org.mockito.junit.MockitoJUnitRunner; |
| 37 | +import org.springframework.test.util.ReflectionTestUtils; |
36 | 38 |
|
37 |
| -@ExtendWith(MockitoExtension.class) |
| 39 | +@RunWith(MockitoJUnitRunner.class) |
38 | 40 | public class WorkerGroupTaskDispatcherManagerTest {
|
39 | 41 |
|
40 | 42 | @InjectMocks
|
41 |
| - private WorkerGroupTaskDispatcherManager manager; |
| 43 | + private WorkerGroupTaskDispatcherManager workerGroupTaskDispatcherManager; |
42 | 44 |
|
43 | 45 | @Mock
|
44 |
| - private ITaskExecutionRunnable taskExecutionRunnable; |
| 46 | + private ITaskExecutorClient taskExecutorClient; |
45 | 47 |
|
46 | 48 | @Mock
|
47 |
| - private TaskInstance taskInstance; |
| 49 | + private ITaskExecutionRunnable taskExecutionRunnable; |
| 50 | + |
| 51 | + @BeforeEach |
| 52 | + public void setUp() { |
| 53 | + workerGroupTaskDispatcherManager = new WorkerGroupTaskDispatcherManager(); |
| 54 | + ReflectionTestUtils.setField(workerGroupTaskDispatcherManager, "taskExecutorClient", taskExecutorClient); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void addTaskToWorkerGroup_NewWorkerGroup_ShouldAddTask() { |
| 59 | + String workerGroup = "newGroup"; |
| 60 | + long delayTimeMills = 1000; |
| 61 | + |
| 62 | + workerGroupTaskDispatcherManager.addTaskToWorkerGroup(workerGroup, taskExecutionRunnable, delayTimeMills); |
| 63 | + |
| 64 | + ConcurrentHashMap<String, WorkerGroupTaskDispatcher> dispatchWorkerMap = |
| 65 | + (ConcurrentHashMap<String, WorkerGroupTaskDispatcher>) ReflectionTestUtils |
| 66 | + .getField(workerGroupTaskDispatcherManager, "dispatchWorkerMap"); |
| 67 | + |
| 68 | + assert dispatchWorkerMap != null; |
| 69 | + assertTrue(dispatchWorkerMap.containsKey(workerGroup)); |
| 70 | + } |
48 | 71 |
|
49 | 72 | @Test
|
50 |
| - public void testAddTaskToWorkerGroupTaskToWorkerGroupQueueTaskToNonExistingWorkerGroup_ShouldReturnFalse() { |
51 |
| - Mockito.when(taskExecutionRunnable.getTaskInstance()).thenReturn(taskInstance); |
52 |
| - String workerGroupName = "nonExistingGroup"; |
53 |
| - boolean result = manager.addTaskToWorkerGroup(workerGroupName, taskExecutionRunnable, 0L); |
54 |
| - assertFalse(result); |
| 73 | + public void addTaskToWorkerGroup_ExistingWorkerGroup_ShouldAddTask() { |
| 74 | + String workerGroup = "existingGroup"; |
| 75 | + long delayTimeMills = 1000; |
| 76 | + |
| 77 | + WorkerGroupTaskDispatcher mockDispatcher = mock(WorkerGroupTaskDispatcher.class); |
| 78 | + |
| 79 | + ConcurrentHashMap<String, WorkerGroupTaskDispatcher> dispatchWorkerMap = new ConcurrentHashMap<>(); |
| 80 | + dispatchWorkerMap.put(workerGroup, mockDispatcher); |
| 81 | + |
| 82 | + ReflectionTestUtils.setField(workerGroupTaskDispatcherManager, "dispatchWorkerMap", dispatchWorkerMap); |
| 83 | + doNothing().when(mockDispatcher).start(); |
| 84 | + workerGroupTaskDispatcherManager.addTaskToWorkerGroup(workerGroup, taskExecutionRunnable, delayTimeMills); |
| 85 | + |
| 86 | + verify(mockDispatcher, times(1)).addTaskToWorkerGroupQueue(taskExecutionRunnable, delayTimeMills); |
55 | 87 | }
|
56 | 88 |
|
57 | 89 | @Test
|
58 |
| - public void testOnWorkerGroupAdd_ShouldAddTaskToWorkerGroupTaskToWorkerGroupQueueWorkerGroups() { |
59 |
| - WorkerGroup group1 = new WorkerGroup(); |
60 |
| - WorkerGroup group2 = new WorkerGroup(); |
61 |
| - group1.setName("testGroup1"); |
62 |
| - group2.setName("testGroup2"); |
63 |
| - List<WorkerGroup> workerGroups = Arrays.asList(group1, group2); |
64 |
| - manager.onWorkerGroupAdd(workerGroups); |
65 |
| - assertEquals(2, manager.getDispatchWorkerMap().size()); |
| 90 | + public void close_ShouldCloseAllWorkerGroups() throws Exception { |
| 91 | + WorkerGroupTaskDispatcher mockDispatcher1 = mock(WorkerGroupTaskDispatcher.class); |
| 92 | + WorkerGroupTaskDispatcher mockDispatcher2 = mock(WorkerGroupTaskDispatcher.class); |
| 93 | + |
| 94 | + ConcurrentHashMap<String, WorkerGroupTaskDispatcher> dispatchWorkerMap = new ConcurrentHashMap<>(); |
| 95 | + dispatchWorkerMap.put("group1", mockDispatcher1); |
| 96 | + dispatchWorkerMap.put("group2", mockDispatcher2); |
| 97 | + |
| 98 | + ReflectionTestUtils.setField(workerGroupTaskDispatcherManager, "dispatchWorkerMap", dispatchWorkerMap); |
| 99 | + |
| 100 | + workerGroupTaskDispatcherManager.close(); |
| 101 | + |
| 102 | + verify(mockDispatcher1, times(1)).close(); |
| 103 | + verify(mockDispatcher2, times(1)).close(); |
66 | 104 | }
|
67 | 105 | }
|
0 commit comments