diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/lifecycle/impl/RuleCacheLifeCycle.java b/liteflow-core/src/main/java/com/yomahub/liteflow/lifecycle/impl/RuleCacheLifeCycle.java index a6ef2fb8f..b7ceb80ca 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/lifecycle/impl/RuleCacheLifeCycle.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/lifecycle/impl/RuleCacheLifeCycle.java @@ -32,13 +32,12 @@ public class RuleCacheLifeCycle implements PostProcessFlowExecuteLifeCycle { @Override public void postProcessBeforeFlowExecute(String chainId, Slot slot) { - if (!FlowBus.containChain(chainId)) { - return; + if (FlowBus.containChain(chainId)) { + // 记录chainId在缓存中 + // 这里不记录实际的chain是因为chain之后有可能在FlowBus中被移除 + // 或被更新,以FlowBus中实际存在的chain为准 + cache.put(chainId, PRESENT); } - // 记录chainId在缓存中 - // 这里不记录实际的chain是因为chain之后有可能在FlowBus中被移除 - // 以FlowBus中实际存在的chain为准 - cache.put(chainId, PRESENT); } @Override 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 806e676d1..c726cf940 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,13 +1,14 @@ package com.yomahub.liteflow.test.ruleCache; import cn.hutool.core.collection.CollUtil; +import com.yomahub.liteflow.builder.el.LiteFlowChainELBuilder; import com.yomahub.liteflow.core.FlowExecutor; +import com.yomahub.liteflow.exception.ChainNotFoundException; 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; @@ -19,6 +20,10 @@ import javax.annotation.Resource; import java.util.List; import java.util.Random; +/** + * Springboot环境下规则缓存测试 + * @author DaleLee + */ @TestPropertySource(value = "classpath:/ruleCache/application.properties") @SpringBootTest(classes = RuleCacheSpringbootTest.class) @EnableAutoConfiguration @@ -30,6 +35,7 @@ public class RuleCacheSpringbootTest extends BaseTest { // 测试chain被淘汰 @Test public void testRuleCache1() throws InterruptedException { + flowExecutor.reloadRule(); // 加满缓存 loadCache(); LiteflowResponse response = flowExecutor.execute2Resp("chain4", "arg"); @@ -39,12 +45,16 @@ public class RuleCacheSpringbootTest extends BaseTest { testEvicted("chain1"); } - // 测试chain被手动移除 + // 测试缓存数量 @Test public void testRuleCache2() throws InterruptedException { + flowExecutor.reloadRule(); + // 确保至少执行过3个不同的chain + loadCache(); // 随机执行chain loadCache(100); - Thread.sleep(100); + // 等待缓存淘汰 + Thread.sleep(200); // 测试只有3个chain被编译 int count = 0; for (Chain chain : FlowBus.getChainMap().values()) { @@ -57,9 +67,46 @@ public class RuleCacheSpringbootTest extends BaseTest { } } Assertions.assertEquals(3, count); - } + // 测试chain被更新 + @Test + public void testRuleCache3() throws InterruptedException { + flowExecutor.reloadRule(); + loadCache(); + flowExecutor.execute2Resp("chain5", "arg"); + // chain1 被淘汰 + testEvicted("chain1"); + // 更新chain1 + LiteFlowChainELBuilder + .createChain() + .setChainId("chain1") + .setEL("THEN(a, b, c)") + .build(); + // 重新执行chain1 + LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg"); + Assertions.assertTrue(response.isSuccess()); + Assertions.assertEquals("a==>b==>c", response.getExecuteStepStr()); + } + + // 测试chain被移除 + @Test + public void testRuleCache4() throws InterruptedException { + flowExecutor.reloadRule(); + loadCache(); + LiteflowResponse response = flowExecutor.execute2Resp("chain5", "arg"); + Assertions.assertTrue(response.isSuccess()); + Assertions.assertEquals("x==>a==>c", response.getExecuteStepStr()); + // chain1被淘汰 + testEvicted("chain1"); + // 手动移除chain5 + FlowBus.removeChain("chain5"); + response = flowExecutor.execute2Resp("chain5", "arg"); + Assertions.assertFalse(response.isSuccess()); + Assertions.assertEquals(ChainNotFoundException.class, response.getCause().getClass()); + } + + // 加载缓存 private void loadCache() { // 容量上限为3