From 6b1c3edb0be73a5d5da2eb0ffe4466d7a6eb489e Mon Sep 17 00:00:00 2001 From: houxinyu Date: Mon, 21 Aug 2023 20:37:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=B5=8B=E8=AF=95=E7=94=A8?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mode/polling/RedisParserPollingMode.java | 2 +- ...RedisWithXmlELPollChainSpringbootTest.java | 89 +++++++++++++++++++ ...disWithXmlELPollScriptSpringbootTest.java} | 86 ++++++------------ ...RedisWithXmlELSubscribeSpringbootTest.java | 13 +-- .../application-poll-chain-xml.properties | 9 ++ ...=> application-poll-script-xml.properties} | 0 6 files changed, 134 insertions(+), 65 deletions(-) create mode 100644 liteflow-testcase-el/liteflow-testcase-el-redis-springboot/src/test/java/com/yomahub/liteflow/test/redis/RedisWithXmlELPollChainSpringbootTest.java rename liteflow-testcase-el/liteflow-testcase-el-redis-springboot/src/test/java/com/yomahub/liteflow/test/redis/{RedisWithXmlELPollSpringbootTest.java => RedisWithXmlELPollScriptSpringbootTest.java} (64%) create mode 100644 liteflow-testcase-el/liteflow-testcase-el-redis-springboot/src/test/resources/redis/application-poll-chain-xml.properties rename liteflow-testcase-el/liteflow-testcase-el-redis-springboot/src/test/resources/redis/{application-poll-xml.properties => application-poll-script-xml.properties} (100%) diff --git a/liteflow-rule-plugin/liteflow-rule-redis/src/main/java/com/yomahub/liteflow/parser/redis/mode/polling/RedisParserPollingMode.java b/liteflow-rule-plugin/liteflow-rule-redis/src/main/java/com/yomahub/liteflow/parser/redis/mode/polling/RedisParserPollingMode.java index 231b9907d..662b801b5 100644 --- a/liteflow-rule-plugin/liteflow-rule-redis/src/main/java/com/yomahub/liteflow/parser/redis/mode/polling/RedisParserPollingMode.java +++ b/liteflow-rule-plugin/liteflow-rule-redis/src/main/java/com/yomahub/liteflow/parser/redis/mode/polling/RedisParserPollingMode.java @@ -49,7 +49,7 @@ public class RedisParserPollingMode implements RedisParserHelper { private static final int CORE_POOL_SIZE = 2; //定时任务线程池 - private ScheduledThreadPoolExecutor pollExecutor; + private static ScheduledThreadPoolExecutor pollExecutor; //计算hash中field数量的lua脚本 private final String luaOfKey = "local keys = redis.call(\"hkeys\", KEYS[1]);\n" + diff --git a/liteflow-testcase-el/liteflow-testcase-el-redis-springboot/src/test/java/com/yomahub/liteflow/test/redis/RedisWithXmlELPollChainSpringbootTest.java b/liteflow-testcase-el/liteflow-testcase-el-redis-springboot/src/test/java/com/yomahub/liteflow/test/redis/RedisWithXmlELPollChainSpringbootTest.java new file mode 100644 index 000000000..2e8b6aec8 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-redis-springboot/src/test/java/com/yomahub/liteflow/test/redis/RedisWithXmlELPollChainSpringbootTest.java @@ -0,0 +1,89 @@ +package com.yomahub.liteflow.test.redis; + +import cn.hutool.crypto.digest.DigestUtil; +import com.yomahub.liteflow.core.FlowExecutor; +import com.yomahub.liteflow.flow.LiteflowResponse; +import com.yomahub.liteflow.parser.redis.mode.RClient; +import com.yomahub.liteflow.test.BaseTest; +import org.junit.jupiter.api.*; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import javax.annotation.Resource; +import java.util.HashSet; +import java.util.Set; + +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.Mockito.when; + +/** + * springboot环境下的redis配置源chain轮询拉取模式功能测试 + * + * @author hxinyu + * @since 2.11.0 + */ +@ExtendWith(SpringExtension.class) +@TestPropertySource(value = "classpath:/redis/application-poll-chain-xml.properties") +@SpringBootTest(classes = RedisWithXmlELPollChainSpringbootTest.class) +@EnableAutoConfiguration +@ComponentScan({"com.yomahub.liteflow.test.redis.cmp"}) +public class RedisWithXmlELPollChainSpringbootTest extends BaseTest { + + @MockBean(name = "chainClient") + private static RClient chainClient; + + @Resource + private FlowExecutor flowExecutor; + + //计算hash中field数量的lua脚本 + private final String luaOfKey = "local keys = redis.call(\"hkeys\", KEYS[1]);\n" + + "return #keys;\n"; + + //计算hash中value的SHA值的lua脚本 + private final String luaOfValue = "local key = KEYS[1];\n" + + "local field = KEYS[2];\n" + + "local value, err = redis.call(\"hget\", key, field);\n" + + "if value == false or value == nil then\n" + + " return \"nil\";\n" + + "end\n" + + "local sha1 = redis.sha1hex(value);\n" + + "return sha1;"; + + /** + * 测试chain + */ + @Test + public void testPollWithXml() throws InterruptedException { + Set chainNameSet = new HashSet<>(); + chainNameSet.add("chain11"); + String chainValue = "THEN(a, b, c);"; + //SHA值用于测试修改chain的轮询刷新功能 + String chainSHA = DigestUtil.sha1Hex(chainValue); + + //修改chain并更新SHA值 + String changeChainValue = "THEN(a, c);"; + String changeChainSHA = DigestUtil.sha1Hex(changeChainValue); + when(chainClient.hkeys("pollChainKey")).thenReturn(chainNameSet); + when(chainClient.hget("pollChainKey", "chain11")).thenReturn(chainValue).thenReturn(changeChainValue); + when(chainClient.scriptLoad(luaOfKey)).thenReturn("keysha"); + when(chainClient.scriptLoad(luaOfValue)).thenReturn("valuesha"); + when(chainClient.evalSha(eq("keysha"), anyString())).thenReturn("1"); + when(chainClient.evalSha(eq("valuesha"), anyString(), anyString())).thenReturn(chainSHA).thenReturn(changeChainSHA); + + //测试修改前的chain + LiteflowResponse response = flowExecutor.execute2Resp("chain11", "arg"); + Assertions.assertTrue(response.isSuccess()); + Assertions.assertEquals("a==>b==>c", response.getExecuteStepStr()); + + Thread.sleep(4000); + + //测试修改后的chain + response = flowExecutor.execute2Resp("chain11", "arg"); + Assertions.assertTrue(response.isSuccess()); + Assertions.assertEquals("a==>c", response.getExecuteStepStr()); + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-redis-springboot/src/test/java/com/yomahub/liteflow/test/redis/RedisWithXmlELPollSpringbootTest.java b/liteflow-testcase-el/liteflow-testcase-el-redis-springboot/src/test/java/com/yomahub/liteflow/test/redis/RedisWithXmlELPollScriptSpringbootTest.java similarity index 64% rename from liteflow-testcase-el/liteflow-testcase-el-redis-springboot/src/test/java/com/yomahub/liteflow/test/redis/RedisWithXmlELPollSpringbootTest.java rename to liteflow-testcase-el/liteflow-testcase-el-redis-springboot/src/test/java/com/yomahub/liteflow/test/redis/RedisWithXmlELPollScriptSpringbootTest.java index 05749ee9a..b2770143b 100644 --- a/liteflow-testcase-el/liteflow-testcase-el-redis-springboot/src/test/java/com/yomahub/liteflow/test/redis/RedisWithXmlELPollSpringbootTest.java +++ b/liteflow-testcase-el/liteflow-testcase-el-redis-springboot/src/test/java/com/yomahub/liteflow/test/redis/RedisWithXmlELPollScriptSpringbootTest.java @@ -2,16 +2,13 @@ package com.yomahub.liteflow.test.redis; import cn.hutool.crypto.digest.DigestUtil; import com.yomahub.liteflow.core.FlowExecutor; -import com.yomahub.liteflow.core.FlowInitHook; -import com.yomahub.liteflow.flow.FlowBus; import com.yomahub.liteflow.flow.LiteflowResponse; +import com.yomahub.liteflow.log.LFLog; +import com.yomahub.liteflow.log.LFLoggerManager; import com.yomahub.liteflow.parser.redis.mode.RClient; -import com.yomahub.liteflow.property.LiteflowConfigGetter; +import com.yomahub.liteflow.parser.redis.mode.polling.RedisParserPollingMode; import com.yomahub.liteflow.slot.DefaultContext; -import com.yomahub.liteflow.spi.holder.SpiFactoryCleaner; -import com.yomahub.liteflow.spring.ComponentScanner; import com.yomahub.liteflow.test.BaseTest; -import com.yomahub.liteflow.thread.ExecutorHelper; import org.junit.jupiter.api.*; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; @@ -20,25 +17,29 @@ import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.annotation.ComponentScan; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit.jupiter.SpringExtension; + import javax.annotation.Resource; +import java.lang.reflect.Field; import java.util.HashSet; import java.util.Set; +import java.util.concurrent.ScheduledThreadPoolExecutor; -import static org.mockito.ArgumentMatchers.*; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.when; /** - * springboot环境下的redis配置源轮询拉取模式功能测试 + * springboot环境下的redis配置源script轮询拉取模式功能测试 * * @author hxinyu * @since 2.11.0 */ @ExtendWith(SpringExtension.class) -@TestPropertySource(value = "classpath:/redis/application-poll-xml.properties") -@SpringBootTest(classes = RedisWithXmlELPollSpringbootTest.class) +@TestPropertySource(value = "classpath:/redis/application-poll-script-xml.properties") +@SpringBootTest(classes = RedisWithXmlELPollScriptSpringbootTest.class) @EnableAutoConfiguration @ComponentScan({"com.yomahub.liteflow.test.redis.cmp"}) -public class RedisWithXmlELPollSpringbootTest extends BaseTest { +public class RedisWithXmlELPollScriptSpringbootTest extends BaseTest { @MockBean(name = "chainClient") private static RClient chainClient; @@ -63,68 +64,37 @@ public class RedisWithXmlELPollSpringbootTest extends BaseTest { "local sha1 = redis.sha1hex(value);\n" + "return sha1;"; + static LFLog LOG = LFLoggerManager.getLogger(RedisWithXmlELPollChainSpringbootTest.class); - @AfterEach - public void after() { - FlowBus.cleanCache(); - FlowInitHook.cleanHook(); - ExecutorHelper.loadInstance().clearExecutorServiceMap(); - SpiFactoryCleaner.clean(); - } - /** - * 测试chain - */ - @Test - public void testPollWithXml() throws InterruptedException { - Set chainNameSet = new HashSet<>(); - chainNameSet.add("chain11"); - String chainValue = "THEN(a, b, c);"; - //SHA值用于测试修改chain的轮询刷新功能 - String chainSHA = DigestUtil.sha1Hex(chainValue); - - //修改chain并更新SHA值 - String changeChainValue = "THEN(a, c);"; - String changeChainSHA = DigestUtil.sha1Hex(changeChainValue); - when(chainClient.hkeys("pollChainKey")).thenReturn(chainNameSet); - when(chainClient.hget("pollChainKey", "chain11")).thenReturn(chainValue).thenReturn(changeChainValue); - when(chainClient.scriptLoad(luaOfKey)).thenReturn("keysha"); - when(chainClient.scriptLoad(luaOfValue)).thenReturn("valuesha"); - when(chainClient.evalSha(eq("keysha"), anyString())).thenReturn("1"); - when(chainClient.evalSha(eq("valuesha"), anyString(), anyString())).thenReturn(chainSHA).thenReturn(changeChainSHA); - //这里其实并没有script数据 预设数据只是为了不产生NumberFormatException - when(scriptClient.scriptLoad(luaOfKey)).thenReturn("keysha"); - when(scriptClient.scriptLoad(luaOfValue)).thenReturn("valuesha"); - when(scriptClient.evalSha(eq("keysha"), anyString())).thenReturn("0"); - when(scriptClient.evalSha(eq("valuesha"), anyString(), anyString())).thenReturn(""); - - //测试修改前的chain - LiteflowResponse response = flowExecutor.execute2Resp("chain11", "arg"); - Assertions.assertTrue(response.isSuccess()); - Assertions.assertEquals("a==>b==>c", response.getExecuteStepStr()); - - Thread.sleep(4000); - - //测试修改后的chain - response = flowExecutor.execute2Resp("chain11", "arg"); - Assertions.assertTrue(response.isSuccess()); - Assertions.assertEquals("a==>c", response.getExecuteStepStr()); + @AfterAll + public static void after() { + //关闭poll模式的轮询线程池 + try{ + Field pollExecutor = RedisParserPollingMode.class.getDeclaredField("pollExecutor"); + pollExecutor.setAccessible(true); + ScheduledThreadPoolExecutor threadPoolExecutor = (ScheduledThreadPoolExecutor) pollExecutor.get(null); + threadPoolExecutor.shutdownNow(); + } catch (Exception ignored) { + LOG.error("[Polling thread pool not closed]", ignored); + } } /** * 测试script */ @Test - public void testPollWithScript() throws InterruptedException { + public void testPollWithScriptXml() throws InterruptedException { Set chainNameSet = new HashSet<>(); chainNameSet.add("chain22"); String chainValue = "THEN(s11, s22, s33, a, b);"; + String chainSHA = DigestUtil.sha1Hex(chainValue); when(chainClient.hkeys("pollChainKey")).thenReturn(chainNameSet); when(chainClient.hget("pollChainKey", "chain22")).thenReturn(chainValue); when(chainClient.scriptLoad(luaOfKey)).thenReturn("keysha"); when(chainClient.scriptLoad(luaOfValue)).thenReturn("valuesha"); - when(chainClient.evalSha(eq("keysha"), anyString())).thenReturn("1"); - when(chainClient.evalSha(eq("valuesha"), anyString(), anyString())).thenReturn(""); + when(chainClient.evalSha(eq("keysha"), anyString())).thenReturn(null); + when(chainClient.evalSha(eq("valuesha"), anyString(), anyString())).thenReturn(chainSHA); Set scriptFieldSet = new HashSet<>(); scriptFieldSet.add("s11:script:脚本s11:groovy"); diff --git a/liteflow-testcase-el/liteflow-testcase-el-redis-springboot/src/test/java/com/yomahub/liteflow/test/redis/RedisWithXmlELSubscribeSpringbootTest.java b/liteflow-testcase-el/liteflow-testcase-el-redis-springboot/src/test/java/com/yomahub/liteflow/test/redis/RedisWithXmlELSubscribeSpringbootTest.java index 02575d0ea..cfd7f2d1e 100644 --- a/liteflow-testcase-el/liteflow-testcase-el-redis-springboot/src/test/java/com/yomahub/liteflow/test/redis/RedisWithXmlELSubscribeSpringbootTest.java +++ b/liteflow-testcase-el/liteflow-testcase-el-redis-springboot/src/test/java/com/yomahub/liteflow/test/redis/RedisWithXmlELSubscribeSpringbootTest.java @@ -2,6 +2,7 @@ package com.yomahub.liteflow.test.redis; import cn.hutool.core.util.ObjectUtil; import com.yomahub.liteflow.core.FlowExecutor; +import com.yomahub.liteflow.exception.ChainNotFoundException; import com.yomahub.liteflow.flow.LiteflowResponse; import com.yomahub.liteflow.parser.redis.vo.RedisParserVO; import com.yomahub.liteflow.property.LiteflowConfig; @@ -30,11 +31,11 @@ import javax.annotation.Resource; /** * springboot环境下的redis配置源订阅模式功能测试 - * + *

* 由于Redisson中RMapCache的监听器功能无法mock测试 * 故Sub模式测试用例需本地启动Redis服务 连接地址: 127.0.0.1:6379 * 若本地该端口号未启动Redis 则自动忽略本类中测试用例 - * + *

* 测试用例会在1号database中添加测试数据 chainKey:testChainKey; scriptKey:testScriptKey * 测试完成后清除测试数据 * @@ -70,7 +71,7 @@ public class RedisWithXmlELSubscribeSpringbootTest extends BaseTest { } @AfterAll - public static void after(){ + public static void after() { testCleanData(); } @@ -153,7 +154,7 @@ public class RedisWithXmlELSubscribeSpringbootTest extends BaseTest { LiteflowConfig liteflowConfig = LiteflowConfigGetter.get(); RedisParserVO redisParserVO = JsonUtil.parseObject(liteflowConfig.getRuleSourceExtData(), RedisParserVO.class); RMapCache chainKey = redissonClient.getMapCache(redisParserVO.getChainKey()); - chainKey.put("chain4","THEN(b, c);"); + chainKey.put("chain4", "THEN(b, c);"); } /** @@ -179,8 +180,8 @@ public class RedisWithXmlELSubscribeSpringbootTest extends BaseTest { } //redis内规则数据数据清空 - public static void testCleanData(){ - if(ObjectUtil.isNotNull(redissonClient)){ + public static void testCleanData() { + if (ObjectUtil.isNotNull(redissonClient)) { RMapCache chainKey = redissonClient.getMapCache("testChainKey"); RMapCache scriptKey = redissonClient.getMapCache("testScriptKey"); for (String key : chainKey.keySet()) { diff --git a/liteflow-testcase-el/liteflow-testcase-el-redis-springboot/src/test/resources/redis/application-poll-chain-xml.properties b/liteflow-testcase-el/liteflow-testcase-el-redis-springboot/src/test/resources/redis/application-poll-chain-xml.properties new file mode 100644 index 000000000..a19a01ef9 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-redis-springboot/src/test/resources/redis/application-poll-chain-xml.properties @@ -0,0 +1,9 @@ +liteflow.rule-source-ext-data={\ + "host":"localhost",\ + "port":6379,\ + "pollingInterval":1,\ + "pollingStartTime":2,\ + "chainDataBase":1,\ + "chainKey":"pollChainKey"\ + } +liteflow.parse-on-start=false \ No newline at end of file diff --git a/liteflow-testcase-el/liteflow-testcase-el-redis-springboot/src/test/resources/redis/application-poll-xml.properties b/liteflow-testcase-el/liteflow-testcase-el-redis-springboot/src/test/resources/redis/application-poll-script-xml.properties similarity index 100% rename from liteflow-testcase-el/liteflow-testcase-el-redis-springboot/src/test/resources/redis/application-poll-xml.properties rename to liteflow-testcase-el/liteflow-testcase-el-redis-springboot/src/test/resources/redis/application-poll-script-xml.properties