diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/LiteFlowChainELBuilder.java b/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/LiteFlowChainELBuilder.java index 327e5b60b..97e3de3e5 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/LiteFlowChainELBuilder.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/LiteFlowChainELBuilder.java @@ -78,6 +78,7 @@ public class LiteFlowChainELBuilder { EXPRESS_RUNNER.addFunctionAndClassMethod(ChainConstant.DO, Object.class, new DoOperator()); EXPRESS_RUNNER.addFunctionAndClassMethod(ChainConstant.BREAK, Object.class, new BreakOperator()); EXPRESS_RUNNER.addFunctionAndClassMethod(ChainConstant.DATA, Object.class, new DataOperator()); + EXPRESS_RUNNER.addFunctionAndClassMethod(ChainConstant.MAX_WAIT_SECONDS, Object.class, new MaxWaitSecondsOperator()); EXPRESS_RUNNER.addFunctionAndClassMethod(ChainConstant.PARALLEL, Object.class, new ParallelOperator()); } diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/operator/MaxWaitSecondsOperator.java b/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/operator/MaxWaitSecondsOperator.java new file mode 100644 index 000000000..e76252402 --- /dev/null +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/operator/MaxWaitSecondsOperator.java @@ -0,0 +1,104 @@ +package com.yomahub.liteflow.builder.el.operator; + +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.text.StrFormatter; +import com.ql.util.express.exception.QLException; +import com.yomahub.liteflow.builder.el.operator.base.BaseOperator; +import com.yomahub.liteflow.builder.el.operator.base.OperatorHelper; +import com.yomahub.liteflow.flow.element.Condition; +import com.yomahub.liteflow.flow.element.Executable; +import com.yomahub.liteflow.flow.element.condition.*; + +import java.util.List; +import java.util.concurrent.TimeUnit; + +/** + * EL 规则中的 maxWaitSeconds 的操作符 + * + * @author DaleLee + * @since 2.11.0 + */ +public class MaxWaitSecondsOperator extends BaseOperator { + @Override + public Condition build(Object[] objects) throws Exception { + OperatorHelper.checkObjectSizeEqTwo(objects); + Executable executable = OperatorHelper.convert(objects[0], Executable.class); + // 获取传入的时间参数 + Integer maxWaitSeconds = OperatorHelper.convert(objects[1], Integer.class); + if (executable instanceof WhenCondition) { + // WhenCondition,直接设置等待时间 + WhenCondition whenCondition = OperatorHelper.convert(executable, WhenCondition.class); + whenCondition.setMaxWaitTime(maxWaitSeconds); + whenCondition.setMaxWaitTimeUnit(TimeUnit.SECONDS); + return whenCondition; + } else if (executable instanceof FinallyCondition) { + // FINALLY,报错 + String errorMsg = StrFormatter.format("The caller [{}] cannot use the keyword \"maxWaitSeconds'\"", executable.toString()); + throw new QLException(errorMsg); + } else if (containsFinally(executable)) { + // 处理 THEN 中的 FINALLY + ThenCondition thenCondition = OperatorHelper.convert(executable, ThenCondition.class); + return handleFinally(thenCondition, maxWaitSeconds); + } else { + // 其他情况,被 WHEN 包装 + return wrappedByTimeout(executable, maxWaitSeconds); + } + } + + /** + * 将一个 Executable 包装为带有单独超时控制的 TimeoutCondition + * + * @param executable 待包装对象 + * @param maxWaitSeconds 最大等待秒数 + * @return 包装后的 TimeoutCondition + */ + private TimeoutCondition wrappedByTimeout(Executable executable, Integer maxWaitSeconds) { + TimeoutCondition timeoutCondition = new TimeoutCondition(); + timeoutCondition.addExecutable(executable); + timeoutCondition.setMaxWaitTime(maxWaitSeconds); + timeoutCondition.setMaxWaitTimeUnit(TimeUnit.SECONDS); + return timeoutCondition; + } + + /** + * 判断 THEN 中是否含有 FINALLY 组件 + * + * @param executable 判断对象 + * @return 含有 FINALLY 组件返回 true,否则返回 false + */ + private boolean containsFinally(Executable executable) { + return executable instanceof ThenCondition + && CollUtil.isNotEmpty(((ThenCondition) executable).getFinallyConditionList()); + } + + /** + * 将 FINALLY 排除在超时控制之外 + * + * @param thenCondition 待处理的 ThenCondition + * @param maxWaitSeconds 最大等待秒数 + * @return 处理后的 ThenCondition + */ + private ThenCondition handleFinally(ThenCondition thenCondition, Integer maxWaitSeconds) { + // 进行如下转换 + // THEN(PRE(a),b,FINALLY(c)) + // => THEN( + // WHEN(THEN(PRE(a),b)), + // FINALLY(c)) + + // 定义外层 THEN + ThenCondition outerThenCondition = new ThenCondition(); + + // 把 FINALLY 转移到外层 THEN + List finallyList = thenCondition.getExecutableList(ConditionKey.FINALLY_KEY); + finallyList.forEach(executable + -> outerThenCondition + .addFinallyCondition((FinallyCondition) executable)); + finallyList.clear(); + + // 包装内部 THEN + WhenCondition whenCondition = wrappedByTimeout(thenCondition, maxWaitSeconds); + outerThenCondition.addExecutable(whenCondition); + + return outerThenCondition; + } +} diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/common/ChainConstant.java b/liteflow-core/src/main/java/com/yomahub/liteflow/common/ChainConstant.java index 35037f481..7379f26ea 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/common/ChainConstant.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/common/ChainConstant.java @@ -82,4 +82,6 @@ public interface ChainConstant { String NOT = "NOT"; + String MAX_WAIT_SECONDS = "maxWaitSeconds"; + } diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/TimeoutCondition.java b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/TimeoutCondition.java new file mode 100644 index 000000000..290535f62 --- /dev/null +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/TimeoutCondition.java @@ -0,0 +1,58 @@ +package com.yomahub.liteflow.flow.element.condition; + +import cn.hutool.core.text.StrFormatter; +import cn.hutool.core.util.ObjectUtil; +import com.yomahub.liteflow.exception.WhenTimeoutException; +import com.yomahub.liteflow.flow.element.Chain; +import com.yomahub.liteflow.flow.element.Condition; +import com.yomahub.liteflow.flow.element.Executable; +import com.yomahub.liteflow.flow.element.Node; + +import java.util.concurrent.TimeoutException; + +/** + * 超时控制 Condition + * + * @author DaleLee + * @since 2.11.0 + */ +public class TimeoutCondition extends WhenCondition { + + @Override + public void executeCondition(Integer slotIndex) throws Exception { + try { + super.executeCondition(slotIndex); + } catch (WhenTimeoutException ex) { + // 将 WhenTimeoutException 转换为 TimeoutException + String errMsg = StrFormatter.format("Timed out when executing the chain [{}] because [{}] exceeded {} {}.", + this.getCurrChainId(), this.getCurrentExecutableId(), this.getMaxWaitTime(), this.getMaxWaitTimeUnit().toString().toLowerCase()); + throw new TimeoutException(errMsg); + } + } + + /** + * 获取当前组件的 id + * + * @return + */ + private String getCurrentExecutableId() { + // TimeoutCondition 只有一个 Executable + Executable executable = this.getExecutableList().get(0); + if (ObjectUtil.isNotNull(executable.getId())) { + // 已经有 id 了 + return executable.getId(); + } + // 定义 id + switch (executable.getExecuteType()) { + // chain 和 node 一般都有 id + case CHAIN: + return ((Chain) executable).getChainId(); + case CONDITION: + return "condition-" + ((Condition) executable).getConditionType().getName(); + case NODE: + return "node-" + ((Node) executable).getType().getCode(); + default: + return "unknown-executable"; + } + } +} diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/WhenCondition.java b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/WhenCondition.java index f18c59d57..830aee652 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/WhenCondition.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/WhenCondition.java @@ -52,6 +52,12 @@ public class WhenCondition extends Condition { // when单独的线程池名称 private String threadExecutorClass; + // 异步线程最⻓的等待时间 + private Integer maxWaitTime; + + // 等待时间单位 + private TimeUnit maxWaitTimeUnit; + @Override public void executeCondition(Integer slotIndex) throws Exception { executeAsyncCondition(slotIndex); @@ -86,15 +92,20 @@ public class WhenCondition extends Condition { // 3.根据condition.getNodeList()的集合进行流处理,用map进行把executable对象转换成List> // 4.在转的过程中,套入CompletableFutureTimeout方法进行超时判断,如果超时则用WhenFutureObj.timeOut返回超时的对象 // 5.第2个参数是主要的本体CompletableFuture,传入了ParallelSupplier和线程池对象 - Integer whenMaxWaitTime; - TimeUnit whenMaxWaitTimeUnit; + if (ObjectUtil.isNull(this.getMaxWaitTime())) { + if (ObjectUtil.isNotNull(liteflowConfig.getWhenMaxWaitSeconds())) { + // 获取全局异步线程最长等待秒数 + this.setMaxWaitTime(liteflowConfig.getWhenMaxWaitSeconds()); + this.setMaxWaitTimeUnit(TimeUnit.SECONDS); + } else { + // 获取全局异步线程最⻓的等待时间 + this.setMaxWaitTime(liteflowConfig.getWhenMaxWaitTime()); + } + } - if (ObjectUtil.isNotNull(liteflowConfig.getWhenMaxWaitSeconds())){ - whenMaxWaitTime = liteflowConfig.getWhenMaxWaitSeconds(); - whenMaxWaitTimeUnit = TimeUnit.SECONDS; - }else{ - whenMaxWaitTime = liteflowConfig.getWhenMaxWaitTime(); - whenMaxWaitTimeUnit = liteflowConfig.getWhenMaxWaitTimeUnit(); + if (ObjectUtil.isNull(this.getMaxWaitTimeUnit())) { + // 获取全局异步线程最⻓的等待时间单位 + this.setMaxWaitTimeUnit(liteflowConfig.getWhenMaxWaitTimeUnit()); } List> completableFutureList = this.getExecutableList() @@ -112,7 +123,7 @@ public class WhenCondition extends Condition { WhenFutureObj.timeOut(executable.getId()), CompletableFuture.supplyAsync(new ParallelSupplier(executable, currChainName, slotIndex), parallelExecutor), - whenMaxWaitTime, whenMaxWaitTimeUnit)) + this.getMaxWaitTime(), this.getMaxWaitTimeUnit())) .collect(Collectors.toList()); CompletableFuture resultCompletableFuture; @@ -221,4 +232,19 @@ public class WhenCondition extends Condition { this.threadExecutorClass = threadExecutorClass; } + public Integer getMaxWaitTime() { + return maxWaitTime; + } + + public void setMaxWaitTime(Integer maxWaitTime) { + this.maxWaitTime = maxWaitTime; + } + + public TimeUnit getMaxWaitTimeUnit() { + return maxWaitTimeUnit; + } + + public void setMaxWaitTimeUnit(TimeUnit maxWaitTimeUnit) { + this.maxWaitTimeUnit = maxWaitTimeUnit; + } } diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/thread/LiteFlowDefaultWhenExecutorBuilder.java b/liteflow-core/src/main/java/com/yomahub/liteflow/thread/LiteFlowDefaultWhenExecutorBuilder.java index 8ae444c05..32242b0c8 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/thread/LiteFlowDefaultWhenExecutorBuilder.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/thread/LiteFlowDefaultWhenExecutorBuilder.java @@ -22,7 +22,7 @@ public class LiteFlowDefaultWhenExecutorBuilder implements ExecutorBuilder { liteflowConfig = new LiteflowConfig(); } return buildDefaultExecutor(liteflowConfig.getWhenMaxWorkers(), liteflowConfig.getWhenMaxWorkers(), - liteflowConfig.getWhenQueueLimit(), "lf-when-thead-"); + liteflowConfig.getWhenQueueLimit(), "when-thread-"); } } diff --git a/liteflow-testcase-el/liteflow-testcase-el-declare-multi-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/MaxWaitSecondsELDeclMultiSpringbootTest.java b/liteflow-testcase-el/liteflow-testcase-el-declare-multi-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/MaxWaitSecondsELDeclMultiSpringbootTest.java new file mode 100644 index 000000000..cbcdf5f71 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-declare-multi-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/MaxWaitSecondsELDeclMultiSpringbootTest.java @@ -0,0 +1,179 @@ +package com.yomahub.liteflow.test.maxWaitSeconds; + +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 环境下超时控制测试 + * + * @author DaleLee + * @since 2.11.0 + */ +@ExtendWith(SpringExtension.class) +@TestPropertySource(value = "classpath:/maxWaitSeconds/application.properties") +@SpringBootTest(classes = MaxWaitSecondsELDeclMultiSpringbootTest.class) +@EnableAutoConfiguration +@ComponentScan({"com.yomahub.liteflow.test.maxWaitSeconds.cmp"}) +public class MaxWaitSecondsELDeclMultiSpringbootTest 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)); + } + + // 测试 maxWaitSeconds 关键字不能作用于 Finally + @Test + public void testFinally2() { + Assertions.assertFalse(LiteFlowChainELBuilder.validate("THEN(a, b, FINALLY(c).maxWaitSeconds(10))")); + } + + // 测试 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/maxWaitSeconds/cmp/CmpConfig.java b/liteflow-testcase-el/liteflow-testcase-el-declare-multi-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/CmpConfig.java new file mode 100644 index 000000000..db6967be4 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-declare-multi-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/CmpConfig.java @@ -0,0 +1,98 @@ +package com.yomahub.liteflow.test.maxWaitSeconds.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(1000); + } catch (Exception e) { + e.printStackTrace(); + } + + System.out.println("ACmp executed!"); + } + + @LiteflowMethod(value = LiteFlowMethodEnum.PROCESS, nodeId = "b") + public void processB(NodeComponent bindCmp) { + try { + Thread.sleep(2000); + } catch (Exception e) { + e.printStackTrace(); + } + + System.out.println("BCmp executed!"); + } + + @LiteflowMethod(value = LiteFlowMethodEnum.PROCESS, nodeId = "c") + public void process(NodeComponent bindCmp) { + try { + Thread.sleep(5000); + } 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/maxWaitSeconds/application.properties b/liteflow-testcase-el/liteflow-testcase-el-declare-multi-springboot/src/test/resources/maxWaitSeconds/application.properties new file mode 100644 index 000000000..4dae0deff --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-declare-multi-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-multi-springboot/src/test/resources/maxWaitSeconds/flow.el.xml b/liteflow-testcase-el/liteflow-testcase-el-declare-multi-springboot/src/test/resources/maxWaitSeconds/flow.el.xml new file mode 100644 index 000000000..91869ae50 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-declare-multi-springboot/src/test/resources/maxWaitSeconds/flow.el.xml @@ -0,0 +1,110 @@ + + + + + + + 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); + + + + + THEN(b) + + + + testChain.maxWaitSeconds(1); + + + + testChain.maxWaitSeconds(3); + + \ No newline at end of file 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..0fe2a68fe --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/MaxWaitSecondsELDeclSpringbootTest.java @@ -0,0 +1,202 @@ +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.maxWaitSeconds.cmp.ACmp; +import com.yomahub.liteflow.test.maxWaitSeconds.cmp.BCmp; +import com.yomahub.liteflow.test.maxWaitSeconds.cmp.CCmp; +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.DCmp.CONTENT_KEY; + +/** + * Spring Boot 环境下超时控制测试 + * + * @author DaleLee + * @since 2.11.0 + */ +@ExtendWith(SpringExtension.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() { + 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)); + } + + // 测试 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(); + Assertions.assertFalse(LiteFlowChainELBuilder.validate("THEN(a, b, FINALLY(c).maxWaitSeconds(10))")); + } + + // 测试 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-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..4085405c2 --- /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,31 @@ +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; + +import java.util.HashSet; +import java.util.Set; + +@LiteflowComponent("w") +@LiteflowCmpDefine(NodeTypeEnum.WHILE) +public class WCmp { + private int count = 0; + + // 执行过的 chain + Set executedChain = new HashSet<>(); + + @LiteflowMethod(value = LiteFlowMethodEnum.PROCESS_WHILE, 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; + } +} 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..91869ae50 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-declare-springboot/src/test/resources/maxWaitSeconds/flow.el.xml @@ -0,0 +1,110 @@ + + + + + + + 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); + + + + + THEN(b) + + + + testChain.maxWaitSeconds(1); + + + + testChain.maxWaitSeconds(3); + + \ No newline at end of file diff --git a/liteflow-testcase-el/liteflow-testcase-el-nospring/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/MaxWaitSecondsTest.java b/liteflow-testcase-el/liteflow-testcase-el-nospring/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/MaxWaitSecondsTest.java new file mode 100644 index 000000000..3bf51e5c0 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-nospring/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/MaxWaitSecondsTest.java @@ -0,0 +1,203 @@ +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.core.FlowExecutorHolder; +import com.yomahub.liteflow.enums.NodeTypeEnum; +import com.yomahub.liteflow.exception.WhenTimeoutException; +import com.yomahub.liteflow.flow.LiteflowResponse; +import com.yomahub.liteflow.property.LiteflowConfig; +import com.yomahub.liteflow.slot.DefaultContext; +import com.yomahub.liteflow.test.BaseTest; +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.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + + +import java.util.concurrent.TimeoutException; + +import static com.yomahub.liteflow.test.maxWaitSeconds.cmp.DCmp.CONTENT_KEY; + +/** + * 非 Spring 环境下超时控制测试 + * + * @author DaleLee + * @since 2.11.0 + */ +public class MaxWaitSecondsTest extends BaseTest { + + private static FlowExecutor flowExecutor; + + @BeforeAll + public static void init() { + LiteflowConfig config = new LiteflowConfig(); + config.setRuleSource("maxWaitSeconds/flow.el.xml"); + flowExecutor = FlowExecutorHolder.loadInstance(config); + } + + // 测试 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)); + + } + + // 测试 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(); + Assertions.assertFalse(LiteFlowChainELBuilder.validate("THEN(a, b, FINALLY(c).maxWaitSeconds(10))")); + } + + // 测试 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-nospring/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/ACmp.java b/liteflow-testcase-el/liteflow-testcase-el-nospring/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/ACmp.java new file mode 100644 index 000000000..9973aafbc --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-nospring/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/ACmp.java @@ -0,0 +1,16 @@ +package com.yomahub.liteflow.test.maxWaitSeconds.cmp; + +import com.yomahub.liteflow.core.NodeComponent; + +public class ACmp extends NodeComponent { + @Override + public void process() { + try { + Thread.sleep(1000); + } catch (Exception e) { + e.printStackTrace(); + } + + System.out.println("ACmp executed!"); + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-nospring/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/BCmp.java b/liteflow-testcase-el/liteflow-testcase-el-nospring/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/BCmp.java new file mode 100644 index 000000000..318ee981a --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-nospring/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/BCmp.java @@ -0,0 +1,16 @@ +package com.yomahub.liteflow.test.maxWaitSeconds.cmp; + +import com.yomahub.liteflow.core.NodeComponent; + +public class BCmp extends NodeComponent { + @Override + public void process() { + try { + Thread.sleep(2000); + } catch (Exception e) { + e.printStackTrace(); + } + + System.out.println("BCmp executed!"); + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-nospring/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/CCmp.java b/liteflow-testcase-el/liteflow-testcase-el-nospring/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/CCmp.java new file mode 100644 index 000000000..ae8d7df50 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-nospring/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/CCmp.java @@ -0,0 +1,16 @@ +package com.yomahub.liteflow.test.maxWaitSeconds.cmp; + +import com.yomahub.liteflow.core.NodeComponent; + +public class CCmp extends NodeComponent { + @Override + public void process() { + try { + Thread.sleep(5000); + } catch (Exception e) { + e.printStackTrace(); + } + + System.out.println("CCmp executed!"); + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-nospring/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/DCmp.java b/liteflow-testcase-el/liteflow-testcase-el-nospring/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/DCmp.java new file mode 100644 index 000000000..0a018819a --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-nospring/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/DCmp.java @@ -0,0 +1,23 @@ +package com.yomahub.liteflow.test.maxWaitSeconds.cmp; + +import com.yomahub.liteflow.core.NodeComponent; +import com.yomahub.liteflow.slot.DefaultContext; + + +public class DCmp extends NodeComponent { + + public static final String CONTENT_KEY = "testKey"; + + @Override + public void process() { + try { + Thread.sleep(500); + DefaultContext contextBean = this.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-nospring/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/FCmp.java b/liteflow-testcase-el/liteflow-testcase-el-nospring/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/FCmp.java new file mode 100644 index 000000000..d22ecdf4f --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-nospring/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/FCmp.java @@ -0,0 +1,10 @@ +package com.yomahub.liteflow.test.maxWaitSeconds.cmp; + +import com.yomahub.liteflow.core.NodeIfComponent; + +public class FCmp extends NodeIfComponent { + @Override + public boolean processIf() throws Exception { + return true; + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-nospring/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/SCmp.java b/liteflow-testcase-el/liteflow-testcase-el-nospring/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/SCmp.java new file mode 100644 index 000000000..fef1f06c0 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-nospring/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/SCmp.java @@ -0,0 +1,11 @@ +package com.yomahub.liteflow.test.maxWaitSeconds.cmp; + +import com.yomahub.liteflow.core.NodeSwitchComponent; + +public class SCmp extends NodeSwitchComponent { + + @Override + public String processSwitch() throws Exception { + return "b"; + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-nospring/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/WCmp.java b/liteflow-testcase-el/liteflow-testcase-el-nospring/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/WCmp.java new file mode 100644 index 000000000..ecad318d1 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-nospring/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/WCmp.java @@ -0,0 +1,24 @@ +package com.yomahub.liteflow.test.maxWaitSeconds.cmp; + +import com.yomahub.liteflow.core.NodeWhileComponent; + +import java.util.HashSet; +import java.util.Set; + +public class WCmp extends NodeWhileComponent { + private int count = 0; + + // 执行过的 chain + Set executedChain = new HashSet<>(); + + @Override + public boolean processWhile() throws Exception { + // 判断是否切换了 chain + if (!executedChain.contains(this.getCurrChainId())) { + count = 0; + executedChain.add(this.getCurrChainId()); + } + count++; + return count <= 2; + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-nospring/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/XCmp.java b/liteflow-testcase-el/liteflow-testcase-el-nospring/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/XCmp.java new file mode 100644 index 000000000..0a637bde0 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-nospring/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/XCmp.java @@ -0,0 +1,15 @@ +package com.yomahub.liteflow.test.maxWaitSeconds.cmp; + +import cn.hutool.core.collection.ListUtil; +import com.yomahub.liteflow.core.NodeIteratorComponent; + +import java.util.Iterator; +import java.util.List; + +public class XCmp extends NodeIteratorComponent { + @Override + public Iterator processIterator() throws Exception { + List list = ListUtil.toList("one", "two"); + return list.iterator(); + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-nospring/src/test/resources/maxWaitSeconds/flow.el.xml b/liteflow-testcase-el/liteflow-testcase-el-nospring/src/test/resources/maxWaitSeconds/flow.el.xml new file mode 100644 index 000000000..09c955ca6 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-nospring/src/test/resources/maxWaitSeconds/flow.el.xml @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + 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); + + + + + THEN(b) + + + + testChain.maxWaitSeconds(1); + + + + testChain.maxWaitSeconds(3); + + \ No newline at end of file diff --git a/liteflow-testcase-el/liteflow-testcase-el-solon/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/MaxWaitSecondsSolonTest.java b/liteflow-testcase-el/liteflow-testcase-el-solon/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/MaxWaitSecondsSolonTest.java new file mode 100644 index 000000000..981d6b215 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-solon/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/MaxWaitSecondsSolonTest.java @@ -0,0 +1,198 @@ +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.maxWaitSeconds.cmp.ACmp; +import com.yomahub.liteflow.test.maxWaitSeconds.cmp.BCmp; +import com.yomahub.liteflow.test.maxWaitSeconds.cmp.CCmp; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.noear.solon.annotation.Inject; +import org.noear.solon.test.SolonJUnit5Extension; +import org.noear.solon.test.annotation.TestPropertySource; + +import java.util.concurrent.TimeoutException; + +import static com.yomahub.liteflow.test.maxWaitSeconds.cmp.DCmp.CONTENT_KEY; + +/** + * Solon 环境下超时控制测试 + * + * @author DaleLee + * @since 2.11.0 + */ +@ExtendWith(SolonJUnit5Extension.class) +@TestPropertySource("classpath:/maxWaitSeconds/application.properties") +public class MaxWaitSecondsSolonTest extends BaseTest { + + @Inject + 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)); + + } + + // 测试 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(); + Assertions.assertFalse(LiteFlowChainELBuilder.validate("THEN(a, b, FINALLY(c).maxWaitSeconds(10))")); + } + + // 测试 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-solon/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/ACmp.java b/liteflow-testcase-el/liteflow-testcase-el-solon/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/ACmp.java new file mode 100644 index 000000000..1b76405be --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-solon/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/ACmp.java @@ -0,0 +1,18 @@ +package com.yomahub.liteflow.test.maxWaitSeconds.cmp; + +import com.yomahub.liteflow.annotation.LiteflowComponent; +import com.yomahub.liteflow.core.NodeComponent; + +@LiteflowComponent("a") +public class ACmp extends NodeComponent { + @Override + public void process() { + try { + Thread.sleep(1000); + } catch (Exception e) { + e.printStackTrace(); + } + + System.out.println("ACmp executed!"); + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-solon/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/BCmp.java b/liteflow-testcase-el/liteflow-testcase-el-solon/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/BCmp.java new file mode 100644 index 000000000..08bd6d21c --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-solon/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/BCmp.java @@ -0,0 +1,18 @@ +package com.yomahub.liteflow.test.maxWaitSeconds.cmp; + +import com.yomahub.liteflow.annotation.LiteflowComponent; +import com.yomahub.liteflow.core.NodeComponent; + +@LiteflowComponent("b") +public class BCmp extends NodeComponent { + @Override + public void process() { + try { + Thread.sleep(2000); + } catch (Exception e) { + e.printStackTrace(); + } + + System.out.println("BCmp executed!"); + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-solon/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/CCmp.java b/liteflow-testcase-el/liteflow-testcase-el-solon/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/CCmp.java new file mode 100644 index 000000000..e991188fd --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-solon/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/CCmp.java @@ -0,0 +1,18 @@ +package com.yomahub.liteflow.test.maxWaitSeconds.cmp; + +import com.yomahub.liteflow.annotation.LiteflowComponent; +import com.yomahub.liteflow.core.NodeComponent; + +@LiteflowComponent("c") +public class CCmp extends NodeComponent { + @Override + public void process() { + try { + Thread.sleep(5000); + } catch (Exception e) { + e.printStackTrace(); + } + + System.out.println("CCmp executed!"); + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-solon/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/DCmp.java b/liteflow-testcase-el/liteflow-testcase-el-solon/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/DCmp.java new file mode 100644 index 000000000..c502d7c31 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-solon/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/DCmp.java @@ -0,0 +1,25 @@ +package com.yomahub.liteflow.test.maxWaitSeconds.cmp; + +import com.yomahub.liteflow.annotation.LiteflowComponent; +import com.yomahub.liteflow.core.NodeComponent; +import com.yomahub.liteflow.slot.DefaultContext; + + +@LiteflowComponent("d") +public class DCmp extends NodeComponent { + + public static final String CONTENT_KEY = "testKey"; + + @Override + public void process() { + try { + Thread.sleep(500); + DefaultContext contextBean = this.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-solon/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/FCmp.java b/liteflow-testcase-el/liteflow-testcase-el-solon/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/FCmp.java new file mode 100644 index 000000000..89ef9f92d --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-solon/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/FCmp.java @@ -0,0 +1,12 @@ +package com.yomahub.liteflow.test.maxWaitSeconds.cmp; + +import com.yomahub.liteflow.annotation.LiteflowComponent; +import com.yomahub.liteflow.core.NodeIfComponent; + +@LiteflowComponent("f") +public class FCmp extends NodeIfComponent { + @Override + public boolean processIf() throws Exception { + return true; + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-solon/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/SCmp.java b/liteflow-testcase-el/liteflow-testcase-el-solon/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/SCmp.java new file mode 100644 index 000000000..e0e138ef0 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-solon/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/SCmp.java @@ -0,0 +1,13 @@ +package com.yomahub.liteflow.test.maxWaitSeconds.cmp; + +import com.yomahub.liteflow.annotation.LiteflowComponent; +import com.yomahub.liteflow.core.NodeSwitchComponent; + +@LiteflowComponent("s") +public class SCmp extends NodeSwitchComponent { + + @Override + public String processSwitch() throws Exception { + return "b"; + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-solon/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/WCmp.java b/liteflow-testcase-el/liteflow-testcase-el-solon/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/WCmp.java new file mode 100644 index 000000000..1db12e3de --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-solon/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/WCmp.java @@ -0,0 +1,26 @@ +package com.yomahub.liteflow.test.maxWaitSeconds.cmp; + +import com.yomahub.liteflow.annotation.LiteflowComponent; +import com.yomahub.liteflow.core.NodeWhileComponent; + +import java.util.HashSet; +import java.util.Set; + +@LiteflowComponent("w") +public class WCmp extends NodeWhileComponent { + private int count = 0; + + // 执行过的 chain + Set executedChain = new HashSet<>(); + + @Override + public boolean processWhile() throws Exception { + // 判断是否切换了 chain + if (!executedChain.contains(this.getCurrChainId())) { + count = 0; + executedChain.add(this.getCurrChainId()); + } + count++; + return count <= 2; + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-solon/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/XCmp.java b/liteflow-testcase-el/liteflow-testcase-el-solon/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/XCmp.java new file mode 100644 index 000000000..c8bf8b6df --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-solon/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/XCmp.java @@ -0,0 +1,17 @@ +package com.yomahub.liteflow.test.maxWaitSeconds.cmp; + +import cn.hutool.core.collection.ListUtil; +import com.yomahub.liteflow.annotation.LiteflowComponent; +import com.yomahub.liteflow.core.NodeIteratorComponent; + +import java.util.Iterator; +import java.util.List; + +@LiteflowComponent("x") +public class XCmp extends NodeIteratorComponent { + @Override + public Iterator processIterator() throws Exception { + List list = ListUtil.toList("one", "two"); + return list.iterator(); + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-solon/src/test/resources/maxWaitSeconds/application.properties b/liteflow-testcase-el/liteflow-testcase-el-solon/src/test/resources/maxWaitSeconds/application.properties new file mode 100644 index 000000000..4dae0deff --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-solon/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-solon/src/test/resources/maxWaitSeconds/flow.el.xml b/liteflow-testcase-el/liteflow-testcase-el-solon/src/test/resources/maxWaitSeconds/flow.el.xml new file mode 100644 index 000000000..91869ae50 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-solon/src/test/resources/maxWaitSeconds/flow.el.xml @@ -0,0 +1,110 @@ + + + + + + + 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); + + + + + THEN(b) + + + + testChain.maxWaitSeconds(1); + + + + testChain.maxWaitSeconds(3); + + \ No newline at end of file diff --git a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/MaxWaitSecondsELSpringbootTest.java b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/MaxWaitSecondsELSpringbootTest.java new file mode 100644 index 000000000..a71ce42e4 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/MaxWaitSecondsELSpringbootTest.java @@ -0,0 +1,201 @@ +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.validateRule.cmp.ACmp; +import com.yomahub.liteflow.test.validateRule.cmp.BCmp; +import com.yomahub.liteflow.test.validateRule.cmp.CCmp; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +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 javax.annotation.Resource; +import java.util.concurrent.TimeoutException; + +import static com.yomahub.liteflow.test.maxWaitSeconds.cmp.DCmp.CONTENT_KEY; + +/** + * Spring Boot 环境下超时控制测试 + * + * @author DaleLee + * @since 2.11.0 + */ +@TestPropertySource(value = "classpath:/maxWaitSeconds/application.properties") +@SpringBootTest(classes = MaxWaitSecondsELSpringbootTest.class) +@EnableAutoConfiguration +@ComponentScan({"com.yomahub.liteflow.test.maxWaitSeconds.cmp"}) +public class MaxWaitSecondsELSpringbootTest 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() { + // WHEN 抛出的是 WhenTimeoutException + 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)); + } + + // 测试 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(); + Assertions.assertFalse(LiteFlowChainELBuilder.validate("THEN(a, b, FINALLY(c).maxWaitSeconds(10))")); + } + + // 测试 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-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/ACmp.java b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/ACmp.java new file mode 100644 index 000000000..28a669d83 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/ACmp.java @@ -0,0 +1,20 @@ +package com.yomahub.liteflow.test.maxWaitSeconds.cmp; + +import com.yomahub.liteflow.annotation.LiteflowComponent; +import com.yomahub.liteflow.core.NodeComponent; + +import java.util.Random; + +@LiteflowComponent("a") +public class ACmp extends NodeComponent { + @Override + public void process() { + try { + Thread.sleep(1000); + } catch (Exception e) { + e.printStackTrace(); + } + + System.out.println("ACmp executed!"); + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/BCmp.java b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/BCmp.java new file mode 100644 index 000000000..08bd6d21c --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/BCmp.java @@ -0,0 +1,18 @@ +package com.yomahub.liteflow.test.maxWaitSeconds.cmp; + +import com.yomahub.liteflow.annotation.LiteflowComponent; +import com.yomahub.liteflow.core.NodeComponent; + +@LiteflowComponent("b") +public class BCmp extends NodeComponent { + @Override + public void process() { + try { + Thread.sleep(2000); + } catch (Exception e) { + e.printStackTrace(); + } + + System.out.println("BCmp executed!"); + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/CCmp.java b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/CCmp.java new file mode 100644 index 000000000..e991188fd --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/CCmp.java @@ -0,0 +1,18 @@ +package com.yomahub.liteflow.test.maxWaitSeconds.cmp; + +import com.yomahub.liteflow.annotation.LiteflowComponent; +import com.yomahub.liteflow.core.NodeComponent; + +@LiteflowComponent("c") +public class CCmp extends NodeComponent { + @Override + public void process() { + try { + Thread.sleep(5000); + } catch (Exception e) { + e.printStackTrace(); + } + + System.out.println("CCmp executed!"); + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/DCmp.java b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/DCmp.java new file mode 100644 index 000000000..648c2f447 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/DCmp.java @@ -0,0 +1,24 @@ +package com.yomahub.liteflow.test.maxWaitSeconds.cmp; + +import com.yomahub.liteflow.annotation.LiteflowComponent; +import com.yomahub.liteflow.core.NodeComponent; +import com.yomahub.liteflow.slot.DefaultContext; + +@LiteflowComponent("d") +public class DCmp extends NodeComponent { + + public static final String CONTENT_KEY = "testKey"; + + @Override + public void process() { + try { + Thread.sleep(500); + DefaultContext contextBean = this.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-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/FCmp.java b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/FCmp.java new file mode 100644 index 000000000..89ef9f92d --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/FCmp.java @@ -0,0 +1,12 @@ +package com.yomahub.liteflow.test.maxWaitSeconds.cmp; + +import com.yomahub.liteflow.annotation.LiteflowComponent; +import com.yomahub.liteflow.core.NodeIfComponent; + +@LiteflowComponent("f") +public class FCmp extends NodeIfComponent { + @Override + public boolean processIf() throws Exception { + return true; + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/SCmp.java b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/SCmp.java new file mode 100644 index 000000000..e0e138ef0 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/SCmp.java @@ -0,0 +1,13 @@ +package com.yomahub.liteflow.test.maxWaitSeconds.cmp; + +import com.yomahub.liteflow.annotation.LiteflowComponent; +import com.yomahub.liteflow.core.NodeSwitchComponent; + +@LiteflowComponent("s") +public class SCmp extends NodeSwitchComponent { + + @Override + public String processSwitch() throws Exception { + return "b"; + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/WCmp.java b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/WCmp.java new file mode 100644 index 000000000..1db12e3de --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/WCmp.java @@ -0,0 +1,26 @@ +package com.yomahub.liteflow.test.maxWaitSeconds.cmp; + +import com.yomahub.liteflow.annotation.LiteflowComponent; +import com.yomahub.liteflow.core.NodeWhileComponent; + +import java.util.HashSet; +import java.util.Set; + +@LiteflowComponent("w") +public class WCmp extends NodeWhileComponent { + private int count = 0; + + // 执行过的 chain + Set executedChain = new HashSet<>(); + + @Override + public boolean processWhile() throws Exception { + // 判断是否切换了 chain + if (!executedChain.contains(this.getCurrChainId())) { + count = 0; + executedChain.add(this.getCurrChainId()); + } + count++; + return count <= 2; + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/XCmp.java b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/XCmp.java new file mode 100644 index 000000000..c8bf8b6df --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/XCmp.java @@ -0,0 +1,17 @@ +package com.yomahub.liteflow.test.maxWaitSeconds.cmp; + +import cn.hutool.core.collection.ListUtil; +import com.yomahub.liteflow.annotation.LiteflowComponent; +import com.yomahub.liteflow.core.NodeIteratorComponent; + +import java.util.Iterator; +import java.util.List; + +@LiteflowComponent("x") +public class XCmp extends NodeIteratorComponent { + @Override + public Iterator processIterator() throws Exception { + List list = ListUtil.toList("one", "two"); + return list.iterator(); + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/maxWaitSeconds/application.properties b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/maxWaitSeconds/application.properties new file mode 100644 index 000000000..4dae0deff --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-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-springboot/src/test/resources/maxWaitSeconds/flow.el.xml b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/maxWaitSeconds/flow.el.xml new file mode 100644 index 000000000..91869ae50 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/maxWaitSeconds/flow.el.xml @@ -0,0 +1,110 @@ + + + + + + + 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); + + + + + THEN(b) + + + + testChain.maxWaitSeconds(1); + + + + testChain.maxWaitSeconds(3); + + \ No newline at end of file diff --git a/liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/MaxWaitSecondsELSpringTest.java b/liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/MaxWaitSecondsELSpringTest.java new file mode 100644 index 000000000..f4aceb423 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/MaxWaitSecondsELSpringTest.java @@ -0,0 +1,196 @@ +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.maxWaitSeconds.cmp.ACmp; +import com.yomahub.liteflow.test.maxWaitSeconds.cmp.BCmp; +import com.yomahub.liteflow.test.maxWaitSeconds.cmp.CCmp; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.test.context.ContextConfiguration; +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.DCmp.CONTENT_KEY; + +/** + * Spring 环境下超时控制测试 + * + * @author DaleLee + * @since 2.11.0 + */ +@ExtendWith(SpringExtension.class) +@ContextConfiguration("classpath:/maxWaitSeconds/application.xml") +public class MaxWaitSecondsELSpringTest 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)); + } + + // 测试 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(); + Assertions.assertFalse(LiteFlowChainELBuilder.validate("THEN(a, b, FINALLY(c).maxWaitSeconds(10))")); + } + + // 测试 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-springnative/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/ACmp.java b/liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/ACmp.java new file mode 100644 index 000000000..1b76405be --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/ACmp.java @@ -0,0 +1,18 @@ +package com.yomahub.liteflow.test.maxWaitSeconds.cmp; + +import com.yomahub.liteflow.annotation.LiteflowComponent; +import com.yomahub.liteflow.core.NodeComponent; + +@LiteflowComponent("a") +public class ACmp extends NodeComponent { + @Override + public void process() { + try { + Thread.sleep(1000); + } catch (Exception e) { + e.printStackTrace(); + } + + System.out.println("ACmp executed!"); + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/BCmp.java b/liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/BCmp.java new file mode 100644 index 000000000..08bd6d21c --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/BCmp.java @@ -0,0 +1,18 @@ +package com.yomahub.liteflow.test.maxWaitSeconds.cmp; + +import com.yomahub.liteflow.annotation.LiteflowComponent; +import com.yomahub.liteflow.core.NodeComponent; + +@LiteflowComponent("b") +public class BCmp extends NodeComponent { + @Override + public void process() { + try { + Thread.sleep(2000); + } catch (Exception e) { + e.printStackTrace(); + } + + System.out.println("BCmp executed!"); + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/CCmp.java b/liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/CCmp.java new file mode 100644 index 000000000..e991188fd --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/CCmp.java @@ -0,0 +1,18 @@ +package com.yomahub.liteflow.test.maxWaitSeconds.cmp; + +import com.yomahub.liteflow.annotation.LiteflowComponent; +import com.yomahub.liteflow.core.NodeComponent; + +@LiteflowComponent("c") +public class CCmp extends NodeComponent { + @Override + public void process() { + try { + Thread.sleep(5000); + } catch (Exception e) { + e.printStackTrace(); + } + + System.out.println("CCmp executed!"); + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/DCmp.java b/liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/DCmp.java new file mode 100644 index 000000000..c502d7c31 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/DCmp.java @@ -0,0 +1,25 @@ +package com.yomahub.liteflow.test.maxWaitSeconds.cmp; + +import com.yomahub.liteflow.annotation.LiteflowComponent; +import com.yomahub.liteflow.core.NodeComponent; +import com.yomahub.liteflow.slot.DefaultContext; + + +@LiteflowComponent("d") +public class DCmp extends NodeComponent { + + public static final String CONTENT_KEY = "testKey"; + + @Override + public void process() { + try { + Thread.sleep(500); + DefaultContext contextBean = this.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-springnative/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/FCmp.java b/liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/FCmp.java new file mode 100644 index 000000000..89ef9f92d --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/FCmp.java @@ -0,0 +1,12 @@ +package com.yomahub.liteflow.test.maxWaitSeconds.cmp; + +import com.yomahub.liteflow.annotation.LiteflowComponent; +import com.yomahub.liteflow.core.NodeIfComponent; + +@LiteflowComponent("f") +public class FCmp extends NodeIfComponent { + @Override + public boolean processIf() throws Exception { + return true; + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/SCmp.java b/liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/SCmp.java new file mode 100644 index 000000000..e0e138ef0 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/SCmp.java @@ -0,0 +1,13 @@ +package com.yomahub.liteflow.test.maxWaitSeconds.cmp; + +import com.yomahub.liteflow.annotation.LiteflowComponent; +import com.yomahub.liteflow.core.NodeSwitchComponent; + +@LiteflowComponent("s") +public class SCmp extends NodeSwitchComponent { + + @Override + public String processSwitch() throws Exception { + return "b"; + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/WCmp.java b/liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/WCmp.java new file mode 100644 index 000000000..1db12e3de --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/WCmp.java @@ -0,0 +1,26 @@ +package com.yomahub.liteflow.test.maxWaitSeconds.cmp; + +import com.yomahub.liteflow.annotation.LiteflowComponent; +import com.yomahub.liteflow.core.NodeWhileComponent; + +import java.util.HashSet; +import java.util.Set; + +@LiteflowComponent("w") +public class WCmp extends NodeWhileComponent { + private int count = 0; + + // 执行过的 chain + Set executedChain = new HashSet<>(); + + @Override + public boolean processWhile() throws Exception { + // 判断是否切换了 chain + if (!executedChain.contains(this.getCurrChainId())) { + count = 0; + executedChain.add(this.getCurrChainId()); + } + count++; + return count <= 2; + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/XCmp.java b/liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/XCmp.java new file mode 100644 index 000000000..c8bf8b6df --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/java/com/yomahub/liteflow/test/maxWaitSeconds/cmp/XCmp.java @@ -0,0 +1,17 @@ +package com.yomahub.liteflow.test.maxWaitSeconds.cmp; + +import cn.hutool.core.collection.ListUtil; +import com.yomahub.liteflow.annotation.LiteflowComponent; +import com.yomahub.liteflow.core.NodeIteratorComponent; + +import java.util.Iterator; +import java.util.List; + +@LiteflowComponent("x") +public class XCmp extends NodeIteratorComponent { + @Override + public Iterator processIterator() throws Exception { + List list = ListUtil.toList("one", "two"); + return list.iterator(); + } +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/resources/maxWaitSeconds/application.xml b/liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/resources/maxWaitSeconds/application.xml new file mode 100644 index 000000000..3d968adea --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/resources/maxWaitSeconds/application.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/resources/maxWaitSeconds/flow.el.xml b/liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/resources/maxWaitSeconds/flow.el.xml new file mode 100644 index 000000000..91869ae50 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-springnative/src/test/resources/maxWaitSeconds/flow.el.xml @@ -0,0 +1,110 @@ + + + + + + + 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); + + + + + THEN(b) + + + + testChain.maxWaitSeconds(1); + + + + testChain.maxWaitSeconds(3); + + \ No newline at end of file