feat client & add test
This commit is contained in:
parent
c5e221c9a5
commit
8242c97f24
|
@ -0,0 +1,59 @@
|
|||
package com.yomahub.liteflow.parser.redis.mode.subscribe;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import org.redisson.api.RMapCache;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.redisson.api.map.event.MapEntryListener;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Redisson 客户端封装类.
|
||||
*
|
||||
* @author hxinyu
|
||||
* @since 2.11.0
|
||||
*/
|
||||
public class RClient {
|
||||
|
||||
private final RedissonClient redissonClient;
|
||||
|
||||
private Map<String, String> map = new HashMap<>();
|
||||
|
||||
public RClient(RedissonClient redissonClient) {
|
||||
this.redissonClient = redissonClient;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* get hashmap of the key
|
||||
*
|
||||
* @param key
|
||||
* @return hashmap
|
||||
*/
|
||||
public Map<String, String> getMap(String key) {
|
||||
RMapCache<String, String> mapCache = redissonClient.getMapCache(key);
|
||||
Set<String> mapFieldSet = mapCache.keySet();
|
||||
if (CollectionUtil.isEmpty(mapFieldSet)) {
|
||||
return map;
|
||||
}
|
||||
for (String field : mapFieldSet) {
|
||||
String value = mapCache.get(field);
|
||||
map.put(field, value);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* add listener of the key
|
||||
* @param key
|
||||
* @param listener
|
||||
* @return listener id
|
||||
*/
|
||||
public int addListener(String key, MapEntryListener listener) {
|
||||
RMapCache<Object, Object> mapCache = redissonClient.getMapCache(key);
|
||||
return mapCache.addListener(listener);
|
||||
}
|
||||
}
|
|
@ -21,11 +21,12 @@ import org.redisson.config.Config;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Redis Pub/Sub机制实现类
|
||||
* Redisson客户端 RMapCache存储结构
|
||||
* 使用 Redisson客户端 RMapCache存储结构
|
||||
*
|
||||
* @author hxinyu
|
||||
* @since 2.11.0
|
||||
|
@ -35,9 +36,9 @@ public class RedisParserSubscribeMode implements RedisParserHelper {
|
|||
|
||||
private final RedisParserVO redisParserVO;
|
||||
|
||||
private RedissonClient chainClient;
|
||||
private RClient chainClient;
|
||||
|
||||
private RedissonClient scriptClient;
|
||||
private RClient scriptClient;
|
||||
|
||||
public RedisParserSubscribeMode(RedisParserVO redisParserVO) {
|
||||
this.redisParserVO = redisParserVO;
|
||||
|
@ -51,11 +52,11 @@ public class RedisParserSubscribeMode implements RedisParserHelper {
|
|||
}
|
||||
if (ObjectUtil.isNull(chainClient)) {
|
||||
Config config = getRedissonConfig(redisParserVO, redisParserVO.getChainDataBase());
|
||||
this.chainClient = Redisson.create(config);
|
||||
this.chainClient = new RClient(Redisson.create(config));
|
||||
//如果有脚本数据
|
||||
if (ObjectUtil.isNotNull(redisParserVO.getScriptDataBase())) {
|
||||
config = getRedissonConfig(redisParserVO, redisParserVO.getScriptDataBase());
|
||||
this.scriptClient = Redisson.create(config);
|
||||
this.scriptClient = new RClient(Redisson.create(config));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -86,16 +87,16 @@ public class RedisParserSubscribeMode implements RedisParserHelper {
|
|||
public String getContent() {
|
||||
try {
|
||||
// 检查chainKey下有没有子节点
|
||||
RMapCache<String, String> chainKey = chainClient.getMapCache(redisParserVO.getChainKey());
|
||||
Set<String> chainNameSet = chainKey.keySet();
|
||||
if (CollectionUtil.isEmpty(chainNameSet)) {
|
||||
Map<String, String> chainMap = chainClient.getMap(redisParserVO.getChainKey());
|
||||
if (CollectionUtil.isEmpty(chainMap)) {
|
||||
throw new RedisException(StrUtil.format("There are no chains in key [{}]",
|
||||
redisParserVO.getChainKey()));
|
||||
}
|
||||
// 获取chainKey下的所有子节点内容List
|
||||
List<String> chainItemContentList = new ArrayList<>();
|
||||
for (String chainId : chainNameSet) {
|
||||
String chainData = chainKey.get(chainId);
|
||||
for (Map.Entry<String, String> entry : chainMap.entrySet()) {
|
||||
String chainId = entry.getKey();
|
||||
String chainData = entry.getValue();
|
||||
if (StrUtil.isNotBlank(chainData)) {
|
||||
chainItemContentList.add(StrUtil.format(CHAIN_XML_PATTERN, chainId, chainData));
|
||||
}
|
||||
|
@ -106,19 +107,17 @@ public class RedisParserSubscribeMode implements RedisParserHelper {
|
|||
// 检查是否有脚本内容,如果有,进行脚本内容的获取
|
||||
String scriptAllContent = StrUtil.EMPTY;
|
||||
if (hasScript()) {
|
||||
RMapCache<String, String> scriptKey = scriptClient.getMapCache(redisParserVO.getScriptKey());
|
||||
Set<String> scriptFieldSet = scriptKey.keySet();
|
||||
|
||||
Map<String, String> scriptMap = scriptClient.getMap(redisParserVO.getScriptKey());
|
||||
List<String> scriptItemContentList = new ArrayList<>();
|
||||
for (String scriptFieldValue : scriptFieldSet) {
|
||||
for (Map.Entry<String, String> entry : scriptMap.entrySet()) {
|
||||
String scriptFieldValue = entry.getKey();
|
||||
String scriptData = entry.getValue();
|
||||
NodeSimpleVO nodeSimpleVO = RedisParserHelper.convert(scriptFieldValue);
|
||||
if (ObjectUtil.isNull(nodeSimpleVO)) {
|
||||
throw new RedisException(
|
||||
StrUtil.format("The name of the redis field [{}] in scriptKey [{}] is invalid",
|
||||
scriptFieldValue, scriptKey));
|
||||
scriptFieldValue, redisParserVO.getScriptKey()));
|
||||
}
|
||||
String scriptData = scriptKey.get(scriptFieldValue);
|
||||
|
||||
// 有语言类型
|
||||
if (StrUtil.isNotBlank(nodeSimpleVO.getLanguage())) {
|
||||
scriptItemContentList.add(StrUtil.format(NODE_ITEM_WITH_LANGUAGE_XML_PATTERN,
|
||||
|
@ -150,9 +149,8 @@ public class RedisParserSubscribeMode implements RedisParserHelper {
|
|||
}
|
||||
try {
|
||||
// 存在这个节点,但是子节点不存在
|
||||
RMapCache<String, String> scriptKey = scriptClient.getMapCache(redisParserVO.getScriptKey());
|
||||
Set<String> scriptKeySet = scriptKey.keySet();
|
||||
return !CollUtil.isEmpty(scriptKeySet);
|
||||
Map<String, String> scriptMap = scriptClient.getMap(redisParserVO.getScriptKey());
|
||||
return !CollUtil.isEmpty(scriptMap);
|
||||
}
|
||||
catch (Exception e) {
|
||||
return false;
|
||||
|
@ -165,38 +163,38 @@ public class RedisParserSubscribeMode implements RedisParserHelper {
|
|||
@Override
|
||||
public void listenRedis() {
|
||||
//监听 chain
|
||||
RMapCache<String, String> chainKey = chainClient.getMapCache(redisParserVO.getChainKey());
|
||||
String chainKey = redisParserVO.getChainKey();
|
||||
//添加新 chain
|
||||
chainKey.addListener((EntryCreatedListener<String, String>) event -> {
|
||||
chainClient.addListener(chainKey, (EntryCreatedListener<String, String>) event -> {
|
||||
LOG.info("starting reload flow config... create key={} value={},", event.getKey(), event.getValue());
|
||||
LiteFlowChainELBuilder.createChain().setChainId(event.getKey()).setEL(event.getValue()).build();
|
||||
});
|
||||
//修改 chain
|
||||
chainKey.addListener((EntryUpdatedListener<String, String>) event -> {
|
||||
chainClient.addListener(chainKey, (EntryUpdatedListener<String, String>) event -> {
|
||||
LOG.info("starting reload flow config... update key={} new value={},", event.getKey(), event.getValue());
|
||||
LiteFlowChainELBuilder.createChain().setChainId(event.getKey()).setEL(event.getValue()).build();
|
||||
});
|
||||
//删除 chain
|
||||
chainKey.addListener((EntryRemovedListener<String, String>) event -> {
|
||||
chainClient.addListener(chainKey, (EntryRemovedListener<String, String>) event -> {
|
||||
LOG.info("starting reload flow config... delete key={}", event.getKey());
|
||||
FlowBus.removeChain(event.getKey());
|
||||
});
|
||||
|
||||
//监听 script
|
||||
if (ObjectUtil.isNotNull(scriptClient) && ObjectUtil.isNotNull(redisParserVO.getScriptDataBase())) {
|
||||
RMapCache<String, String> scriptKey = scriptClient.getMapCache(redisParserVO.getScriptKey());
|
||||
String scriptKey = redisParserVO.getScriptKey();
|
||||
//添加 script
|
||||
scriptKey.addListener((EntryCreatedListener<String, String>) event -> {
|
||||
scriptClient.addListener(scriptKey, (EntryCreatedListener<String, String>) event -> {
|
||||
LOG.info("starting reload flow config... create key={} value={},", event.getKey(), event.getValue());
|
||||
RedisParserHelper.changeScriptNode(event.getKey(), event.getValue());
|
||||
});
|
||||
//修改 script
|
||||
scriptKey.addListener((EntryUpdatedListener<String, String>) event -> {
|
||||
scriptClient.addListener(scriptKey, (EntryUpdatedListener<String, String>) event -> {
|
||||
LOG.info("starting reload flow config... update key={} new value={},", event.getKey(), event.getValue());
|
||||
RedisParserHelper.changeScriptNode(event.getKey(), event.getValue());
|
||||
});
|
||||
//删除 script
|
||||
scriptKey.addListener((EntryRemovedListener<String, String>) event -> {
|
||||
scriptClient.addListener(scriptKey, (EntryRemovedListener<String, String>) event -> {
|
||||
LOG.info("starting reload flow config... delete key={}", event.getKey());
|
||||
NodeSimpleVO nodeSimpleVO = RedisParserHelper.convert(event.getKey());
|
||||
FlowBus.getNodeMap().remove(nodeSimpleVO.getNodeId());
|
||||
|
|
|
@ -21,8 +21,7 @@ import javax.annotation.Resource;
|
|||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
|
@ -65,21 +64,24 @@ public class RedisWithXmlELPollSpringbootTest extends BaseTest {
|
|||
Set<String> chainNameSet = new HashSet<>();
|
||||
chainNameSet.add("chain11");
|
||||
String chainValue = "THEN(a, b, c);";
|
||||
//SHA值用于测试修改chain的轮询刷新功能
|
||||
Object chainSHA = DigestUtil.sha1Hex(chainValue);
|
||||
|
||||
//SHA值用于测试修改chain的轮询刷新功能
|
||||
//修改chain并更新SHA值
|
||||
String changeChainValue = "THEN(a, c);";
|
||||
Object changeChainSHA = DigestUtil.sha1Hex(changeChainValue);
|
||||
when(chainJedis.hkeys("pollChainKey")).thenReturn(chainNameSet);
|
||||
when(chainJedis.hget("pollChainKey", "chain11")).thenReturn(chainValue).thenReturn(changeChainValue);
|
||||
when(chainJedis.evalsha(anyString(), anyInt(), anyString())).thenReturn(chainSHA).thenReturn(changeChainSHA);
|
||||
|
||||
//测试修改前的chain
|
||||
LiteflowResponse response = flowExecutor.execute2Resp("chain11", "arg");
|
||||
Assertions.assertTrue(response.isSuccess());
|
||||
Assertions.assertEquals("a==>b==>c", response.getExecuteStepStr());
|
||||
|
||||
flowExecutor.reloadRule();
|
||||
|
||||
//测试修改后的chain
|
||||
response = flowExecutor.execute2Resp("chain11", "arg");
|
||||
Assertions.assertTrue(response.isSuccess());
|
||||
Assertions.assertEquals("a==>c", response.getExecuteStepStr());
|
||||
|
@ -103,16 +105,37 @@ public class RedisWithXmlELPollSpringbootTest extends BaseTest {
|
|||
String s11 = "defaultContext.setData(\"test11\",\"hello s11\");";
|
||||
String s22 = "defaultContext.setData(\"test22\",\"hello s22\");";
|
||||
String s33 = "defaultContext.setData(\"test33\",\"hello s33\");";
|
||||
//SHA值用于测试修改script的轮询刷新功能
|
||||
Object s11SHA = DigestUtil.sha1Hex(s11);
|
||||
Object s22SHA = DigestUtil.sha1Hex(s22);
|
||||
Object s33SHA = DigestUtil.sha1Hex(s33);
|
||||
//修改script值并更新SHA值
|
||||
String changeS11 = "defaultContext.setData(\"test11\",\"hello world\");";
|
||||
Object changeS11SHA = DigestUtil.sha1Hex(changeS11);
|
||||
|
||||
when(scriptJedis.hkeys("pollScriptKey")).thenReturn(scriptFieldSet);
|
||||
when(scriptJedis.hget("pollScriptKey", "s11:script:脚本s11:groovy")).thenReturn(s11);
|
||||
when(scriptJedis.hget("pollScriptKey", "s11:script:脚本s11:groovy")).thenReturn(s11).thenReturn(changeS11);
|
||||
when(scriptJedis.hget("pollScriptKey", "s22:script:脚本s22:js")).thenReturn(s22);
|
||||
when(scriptJedis.hget("pollScriptKey", "s33:script:脚本s33")).thenReturn(s33);
|
||||
//分别模拟三个script的evalsha指纹值计算的返回值, 其中s11脚本修改 指纹值变化
|
||||
when(scriptJedis.evalsha(anyString(), eq(2), eq("pollScriptKey"), eq("s11:script:脚本s11:groovy"))).thenReturn(s11SHA).thenReturn(changeS11SHA);
|
||||
when(scriptJedis.evalsha(anyString(), eq(2), eq("pollScriptKey"), eq("s22:script:脚本s22:js"))).thenReturn(s22SHA);
|
||||
when(scriptJedis.evalsha(anyString(), eq(2), eq("pollScriptKey"), eq("s33:script:脚本s33"))).thenReturn(s33SHA);
|
||||
|
||||
//测试修改前的script
|
||||
LiteflowResponse response = flowExecutor.execute2Resp("chain22", "arg");
|
||||
DefaultContext context = response.getFirstContextBean();
|
||||
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());
|
||||
|
||||
flowExecutor.reloadRule();
|
||||
|
||||
//测试修改后的script
|
||||
response = flowExecutor.execute2Resp("chain22", "arg");
|
||||
context = response.getFirstContextBean();
|
||||
Assertions.assertTrue(response.isSuccess());
|
||||
Assertions.assertEquals("hello world", context.getData("test11"));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,35 +1,31 @@
|
|||
package com.yomahub.liteflow.test.redis;
|
||||
|
||||
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.vo.RedisParserVO;
|
||||
import com.yomahub.liteflow.property.LiteflowConfig;
|
||||
import com.yomahub.liteflow.property.LiteflowConfigGetter;
|
||||
import com.yomahub.liteflow.parser.redis.mode.subscribe.RClient;
|
||||
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 com.yomahub.liteflow.util.JsonUtil;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.redisson.Redisson;
|
||||
import org.redisson.api.RMapCache;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.redisson.config.Config;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
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.Set;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* springboot环境下的redis配置源订阅模式功能测试
|
||||
|
@ -44,140 +40,87 @@ import java.util.Set;
|
|||
@ComponentScan({"com.yomahub.liteflow.test.redis.cmp"})
|
||||
public class RedisWithXmlELSubscribeSpringbootTest extends BaseTest {
|
||||
|
||||
private static RedissonClient redissonClient;
|
||||
@MockBean(name = "chainClient")
|
||||
private static RClient chainClient;
|
||||
|
||||
@MockBean(name = "scriptClient")
|
||||
private static RClient scriptClient;
|
||||
|
||||
@Resource
|
||||
private FlowExecutor flowExecutor;
|
||||
|
||||
@BeforeAll
|
||||
public static void setUpBeforeClass() {
|
||||
Config config = new Config();
|
||||
config.useSingleServer().setAddress("redis://127.0.0.1:6379").setDatabase(1);
|
||||
redissonClient = Redisson.create(config);
|
||||
RMapCache<String, String> chainKey = redissonClient.getMapCache("chainKey");
|
||||
RMapCache<String, String> scriptKey = redissonClient.getMapCache("scriptKey");
|
||||
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\");");
|
||||
chainKey.put("chain1", "THEN(a, b, c);");
|
||||
chainKey.put("chain2", "THEN(a, b, c, s3);");
|
||||
chainKey.put("chain3", "THEN(a, b, c, s1, s2);");
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void after() {
|
||||
FlowBus.cleanCache();
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试 chain
|
||||
*/
|
||||
@Test
|
||||
public void testSubWithXml() throws InterruptedException {
|
||||
public void testSubWithXml() {
|
||||
Map<String, String> chainMap = new HashMap<>();
|
||||
chainMap.put("chain1", "THEN(a, b, c);");
|
||||
//修改chain值
|
||||
Map<String, String> changeChainMap = new HashMap<>();
|
||||
changeChainMap.put("chain1", "THEN(a, c);");
|
||||
when(chainClient.getMap("chainKey")).thenReturn(chainMap).thenReturn(changeChainMap);
|
||||
|
||||
//测试修改前的chain
|
||||
LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg");
|
||||
Assertions.assertTrue(response.isSuccess());
|
||||
Assertions.assertEquals("a==>b==>c", response.getExecuteStepStr());
|
||||
|
||||
//修改redis中规则
|
||||
changeXMLData();
|
||||
//重新加载规则
|
||||
Thread.sleep(50);
|
||||
Assertions.assertEquals("a==>c==>b", flowExecutor.execute2Resp("chain1", "arg").getExecuteStepStr());
|
||||
flowExecutor.reloadRule();
|
||||
|
||||
//删除redis中规则
|
||||
deleteXMLData();
|
||||
//重新加载规则
|
||||
Thread.sleep(50);
|
||||
//测试修改后的chain
|
||||
response = flowExecutor.execute2Resp("chain1", "arg");
|
||||
Assertions.assertTrue(!response.isSuccess());
|
||||
|
||||
//添加redis中规则
|
||||
addXMLData();
|
||||
//重新加载规则
|
||||
Thread.sleep(50);
|
||||
Assertions.assertEquals("b==>c", flowExecutor.execute2Resp("chain4", "arg").getExecuteStepStr());
|
||||
Assertions.assertTrue(response.isSuccess());
|
||||
Assertions.assertEquals("a==>c", response.getExecuteStepStr());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSubWithScriptXml() throws InterruptedException {
|
||||
LiteflowResponse response = flowExecutor.execute2Resp("chain3", "arg");
|
||||
public void testSubWithScriptXml() {
|
||||
Map<String, String> chainMap = new HashMap<>();
|
||||
chainMap.put("chain2", "THEN(a, b, c, s1, s2, s3);");
|
||||
|
||||
Map<String, String> scriptMap = new HashMap<>();
|
||||
scriptMap.put("s1:script:脚本s1:groovy", "defaultContext.setData(\"test1\",\"hello s1\");");
|
||||
scriptMap.put("s2:script:脚本s2:js", "defaultContext.setData(\"test2\",\"hello s2\");");
|
||||
scriptMap.put("s3:script:脚本s3", "defaultContext.setData(\"test3\",\"hello s3\");");
|
||||
//修改chain值和script值
|
||||
Map<String, String> changeChainMap = new HashMap<>();
|
||||
changeChainMap.put("chain2", "THEN(a, c, s1, s3);");
|
||||
Map<String, String> changeScriptMap = new HashMap<>();
|
||||
changeScriptMap.put("s1:script:脚本s1:groovy", "defaultContext.setData(\"test1\",\"hello world\");");
|
||||
changeScriptMap.put("s2:script:脚本s2:js", "defaultContext.setData(\"test2\",\"hello s2\");");
|
||||
changeScriptMap.put("s3:script:脚本s3", "defaultContext.setData(\"test3\",\"hello s3\");");
|
||||
|
||||
when(chainClient.getMap("chainKey")).thenReturn(chainMap).thenReturn(changeChainMap);
|
||||
//这里是因为脚本的getMap方法在一次流程里会执行到两次
|
||||
when(scriptClient.getMap("scriptKey")).thenReturn(scriptMap).thenReturn(scriptMap)
|
||||
.thenReturn(changeScriptMap).thenReturn(changeScriptMap);
|
||||
|
||||
//测试修改前的chain和script
|
||||
LiteflowResponse response = flowExecutor.execute2Resp("chain2", "arg");
|
||||
DefaultContext context = response.getFirstContextBean();
|
||||
Assertions.assertTrue(response.isSuccess());
|
||||
Assertions.assertEquals("hello s1", context.getData("test1"));
|
||||
Assertions.assertEquals("a==>b==>c==>s1[脚本s1]==>s2[脚本s2]", response.getExecuteStepStrWithoutTime());
|
||||
Assertions.assertEquals("hello s2", context.getData("test2"));
|
||||
Assertions.assertEquals("a==>b==>c==>s1[脚本s1]==>s2[脚本s2]==>s3[脚本s3]", response.getExecuteStepStrWithoutTime());
|
||||
|
||||
//添加和删除脚本
|
||||
addAndDeleteScriptData();
|
||||
//修改redis脚本
|
||||
changeScriptData();
|
||||
Thread.sleep(50);
|
||||
context = flowExecutor.execute2Resp("chain3", "arg").getFirstContextBean();
|
||||
Assertions.assertEquals("hello s1 version2", context.getData("test1"));
|
||||
context = flowExecutor.execute2Resp("chain2", "arg").getFirstContextBean();
|
||||
Assertions.assertEquals("hello s3 version2", context.getData("test2"));
|
||||
flowExecutor.reloadRule();
|
||||
|
||||
//测试修改后的chain和script
|
||||
response = flowExecutor.execute2Resp("chain2", "arg");
|
||||
context = response.getFirstContextBean();
|
||||
Assertions.assertTrue(response.isSuccess());
|
||||
Assertions.assertEquals("hello world", context.getData("test1"));
|
||||
Assertions.assertEquals("a==>c==>s1[脚本s1]==>s3[脚本s3]", response.getExecuteStepStrWithoutTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改redisson中的chain
|
||||
*/
|
||||
public void changeXMLData() {
|
||||
LiteflowConfig liteflowConfig = LiteflowConfigGetter.get();
|
||||
RedisParserVO redisParserVO = JsonUtil.parseObject(liteflowConfig.getRuleSourceExtData(), RedisParserVO.class);
|
||||
RMapCache<String, String> chainKey = redissonClient.getMapCache(redisParserVO.getChainKey());
|
||||
chainKey.put("chain1", "THEN(a, c, b);");
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除redisson中的chain
|
||||
*/
|
||||
public void deleteXMLData() {
|
||||
LiteflowConfig liteflowConfig = LiteflowConfigGetter.get();
|
||||
RedisParserVO redisParserVO = JsonUtil.parseObject(liteflowConfig.getRuleSourceExtData(), RedisParserVO.class);
|
||||
RMapCache<String, String> chainKey = redissonClient.getMapCache(redisParserVO.getChainKey());
|
||||
chainKey.remove("chain1");
|
||||
chainKey.remove("chain4");
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增redisson中的chain
|
||||
*/
|
||||
public void addXMLData() {
|
||||
LiteflowConfig liteflowConfig = LiteflowConfigGetter.get();
|
||||
RedisParserVO redisParserVO = JsonUtil.parseObject(liteflowConfig.getRuleSourceExtData(), RedisParserVO.class);
|
||||
RMapCache<String, String> chainKey = redissonClient.getMapCache(redisParserVO.getChainKey());
|
||||
chainKey.put("chain4","THEN(b, c);");
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改redisson中的脚本
|
||||
*/
|
||||
public void changeScriptData() {
|
||||
LiteflowConfig liteflowConfig = LiteflowConfigGetter.get();
|
||||
RedisParserVO redisParserVO = JsonUtil.parseObject(liteflowConfig.getRuleSourceExtData(), RedisParserVO.class);
|
||||
RMapCache<String, String> scriptKey = redissonClient.getMapCache(redisParserVO.getScriptKey());
|
||||
scriptKey.put("s1:script:脚本s1:groovy", "defaultContext.setData(\"test1\",\"hello s1 version2\");");
|
||||
scriptKey.put("s3:script:脚本s3", "defaultContext.setData(\"test2\",\"hello s3 version2\");");
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增和删除redisson中的chain
|
||||
*/
|
||||
public void addAndDeleteScriptData() {
|
||||
LiteflowConfig liteflowConfig = LiteflowConfigGetter.get();
|
||||
RedisParserVO redisParserVO = JsonUtil.parseObject(liteflowConfig.getRuleSourceExtData(), RedisParserVO.class);
|
||||
RMapCache<String, String> scriptKey = redissonClient.getMapCache(redisParserVO.getScriptKey());
|
||||
scriptKey.remove("s3:script:脚本s3");
|
||||
scriptKey.put("s5:script:脚本s5:groovy", "defaultContext.setData(\"test1\",\"hello s5\");");
|
||||
}
|
||||
|
||||
/* //为便于测试的redis内规则数据数据清空
|
||||
@Test
|
||||
public void testCleanData(){
|
||||
LiteflowConfig liteflowConfig = LiteflowConfigGetter.get();
|
||||
RedisParserVO redisParserVO = JsonUtil.parseObject(liteflowConfig.getRuleSourceExtData(), RedisParserVO.class);
|
||||
RMapCache<String, String> scriptKey = redissonClient.getMapCache(redisParserVO.getScriptKey());
|
||||
RMapCache<String, String> chainKey = redissonClient.getMapCache(redisParserVO.getChainKey());
|
||||
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);
|
||||
System.out.println("数据清空完成");
|
||||
}*/
|
||||
}
|
||||
|
|
|
@ -1,89 +0,0 @@
|
|||
package com.yomahub.liteflow.test.redis;
|
||||
|
||||
import com.yomahub.liteflow.core.FlowExecutor;
|
||||
import com.yomahub.liteflow.flow.LiteflowResponse;
|
||||
import com.yomahub.liteflow.slot.DefaultContext;
|
||||
import com.yomahub.liteflow.test.BaseTest;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.redisson.api.RMapCache;
|
||||
import org.redisson.api.RedissonClient;
|
||||
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.List;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* springboot环境下的redis配置源订阅模式功能测试
|
||||
*
|
||||
* @author hxinyu
|
||||
* @since 2.11.0
|
||||
*/
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@TestPropertySource(value = "classpath:/redis/application-sub-xml.properties")
|
||||
@SpringBootTest(classes = RedisWithXmlELSubscribeSpringbootTest2.class)
|
||||
@EnableAutoConfiguration
|
||||
@ComponentScan({"com.yomahub.liteflow.test.redis.cmp"})
|
||||
public class RedisWithXmlELSubscribeSpringbootTest2 extends BaseTest {
|
||||
|
||||
@MockBean(name = "chainClient")
|
||||
private static RedissonClient chainClient;
|
||||
|
||||
@MockBean(name = "scriptClient")
|
||||
private static RedissonClient scriptClient;
|
||||
|
||||
@Resource
|
||||
private FlowExecutor flowExecutor;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
}
|
||||
|
||||
/* @BeforeAll
|
||||
public static void setUpBeforeClass() {
|
||||
Config config = new Config();
|
||||
config.useSingleServer().setAddress("redis://127.0.0.1:6379").setDatabase(1);
|
||||
redissonClient = Redisson.create(config);
|
||||
RMapCache<String, String> chainKey = redissonClient.getMapCache("chainKey");
|
||||
RMapCache<String, String> scriptKey = redissonClient.getMapCache("scriptKey");
|
||||
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\");");
|
||||
chainKey.put("chain1", "THEN(a, b, c);");
|
||||
chainKey.put("chain2", "THEN(a, b, c, s3);");
|
||||
chainKey.put("chain3", "THEN(a, b, c, s1, s2);");
|
||||
}*/
|
||||
|
||||
@Test
|
||||
public void testSubWithXml() throws InterruptedException {
|
||||
RMapCache<Object, Object> chainKey = chainClient.getMapCache("");
|
||||
System.out.println(chainKey);
|
||||
|
||||
/* LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg");
|
||||
Assertions.assertTrue(response.isSuccess());
|
||||
Assertions.assertEquals("a==>b==>c", response.getExecuteStepStr());*/
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSubWithScriptXml() throws InterruptedException {
|
||||
LiteflowResponse response = flowExecutor.execute2Resp("chain3", "arg");
|
||||
DefaultContext context = response.getFirstContextBean();
|
||||
Assertions.assertTrue(response.isSuccess());
|
||||
Assertions.assertEquals("hello s1", context.getData("test1"));
|
||||
Assertions.assertEquals("a==>b==>c==>s1[脚本s1]==>s2[脚本s2]", response.getExecuteStepStrWithoutTime());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue