From e0a20a12794788ca959d821db2a586a2a30f7049 Mon Sep 17 00:00:00 2001 From: Kugaaa Date: Fri, 3 Nov 2023 15:21:49 +0800 Subject: [PATCH] =?UTF-8?q?feature=20=E8=B6=85=E6=97=B6=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=AF=AB=E7=A7=92=20decl=20multi=20springboo?= =?UTF-8?q?t=20UT?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...MillisecondsELDeclMultiSpringbootTest.java | 179 ++++++++++++++++++ .../maxWaitMilliseconds/cmp/CmpConfig.java | 98 ++++++++++ .../application.properties | 1 + .../resources/maxWaitMilliseconds/flow.el.xml | 110 +++++++++++ 4 files changed, 388 insertions(+) create mode 100644 liteflow-testcase-el/liteflow-testcase-el-declare-multi-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitMilliseconds/MaxWaitMillisecondsELDeclMultiSpringbootTest.java create mode 100644 liteflow-testcase-el/liteflow-testcase-el-declare-multi-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitMilliseconds/cmp/CmpConfig.java create mode 100644 liteflow-testcase-el/liteflow-testcase-el-declare-multi-springboot/src/test/resources/maxWaitMilliseconds/application.properties create mode 100644 liteflow-testcase-el/liteflow-testcase-el-declare-multi-springboot/src/test/resources/maxWaitMilliseconds/flow.el.xml diff --git a/liteflow-testcase-el/liteflow-testcase-el-declare-multi-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitMilliseconds/MaxWaitMillisecondsELDeclMultiSpringbootTest.java b/liteflow-testcase-el/liteflow-testcase-el-declare-multi-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitMilliseconds/MaxWaitMillisecondsELDeclMultiSpringbootTest.java new file mode 100644 index 000000000..28561e5ae --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-declare-multi-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitMilliseconds/MaxWaitMillisecondsELDeclMultiSpringbootTest.java @@ -0,0 +1,179 @@ +package com.yomahub.liteflow.test.maxWaitMilliseconds; + +import com.yomahub.liteflow.builder.el.LiteFlowChainELBuilder; +import com.yomahub.liteflow.core.FlowExecutor; +import com.yomahub.liteflow.exception.WhenTimeoutException; +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.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +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.concurrent.TimeoutException; + +import static com.yomahub.liteflow.test.maxWaitSeconds.cmp.CmpConfig.CONTENT_KEY; + +/** + * Spring Boot 环境下超时控制测试 - maxWaitMilliseconds + * + * @author Kugaaa + * @since 2.11.1 + */ +@ExtendWith(SpringExtension.class) +@TestPropertySource(value = "classpath:/maxWaitMilliseconds/application.properties") +@SpringBootTest(classes = MaxWaitMillisecondsELDeclMultiSpringbootTest.class) +@EnableAutoConfiguration +@ComponentScan({"com.yomahub.liteflow.test.maxWaitMilliseconds.cmp"}) +public class MaxWaitMillisecondsELDeclMultiSpringbootTest extends BaseTest { + @Resource + private FlowExecutor flowExecutor; + + // 测试 THEN 的超时情况 + @Test + public void testThen1() { + assertTimeout("then1"); + } + + // 测试 THEN 的非超时情况 + @Test + public void testThen2() { + assertNotTimeout("then2"); + } + + // 测试 When 的超时情况 + @Test + public void testWhen1() { + assertWhenTimeout("when1"); + } + + // 测试 WHEN 的非超时情况 + @Test + public void testWhen2() { + assertNotTimeout("when2"); + } + + // 测试 FOR 的超时情况 + @Test + public void testFor1() { + assertTimeout("for1"); + } + + // 测试 FOR 的非超时情况 + @Test + public void testFor2() { + assertNotTimeout("for2"); + } + + // 测试 WHILE 的超时情况 + @Test + public void testWhile1() { + assertTimeout("while1"); + } + + // 测试 WHILE 的非超时情况 + @Test + public void testWhile2() { + assertNotTimeout("while2"); + } + + // 测试 ITERATOR 的超时情况 + @Test + public void testIterator1() { + assertTimeout("iterator1"); + } + + // 测试 ITERATOR 的非超时情况 + @Test + public void testIterator2() { + assertNotTimeout("iterator2"); + } + + // 测试 SWITCH 的超时情况 + @Test + public void testSwitch1() { + assertTimeout("switch1"); + } + + // 测试 SWITCH 的非超时情况 + @Test + public void testSwitch2() { + assertNotTimeout("switch2"); + } + + // 测试 IF 的超时情况 + @Test + public void testIf1() { + assertTimeout("if1"); + } + + // 测试 SWITCH 的非超时情况 + @Test + public void testIf2() { + assertNotTimeout("if2"); + } + + // 测试单个组件的超时情况 + @Test + public void testComponent1() { + assertTimeout("component1"); + } + + // 测试单个组件的非超时情况 + @Test + public void testComponent2() { + assertNotTimeout("component2"); + } + + // 测试 FINALLY,虽然超时,但 FINALLY 仍会执行 + @Test + public void testFinally1() { + LiteflowResponse response = flowExecutor.execute2Resp("finally", "arg"); + Assertions.assertFalse(response.isSuccess()); + Assertions.assertEquals(TimeoutException.class, response.getCause().getClass()); + // FINALLY 执行时在默认数据上下文中放入了 CONTENT_KEY + DefaultContext contextBean = response.getFirstContextBean(); + Assertions.assertTrue(contextBean.hasData(CONTENT_KEY)); + } + + // 测试 maxWaitMilliseconds 关键字不能作用于 Finally + @Test + public void testFinally2() { + Assertions.assertFalse(LiteFlowChainELBuilder.validate("THEN(a, b, FINALLY(c).maxWaitMilliseconds(100))")); + } + + // 测试 chain 的超时情况 + @Test + public void testChain1() { + assertTimeout("chain1"); + } + + // 测试 chain 的非超时情况 + @Test + public void testChain2() { + assertNotTimeout("chain2"); + } + + private void assertTimeout(String chainId) { + LiteflowResponse response = flowExecutor.execute2Resp(chainId, "arg"); + Assertions.assertFalse(response.isSuccess()); + Assertions.assertEquals(TimeoutException.class, response.getCause().getClass()); + } + + private void assertWhenTimeout(String chainId) { + LiteflowResponse response = flowExecutor.execute2Resp(chainId, "arg"); + Assertions.assertFalse(response.isSuccess()); + Assertions.assertEquals(WhenTimeoutException.class, response.getCause().getClass()); + } + + private void assertNotTimeout(String chainId) { + LiteflowResponse response = flowExecutor.execute2Resp(chainId, "arg"); + Assertions.assertTrue(response.isSuccess()); + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-declare-multi-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitMilliseconds/cmp/CmpConfig.java b/liteflow-testcase-el/liteflow-testcase-el-declare-multi-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitMilliseconds/cmp/CmpConfig.java new file mode 100644 index 000000000..2e2194dfc --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-declare-multi-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitMilliseconds/cmp/CmpConfig.java @@ -0,0 +1,98 @@ +package com.yomahub.liteflow.test.maxWaitMilliseconds.cmp; + +import cn.hutool.core.collection.ListUtil; +import com.yomahub.liteflow.annotation.LiteflowComponent; +import com.yomahub.liteflow.annotation.LiteflowMethod; +import com.yomahub.liteflow.core.NodeComponent; +import com.yomahub.liteflow.enums.LiteFlowMethodEnum; +import com.yomahub.liteflow.enums.NodeTypeEnum; +import com.yomahub.liteflow.slot.DefaultContext; + +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +@LiteflowComponent +public class CmpConfig { + + public static final String CONTENT_KEY = "testKey"; + + private int count = 0; + + // 执行过的 chain + Set executedChain = new HashSet<>(); + + @LiteflowMethod(value = LiteFlowMethodEnum.PROCESS, nodeId = "a") + public void processA(NodeComponent bindCmp) { + try { + Thread.sleep(100); + } catch (Exception e) { + e.printStackTrace(); + } + + System.out.println("ACmp executed!"); + } + + @LiteflowMethod(value = LiteFlowMethodEnum.PROCESS, nodeId = "b") + public void processB(NodeComponent bindCmp) { + try { + Thread.sleep(200); + } catch (Exception e) { + e.printStackTrace(); + } + + System.out.println("BCmp executed!"); + } + + @LiteflowMethod(value = LiteFlowMethodEnum.PROCESS, nodeId = "c") + public void process(NodeComponent bindCmp) { + try { + Thread.sleep(500); + } catch (Exception e) { + e.printStackTrace(); + } + + System.out.println("CCmp executed!"); + } + + @LiteflowMethod(value = LiteFlowMethodEnum.PROCESS, nodeId = "d") + public void processD(NodeComponent bindCmp) { + try { + Thread.sleep(500); + DefaultContext contextBean = bindCmp.getFirstContextBean(); + contextBean.setData(CONTENT_KEY, "value"); + } catch (Exception e) { + e.printStackTrace(); + } + + System.out.println("DCmp executed!"); + } + + @LiteflowMethod(value = LiteFlowMethodEnum.PROCESS_IF, nodeId = "f", nodeType = NodeTypeEnum.IF) + public boolean processIf(NodeComponent bindCmp) throws Exception { + return true; + } + + @LiteflowMethod(value = LiteFlowMethodEnum.PROCESS_SWITCH, nodeId = "s", nodeType = NodeTypeEnum.SWITCH) + public String processSwitch(NodeComponent bindCmp) throws Exception { + return "b"; + } + + @LiteflowMethod(value = LiteFlowMethodEnum.PROCESS_WHILE, nodeId = "w", nodeType = NodeTypeEnum.WHILE) + public boolean processWhile(NodeComponent bindCmp) throws Exception { + // 判断是否切换了 chain + if (!executedChain.contains(bindCmp.getCurrChainId())) { + count = 0; + executedChain.add(bindCmp.getCurrChainId()); + } + count++; + return count <= 2; + } + + @LiteflowMethod(value = LiteFlowMethodEnum.PROCESS_ITERATOR, nodeId = "x", nodeType = NodeTypeEnum.ITERATOR) + public Iterator processIterator(NodeComponent bindCmp) throws Exception { + List list = ListUtil.toList("one", "two"); + return list.iterator(); + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-declare-multi-springboot/src/test/resources/maxWaitMilliseconds/application.properties b/liteflow-testcase-el/liteflow-testcase-el-declare-multi-springboot/src/test/resources/maxWaitMilliseconds/application.properties new file mode 100644 index 000000000..678dc827e --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-declare-multi-springboot/src/test/resources/maxWaitMilliseconds/application.properties @@ -0,0 +1 @@ +liteflow.rule-source=maxWaitMilliseconds/flow.el.xml \ No newline at end of file diff --git a/liteflow-testcase-el/liteflow-testcase-el-declare-multi-springboot/src/test/resources/maxWaitMilliseconds/flow.el.xml b/liteflow-testcase-el/liteflow-testcase-el-declare-multi-springboot/src/test/resources/maxWaitMilliseconds/flow.el.xml new file mode 100644 index 000000000..13ead3d38 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-declare-multi-springboot/src/test/resources/maxWaitMilliseconds/flow.el.xml @@ -0,0 +1,110 @@ + + + + + + + THEN(a,b).maxWaitMilliseconds(200); + + + + THEN(a,b).maxWaitMilliseconds(500); + + + + + + WHEN(a,c).maxWaitMilliseconds(300); + + + + WHEN(a,b).maxWaitMilliseconds(300); + + + + + + FOR(2).DO(a).maxWaitMilliseconds(100); + + + + FOR(2).DO(a).maxWaitMilliseconds(300); + + + + + WHILE(w).DO(a).maxWaitMilliseconds(100); + + + + WHILE(w).DO(a).maxWaitMilliseconds(300); + + + + + ITERATOR(x).DO(a).maxWaitMilliseconds(100); + + + + ITERATOR(x).DO(a).maxWaitMilliseconds(300); + + + + + + + SWITCH(s).TO(a, b).maxWaitMilliseconds(100); + + + + SWITCH(s).TO(a, b).maxWaitMilliseconds(300); + + + + + + + IF(f, b, c).maxWaitMilliseconds(100); + + + + IF(f, b, c).maxWaitMilliseconds(300); + + + + + + WHEN( + a.maxWaitMilliseconds(200), + c.maxWaitMilliseconds(300) + ); + + + + WHEN( + a.maxWaitMilliseconds(200), + b.maxWaitMilliseconds(300) + ); + + + + + + THEN(PRE(a), b, FINALLY(d)).maxWaitMilliseconds(200); + + + + + THEN(b) + + + + testChain.maxWaitMilliseconds(100); + + + + testChain.maxWaitMilliseconds(300); + + \ No newline at end of file