feature #I7YYLE 完善测试用例

This commit is contained in:
Dale Lee
2023-10-06 21:01:52 +08:00
parent 63b1e2744a
commit c422761aef
74 changed files with 2395 additions and 14 deletions

View File

@@ -0,0 +1,221 @@
package com.yomahub.liteflow.test.fallback;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.flow.LiteflowResponse;
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.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import javax.annotation.Resource;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
/**
* Spring 降级组件测试
*
* @author DaleLee
*/
@ExtendWith(SpringExtension.class)
@ContextConfiguration("classpath:/fallback/application.xml")
public class FallbackELSpringTest extends BaseTest {
@Resource
private FlowExecutor flowExecutor;
@Test
public void testThen1() {
LiteflowResponse response = flowExecutor.execute2Resp("then1", "arg");
Assertions.assertTrue(response.isSuccess());
Assertions.assertEquals("a==>c", response.getExecuteStepStrWithoutTime());
}
@Test
public void testThen2() {
LiteflowResponse response = flowExecutor.execute2Resp("then2", "arg");
Assertions.assertTrue(response.isSuccess());
Assertions.assertEquals("c==>c==>c", response.getExecuteStepStrWithoutTime());
}
@Test
public void testWhen1() {
LiteflowResponse response = flowExecutor.execute2Resp("when1", "arg");
Assertions.assertTrue(response.isSuccess());
String executeStepStr = response.getExecuteStepStrWithoutTime();
Assertions.assertTrue("b==>c".equals(executeStepStr) || "c==>b".equals(executeStepStr));
}
@Test
public void testIf1() {
LiteflowResponse response = flowExecutor.execute2Resp("if1", "arg");
Assertions.assertTrue(response.isSuccess());
Assertions.assertEquals("ifn2", response.getExecuteStepStrWithoutTime());
}
@Test
public void testIf2() {
LiteflowResponse response = flowExecutor.execute2Resp("if2", "arg");
Assertions.assertTrue(response.isSuccess());
Assertions.assertEquals("ifn1==>c", response.getExecuteStepStrWithoutTime());
}
@Test
public void testFor1() {
LiteflowResponse response = flowExecutor.execute2Resp("for1", "arg");
Assertions.assertTrue(response.isSuccess());
Assertions.assertEquals("for1==>a==>a==>a", response.getExecuteStepStrWithoutTime());
}
@Test
public void testFor2() {
LiteflowResponse response = flowExecutor.execute2Resp("for2", "arg");
Assertions.assertTrue(response.isSuccess());
Assertions.assertEquals("LOOP_3==>c==>c==>c", response.getExecuteStepStrWithoutTime());
}
@Test
public void testWhile1() {
LiteflowResponse response = flowExecutor.execute2Resp("while1", "arg");
Assertions.assertTrue(response.isSuccess());
Assertions.assertEquals("wn2", response.getExecuteStepStrWithoutTime());
}
@Test
public void testWhile2() {
LiteflowResponse response = flowExecutor.execute2Resp("while2", "arg");
Assertions.assertTrue(response.isSuccess());
Assertions.assertEquals("wn1==>c==>wn1==>c==>wn1==>c==>wn1", response.getExecuteStepStrWithoutTime());
}
@Test
public void testIterator1() {
LiteflowResponse response = flowExecutor.execute2Resp("iterator1", "arg");
Assertions.assertTrue(response.isSuccess());
Assertions.assertEquals("itn2", response.getExecuteStepStrWithoutTime());
}
@Test
public void testIterator2() {
LiteflowResponse response = flowExecutor.execute2Resp("iterator2", "arg");
Assertions.assertTrue(response.isSuccess());
Assertions.assertEquals("itn1==>c==>c==>c", response.getExecuteStepStrWithoutTime());
}
@Test
public void testBreak1() {
LiteflowResponse response = flowExecutor.execute2Resp("break1", "arg");
Assertions.assertTrue(response.isSuccess());
Assertions.assertEquals("LOOP_3==>a==>bn1", response.getExecuteStepStrWithoutTime());
}
@Test
public void testBreak2() {
LiteflowResponse response = flowExecutor.execute2Resp("break2", "arg");
Assertions.assertTrue(response.isSuccess());
Assertions.assertEquals("wn1==>a==>bn1", response.getExecuteStepStrWithoutTime());
}
@Test
public void testBreak3() {
LiteflowResponse response = flowExecutor.execute2Resp("break3", "arg");
Assertions.assertTrue(response.isSuccess());
Assertions.assertEquals("itn1==>a==>bn1", response.getExecuteStepStrWithoutTime());
}
@Test
public void testSwitch1() {
LiteflowResponse response = flowExecutor.execute2Resp("switch1", "arg");
Assertions.assertTrue(response.isSuccess());
Assertions.assertEquals("swn2==>b", response.getExecuteStepStrWithoutTime());
}
@Test
public void testSwitch2() {
LiteflowResponse response = flowExecutor.execute2Resp("switch2", "arg");
Assertions.assertTrue(response.isSuccess());
Assertions.assertEquals("swn1==>a", response.getExecuteStepStrWithoutTime());
}
@Test
public void testAnd1() {
LiteflowResponse response = flowExecutor.execute2Resp("and1", "arg");
Assertions.assertTrue(response.isSuccess());
Assertions.assertEquals("ifn2", response.getExecuteStepStrWithoutTime());
}
@Test
public void testOr1() {
LiteflowResponse response = flowExecutor.execute2Resp("or1", "arg");
Assertions.assertTrue(response.isSuccess());
Assertions.assertEquals("ifn2==>ifn1==>a", response.getExecuteStepStrWithoutTime());
}
@Test
public void testNot1() {
LiteflowResponse response = flowExecutor.execute2Resp("not1", "arg");
Assertions.assertTrue(response.isSuccess());
Assertions.assertEquals("ifn2==>a", response.getExecuteStepStrWithoutTime());
}
@Test
public void testCatch1() {
LiteflowResponse response = flowExecutor.execute2Resp("catch1", "arg");
Assertions.assertTrue(response.isSuccess());
Assertions.assertEquals("a==>d==>c", response.getExecuteStepStrWithoutTime());
}
@Test
public void testMulti1() {
LiteflowResponse response = flowExecutor.execute2Resp("multi1", "arg");
Assertions.assertTrue(response.isSuccess());
Assertions.assertEquals("a==>c==>ifn2", response.getExecuteStepStrWithoutTime());
}
@Test
public void testMulti2() {
LiteflowResponse response = flowExecutor.execute2Resp("multi2", "arg");
Assertions.assertTrue(response.isSuccess());
Assertions.assertEquals("ifn2==>ifn1==>a==>c", response.getExecuteStepStrWithoutTime());
}
@Test
public void testMulti3() {
LiteflowResponse response = flowExecutor.execute2Resp("multi3", "arg");
Assertions.assertTrue(response.isSuccess());
Assertions.assertEquals("for1==>b==>c==>b==>c==>b==>c", response.getExecuteStepStrWithoutTime());
}
@Test
public void testConcurrent1() {
LiteflowResponse response = flowExecutor.execute2Resp("concurrent1", "arg");
Assertions.assertTrue(response.isSuccess());
String stepStr = response.getExecuteStepStrWithoutTime();
Assertions.assertTrue("c==>ifn2".equals(stepStr) || "ifn2==>c".equals(stepStr));
}
@Test
public void testConcurrent2() {
LiteflowResponse response = flowExecutor.execute2Resp("concurrent2", "arg");
Assertions.assertTrue(response.isSuccess());
String stepStr = response.getExecuteStepStrWithoutTime();
Assertions.assertTrue("c==>ifn2".equals(stepStr) || "ifn2==>c".equals(stepStr));
}
@Test
public void testConcurrent3() throws ExecutionException, InterruptedException {
// 执行多条 chain
Future<LiteflowResponse> future1 = flowExecutor.execute2Future("concurrent1", "arg", new Object());
Future<LiteflowResponse> future2 = flowExecutor.execute2Future("concurrent2", "arg", new Object());
Thread.sleep(1000);
LiteflowResponse response1 = future1.get();
LiteflowResponse response2 = future2.get();
Assertions.assertTrue(response1.isSuccess());
String stepStr1 = response1.getExecuteStepStrWithoutTime();
Assertions.assertTrue("c==>ifn2".equals(stepStr1) || "ifn2==>c".equals(stepStr1));
Assertions.assertTrue(response2.isSuccess());
String stepStr2 = response2.getExecuteStepStrWithoutTime();
Assertions.assertTrue("c==>ifn2".equals(stepStr2) || "ifn2==>c".equals(stepStr2));
}
}

