From 9dd4b8898f4411d71afdde7e3f8a3cc4684cfc65 Mon Sep 17 00:00:00 2001 From: zy <953725892@qq.com> Date: Fri, 14 Jul 2023 02:10:26 +0800 Subject: [PATCH] =?UTF-8?q?feature=20#I7HJFX=20=E5=AE=8C=E6=88=90springboo?= =?UTF-8?q?t=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B=E7=BC=96=E5=86=99?= =?UTF-8?q?=E5=8F=8A=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...ParallelLoopELDeclMultiSpringbootTest.java | 119 ++++++++++++++++++ .../ParallelLoopELSpringbootTest.java} | 5 +- .../parallelLoop/application.properties | 4 + .../src/test/resources/parallelLoop/flow.xml | 46 +++++++ 4 files changed, 171 insertions(+), 3 deletions(-) create mode 100644 liteflow-testcase-el/liteflow-testcase-el-declare-multi-springboot/src/test/java/com/yomahub/liteflow/test/parallelLoop/ParallelLoopELDeclMultiSpringbootTest.java rename liteflow-testcase-el/{liteflow-testcase-el-declare-multi-springboot/src/test/java/com/yomahub/liteflow/test/parallelLoop/ParallelLoopELDeclSpringbootTest.java => liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/parallelLoop/ParallelLoopELSpringbootTest.java} (97%) create mode 100644 liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/parallelLoop/application.properties create mode 100644 liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/parallelLoop/flow.xml diff --git a/liteflow-testcase-el/liteflow-testcase-el-declare-multi-springboot/src/test/java/com/yomahub/liteflow/test/parallelLoop/ParallelLoopELDeclMultiSpringbootTest.java b/liteflow-testcase-el/liteflow-testcase-el-declare-multi-springboot/src/test/java/com/yomahub/liteflow/test/parallelLoop/ParallelLoopELDeclMultiSpringbootTest.java new file mode 100644 index 00000000..ffa3b745 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-declare-multi-springboot/src/test/java/com/yomahub/liteflow/test/parallelLoop/ParallelLoopELDeclMultiSpringbootTest.java @@ -0,0 +1,119 @@ +package com.yomahub.liteflow.test.parallelLoop; + +import cn.hutool.core.collection.ListUtil; +import com.yomahub.liteflow.core.FlowExecutor; +import com.yomahub.liteflow.exception.LiteFlowException; +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.List; +import java.util.regex.Pattern; + +/** + * springboot环境EL异步循环测试 + * + * @author zhhhhy + * @since 2.11.0 + */ +@ExtendWith(SpringExtension.class) +@TestPropertySource(value = "classpath:/parallelLoop/application.properties") +@SpringBootTest(classes = ParallelLoopELDeclMultiSpringbootTest.class) +@EnableAutoConfiguration +@ComponentScan({ "com.yomahub.liteflow.test.parallelLoop.cmp" }) +public class ParallelLoopELDeclMultiSpringbootTest extends BaseTest { + + @Resource + private FlowExecutor flowExecutor; + + //测试并行FOR循环,循环次数直接在el中定义 + @Test + public void testParallelLoop1() throws Exception { + LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg"); + Assertions.assertTrue(response.isSuccess()); + } + + //测试并行FOR循环,循环次数由For组件定义 + @Test + public void testParallelLoop2() throws Exception { + LiteflowResponse response = flowExecutor.execute2Resp("chain2", "arg"); + Assertions.assertTrue(response.isSuccess()); + } + + //测试并行FOR循环中的BREAK组件能够正常发挥作用 + @Test + public void testParallelLoop3() throws Exception { + LiteflowResponse response = flowExecutor.execute2Resp("chain3", "arg"); + Assertions.assertTrue(response.isSuccess()); + } + + //测试并行FOR循环中,主线程是否会正常等待所有并行子项完成后再继续执行 + @Test + public void testParallelLoop4() throws Exception { + LiteflowResponse response = flowExecutor.execute2Resp("chain4", "arg"); + Assertions.assertTrue(response.isSuccess()); + } + + @Test + //测试并行FOR循环中,某个并行子项抛出异常 + public void testParallelLoop5() throws Exception { + LiteflowResponse response = flowExecutor.execute2Resp("chain5", "arg"); + Assertions.assertFalse(response.isSuccess()); + Assertions.assertEquals("300", response.getCode()); + Assertions.assertNotNull(response.getCause()); + Assertions.assertTrue(response.getCause() instanceof LiteFlowException); + Assertions.assertNotNull(response.getSlot()); + } + + //并行的条件循环 + @Test + public void testParallelLoop6() throws Exception { + LiteflowResponse response = flowExecutor.execute2Resp("chain6", "arg"); + Assertions.assertTrue(response.isSuccess()); + } + + //并行的迭代循环 + @Test + public void testParallelLoop7() throws Exception { + List list = ListUtil.toList("1", "2", "3", "4", "5"); + LiteflowResponse response = flowExecutor.execute2Resp("chain7", list); + Assertions.assertTrue(response.isSuccess()); + } + + //测试并行FOR循环中的index + @Test + public void testParallelLoop8() throws Exception { + LiteflowResponse response = flowExecutor.execute2Resp("chain8", "arg"); + DefaultContext context = response.getFirstContextBean(); + Assertions.assertTrue(response.isSuccess()); + String regex = "(?!.*(.).*\\1)[0-4]{5}"; //匹配不包含重复数字的0-4的5位数字 + Pattern pattern = Pattern.compile(regex); + //e1,e2,e3分别并行执行5次,因此单个循环的顺序可以是任意的 + Assertions.assertTrue(pattern.matcher(context.getData("loop_e1")).matches()); + Assertions.assertTrue(pattern.matcher(context.getData("loop_e2")).matches()); + Assertions.assertTrue(pattern.matcher(context.getData("loop_e3")).matches()); + } + + + //测试自定义线程池配置是否生效 + @Test + public void testParallelLoop9() throws Exception { + LiteflowResponse response = flowExecutor.execute2Resp("chain9", "arg"); + DefaultContext context = response.getFirstContextBean(); + Assertions.assertTrue(response.isSuccess()); + Assertions.assertTrue(context.getData("threadName").toString().startsWith("customer-loop-thead")); + } + + + +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-declare-multi-springboot/src/test/java/com/yomahub/liteflow/test/parallelLoop/ParallelLoopELDeclSpringbootTest.java b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/parallelLoop/ParallelLoopELSpringbootTest.java similarity index 97% rename from liteflow-testcase-el/liteflow-testcase-el-declare-multi-springboot/src/test/java/com/yomahub/liteflow/test/parallelLoop/ParallelLoopELDeclSpringbootTest.java rename to liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/parallelLoop/ParallelLoopELSpringbootTest.java index e10d003e..9332d19e 100644 --- a/liteflow-testcase-el/liteflow-testcase-el-declare-multi-springboot/src/test/java/com/yomahub/liteflow/test/parallelLoop/ParallelLoopELDeclSpringbootTest.java +++ b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/parallelLoop/ParallelLoopELSpringbootTest.java @@ -15,7 +15,6 @@ import org.springframework.context.annotation.ComponentScan; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit.jupiter.SpringExtension; - import javax.annotation.Resource; import java.util.List; import java.util.regex.Pattern; @@ -28,10 +27,10 @@ import java.util.regex.Pattern; */ @ExtendWith(SpringExtension.class) @TestPropertySource(value = "classpath:/parallelLoop/application.properties") -@SpringBootTest(classes = ParallelLoopELDeclSpringbootTest.class) +@SpringBootTest(classes = ParallelLoopELSpringbootTest.class) @EnableAutoConfiguration @ComponentScan({ "com.yomahub.liteflow.test.parallelLoop.cmp" }) -public class ParallelLoopELDeclSpringbootTest extends BaseTest { +public class ParallelLoopELSpringbootTest extends BaseTest { @Resource private FlowExecutor flowExecutor; diff --git a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/parallelLoop/application.properties b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/parallelLoop/application.properties new file mode 100644 index 00000000..ebf6a6ed --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/parallelLoop/application.properties @@ -0,0 +1,4 @@ +liteflow.rule-source=parallelLoop/flow.xml +liteflow.parallel-max-workers = 10 +liteflow.parallel-queue-limit = 1024 +liteflow.parallel-loop-executor-class =com.yomahub.liteflow.test.parallelLoop.CustomThreadExecutor \ No newline at end of file diff --git a/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/parallelLoop/flow.xml b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/parallelLoop/flow.xml new file mode 100644 index 00000000..c68bbd7c --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/parallelLoop/flow.xml @@ -0,0 +1,46 @@ + + + + FOR(2).parallel(true).DO(THEN(a,b,c)); + + + + FOR(x).parallel(true).DO(THEN(a,b,c)); + + + + FOR(100).parallel(true).DO(THEN(a,b,d)).BREAK(y); + + + + FOR(x).parallel(true).DO(THEN(a,b,f)); + + + + FOR(x).parallel(true).DO(THEN(a,b,g)); + + + + WHILE(z).parallel(true).DO(THEN(a,d)); + + + + ITERATOR(it).parallel(true).DO(THEN(a,b)); + + + + FOR(5).parallel(true).DO( + WHEN( + THEN(a,e.tag("e1")), + THEN(c,e.tag("e2")), + THEN(b,e.tag("e3")) + ) + ); + + + + + FOR(x).parallel(true).DO(THEN(a,b,h)); + + + \ No newline at end of file