|
| 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.worker.registry; |
| 19 | + |
| 20 | +import static org.mockito.ArgumentMatchers.any; |
| 21 | +import static org.mockito.ArgumentMatchers.anyString; |
| 22 | +import static org.mockito.BDDMockito.given; |
| 23 | +import static org.mockito.Mockito.doNothing; |
| 24 | + |
| 25 | +import org.apache.dolphinscheduler.common.IStoppable; |
| 26 | +import org.apache.dolphinscheduler.common.lifecycle.ServerLifeCycleException; |
| 27 | +import org.apache.dolphinscheduler.common.lifecycle.ServerLifeCycleManager; |
| 28 | +import org.apache.dolphinscheduler.registry.api.ConnectStrategyProperties; |
| 29 | +import org.apache.dolphinscheduler.registry.api.RegistryClient; |
| 30 | +import org.apache.dolphinscheduler.registry.api.RegistryException; |
| 31 | +import org.apache.dolphinscheduler.registry.api.StrategyType; |
| 32 | +import org.apache.dolphinscheduler.server.worker.config.WorkerConfig; |
| 33 | +import org.apache.dolphinscheduler.server.worker.message.MessageRetryRunner; |
| 34 | +import org.apache.dolphinscheduler.server.worker.rpc.WorkerRpcServer; |
| 35 | +import org.apache.dolphinscheduler.server.worker.runner.WorkerTaskExecutorThreadPool; |
| 36 | + |
| 37 | +import java.time.Duration; |
| 38 | + |
| 39 | +import org.junit.jupiter.api.Assertions; |
| 40 | +import org.junit.jupiter.api.Test; |
| 41 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 42 | +import org.mockito.Mock; |
| 43 | +import org.mockito.MockedStatic; |
| 44 | +import org.mockito.Mockito; |
| 45 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 46 | +import org.slf4j.Logger; |
| 47 | +import org.slf4j.LoggerFactory; |
| 48 | + |
| 49 | +/** |
| 50 | + * worker registry test |
| 51 | + */ |
| 52 | +@ExtendWith(MockitoExtension.class) |
| 53 | +public class WorkerStrategyTest { |
| 54 | + |
| 55 | + private static final Logger log = LoggerFactory.getLogger(WorkerStrategyTest.class); |
| 56 | + @Mock |
| 57 | + private RegistryClient registryClient; |
| 58 | + @Mock |
| 59 | + private IStoppable stoppable; |
| 60 | + @Mock |
| 61 | + private WorkerConfig workerConfig; |
| 62 | + @Mock |
| 63 | + private WorkerRpcServer workerRpcServer; |
| 64 | + @Mock |
| 65 | + private MessageRetryRunner messageRetryRunner; |
| 66 | + @Mock |
| 67 | + private WorkerTaskExecutorThreadPool workerManagerThread; |
| 68 | + @Mock |
| 69 | + private ConnectStrategyProperties connectStrategyProperties; |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testWorkerStopStrategy() { |
| 73 | + given(registryClient.getStoppable()) |
| 74 | + .willReturn(stoppable); |
| 75 | + WorkerStopStrategy workerStopStrategy = new WorkerStopStrategy(); |
| 76 | + workerStopStrategy.registryClient = registryClient; |
| 77 | + workerStopStrategy.reconnect(); |
| 78 | + workerStopStrategy.disconnect(); |
| 79 | + Assertions.assertEquals(workerStopStrategy.getStrategyType(), StrategyType.STOP); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testWorkerWaitingStrategyreconnect() { |
| 84 | + WorkerWaitingStrategy workerWaitingStrategy = new WorkerWaitingStrategy( |
| 85 | + workerConfig, |
| 86 | + registryClient, |
| 87 | + messageRetryRunner, |
| 88 | + workerManagerThread); |
| 89 | + Assertions.assertEquals(workerWaitingStrategy.getStrategyType(), StrategyType.WAITING); |
| 90 | + |
| 91 | + try ( |
| 92 | + MockedStatic<ServerLifeCycleManager> serverLifeCycleManagerMockedStatic = |
| 93 | + Mockito.mockStatic(ServerLifeCycleManager.class)) { |
| 94 | + serverLifeCycleManagerMockedStatic |
| 95 | + .when(() -> ServerLifeCycleManager.isRunning()) |
| 96 | + .thenReturn(true); |
| 97 | + workerWaitingStrategy.reconnect(); |
| 98 | + |
| 99 | + } |
| 100 | + |
| 101 | + try ( |
| 102 | + MockedStatic<ServerLifeCycleManager> serverLifeCycleManagerMockedStatic = |
| 103 | + Mockito.mockStatic(ServerLifeCycleManager.class)) { |
| 104 | + doNothing().when(stoppable).stop(anyString()); |
| 105 | + given(registryClient.getStoppable()) |
| 106 | + .willReturn(stoppable); |
| 107 | + serverLifeCycleManagerMockedStatic |
| 108 | + .when(() -> ServerLifeCycleManager.recoverFromWaiting()) |
| 109 | + .thenThrow(new ServerLifeCycleException("")); |
| 110 | + workerWaitingStrategy.reconnect(); |
| 111 | + } |
| 112 | + |
| 113 | + try ( |
| 114 | + MockedStatic<ServerLifeCycleManager> serverLifeCycleManagerMockedStatic = |
| 115 | + Mockito.mockStatic(ServerLifeCycleManager.class)) { |
| 116 | + serverLifeCycleManagerMockedStatic |
| 117 | + .when(() -> ServerLifeCycleManager.recoverFromWaiting()) |
| 118 | + .thenAnswer(invocation -> null); |
| 119 | + workerWaitingStrategy.reconnect(); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void testWorkerWaitingStrategydisconnect() { |
| 125 | + WorkerWaitingStrategy workerWaitingStrategy = new WorkerWaitingStrategy( |
| 126 | + workerConfig, |
| 127 | + registryClient, |
| 128 | + messageRetryRunner, |
| 129 | + workerManagerThread); |
| 130 | + Assertions.assertEquals(workerWaitingStrategy.getStrategyType(), StrategyType.WAITING); |
| 131 | + |
| 132 | + try ( |
| 133 | + MockedStatic<ServerLifeCycleManager> serverLifeCycleManagerMockedStatic = |
| 134 | + Mockito.mockStatic(ServerLifeCycleManager.class)) { |
| 135 | + doNothing().when(stoppable).stop(anyString()); |
| 136 | + given(registryClient.getStoppable()) |
| 137 | + .willReturn(stoppable); |
| 138 | + serverLifeCycleManagerMockedStatic |
| 139 | + .when(() -> ServerLifeCycleManager.toWaiting()) |
| 140 | + .thenThrow(new ServerLifeCycleException("")); |
| 141 | + workerWaitingStrategy.disconnect(); |
| 142 | + } |
| 143 | + |
| 144 | + try ( |
| 145 | + MockedStatic<ServerLifeCycleManager> serverLifeCycleManagerMockedStatic = |
| 146 | + Mockito.mockStatic(ServerLifeCycleManager.class)) { |
| 147 | + given(connectStrategyProperties.getMaxWaitingTime()).willReturn(Duration.ofSeconds(1)); |
| 148 | + given(workerConfig.getRegistryDisconnectStrategy()).willReturn(connectStrategyProperties); |
| 149 | + Mockito.reset(registryClient); |
| 150 | + doNothing().when(registryClient).connectUntilTimeout(any()); |
| 151 | + serverLifeCycleManagerMockedStatic |
| 152 | + .when(() -> ServerLifeCycleManager.toWaiting()) |
| 153 | + .thenAnswer(invocation -> null); |
| 154 | + workerWaitingStrategy.disconnect(); |
| 155 | + } |
| 156 | + |
| 157 | + try ( |
| 158 | + MockedStatic<ServerLifeCycleManager> serverLifeCycleManagerMockedStatic = |
| 159 | + Mockito.mockStatic(ServerLifeCycleManager.class)) { |
| 160 | + given(connectStrategyProperties.getMaxWaitingTime()).willReturn(Duration.ofSeconds(1)); |
| 161 | + given(workerConfig.getRegistryDisconnectStrategy()).willReturn(connectStrategyProperties); |
| 162 | + Mockito.reset(registryClient); |
| 163 | + doNothing().when(stoppable).stop(anyString()); |
| 164 | + given(registryClient.getStoppable()) |
| 165 | + .willReturn(stoppable); |
| 166 | + Mockito.doThrow(new RegistryException("TEST")).when(registryClient).connectUntilTimeout(any()); |
| 167 | + serverLifeCycleManagerMockedStatic |
| 168 | + .when(() -> ServerLifeCycleManager.toWaiting()) |
| 169 | + .thenAnswer(invocation -> null); |
| 170 | + workerWaitingStrategy.disconnect(); |
| 171 | + } |
| 172 | + |
| 173 | + try ( |
| 174 | + MockedStatic<ServerLifeCycleManager> serverLifeCycleManagerMockedStatic = |
| 175 | + Mockito.mockStatic(ServerLifeCycleManager.class)) { |
| 176 | + Mockito.reset(workerConfig); |
| 177 | + given(workerConfig.getRegistryDisconnectStrategy()).willThrow(new NullPointerException("")); |
| 178 | + doNothing().when(stoppable).stop(anyString()); |
| 179 | + given(registryClient.getStoppable()) |
| 180 | + .willReturn(stoppable); |
| 181 | + serverLifeCycleManagerMockedStatic |
| 182 | + .when(() -> ServerLifeCycleManager.toWaiting()) |
| 183 | + .thenAnswer(invocation -> null); |
| 184 | + workerWaitingStrategy.disconnect(); |
| 185 | + } |
| 186 | + } |
| 187 | +} |
0 commit comments