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

xiaoyu 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 18580b5  Enhanced integration test of RateLimiter plugin (#2359)
18580b5 is described below

commit 18580b5faf72acf72e3fff9729bfe34337debc14
Author: Kunshuai Zhu <[email protected]>
AuthorDate: Mon Nov 15 14:41:47 2021 +0800

    Enhanced integration test of RateLimiter plugin (#2359)
---
 .../http/combination/RateLimiterPluginTest.java    | 102 ++++++++++++++++++---
 1 file changed, 89 insertions(+), 13 deletions(-)

diff --git 
a/shenyu-integrated-test/shenyu-integrated-test-http/src/test/java/org/apache/shenyu/integrated/test/http/combination/RateLimiterPluginTest.java
 
b/shenyu-integrated-test/shenyu-integrated-test-http/src/test/java/org/apache/shenyu/integrated/test/http/combination/RateLimiterPluginTest.java
index b8c019b..8f6ef56 100644
--- 
a/shenyu-integrated-test/shenyu-integrated-test-http/src/test/java/org/apache/shenyu/integrated/test/http/combination/RateLimiterPluginTest.java
+++ 
b/shenyu-integrated-test/shenyu-integrated-test-http/src/test/java/org/apache/shenyu/integrated/test/http/combination/RateLimiterPluginTest.java
@@ -29,39 +29,115 @@ import 
org.apache.shenyu.integratedtest.common.dto.AdminResponse;
 import org.apache.shenyu.integratedtest.common.dto.UserDTO;
 import org.apache.shenyu.integratedtest.common.helper.HttpHelper;
 import org.apache.shenyu.web.controller.LocalPluginController.RuleLocalData;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
+import org.junit.After;
+import org.junit.Before;
 import org.junit.Test;
 
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
+import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
 
 import static org.hamcrest.Matchers.is;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
 
-public class RateLimiterPluginTest extends AbstractPluginDataInit {
+public final class RateLimiterPluginTest extends AbstractPluginDataInit {
 
-    @BeforeClass
-    public static void setup() throws IOException {
+    @Before
+    public void setup() throws IOException {
         String pluginResult = initPlugin(PluginEnum.RATE_LIMITER.getName(), 
"{\"mode\":\"standalone\",\"master\":\"mymaster\",\"url\":\"shenyu-redis:6379\",\"password\":\"abc\"}");
         assertThat(pluginResult, is("success"));
-        String selectorAndRulesResult = 
initSelectorAndRules(PluginEnum.RATE_LIMITER.getName(), "", 
buildSelectorConditionList(), buildRuleLocalDataList());
+    }
+
+    @Test
+    public void testSlidingWindow() throws IOException, ExecutionException, 
InterruptedException {
+        String selectorAndRulesResult = 
initSelectorAndRules(PluginEnum.RATE_LIMITER.getName(), "", 
buildSelectorConditionList(), buildRuleLocalDataList("slidingWindow"));
         assertThat(selectorAndRulesResult, is("success"));
+
+        Future<UserDTO> allowedRespFuture1 = this.getService().submit(() -> 
HttpHelper.INSTANCE.getFromGateway("/http/test/path/123?name=Tom", 
UserDTO.class));
+        assertEquals("hello world", allowedRespFuture1.get().getUserName());
+
+        Future<AdminResponse<Object>> rejectedRespFuture = 
this.getService().submit(() ->
+                
HttpHelper.INSTANCE.getFromGateway("/http/test/path/123?name=Tom", new 
TypeToken<AdminResponse<Object>>() {
+                }.getType()));
+        AdminResponse<Object> dto = rejectedRespFuture.get();
+        assertEquals("You have been restricted, please try again later!", 
dto.getMessage());
+
+        Thread.sleep(1000);
+        Future<UserDTO> allowedRespFuture2 = this.getService().submit(() -> 
HttpHelper.INSTANCE.getFromGateway("/http/test/path/123?name=Tom", 
UserDTO.class));
+        assertEquals("hello world", allowedRespFuture2.get().getUserName());
     }
 
     @Test
-    public void testSlidingWindow() throws Exception {
-        Future<UserDTO> normalRespFuture = this.getService().submit(() -> 
HttpHelper.INSTANCE.getFromGateway("/http/test/path/123?name=Tom", 
UserDTO.class));
-        assertEquals("hello world", normalRespFuture.get().getUserName());
+    public void testLeakyBucket() throws IOException, ExecutionException, 
InterruptedException {
+        String selectorAndRulesResult = 
initSelectorAndRules(PluginEnum.RATE_LIMITER.getName(), "", 
buildSelectorConditionList(), buildRuleLocalDataList("leakyBucket"));
+        assertThat(selectorAndRulesResult, is("success"));
+
+        Future<UserDTO> allowedRespFuture1 = this.getService().submit(() -> 
HttpHelper.INSTANCE.getFromGateway("/http/test/path/123?name=Tom", 
UserDTO.class));
+        assertEquals("hello world", allowedRespFuture1.get().getUserName());
 
         Future<AdminResponse<Object>> rejectedRespFuture = 
this.getService().submit(() ->
                 
HttpHelper.INSTANCE.getFromGateway("/http/test/path/123?name=Tom", new 
TypeToken<AdminResponse<Object>>() {
                 }.getType()));
         AdminResponse<Object> dto = rejectedRespFuture.get();
         assertEquals("You have been restricted, please try again later!", 
dto.getMessage());
+
+        Thread.sleep(1000);
+        Future<UserDTO> allowedRespFuture2 = this.getService().submit(() -> 
HttpHelper.INSTANCE.getFromGateway("/http/test/path/123?name=Tom", 
UserDTO.class));
+        assertEquals("hello world", allowedRespFuture2.get().getUserName());
+    }
+
+    @Test
+    public void testTokenBucket() throws IOException, ExecutionException, 
InterruptedException {
+        String selectorAndRulesResult = 
initSelectorAndRules(PluginEnum.RATE_LIMITER.getName(), "", 
buildSelectorConditionList(), buildRuleLocalDataList("tokenBucket"));
+        assertThat(selectorAndRulesResult, is("success"));
+
+        Future<UserDTO> allowedRespFuture1 = this.getService().submit(() -> 
HttpHelper.INSTANCE.getFromGateway("/http/test/path/123?name=Tom", 
UserDTO.class));
+        assertEquals("hello world", allowedRespFuture1.get().getUserName());
+
+        Future<AdminResponse<Object>> rejectedRespFuture = 
this.getService().submit(() ->
+                
HttpHelper.INSTANCE.getFromGateway("/http/test/path/123?name=Tom", new 
TypeToken<AdminResponse<Object>>() {
+                }.getType()));
+        AdminResponse<Object> dto = rejectedRespFuture.get();
+        assertEquals("You have been restricted, please try again later!", 
dto.getMessage());
+
+        Thread.sleep(1000);
+        Future<UserDTO> allowedRespFuture2 = this.getService().submit(() -> 
HttpHelper.INSTANCE.getFromGateway("/http/test/path/123?name=Tom", 
UserDTO.class));
+        assertEquals("hello world", allowedRespFuture2.get().getUserName());
+    }
+
+    @Test
+    public void testConcurrentTokenBucket() throws IOException, 
ExecutionException, InterruptedException {
+        String selectorAndRulesResult = 
initSelectorAndRules(PluginEnum.RATE_LIMITER.getName(), "", 
buildSelectorConditionList(), buildRuleLocalDataList("concurrent"));
+        assertThat(selectorAndRulesResult, is("success"));
+
+        UserDTO allowedResp = 
HttpHelper.INSTANCE.getFromGateway("/http/test/path/123?name=Tom", 
UserDTO.class);
+        assertEquals("hello world", allowedResp.getUserName());
+
+        List<Future<AdminResponse<Object>>> futures = new ArrayList<>();
+        for (int i = 0; i < 4; i++) {
+            Future<AdminResponse<Object>> rejectedRespFuture = 
this.getService().submit(() ->
+                    
HttpHelper.INSTANCE.getFromGateway("/http/test/path/123?name=Tom", new 
TypeToken<AdminResponse<Object>>() {
+                    }.getType()));
+            futures.add(rejectedRespFuture);
+        }
+
+        int errorCount = 0;
+        int correctCount = 0;
+        for (Future<AdminResponse<Object>> future : futures) {
+            AdminResponse<Object> adminResponse = future.get();
+            if (adminResponse.getCode() != null && adminResponse.getCode() == 
429) {
+                errorCount++;
+            } else {
+                correctCount++;
+            }
+        }
+        assertTrue(errorCount > 0);
+        assertTrue(correctCount > 0);
     }
 
     private static List<ConditionData> buildSelectorConditionList() {
@@ -72,11 +148,11 @@ public class RateLimiterPluginTest extends 
AbstractPluginDataInit {
         return Collections.singletonList(conditionData);
     }
 
-    private static List<RuleLocalData> buildRuleLocalDataList() {
+    private static List<RuleLocalData> buildRuleLocalDataList(final String 
algorithmName) {
         final RuleLocalData ruleLocalData = new RuleLocalData();
 
         RateLimiterHandle rateLimiterHandle = new RateLimiterHandle();
-        rateLimiterHandle.setAlgorithmName("slidingWindow");
+        rateLimiterHandle.setAlgorithmName(algorithmName);
         rateLimiterHandle.setReplenishRate(1);
         rateLimiterHandle.setBurstCapacity(1);
         // see WholeKeyResolver.java
@@ -92,8 +168,8 @@ public class RateLimiterPluginTest extends 
AbstractPluginDataInit {
         return Collections.singletonList(ruleLocalData);
     }
 
-    @AfterClass
-    public static void clean() throws IOException {
+    @After
+    public void clean() throws IOException {
         cleanPluginData(PluginEnum.RATE_LIMITER.getName());
     }
 }

Reply via email to