You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by li...@apache.org on 2022/06/01 16:21:32 UTC

[incubator-shenyu] branch master updated: [ISSUE #3463] fix tars-plugin test code execution failure. (#3471)

This is an automated email from the ASF dual-hosted git repository.

likeguo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new 246f25c3f [ISSUE #3463] fix tars-plugin test code execution failure. (#3471)
246f25c3f is described below

commit 246f25c3f9f4f050a6890ca0f875cdc1c250956d
Author: 拉姆徐 <la...@gmail.com>
AuthorDate: Thu Jun 2 00:21:25 2022 +0800

    [ISSUE #3463] fix tars-plugin test code execution failure. (#3471)
    
    * [fix:tars plugin #3463] fix test code execution failure
    
    * [fix:tars plugin #3463] adjust test code
    
    * [fix:tars plugin #3463] remove unused imports
    
    * [fix:tars plugin #3463] remove CodeQL alert
---
 .../org/apache/shenyu/admin/utils/HttpUtils.java   | 49 ++++++++++++++--------
 .../plugin/tars/cache/ApplicationConfigCache.java  | 15 ++++---
 .../tars/handler/TarsPluginDataHandlerTest.java    | 14 +++++--
 3 files changed, 49 insertions(+), 29 deletions(-)

diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/utils/HttpUtils.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/utils/HttpUtils.java
index 1c01647e1..1f0cdb199 100644
--- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/utils/HttpUtils.java
+++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/utils/HttpUtils.java
@@ -17,6 +17,19 @@
 
 package org.apache.shenyu.admin.utils;
 
+import okhttp3.Cookie;
+import okhttp3.CookieJar;
+import okhttp3.FormBody;
+import okhttp3.HttpUrl;
+import okhttp3.MediaType;
+import okhttp3.MultipartBody;
+import okhttp3.OkHttpClient;
+import okhttp3.Request;
+import okhttp3.RequestBody;
+import okhttp3.Response;
+import okhttp3.ResponseBody;
+import org.apache.commons.codec.digest.DigestUtils;
+
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
@@ -32,18 +45,6 @@ import java.util.Map;
 import java.util.Objects;
 import java.util.Set;
 import java.util.concurrent.TimeUnit;
-import okhttp3.Cookie;
-import okhttp3.CookieJar;
-import okhttp3.FormBody;
-import okhttp3.HttpUrl;
-import okhttp3.MediaType;
-import okhttp3.MultipartBody;
-import okhttp3.OkHttpClient;
-import okhttp3.Request;
-import okhttp3.RequestBody;
-import okhttp3.Response;
-import okhttp3.ResponseBody;
-import org.apache.commons.codec.digest.DigestUtils;
 
 /**
  * HTTP request tool, based on okhttp3.
@@ -92,19 +93,29 @@ public class HttpUtils {
                     .head();
             case PUT:
                 return new Request.Builder()
-                    .url(url)
+                    .url(buildHttpUrl(url))
                     .put(buildFormBody(form));
             case DELETE:
                 return new Request.Builder()
-                    .url(url)
+                    .url(buildHttpUrl(url))
                     .delete(buildFormBody(form));
             default:
                 return new Request.Builder()
-                    .url(url)
+                    .url(buildHttpUrl(url))
                     .post(buildFormBody(form));
         }
     }
 
+    /**
+     * buildHttpUrl.
+     *
+     * @param url  url
+     * @return HttpUrl
+     */
+    public static HttpUrl buildHttpUrl(final String url) {
+        return buildHttpUrl(url, null);
+    }
+
     /**
      * buildHttpUrl.
      *
@@ -114,8 +125,10 @@ public class HttpUtils {
      */
     public static HttpUrl buildHttpUrl(final String url, final Map<String, ?> form) {
         HttpUrl.Builder urlBuilder = HttpUrl.parse(url).newBuilder();
-        for (Map.Entry<String, ?> entry : form.entrySet()) {
-            urlBuilder.addQueryParameter(entry.getKey(), String.valueOf(entry.getValue()));
+        if (Objects.nonNull(form) && !form.isEmpty()) {
+            for (Map.Entry<String, ?> entry : form.entrySet()) {
+                urlBuilder.addQueryParameter(entry.getKey(), String.valueOf(entry.getValue()));
+            }
         }
         return urlBuilder.build();
     }
@@ -269,7 +282,7 @@ public class HttpUtils {
         }
 
         RequestBody requestBody = bodyBuilder.build();
-        Request.Builder builder = new Request.Builder().url(url).post(requestBody);
+        Request.Builder builder = new Request.Builder().url(buildHttpUrl(url)).post(requestBody);
         addHeader(builder, header);
 
         Request request = builder.build();
diff --git a/shenyu-plugin/shenyu-plugin-tars/src/main/java/org/apache/shenyu/plugin/tars/cache/ApplicationConfigCache.java b/shenyu-plugin/shenyu-plugin-tars/src/main/java/org/apache/shenyu/plugin/tars/cache/ApplicationConfigCache.java
index c87b05f17..547ae7f4e 100644
--- a/shenyu-plugin/shenyu-plugin-tars/src/main/java/org/apache/shenyu/plugin/tars/cache/ApplicationConfigCache.java
+++ b/shenyu-plugin/shenyu-plugin-tars/src/main/java/org/apache/shenyu/plugin/tars/cache/ApplicationConfigCache.java
@@ -71,9 +71,6 @@ import java.util.concurrent.TimeUnit;
 import java.util.concurrent.locks.ReentrantLock;
 import java.util.stream.Collectors;
 
-import static com.qq.tars.common.util.Constants.default_core_pool_size;
-import static com.qq.tars.common.util.Constants.default_max_pool_size;
-
 /**
  * Tars config cache.
  */
@@ -109,11 +106,13 @@ public final class ApplicationConfigCache {
 
     private ApplicationConfigCache() {
         TarsRegisterConfig config = Singleton.INST.get(TarsRegisterConfig.class);
-        if (Objects.isNull(config) || StringUtils.isEmpty(config.getThreadpool())) {
-            CommunicatorConfig communicatorConfig = CommunicatorConfig.getDefault()
-                    .setCorePoolSize(Optional.ofNullable(config.getCorethreads()).orElse(default_core_pool_size))
-                    .setMaxPoolSize(Optional.ofNullable(config.getThreads()).orElse(default_max_pool_size))
-                    .setQueueSize(Optional.ofNullable(config.getQueues()).orElse(20000));
+        if (Objects.isNull(config)) {
+            communicator = CommunicatorFactory.getInstance().getCommunicator(CommunicatorConfig.getDefault());
+        } else if (StringUtils.isEmpty(config.getThreadpool())) {
+            CommunicatorConfig communicatorConfig = CommunicatorConfig.getDefault();
+            Optional.ofNullable(config.getCorethreads()).ifPresent(communicatorConfig::setCorePoolSize);
+            Optional.ofNullable(config.getThreads()).ifPresent(communicatorConfig::setMaxPoolSize);
+            Optional.ofNullable(config.getQueues()).ifPresent(communicatorConfig::setQueueSize);
             communicator = CommunicatorFactory.getInstance().getCommunicator(communicatorConfig);
         } else {
             communicator = CommunicatorFactory.getInstance().getCommunicator(CommunicatorConfig.getDefault());
diff --git a/shenyu-plugin/shenyu-plugin-tars/src/test/java/org/apache/shenyu/plugin/tars/handler/TarsPluginDataHandlerTest.java b/shenyu-plugin/shenyu-plugin-tars/src/test/java/org/apache/shenyu/plugin/tars/handler/TarsPluginDataHandlerTest.java
index ac37972e1..7d61fa37d 100644
--- a/shenyu-plugin/shenyu-plugin-tars/src/test/java/org/apache/shenyu/plugin/tars/handler/TarsPluginDataHandlerTest.java
+++ b/shenyu-plugin/shenyu-plugin-tars/src/test/java/org/apache/shenyu/plugin/tars/handler/TarsPluginDataHandlerTest.java
@@ -18,14 +18,17 @@
 package org.apache.shenyu.plugin.tars.handler;
 
 import org.apache.shenyu.common.dto.PluginData;
+import org.apache.shenyu.common.dto.convert.plugin.TarsRegisterConfig;
 import org.apache.shenyu.common.enums.PluginEnum;
+import org.apache.shenyu.common.utils.Singleton;
+import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
 import org.mockito.junit.jupiter.MockitoExtension;
 
-import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 /**
  * Test case for {@link TarsPluginDataHandler}.
@@ -42,9 +45,14 @@ public final class TarsPluginDataHandlerTest {
     
     @Test
     public void testHandlerPlugin() {
-        final PluginData pluginData = new PluginData("id", "name", "config", "0", false);
+        final PluginData pluginData = new PluginData("id", "name", "{\"threadpool\":\"cached\",\"corethreads\":1,\"threads\":2,\"queues\":3}", "0", true);
         tarsPluginDataHandlerUnderTest.handlerPlugin(pluginData);
-        assertTrue(pluginData.getName().endsWith("tested"));
+        assertTrue(pluginData.getName().endsWith("name"));
+        TarsRegisterConfig config = Singleton.INST.get(TarsRegisterConfig.class);
+        Assertions.assertEquals(config.getThreadpool(), "cached");
+        Assertions.assertEquals(config.getCorethreads(), 1);
+        Assertions.assertEquals(config.getThreads(), 2);
+        Assertions.assertEquals(config.getQueues(), 3);
     }
     
     @Test