|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.dolphinscheduler.server.master.runner; |
| 19 | + |
| 20 | +import static java.time.Duration.ofSeconds; |
| 21 | +import static org.awaitility.Awaitility.await; |
| 22 | +import static org.mockito.ArgumentMatchers.any; |
| 23 | +import static org.mockito.Mockito.atLeastOnce; |
| 24 | +import static org.mockito.Mockito.doNothing; |
| 25 | +import static org.mockito.Mockito.mock; |
| 26 | +import static org.mockito.Mockito.never; |
| 27 | +import static org.mockito.Mockito.verify; |
| 28 | +import static org.mockito.Mockito.when; |
| 29 | + |
| 30 | +import org.apache.dolphinscheduler.common.utils.JSONUtils; |
| 31 | +import org.apache.dolphinscheduler.dao.entity.ProcessInstance; |
| 32 | +import org.apache.dolphinscheduler.dao.entity.TaskInstance; |
| 33 | +import org.apache.dolphinscheduler.plugin.task.api.TaskExecutionContext; |
| 34 | +import org.apache.dolphinscheduler.plugin.task.api.enums.TaskExecutionStatus; |
| 35 | +import org.apache.dolphinscheduler.server.master.runner.dispatcher.TaskDispatchFactory; |
| 36 | +import org.apache.dolphinscheduler.server.master.runner.dispatcher.TaskDispatcher; |
| 37 | +import org.apache.dolphinscheduler.server.master.runner.operator.TaskExecuteRunnableOperatorManager; |
| 38 | + |
| 39 | +import java.util.HashMap; |
| 40 | + |
| 41 | +import org.junit.jupiter.api.Test; |
| 42 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 43 | +import org.mockito.InjectMocks; |
| 44 | +import org.mockito.Mock; |
| 45 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 46 | +import org.mockito.junit.jupiter.MockitoSettings; |
| 47 | +import org.mockito.quality.Strictness; |
| 48 | + |
| 49 | +@ExtendWith(MockitoExtension.class) |
| 50 | +@MockitoSettings(strictness = Strictness.LENIENT) |
| 51 | +class GlobalTaskDispatchWaitingQueueLooperTest { |
| 52 | + |
| 53 | + @InjectMocks |
| 54 | + private GlobalTaskDispatchWaitingQueueLooper globalTaskDispatchWaitingQueueLooper; |
| 55 | + |
| 56 | + @Mock |
| 57 | + private GlobalTaskDispatchWaitingQueue globalTaskDispatchWaitingQueue; |
| 58 | + |
| 59 | + @Mock |
| 60 | + private TaskDispatchFactory taskDispatchFactory; |
| 61 | + |
| 62 | + @Test |
| 63 | + void testTaskExecutionRunnableStatusIsNotSubmitted() throws Exception { |
| 64 | + ProcessInstance processInstance = new ProcessInstance(); |
| 65 | + TaskInstance taskInstance = new TaskInstance(); |
| 66 | + taskInstance.setState(TaskExecutionStatus.KILL); |
| 67 | + taskInstance.setTaskParams(JSONUtils.toJsonString(new HashMap<>())); |
| 68 | + TaskExecutionContext taskExecutionContext = new TaskExecutionContext(); |
| 69 | + TaskExecuteRunnableOperatorManager taskExecuteRunnableOperatorManager = |
| 70 | + new TaskExecuteRunnableOperatorManager(); |
| 71 | + DefaultTaskExecuteRunnable defaultTaskExecuteRunnable = new DefaultTaskExecuteRunnable(processInstance, |
| 72 | + taskInstance, taskExecutionContext, taskExecuteRunnableOperatorManager); |
| 73 | + |
| 74 | + TaskDispatcher taskDispatcher = mock(TaskDispatcher.class); |
| 75 | + when(taskDispatchFactory.getTaskDispatcher(taskInstance)).thenReturn(taskDispatcher); |
| 76 | + doNothing().when(taskDispatcher).dispatchTask(any()); |
| 77 | + |
| 78 | + when(globalTaskDispatchWaitingQueue.takeTaskExecuteRunnable()).thenReturn(defaultTaskExecuteRunnable); |
| 79 | + globalTaskDispatchWaitingQueueLooper.start(); |
| 80 | + await().during(ofSeconds(1)) |
| 81 | + .untilAsserted(() -> verify(taskDispatchFactory, never()).getTaskDispatcher(taskInstance)); |
| 82 | + globalTaskDispatchWaitingQueueLooper.close(); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + void testTaskExecutionRunnableStatusIsSubmitted() throws Exception { |
| 87 | + ProcessInstance processInstance = new ProcessInstance(); |
| 88 | + TaskInstance taskInstance = new TaskInstance(); |
| 89 | + taskInstance.setState(TaskExecutionStatus.SUBMITTED_SUCCESS); |
| 90 | + taskInstance.setTaskParams(JSONUtils.toJsonString(new HashMap<>())); |
| 91 | + TaskExecutionContext taskExecutionContext = new TaskExecutionContext(); |
| 92 | + TaskExecuteRunnableOperatorManager taskExecuteRunnableOperatorManager = |
| 93 | + new TaskExecuteRunnableOperatorManager(); |
| 94 | + DefaultTaskExecuteRunnable defaultTaskExecuteRunnable = new DefaultTaskExecuteRunnable(processInstance, |
| 95 | + taskInstance, taskExecutionContext, taskExecuteRunnableOperatorManager); |
| 96 | + |
| 97 | + TaskDispatcher taskDispatcher = mock(TaskDispatcher.class); |
| 98 | + when(taskDispatchFactory.getTaskDispatcher(taskInstance)).thenReturn(taskDispatcher); |
| 99 | + doNothing().when(taskDispatcher).dispatchTask(any()); |
| 100 | + |
| 101 | + when(globalTaskDispatchWaitingQueue.takeTaskExecuteRunnable()).thenReturn(defaultTaskExecuteRunnable); |
| 102 | + globalTaskDispatchWaitingQueueLooper.start(); |
| 103 | + await().atMost(ofSeconds(1)).untilAsserted(() -> { |
| 104 | + verify(taskDispatchFactory, atLeastOnce()).getTaskDispatcher(any(TaskInstance.class)); |
| 105 | + verify(taskDispatcher, atLeastOnce()).dispatchTask(any(TaskExecuteRunnable.class)); |
| 106 | + }); |
| 107 | + globalTaskDispatchWaitingQueueLooper.close(); |
| 108 | + |
| 109 | + } |
| 110 | +} |
0 commit comments