Browse Source

[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)

springmonster 3 years ago
parent
commit
0911fd711d

+ 5 - 0
dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/HttpUtils.java

@@ -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 {

+ 6 - 0
dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/HttpUtilsTest.java

@@ -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);
     }
 
 

+ 16 - 5
dolphinscheduler-worker/src/test/java/org/apache/dolphinscheduler/server/worker/worker/registry/WorkerRegistryClientTest.java

@@ -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