From d510eb6cd332adfbe83dec54b88ae9c82f513c28 Mon Sep 17 00:00:00 2001 From: Jason Joo Date: Thu, 29 Oct 2020 14:22:36 +0800 Subject: [PATCH] test: Fix overrunning test `FlowRuleManagerTest.testLoadAndGetRules` (#1823) Signed-off-by: Jason Joo --- .../flow/FlowPartialIntegrationTest.java | 9 +++-- .../slots/block/flow/FlowRuleManagerTest.java | 37 ++++++++++++------- .../slots/block/flow/FlowSlotTest.java | 1 - 3 files changed, 28 insertions(+), 19 deletions(-) diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/FlowPartialIntegrationTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/FlowPartialIntegrationTest.java index ae43a852..faf8cca7 100755 --- a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/FlowPartialIntegrationTest.java +++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/FlowPartialIntegrationTest.java @@ -116,15 +116,16 @@ public class FlowPartialIntegrationTest { @Test public void testOriginFlowRule() { + String RESOURCE_NAME = "testOriginFlowRule"; // normal FlowRule flowRule = new FlowRule(); - flowRule.setResource("testOriginFlowRule"); + flowRule.setResource(RESOURCE_NAME); flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS); flowRule.setCount(0); flowRule.setLimitApp("other"); FlowRule flowRule2 = new FlowRule(); - flowRule2.setResource("testOriginFlowRule"); + flowRule2.setResource(RESOURCE_NAME); flowRule2.setGrade(RuleConstant.FLOW_GRADE_QPS); flowRule2.setCount(1); flowRule2.setLimitApp("app2"); @@ -134,7 +135,7 @@ public class FlowPartialIntegrationTest { ContextUtil.enter("node1", "app1"); Entry e = null; try { - e = SphU.entry("testOriginFlowRule"); + e = SphU.entry(RESOURCE_NAME); fail("Should had failed"); } catch (BlockException e1) { e1.printStackTrace(); @@ -146,7 +147,7 @@ public class FlowPartialIntegrationTest { ContextUtil.enter("node1", "app2"); e = null; try { - e = SphU.entry("testOriginFlowRule"); + e = SphU.entry(RESOURCE_NAME); } catch (BlockException e1) { fail("Should had failed"); } diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/FlowRuleManagerTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/FlowRuleManagerTest.java index 0871532c..867dad82 100644 --- a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/FlowRuleManagerTest.java +++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/FlowRuleManagerTest.java @@ -19,6 +19,8 @@ import org.junit.Test; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; import static org.junit.Assert.assertEquals; @@ -44,27 +46,34 @@ public class FlowRuleManagerTest { } @Test - public void testLoadAndGetRules(){ + public void testLoadAndGetRules() throws InterruptedException{ FlowRuleManager.loadRules(STATIC_RULES_1); assertEquals(1, FlowRuleManager.getRules().size()); // the initial size - new Thread(loader, "Loader").start(); + final CountDownLatch latchStart = new CountDownLatch(1); + final CountDownLatch latchEnd = new CountDownLatch(1); + new Thread(new Runnable() { + @Override + public void run() { + try { + latchStart.await(10, TimeUnit.SECONDS); + } catch (InterruptedException e) { + return; + } + for(int i = 0; i < 10000; i++){ + //to guarantee that they're different and change happens + FlowRuleManager.loadRules(i % 2 == 0 ? STATIC_RULES_2 : STATIC_RULES_1); + } + latchEnd.countDown(); + } + }).start(); - for(int i = 0; i < 10000; i++){ + latchStart.countDown(); + for (int i = 0; i < 10000; i++) { //The initial size is 1, and the size after updating should also be 1, //if the actual size is 0, that must be called after clear(), // but before putAll() in FlowPropertyListener.configUpdate assertEquals(1, FlowRuleManager.getRules().size()); } + latchEnd.await(10, TimeUnit.SECONDS); } - - public Runnable loader = new Runnable() { - @Override - public void run() { - for(int i = 0; i < 10000; i++){ - //to guarantee that they're different and change happens - FlowRuleManager.loadRules(i % 2 == 0 ? STATIC_RULES_2 : STATIC_RULES_1); - } - } - }; - } diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/FlowSlotTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/FlowSlotTest.java index 66026e2b..1a29e273 100644 --- a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/FlowSlotTest.java +++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/FlowSlotTest.java @@ -29,7 +29,6 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; -import static org.junit.Assert.*; import static org.mockito.Mockito.*; /**