From 9467f8ba6b0eaf858eaf3b374c82322edabb6240 Mon Sep 17 00:00:00 2001 From: zendwang Date: Tue, 8 Nov 2022 20:10:28 +0800 Subject: [PATCH] =?UTF-8?q?enhancement=20=E6=94=AF=E6=8C=81etcd=E5=88=86?= =?UTF-8?q?=E7=A6=BBchain=E4=BB=A5=E5=8F=8A=E8=84=9A=E6=9C=AC=E7=9A=84?= =?UTF-8?q?=E5=AD=98=E5=82=A8=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../parser/etcd/util/EtcdParserHelper.java | 4 +- .../etcd/EtcdWithXmlELSpringbootTest.java | 75 +++++++++++-------- .../resources/etcd/application-xml.properties | 3 +- 3 files changed, 47 insertions(+), 35 deletions(-) diff --git a/liteflow-rule-plugin/liteflow-rule-etcd/src/main/java/com/yomahub/liteflow/parser/etcd/util/EtcdParserHelper.java b/liteflow-rule-plugin/liteflow-rule-etcd/src/main/java/com/yomahub/liteflow/parser/etcd/util/EtcdParserHelper.java index 45b378fc1..4cb4aaebc 100644 --- a/liteflow-rule-plugin/liteflow-rule-etcd/src/main/java/com/yomahub/liteflow/parser/etcd/util/EtcdParserHelper.java +++ b/liteflow-rule-plugin/liteflow-rule-etcd/src/main/java/com/yomahub/liteflow/parser/etcd/util/EtcdParserHelper.java @@ -142,7 +142,9 @@ public class EtcdParserHelper { public void listen(Consumer parseConsumer) { this.client.watchChildChange(this.etcdParserVO.getChainPath(), (updatePath, updateValue) -> { LOG.info("update path={} value={},starting reload flow config...", updatePath, updateValue); - parseConsumer.accept(getContent()); + String content = getContent(); + LOG.info("update path={} value={},content={}", content); +// parseConsumer.accept(content); }, (deletePath) -> { LOG.info("delete path={},starting reload flow config...", deletePath); parseConsumer.accept(getContent()); diff --git a/liteflow-testcase-el/liteflow-testcase-el-etcd-springboot/src/test/java/com/yomahub/liteflow/test/etcd/EtcdWithXmlELSpringbootTest.java b/liteflow-testcase-el/liteflow-testcase-el-etcd-springboot/src/test/java/com/yomahub/liteflow/test/etcd/EtcdWithXmlELSpringbootTest.java index 082e36f10..283edd5fe 100644 --- a/liteflow-testcase-el/liteflow-testcase-el-etcd-springboot/src/test/java/com/yomahub/liteflow/test/etcd/EtcdWithXmlELSpringbootTest.java +++ b/liteflow-testcase-el/liteflow-testcase-el-etcd-springboot/src/test/java/com/yomahub/liteflow/test/etcd/EtcdWithXmlELSpringbootTest.java @@ -1,53 +1,49 @@ package com.yomahub.liteflow.test.etcd; -import cn.hutool.core.lang.Console; -import cn.hutool.core.util.ReflectUtil; +import com.google.common.collect.Lists; import com.yomahub.liteflow.core.FlowExecutor; -import com.yomahub.liteflow.enums.FlowParserTypeEnum; import com.yomahub.liteflow.flow.FlowBus; import com.yomahub.liteflow.flow.LiteflowResponse; import com.yomahub.liteflow.parser.etcd.EtcdClient; -import com.yomahub.liteflow.parser.etcd.EtcdXmlELParser; -import com.yomahub.liteflow.parser.etcd.util.EtcdParserHelper; import com.yomahub.liteflow.slot.DefaultContext; -import com.yomahub.liteflow.spi.holder.ContextAwareHolder; import com.yomahub.liteflow.test.BaseTest; import org.junit.*; import org.junit.runner.RunWith; -import org.mockito.Answers; -import org.mockito.Mock; 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.boot.test.mock.mockito.MockitoTestExecutionListener; import org.springframework.context.annotation.ComponentScan; -import org.springframework.test.context.TestExecutionListeners; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringRunner; import javax.annotation.Resource; -import java.util.function.Consumer; - -import static org.mockito.ArgumentMatchers.any; +import java.util.List; import static org.mockito.Mockito.*; /** * springboot环境下的etcd 规则解析器 测试 */ @RunWith(SpringRunner.class) -@TestPropertySource(value = "classpath:/etcd/application-xml.properties") +@TestPropertySource(value = "classpath:/etcd/application-xml-cluster.properties") @SpringBootTest(classes = EtcdWithXmlELSpringbootTest.class) @EnableAutoConfiguration @ComponentScan({"com.yomahub.liteflow.test.etcd.cmp"}) public class EtcdWithXmlELSpringbootTest extends BaseTest { - //@MockBean - //private EtcdClient etcdClient; + @MockBean + private EtcdClient etcdClient; @Resource private FlowExecutor flowExecutor; + private static final String SEPARATOR = "/"; + + private static final String CHAIN_PATH = "/liteflow/chain"; + + private static final String SCRIPT_PATH = "/liteflow/script"; + + @Before public void setUp(){ MockitoAnnotations.initMocks(this); @@ -60,35 +56,48 @@ public class EtcdWithXmlELSpringbootTest extends BaseTest { @Test public void testEtcdNodeWithXml1() throws Exception { - //String flowXml = "THEN(a, b, c);"; - //when(etcdClient.get(anyString())).thenReturn(flowXml); + List chainNameList = Lists.newArrayList("chain1"); + List scriptNodeValueList = Lists.newArrayList("s1:script:脚本s1"); + when(etcdClient.getChildrenKeys(anyString(), anyString())).thenReturn(chainNameList).thenReturn(scriptNodeValueList); + + String chain1Data = "THEN(a, b, c, s1);"; + String scriptNodeValue = "defaultContext.setData(\"test\",\"hello\");"; + when(etcdClient.get(anyString())).thenReturn(chain1Data).thenReturn(scriptNodeValue); LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg"); DefaultContext context = response.getFirstContextBean(); Assert.assertTrue(response.isSuccess()); - Assert.assertEquals("a==>b==>s1[脚本s1]", response.getExecuteStepStr()); + Assert.assertEquals("a==>b==>c==>s1[脚本s1]", response.getExecuteStepStr()); Assert.assertEquals("hello", context.getData("test")); } @Test public void testEtcdNodeWithXml2() throws Exception { -// String flowXml = "THEN(a, b, c);"; -// String changedFlowXml = "THEN(a, c);"; -// when(etcdClient.get(anyString())).thenReturn(flowXml).thenReturn(changedFlowXml); -// - LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg"); - Assert.assertTrue(response.isSuccess()); - Assert.assertEquals("a==>b==>s1[脚本s1]", response.getExecuteStepStr()); + List chainNameList = Lists.newArrayList("chain1"); + List scriptNodeValueList = Lists.newArrayList("s1:script:脚本s1"); + when(etcdClient.getChildrenKeys(CHAIN_PATH, SEPARATOR)).thenReturn(chainNameList); + when(etcdClient.getChildrenKeys(SCRIPT_PATH, SEPARATOR)).thenReturn(scriptNodeValueList); - int i=0; - while (i <= 100000) { - i++; - } - // 手动触发一次 模拟节点数据变更 - //FlowBus.refreshFlowMetaData(FlowParserTypeEnum.TYPE_EL_XML,changedFlowXml); + String chain1Data = "THEN(a, b, c, s1);"; + String chain1ChangedData = "THEN(a, b, s1);"; + String scriptNodeValue = "defaultContext.setData(\"test\",\"hello\");"; + String scriptNodeChangedValue = "defaultContext.setData(\"test\",\"hello world\");"; + when(etcdClient.get(CHAIN_PATH + SEPARATOR + "chain1")).thenReturn(chain1Data).thenReturn(chain1ChangedData); + when(etcdClient.get(SCRIPT_PATH + SEPARATOR + "s1:script:脚本s1")).thenReturn(scriptNodeValue).thenReturn(scriptNodeChangedValue); + + LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg"); + DefaultContext context = response.getFirstContextBean(); + Assert.assertTrue(response.isSuccess()); + Assert.assertEquals("a==>b==>c==>s1[脚本s1]", response.getExecuteStepStr()); + Assert.assertEquals("hello", context.getData("test")); + + flowExecutor.reloadRule(); LiteflowResponse response2 = flowExecutor.execute2Resp("chain1", "arg"); + DefaultContext context2 = response2.getFirstContextBean(); Assert.assertTrue(response2.isSuccess()); - Assert.assertEquals("a==>b==>c==>s1[脚本s1]", response.getExecuteStepStr()); + Assert.assertEquals("a==>b==>s1[脚本s1]", response2.getExecuteStepStr()); + Assert.assertEquals("hello world", context2.getData("test")); + } } diff --git a/liteflow-testcase-el/liteflow-testcase-el-etcd-springboot/src/test/resources/etcd/application-xml.properties b/liteflow-testcase-el/liteflow-testcase-el-etcd-springboot/src/test/resources/etcd/application-xml.properties index 9f880ba1b..60f528aa4 100644 --- a/liteflow-testcase-el/liteflow-testcase-el-etcd-springboot/src/test/resources/etcd/application-xml.properties +++ b/liteflow-testcase-el/liteflow-testcase-el-etcd-springboot/src/test/resources/etcd/application-xml.properties @@ -2,4 +2,5 @@ liteflow.rule-source-ext-data={\ "endpoints":"http://127.0.0.1:2379",\ "chainPath": "/liteflow/chain",\ "scriptPath": "/liteflow/script"\ - } \ No newline at end of file + } +liteflow.parse-on-start=false \ No newline at end of file