diff --git a/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/MaxWaitSecondsELDeclSpringBootTest.java b/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/MaxWaitSecondsELDeclSpringBootTest.java new file mode 100644 index 000000000..c74ee5796 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/MaxWaitSecondsELDeclSpringBootTest.java @@ -0,0 +1,184 @@ +package com.yomahub.liteflow.test.maxWaitSeconds; + +import com.yomahub.liteflow.builder.LiteFlowNodeBuilder; +import com.yomahub.liteflow.builder.el.LiteFlowChainELBuilder; +import com.yomahub.liteflow.core.FlowExecutor; +import com.yomahub.liteflow.enums.NodeTypeEnum; +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 com.yomahub.liteflow.test.loop.LoopELDeclSpringbootTest; +import com.yomahub.liteflow.test.maxWaitSeconds.cmp.ACmp; +import com.yomahub.liteflow.test.maxWaitSeconds.cmp.BCmp; +import com.yomahub.liteflow.test.maxWaitSeconds.cmp.CCmp; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +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.junit4.SpringRunner; + +import javax.annotation.Resource; + +import static com.yomahub.liteflow.test.maxWaitSeconds.cmp.DCmp.CONTENT_KEY; + +/** + * springboot环境下超时控制测试 + * + * @author DaleLee + * @since 2.11.0 + */ +@RunWith(SpringRunner.class) +@TestPropertySource(value = "classpath:/maxWaitSeconds/application.properties") +@SpringBootTest(classes = MaxWaitSecondsELDeclSpringBootTest.class) +@EnableAutoConfiguration +@ComponentScan({ "com.yomahub.liteflow.test.maxWaitSeconds.cmp" }) +public class MaxWaitSecondsELDeclSpringBootTest 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() { + assertTimeout("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"); + Assert.assertFalse(response.isSuccess()); + Assert.assertEquals(WhenTimeoutException.class, response.getCause().getClass()); + // FINALLY 执行时在默认数据上下文中放入了 CONTENT_KEY + DefaultContext contextBean = response.getFirstContextBean(); + Assert.assertTrue(contextBean.hasData(CONTENT_KEY)); + } + + // 测试 maxWaitSeconds 关键字不能作用于 Finally + @Test + public void testFinally2() { + LiteFlowNodeBuilder.createNode() + .setId("a") + .setName("组件A") + .setType(NodeTypeEnum.COMMON) + .setClazz(ACmp.class) + .build(); + LiteFlowNodeBuilder.createNode() + .setId("b") + .setName("组件B") + .setType(NodeTypeEnum.COMMON) + .setClazz(BCmp.class) + .build(); + LiteFlowNodeBuilder.createNode() + .setId("c") + .setName("组件C") + .setType(NodeTypeEnum.COMMON) + .setClazz(CCmp.class) + .build(); + Assert.assertFalse(LiteFlowChainELBuilder.validate("THEN(a, b, FINALLY(c).maxWaitSeconds(10))")); + } + + private void assertTimeout(String chainId) { + LiteflowResponse response = flowExecutor.execute2Resp(chainId, "arg"); + Assert.assertFalse(response.isSuccess()); + Assert.assertEquals(WhenTimeoutException.class, response.getCause().getClass()); + } + + private void assertNotTimeout(String chainId) { + LiteflowResponse response = flowExecutor.execute2Resp(chainId, "arg"); + Assert.assertTrue(response.isSuccess()); + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/ACmp.java b/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/ACmp.java new file mode 100644 index 000000000..9afb4ff80 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/ACmp.java @@ -0,0 +1,21 @@ +package com.yomahub.liteflow.test.maxWaitSeconds.cmp; + +import com.yomahub.liteflow.annotation.LiteflowComponent; +import com.yomahub.liteflow.annotation.LiteflowMethod; +import com.yomahub.liteflow.core.NodeComponent; +import com.yomahub.liteflow.enums.LiteFlowMethodEnum; + +@LiteflowComponent("a") +public class ACmp { + + @LiteflowMethod(LiteFlowMethodEnum.PROCESS) + public void process(NodeComponent bindCmp) { + try { + Thread.sleep(1000); + } catch (Exception e) { + e.printStackTrace(); + } + + System.out.println("ACmp executed!"); + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/BCmp.java b/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/BCmp.java new file mode 100644 index 000000000..aefcba9ea --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/BCmp.java @@ -0,0 +1,21 @@ +package com.yomahub.liteflow.test.maxWaitSeconds.cmp; + +import com.yomahub.liteflow.annotation.LiteflowComponent; +import com.yomahub.liteflow.annotation.LiteflowMethod; +import com.yomahub.liteflow.core.NodeComponent; +import com.yomahub.liteflow.enums.LiteFlowMethodEnum; + +@LiteflowComponent("b") +public class BCmp { + + @LiteflowMethod(LiteFlowMethodEnum.PROCESS) + public void process(NodeComponent bindCmp) { + try { + Thread.sleep(2000); + } catch (Exception e) { + e.printStackTrace(); + } + + System.out.println("BCmp executed!"); + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/CCmp.java b/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/CCmp.java new file mode 100644 index 000000000..4127c8bb6 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/CCmp.java @@ -0,0 +1,21 @@ +package com.yomahub.liteflow.test.maxWaitSeconds.cmp; + +import com.yomahub.liteflow.annotation.LiteflowComponent; +import com.yomahub.liteflow.annotation.LiteflowMethod; +import com.yomahub.liteflow.core.NodeComponent; +import com.yomahub.liteflow.enums.LiteFlowMethodEnum; + +@LiteflowComponent("c") +public class CCmp { + + @LiteflowMethod(LiteFlowMethodEnum.PROCESS) + public void process(NodeComponent bindCmp) { + try { + Thread.sleep(5000); + } catch (Exception e) { + e.printStackTrace(); + } + + System.out.println("CCmp executed!"); + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/DCmp.java b/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/DCmp.java new file mode 100644 index 000000000..248f31471 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/DCmp.java @@ -0,0 +1,26 @@ +package com.yomahub.liteflow.test.maxWaitSeconds.cmp; + +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.slot.DefaultContext; + +@LiteflowComponent("d") +public class DCmp { + + public static final String CONTENT_KEY = "testKey"; + + @LiteflowMethod(LiteFlowMethodEnum.PROCESS) + public void process(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!"); + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/FCmp.java b/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/FCmp.java new file mode 100644 index 000000000..f2b3bd1c3 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/FCmp.java @@ -0,0 +1,18 @@ +package com.yomahub.liteflow.test.maxWaitSeconds.cmp; + +import com.yomahub.liteflow.annotation.LiteflowCmpDefine; +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; + +@LiteflowComponent("f") +@LiteflowCmpDefine(NodeTypeEnum.IF) +public class FCmp { + + @LiteflowMethod(value = LiteFlowMethodEnum.PROCESS_IF, nodeType = NodeTypeEnum.IF) + public boolean processIf(NodeComponent bindCmp) throws Exception { + return true; + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/SCmp.java b/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/SCmp.java new file mode 100644 index 000000000..d81b5d471 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/SCmp.java @@ -0,0 +1,18 @@ +package com.yomahub.liteflow.test.maxWaitSeconds.cmp; + +import com.yomahub.liteflow.annotation.LiteflowCmpDefine; +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; + +@LiteflowComponent("s") +@LiteflowCmpDefine(NodeTypeEnum.SWITCH) +public class SCmp { + + @LiteflowMethod(value = LiteFlowMethodEnum.PROCESS_SWITCH, nodeType = NodeTypeEnum.SWITCH) + public String processSwitch(NodeComponent bindCmp) throws Exception { + return "b"; + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/WCmp.java b/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/WCmp.java new file mode 100644 index 000000000..3f1dbcdd3 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/WCmp.java @@ -0,0 +1,20 @@ +package com.yomahub.liteflow.test.maxWaitSeconds.cmp; + +import com.yomahub.liteflow.annotation.LiteflowCmpDefine; +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; + +@LiteflowComponent("w") +@LiteflowCmpDefine(NodeTypeEnum.WHILE) +public class WCmp { + private int count = 0; + + @LiteflowMethod(value = LiteFlowMethodEnum.PROCESS_WHILE, nodeType = NodeTypeEnum.WHILE) + public boolean processWhile(NodeComponent bindCmp) throws Exception { + count++; + return count <= 2; + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/XCmp.java b/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/XCmp.java new file mode 100644 index 000000000..b1c06ade4 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/XCmp.java @@ -0,0 +1,23 @@ +package com.yomahub.liteflow.test.maxWaitSeconds.cmp; + +import cn.hutool.core.collection.ListUtil; +import com.yomahub.liteflow.annotation.LiteflowCmpDefine; +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 java.util.Iterator; +import java.util.List; + +@LiteflowComponent("x") +@LiteflowCmpDefine(NodeTypeEnum.ITERATOR) +public class XCmp { + + @LiteflowMethod(value = LiteFlowMethodEnum.PROCESS_ITERATOR, 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-springboot/src/test/resources/maxWaitSeconds/application.properties b/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/resources/maxWaitSeconds/application.properties new file mode 100644 index 000000000..4dae0deff --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/resources/maxWaitSeconds/application.properties @@ -0,0 +1 @@ +liteflow.rule-source=maxWaitSeconds/flow.el.xml \ No newline at end of file diff --git a/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/resources/maxWaitSeconds/flow.el.xml b/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/resources/maxWaitSeconds/flow.el.xml new file mode 100644 index 000000000..e32dc3203 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/resources/maxWaitSeconds/flow.el.xml @@ -0,0 +1,98 @@ + + + + + + + THEN(a,b).maxWaitSeconds(2); + + + + THEN(a,b).maxWaitSeconds(5); + + + + + + WHEN(a,c).maxWaitSeconds(3); + + + + WHEN(a,b).maxWaitSeconds(3); + + + + + + FOR(2).DO(a).maxWaitSeconds(1); + + + + FOR(2).DO(a).maxWaitSeconds(3); + + + + + WHILE(w).DO(a).maxWaitSeconds(1); + + + + WHILE(w).DO(a).maxWaitSeconds(3); + + + + + ITERATOR(x).DO(a).maxWaitSeconds(1); + + + + ITERATOR(x).DO(a).maxWaitSeconds(3); + + + + + + + SWITCH(s).TO(a, b).maxWaitSeconds(1); + + + + SWITCH(s).TO(a, b).maxWaitSeconds(3); + + + + + + + IF(f, b, c).maxWaitSeconds(1); + + + + IF(f, b, c).maxWaitSeconds(3); + + + + + + WHEN( + a.maxWaitSeconds(2), + c.maxWaitSeconds(3) + ); + + + + WHEN( + a.maxWaitSeconds(2), + b.maxWaitSeconds(3) + ); + + + + + + THEN(PRE(a), b, FINALLY(d)).maxWaitSeconds(2); + + + \ No newline at end of file