View File

@@ -0,0 +1,14 @@
package com.yomahub.liteflow.test.fallback.cmp;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
@LiteflowComponent("a")
public class ACmp extends NodeComponent {
@Override
public void process() {
System.out.println("ACmp executed!");
}
}

View File

@@ -0,0 +1,14 @@
package com.yomahub.liteflow.test.fallback.cmp;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
@LiteflowComponent("b")
public class BCmp extends NodeComponent {
@Override
public void process() {
System.out.println("BCmp executed!");
}
}

View File

@@ -0,0 +1,15 @@
package com.yomahub.liteflow.test.fallback.cmp;
import com.yomahub.liteflow.annotation.FallbackCmp;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeBreakComponent;
@LiteflowComponent("bn1")
@FallbackCmp
public class BreakCmp extends NodeBreakComponent {
@Override
public boolean processBreak() throws Exception {
return true;
}
}

View File

@@ -0,0 +1,16 @@
package com.yomahub.liteflow.test.fallback.cmp;
import com.yomahub.liteflow.annotation.FallbackCmp;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
@LiteflowComponent("c")
@FallbackCmp
public class CCmp extends NodeComponent {
@Override
public void process() {
System.out.println("CCmp executed!");
}
}

View File

@@ -0,0 +1,13 @@
package com.yomahub.liteflow.test.fallback.cmp;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
@LiteflowComponent("d")
public class DCmp extends NodeComponent {
@Override
public void process() throws Exception {
throw new RuntimeException("component[d]");
}
}

