[Bug-7832, Improvement] Add unit test for worker module, fix apache#7832. Optimize HttpUtils and HttpUtilsTest 1. Checking input parameters of getResponseContentString 2. Add unit test (#7798)

dailidong-patch-1
springmonster 2022-01-06 09:56:30 +08:00 committed by GitHub
parent 6d6babff5a
commit 0911fd711d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 5 deletions

View File

@ -41,6 +41,7 @@ import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.Objects;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
@ -141,6 +142,10 @@ public class HttpUtils {
* @return http get request response content
*/
public static String getResponseContentString(HttpGet httpget, CloseableHttpClient httpClient) {
if (Objects.isNull(httpget) || Objects.isNull(httpClient)) {
logger.error("HttpGet or HttpClient parameter is null");
return null;
}
String responseContent = null;
CloseableHttpResponse response = null;
try {

View File

@ -73,6 +73,12 @@ public class HttpUtilsTest {
httpget.setConfig(requestConfig);
String responseContent = HttpUtils.getResponseContentString(httpget, httpclient);
Assert.assertNotNull(responseContent);
responseContent = HttpUtils.getResponseContentString(null, httpclient);
Assert.assertNull(responseContent);
responseContent = HttpUtils.getResponseContentString(httpget, null);
Assert.assertNull(responseContent);
}

View File

@ -19,7 +19,9 @@ package org.apache.dolphinscheduler.server.worker.registry;
import static org.mockito.BDDMockito.given;
import org.apache.dolphinscheduler.common.enums.NodeType;
import org.apache.dolphinscheduler.server.worker.config.WorkerConfig;
import org.apache.dolphinscheduler.server.worker.runner.WorkerManagerThread;
import org.apache.dolphinscheduler.service.registry.RegistryClient;
import java.util.Set;
@ -63,6 +65,9 @@ public class WorkerRegistryClientTest {
@Mock
private ScheduledExecutorService heartBeatExecutor;
@Mock
private WorkerManagerThread workerManagerThread;
//private static final Set<String> workerGroups;
static {
@ -80,11 +85,17 @@ public class WorkerRegistryClientTest {
@Test
public void testRegistry() {
//workerRegistryClient.initWorkRegistry();
//Set<String> workerGroups = Sets.newHashSet("127.0.0.1");
//workerRegistryClient.registry();
// workerRegistryClient.handleDeadServer();
workerRegistryClient.initWorkRegistry();
given(workerManagerThread.getThreadPoolQueueSize()).willReturn(1);
given(registryClient.checkNodeExists(Mockito.anyString(), Mockito.any(NodeType.class))).willReturn(true);
given(workerConfig.getHeartbeatInterval()).willReturn(1);
workerRegistryClient.registry();
Mockito.verify(registryClient, Mockito.times(1)).handleDeadServer(Mockito.anyCollection(), Mockito.any(NodeType.class), Mockito.anyString());
}
@Test