From 3c5d22c62ce3fd6490ac3d0530988febdbc7fcbc Mon Sep 17 00:00:00 2001 From: houxinyu Date: Sun, 20 Aug 2023 15:28:43 +0800 Subject: [PATCH] fix test --- .../mode/polling/RedisParserPollingMode.java | 2 +- .../redis/RedisSubscribeTestCondition.java | 29 +++++++++++++++++++ .../RedisWithXmlELPollSpringbootTest.java | 19 ++++++++---- ...RedisWithXmlELSubscribeSpringbootTest.java | 21 +++++++------- .../redis/application-poll-xml.properties | 4 +-- .../redis/application-sub-xml.properties | 4 +-- 6 files changed, 59 insertions(+), 20 deletions(-) create mode 100644 liteflow-testcase-el/liteflow-testcase-el-redis-springboot/src/test/java/com/yomahub/liteflow/test/redis/RedisSubscribeTestCondition.java 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 b9c3b2ea7..231b9907d 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 @@ -102,7 +102,7 @@ public class RedisParserPollingMode implements RedisParserHelper { } //创建定时任务线程池 if (ObjectUtil.isNull(pollExecutor)) { - ThreadFactory namedThreadFactory = new NamedThreadFactory("RedisParser-Polling-Thread-", false); + ThreadFactory namedThreadFactory = new NamedThreadFactory("Redis-Polling-", false); pollExecutor = new ScheduledThreadPoolExecutor( CORE_POOL_SIZE, namedThreadFactory, diff --git a/liteflow-testcase-el/liteflow-testcase-el-redis-springboot/src/test/java/com/yomahub/liteflow/test/redis/RedisSubscribeTestCondition.java b/liteflow-testcase-el/liteflow-testcase-el-redis-springboot/src/test/java/com/yomahub/liteflow/test/redis/RedisSubscribeTestCondition.java new file mode 100644 index 000000000..82a307efc --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-redis-springboot/src/test/java/com/yomahub/liteflow/test/redis/RedisSubscribeTestCondition.java @@ -0,0 +1,29 @@ +package com.yomahub.liteflow.test.redis; + +import org.redisson.Redisson; +import org.redisson.api.RedissonClient; +import org.redisson.api.redisnode.RedisNodes; +import org.redisson.api.redisnode.RedisSingle; +import org.redisson.config.Config; + +/** + * 判断本地是否启动Redis + * + * @author hxinyu + * @since 2.11.0 + */ +public class RedisSubscribeTestCondition { + + /* 若6379端口未启动Redis则返回true */ + public static boolean notStartRedis() { + try{ + Config config = new Config(); + config.useSingleServer().setAddress("redis://127.0.0.1:6379"); + RedissonClient redissonClient = Redisson.create(config); + RedisSingle redisNode = redissonClient.getRedisNodes(RedisNodes.SINGLE); + return !redisNode.pingAll(); + } catch (Exception e) { + return true; + } + } +} 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/RedisWithXmlELPollSpringbootTest.java index 18786d427..05749ee9a 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/RedisWithXmlELPollSpringbootTest.java @@ -2,11 +2,16 @@ 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.parser.redis.mode.RClient; +import com.yomahub.liteflow.property.LiteflowConfigGetter; 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; @@ -58,9 +63,13 @@ public class RedisWithXmlELPollSpringbootTest extends BaseTest { "local sha1 = redis.sha1hex(value);\n" + "return sha1;"; + @AfterEach public void after() { FlowBus.cleanCache(); + FlowInitHook.cleanHook(); + ExecutorHelper.loadInstance().clearExecutorServiceMap(); + SpiFactoryCleaner.clean(); } /** @@ -94,7 +103,7 @@ public class RedisWithXmlELPollSpringbootTest extends BaseTest { Assertions.assertTrue(response.isSuccess()); Assertions.assertEquals("a==>b==>c", response.getExecuteStepStr()); - Thread.sleep(2000); + Thread.sleep(4000); //测试修改后的chain response = flowExecutor.execute2Resp("chain11", "arg"); @@ -106,10 +115,10 @@ public class RedisWithXmlELPollSpringbootTest extends BaseTest { * 测试script */ @Test - public void testPollWithScriptXml() throws InterruptedException { + public void testPollWithScript() throws InterruptedException { Set chainNameSet = new HashSet<>(); chainNameSet.add("chain22"); - String chainValue = "THEN(a, b, c, s11, s22, s33);"; + String chainValue = "THEN(s11, s22, s33, a, b);"; when(chainClient.hkeys("pollChainKey")).thenReturn(chainNameSet); when(chainClient.hget("pollChainKey", "chain22")).thenReturn(chainValue); when(chainClient.scriptLoad(luaOfKey)).thenReturn("keysha"); @@ -150,9 +159,9 @@ public class RedisWithXmlELPollSpringbootTest extends BaseTest { Assertions.assertTrue(response.isSuccess()); Assertions.assertEquals("hello s11", context.getData("test11")); Assertions.assertEquals("hello s22", context.getData("test22")); - Assertions.assertEquals("a==>b==>c==>s11[脚本s11]==>s22[脚本s22]==>s33[脚本s33]", response.getExecuteStepStrWithoutTime()); + Assertions.assertEquals("s11[脚本s11]==>s22[脚本s22]==>s33[脚本s33]==>a==>b", response.getExecuteStepStrWithoutTime()); - Thread.sleep(2000); + Thread.sleep(4000); //测试修改后的script response = flowExecutor.execute2Resp("chain22", "arg"); 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 0d815d23a..3b82456f9 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 @@ -31,9 +31,12 @@ import javax.annotation.Resource; /** * springboot环境下的redis配置源订阅模式功能测试 * - * 由于redisson中RMapCache监听器功能无法mock测试 + * 由于Redisson中RMapCache的监听器功能无法mock测试 * 故Sub模式测试用例需本地启动Redis服务 连接地址: 127.0.0.1:6379 - * 若本地该端口号未启动Redis服务 则自动忽略本类中测试用例 + * 若本地该端口号未启动Redis 则自动忽略本类中测试用例 + * + * 测试用例会在1号database中添加测试数据 chainKey:testChainKey; scriptKey:testScriptKey + * 测试完成后清除测试数据 * * @author hxinyu * @since 2.11.0 @@ -56,8 +59,8 @@ public class RedisWithXmlELSubscribeSpringbootTest extends BaseTest { Config config = new Config(); config.useSingleServer().setAddress("redis://127.0.0.1:6379").setDatabase(1); redissonClient = Redisson.create(config); - RMapCache chainKey = redissonClient.getMapCache("chainKey"); - RMapCache scriptKey = redissonClient.getMapCache("scriptKey"); + RMapCache chainKey = redissonClient.getMapCache("testChainKey"); + RMapCache scriptKey = redissonClient.getMapCache("testScriptKey"); scriptKey.put("s1:script:脚本s1:groovy", "defaultContext.setData(\"test1\",\"hello s1\");"); scriptKey.put("s2:script:脚本s2:js", "defaultContext.setData(\"test2\",\"hello s2\");"); scriptKey.put("s3:script:脚本s3", "defaultContext.setData(\"test3\",\"hello s3\");"); @@ -68,7 +71,6 @@ public class RedisWithXmlELSubscribeSpringbootTest extends BaseTest { @AfterAll public static void after(){ - System.out.println("after"); testCleanData(); } @@ -179,17 +181,16 @@ public class RedisWithXmlELSubscribeSpringbootTest extends BaseTest { //redis内规则数据数据清空 public static void testCleanData(){ if(ObjectUtil.isNotNull(redissonClient)){ - RMapCache scriptKey = redissonClient.getMapCache("chainKey"); - RMapCache chainKey = redissonClient.getMapCache("scriptKey"); + RMapCache chainKey = redissonClient.getMapCache("testChainKey"); + RMapCache scriptKey = redissonClient.getMapCache("testScriptKey"); for (String key : chainKey.keySet()) { chainKey.remove(key); } for (String key : scriptKey.keySet()) { scriptKey.remove(key); } - chainKey.keySet().forEach(System.out::println); - System.out.println(""); - scriptKey.keySet().forEach(System.out::println); + chainKey.delete(); + scriptKey.delete(); } } } 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-xml.properties index 567bff7b4..aea34f3c5 100644 --- 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-xml.properties @@ -1,8 +1,8 @@ liteflow.rule-source-ext-data={\ "host":"localhost",\ "port":6379,\ - "pollingInterval":1,\ - "pollingStartTime":1,\ + "pollingInterval":2,\ + "pollingStartTime":2,\ "chainDataBase":1,\ "chainKey":"pollChainKey",\ "scriptDataBase":1,\ diff --git a/liteflow-testcase-el/liteflow-testcase-el-redis-springboot/src/test/resources/redis/application-sub-xml.properties b/liteflow-testcase-el/liteflow-testcase-el-redis-springboot/src/test/resources/redis/application-sub-xml.properties index 22a58eed0..6d841fea1 100644 --- a/liteflow-testcase-el/liteflow-testcase-el-redis-springboot/src/test/resources/redis/application-sub-xml.properties +++ b/liteflow-testcase-el/liteflow-testcase-el-redis-springboot/src/test/resources/redis/application-sub-xml.properties @@ -3,8 +3,8 @@ liteflow.rule-source-ext-data={\ "port":6379,\ "mode":"sub",\ "chainDataBase":1,\ - "chainKey":"chainKey",\ + "chainKey":"testChainKey",\ "scriptDataBase":1,\ - "scriptKey":"scriptKey"\ + "scriptKey":"testScriptKey"\ } liteflow.parse-on-start=false \ No newline at end of file