View File

@@ -0,0 +1,15 @@
package com.yomahub.liteflow.test.fallback.cmp;
import com.yomahub.liteflow.annotation.FallbackCmp;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeForComponent;
@LiteflowComponent("for1")
@FallbackCmp
public class ForCmp extends NodeForComponent {
@Override
public int processFor() throws Exception {
return 3;
}
}

View File

@@ -0,0 +1,13 @@
package com.yomahub.liteflow.test.fallback.cmp;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeIfComponent;
@LiteflowComponent("ifn1")
public class IfCmp1 extends NodeIfComponent {
@Override
public boolean processIf() throws Exception {
return true;
}
}

View File

@@ -0,0 +1,15 @@
package com.yomahub.liteflow.test.fallback.cmp;
import com.yomahub.liteflow.annotation.FallbackCmp;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeIfComponent;
@LiteflowComponent("ifn2")
@FallbackCmp
public class IfCmp2 extends NodeIfComponent {
@Override
public boolean processIf() throws Exception {
return false;
}
}

View File

@@ -0,0 +1,16 @@
package com.yomahub.liteflow.test.fallback.cmp;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeIteratorComponent;
import java.util.Arrays;
import java.util.Iterator;
@LiteflowComponent("itn1")
public class IteratorCmp1 extends NodeIteratorComponent {
@Override
public Iterator<?> processIterator() throws Exception {
return Arrays.asList("a", "b", "c").iterator();
}
}

View File

@@ -0,0 +1,18 @@
package com.yomahub.liteflow.test.fallback.cmp;
import com.yomahub.liteflow.annotation.FallbackCmp;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeIteratorComponent;
import java.util.Collections;
import java.util.Iterator;
@LiteflowComponent("itn2")
@FallbackCmp
public class IteratorCmp2 extends NodeIteratorComponent {
@Override
public Iterator<?> processIterator() throws Exception {
return Collections.emptyIterator();
}
}

View File

@@ -0,0 +1,13 @@
package com.yomahub.liteflow.test.fallback.cmp;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeSwitchComponent;
@LiteflowComponent("swn1")
public class SwitchCmp1 extends NodeSwitchComponent {
@Override
public String processSwitch() throws Exception {
return "a";
}
}

View File

@@ -0,0 +1,15 @@
package com.yomahub.liteflow.test.fallback.cmp;
import com.yomahub.liteflow.annotation.FallbackCmp;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeSwitchComponent;
@LiteflowComponent("swn2")
@FallbackCmp
public class SwitchCmp2 extends NodeSwitchComponent {
@Override
public String processSwitch() throws Exception {
return "b";
}
}

View File

@@ -0,0 +1,26 @@
package com.yomahub.liteflow.test.fallback.cmp;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeWhileComponent;
import java.util.HashSet;
import java.util.Set;
@LiteflowComponent("wn1")
public class WhileCmp1 extends NodeWhileComponent {
private int count = 0;
// 执行过的 chain
Set<String> executedChain = new HashSet<>();
@Override
public boolean processWhile() throws Exception {
// 判断是否切换了 chain
if (!executedChain.contains(this.getCurrChainId())) {
count = 0;
executedChain.add(this.getCurrChainId());
}
count++;
return count <= 3;
}
}

