From 6cb79573d98f3fa324ac7d0c1db37cb1ec0e9a6b Mon Sep 17 00:00:00 2001 From: DaleLee <1658850308@qq.com> Date: Sat, 9 Nov 2024 22:49:28 +0800 Subject: [PATCH] =?UTF-8?q?feature=20#IAY66T=20=E6=B7=BB=E5=8A=A0=E5=8D=95?= =?UTF-8?q?=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ruleCache/RuleCacheSpringbootTest.java | 78 ++++++++++++++++++- .../src/test/resources/ruleCache/flow.el.xml | 2 +- 2 files changed, 78 insertions(+), 2 deletions(-) diff --git a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/ruleCache/RuleCacheSpringbootTest.java b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/ruleCache/RuleCacheSpringbootTest.java index 51cecb3c0..806e676d1 100644 --- a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/ruleCache/RuleCacheSpringbootTest.java +++ b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/ruleCache/RuleCacheSpringbootTest.java @@ -1,8 +1,14 @@ package com.yomahub.liteflow.test.ruleCache; +import cn.hutool.core.collection.CollUtil; import com.yomahub.liteflow.core.FlowExecutor; +import com.yomahub.liteflow.flow.FlowBus; +import com.yomahub.liteflow.flow.LiteflowResponse; +import com.yomahub.liteflow.flow.element.Chain; +import com.yomahub.liteflow.flow.element.Condition; import com.yomahub.liteflow.test.BaseTest; import com.yomahub.liteflow.test.rollback.RollbackSpringbootTest; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; @@ -10,6 +16,8 @@ import org.springframework.context.annotation.ComponentScan; import org.springframework.test.context.TestPropertySource; import javax.annotation.Resource; +import java.util.List; +import java.util.Random; @TestPropertySource(value = "classpath:/ruleCache/application.properties") @SpringBootTest(classes = RuleCacheSpringbootTest.class) @@ -19,8 +27,76 @@ public class RuleCacheSpringbootTest extends BaseTest { @Resource private FlowExecutor flowExecutor; + // 测试chain被淘汰 @Test - public void testRuleCache() throws Exception{ + public void testRuleCache1() throws InterruptedException { + // 加满缓存 + loadCache(); + LiteflowResponse response = flowExecutor.execute2Resp("chain4", "arg"); + // chain1 被淘汰 + Assertions.assertTrue(response.isSuccess()); + Assertions.assertEquals("x==>a==>b", response.getExecuteStepStr()); + testEvicted("chain1"); + } + + // 测试chain被手动移除 + @Test + public void testRuleCache2() throws InterruptedException { + // 随机执行chain + loadCache(100); + Thread.sleep(100); + // 测试只有3个chain被编译 + int count = 0; + for (Chain chain : FlowBus.getChainMap().values()) { + List conditionList = chain.getConditionList(); + if (chain.isCompiled()) { + Assertions.assertTrue(CollUtil.isNotEmpty(conditionList)); + count++; + } else { + Assertions.assertNull(conditionList); + } + } + Assertions.assertEquals(3, count); } + + // 加载缓存 + private void loadCache() { + // 容量上限为3 + LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg"); + Assertions.assertTrue(response.isSuccess()); + Assertions.assertEquals("a==>b", response.getExecuteStepStr()); + response = flowExecutor.execute2Resp("chain2", "arg"); + Assertions.assertTrue(response.isSuccess()); + Assertions.assertEquals("a==>c", response.getExecuteStepStr()); + response = flowExecutor.execute2Resp("chain3", "arg"); + Assertions.assertTrue(response.isSuccess()); + Assertions.assertEquals("b==>c", response.getExecuteStepStr()); + } + + private void loadCache(int count) { + // 随机执行chain + Random random = new Random(); + for (int i = 0; i < count; i++) { + int id = random.nextInt(5) + 1; + flowExecutor.execute2Resp("chain" + id); + } + } + + // 测试 chain 被淘汰 + private void testEvicted(String chanId) throws InterruptedException { + Chain chain = FlowBus.getChain(chanId); + int limit = 10; // 重试上限 + int count = 0; + while (chain.isCompiled()) { + // 等待 chain 被淘汰 + Thread.sleep(100); + count++; + if (count > limit) { + throw new RuntimeException(chanId + " not be evicted"); + } + System.out.println(count); + } + Assertions.assertNull(chain.getConditionList()); + } } diff --git a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/ruleCache/flow.el.xml b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/ruleCache/flow.el.xml index 1de85b0da..08df154be 100644 --- a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/ruleCache/flow.el.xml +++ b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/ruleCache/flow.el.xml @@ -9,7 +9,7 @@ - THEN(c, b); + THEN(b, c);