View File

@@ -0,0 +1,15 @@
package com.yomahub.liteflow.test.fallback.cmp;
import com.yomahub.liteflow.annotation.FallbackCmp;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeWhileComponent;
@LiteflowComponent("wn2")
@FallbackCmp
public class WhileCmp2 extends NodeWhileComponent {
@Override
public boolean processWhile() throws Exception {
return false;
}
}

View File

@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="com.yomahub.liteflow.test.fallback.cmp" />
<bean id="springAware" class="com.yomahub.liteflow.spi.spring.SpringAware"/>
<bean class="com.yomahub.liteflow.spring.ComponentScanner"/>
<bean id="liteflowConfig" class="com.yomahub.liteflow.property.LiteflowConfig">
<property name="ruleSource" value="fallback/flow.el.xml"/>
<property name="fallbackCmpEnable" value="true"/>
</bean>
<bean id="flowExecutor" class="com.yomahub.liteflow.core.FlowExecutor">
<constructor-arg name="liteflowConfig" ref="liteflowConfig"/>
</bean>
</beans>

View File

@@ -0,0 +1,136 @@
<?xml version="1.0" encoding="UTF-8"?>
<flow>
<!-- THEN 普通组件降级 -->
<chain name="then1">
THEN(a, node("x"));
</chain>
<chain name="then2">
THEN(PRE(node("x1")), node("x2"), FINALLY(node("x3")));
</chain>
<!-- WHEN 普通组件降级 -->
<chain name="when1">
WHEN(b, node("x"));
</chain>
<!-- IF 条件组件降级 -->
<chain name="if1">
IF(node("x"), a)
</chain>
<!-- IF 普通组件降级 -->
<chain name="if2">
IF(ifn1, node("x"))
</chain>
<!-- FOR 次数循环组件降级 -->
<chain name="for1">
FOR(node("x")).DO(a);
</chain>
<!-- FOR 普通组件降级 -->
<chain name="for2">
FOR(3).DO(node("x"));
</chain>
<!-- WHILE 条件循环组件降级 -->
<chain name="while1">
WHILE(node("x")).DO(a)
</chain>
<!-- WHILE 普通组件降级 -->
<chain name="while2">
WHILE(wn1).DO(node("x"))
</chain>
<!-- ITERATOR 迭代组件降级 -->
<chain name="iterator1">
ITERATOR(node("x")).DO(a)
</chain>
<!-- ITERATOR 普通组件降级 -->
<chain name="iterator2">
ITERATOR(itn1).DO(node("x"))
</chain>
<!-- BREAK 退出循环组件降级 -->
<chain name="break1">
FOR(3).DO(a).BREAK(node("x"));
</chain>
<chain name="break2">
WHILE(wn1).DO(a).BREAK(node("x"));
</chain>
<chain name="break3">
ITERATOR(itn1).DO(a).BREAK(node("x"));
</chain>
<!-- SWITCH 选择组件降级 -->
<chain name="switch1">
SWITCH(node("x")).to(a,b);
</chain>
<!-- SWITCH 普通组件降级 -->
<chain name="switch2">
SWITCH(swn1).to(node("x"),a);
</chain>
<!-- AND 条件组件降级 -->
<chain name="and1">
IF(AND(node("x"),ifn1), a);
</chain>
<!-- OR 条件组件降级 -->
<chain name="or1">
IF(OR(node("x"),ifn1), a);
</chain>
<!-- NOT 条件组件降级 -->
<chain name="not1">
IF(NOT(node("x")), a);
</chain>
<!-- CATCH 普通组件降级 -->
<chain name="catch1">
CATCH(THEN(a, d)).DO(node("x"))
</chain>
<!-- 多个组件降级 -->
<chain name="multi1">
THEN(
a,
node("x1"),
IF(node("x2"), b)
);
</chain>
<chain name="multi2">
IF(
OR(node("x1"), ifn1),
THEN(a, node("x2"))
);
</chain>
<chain name="multi3">
FOR(node("x1")).DO(
THEN(b, node("x2"))
);
</chain>
<!-- 并发降级测试 -->
<chain name="concurrent1">
WHEN(
THEN(node("x1")),
IF(node("x2"), b)
).maxWaitSeconds(10000);
</chain>
<chain name="concurrent2">
WHEN(
node("x1"),
IF(node("x2"), b)
).maxWaitSeconds(10000);
</chain>
</flow>