!208 实现组件的回滚机制

Merge pull request !208 from Rain/dev
This commit is contained in:
铂赛东
2023-08-20 08:01:12 +00:00
committed by Gitee
82 changed files with 2374 additions and 3 deletions

View File

@@ -0,0 +1,95 @@
package com.yomahub.liteflow.test.rollback;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.flow.LiteflowResponse;
import com.yomahub.liteflow.test.BaseTest;
import com.yomahub.liteflow.test.whenTimeOut.WhenTimeOutELDeclSpringbootTest1;
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;
@ExtendWith(SpringExtension.class)
@TestPropertySource(value = "classpath:/rollback/application.properties")
@SpringBootTest(classes = RollbackELDeclSpringbootTest.class)
@EnableAutoConfiguration
@ComponentScan({ "com.yomahub.liteflow.test.rollback.cmp" })
public class RollbackELDeclSpringbootTest extends BaseTest {
@Resource
private FlowExecutor flowExecutor;
// 在流程正常执行结束情况下的测试
@Test
public void testRollback() throws Exception {
LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg");
Assertions.assertTrue(response.isSuccess());
Assertions.assertNull(response.getCause());
Assertions.assertEquals("", response.getRollbackStepStr());
}
// 对串行编排与并行编排语法的测试
@Test
public void testWhenAndThen() throws Exception {
LiteflowResponse response = flowExecutor.execute2Resp("chain2", "arg");
Assertions.assertFalse(response.isSuccess());
Assertions.assertEquals("d==>b==>a", response.getRollbackStepStr());
}
// 对条件编排语法的测试
@Test
public void testIf() throws Exception {
LiteflowResponse response = flowExecutor.execute2Resp("chain3", "arg");
Assertions.assertFalse(response.isSuccess());
Assertions.assertEquals("d==>x", response.getRollbackStepStr());
}
// 对选择编排语法的测试
@Test
public void testSwitch() throws Exception {
LiteflowResponse response = flowExecutor.execute2Resp("chain4", "arg");
Assertions.assertFalse(response.isSuccess());
Assertions.assertEquals("d==>f", response.getRollbackStepStr());
}
// 对FOR循环编排语法的测试
@Test
public void testFor() throws Exception {
LiteflowResponse response = flowExecutor.execute2Resp("chain5", "arg");
Assertions.assertFalse(response.isSuccess());
Assertions.assertEquals("h==>b==>g", response.getRollbackStepStr());
}
// 对WHILE循环编排语法的测试
@Test
public void testWhile() throws Exception {
LiteflowResponse response = flowExecutor.execute2Resp("chain6", "arg");
Assertions.assertFalse(response.isSuccess());
Assertions.assertEquals("d==>b==>a==>w", response.getRollbackStepStr());
}
// 对ITERATOR迭代循环编排语法的测试
@Test
public void testIterator() throws Exception {
LiteflowResponse response = flowExecutor.execute2Resp("chain7", "arg");
Assertions.assertFalse(response.isSuccess());
Assertions.assertEquals("d==>b==>a==>i", response.getRollbackStepStr());
}
@Test
// 对捕获异常表达式的测试
public void testCatch() throws Exception {
LiteflowResponse response = flowExecutor.execute2Resp("chain8", "arg");
Assertions.assertTrue(response.isSuccess());
Assertions.assertNull(response.getCause());
Assertions.assertEquals("", response.getRollbackStepStr());
}
}

View File

@@ -0,0 +1,25 @@
/**
* <p>Title: liteflow</p>
* <p>Description: 轻量级的组件式流程框架</p>
* @author Bryan.Zhang
* @email weenyc31@163.com
* @Date 2020/4/1
*/
package com.yomahub.liteflow.test.rollback.cmp;
import com.yomahub.liteflow.core.NodeComponent;
import org.springframework.stereotype.Component;
@Component("a")
public class ACmp extends NodeComponent {
@Override
public void process() {
System.out.println("ACmp executed!");
}
@Override
public void rollback() throws Exception {
System.out.println("ACmp rollback!");
}
}

View File

@@ -0,0 +1,31 @@
/**
* <p>Title: liteflow</p>
* <p>Description: 轻量级的组件式流程框架</p>
* @author Bryan.Zhang
* @email weenyc31@163.com
* @Date 2020/4/1
*/
package com.yomahub.liteflow.test.rollback.cmp;
import com.yomahub.liteflow.core.NodeComponent;
import org.springframework.stereotype.Component;
@Component("b")
public class BCmp extends NodeComponent {
@Override
public void process() {
System.out.println("BCmp executed!");
throw new RuntimeException();
}
@Override
public void rollback() throws Exception {
System.out.println("BCmp rollback!");
}
@Override
public boolean isContinueOnError() {
return true;
}
}

View File

@@ -0,0 +1,21 @@
/**
* <p>Title: liteflow</p>
* <p>Description: 轻量级的组件式流程框架</p>
* @author Bryan.Zhang
* @email weenyc31@163.com
* @Date 2020/4/1
*/
package com.yomahub.liteflow.test.rollback.cmp;
import com.yomahub.liteflow.core.NodeComponent;
import org.springframework.stereotype.Component;
@Component("c")
public class CCmp extends NodeComponent {
@Override
public void process() {
System.out.println("CCmp executed!");
}
}

View File

@@ -0,0 +1,26 @@
/**
* <p>Title: liteflow</p>
* <p>Description: 轻量级的组件式流程框架</p>
* @author Bryan.Zhang
* @email weenyc31@163.com
* @Date 2020/4/1
*/
package com.yomahub.liteflow.test.rollback.cmp;
import com.yomahub.liteflow.core.NodeComponent;
import org.springframework.stereotype.Component;
@Component("d")
public class DCmp extends NodeComponent {
@Override
public void process() {
System.out.println("DCmp executed!");
throw new RuntimeException();
}
@Override
public void rollback() throws Exception {
System.out.println("DCmp rollback!");
}
}

View File

@@ -0,0 +1,26 @@
/**
* <p>Title: liteflow</p>
* <p>Description: 轻量级的组件式流程框架</p>
* @author Bryan.Zhang
* @email weenyc31@163.com
* @Date 2020/4/1
*/
package com.yomahub.liteflow.test.rollback.cmp;
import com.yomahub.liteflow.core.NodeComponent;
import org.springframework.stereotype.Component;
@Component("e")
public class ECmp extends NodeComponent {
@Override
public void process() {
System.out.println("ECmp executed!");
throw new RuntimeException();
}
@Override
public void rollback() throws Exception {
System.out.println("ECmp rollback!");
}
}

View File

@@ -0,0 +1,19 @@
package com.yomahub.liteflow.test.rollback.cmp;
import com.yomahub.liteflow.core.NodeSwitchComponent;
import org.springframework.stereotype.Component;
@Component("f")
public class FCmp extends NodeSwitchComponent {
@Override
public String processSwitch() {
System.out.println("FCmp executed!");
return "abc";
}
@Override
public void rollback() throws Exception {
System.out.println("FCmp rollback!");
}
}

View File

@@ -0,0 +1,19 @@
package com.yomahub.liteflow.test.rollback.cmp;
import com.yomahub.liteflow.core.NodeForComponent;
import org.springframework.stereotype.Component;
@Component("g")
public class GCmp extends NodeForComponent {
@Override
public int processFor() throws Exception {
System.out.println("GCmp executed!");
return 3;
}
@Override
public void rollback() throws Exception {
System.out.println("GCmp rollback!");
}
}

View File

@@ -0,0 +1,19 @@
package com.yomahub.liteflow.test.rollback.cmp;
import com.yomahub.liteflow.core.NodeBreakComponent;
import org.springframework.stereotype.Component;
@Component("h")
public class HCmp extends NodeBreakComponent {
@Override
public boolean processBreak() throws Exception {
System.out.println("HCmp executed!");
throw new RuntimeException();
}
@Override
public void rollback() throws Exception {
System.out.println("HCmp rollback!");
}
}

View File

@@ -0,0 +1,23 @@
package com.yomahub.liteflow.test.rollback.cmp;
import cn.hutool.core.collection.ListUtil;
import com.yomahub.liteflow.core.NodeIteratorComponent;
import org.springframework.stereotype.Component;
import java.util.Iterator;
import java.util.List;
@Component("i")
public class ICmp extends NodeIteratorComponent {
@Override
public Iterator<?> processIterator() throws Exception {
List<String> list = ListUtil.toList("jack", "mary", "tom");
return list.iterator();
}
@Override
public void rollback() throws Exception {
System.out.println("ICmp rollback!");
}
}

View File

@@ -0,0 +1,19 @@
package com.yomahub.liteflow.test.rollback.cmp;
import com.yomahub.liteflow.core.NodeWhileComponent;
import org.springframework.stereotype.Component;
@Component("w")
public class WCmp extends NodeWhileComponent {
@Override
public boolean processWhile() throws Exception {
System.out.println("WCmp executed!");
return true;
}
@Override
public void rollback() throws Exception {
System.out.println("WCmp rollback!");
}
}

View File

@@ -0,0 +1,18 @@
package com.yomahub.liteflow.test.rollback.cmp;
import com.yomahub.liteflow.core.NodeIfComponent;
import org.springframework.stereotype.Component;
@Component("x")
public class XCmp extends NodeIfComponent {
@Override
public boolean processIf() throws Exception {
System.out.println("XCmp executed!");
return true;
}
@Override
public void rollback() throws Exception {
System.out.println("XCmp rollback!");
}
}

View File

@@ -0,0 +1 @@
liteflow.rule-source=rollback/flow.el.xml

View File

@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<flow>
<chain name="chain1">
THEN( a, b, WHEN(c, d).ignoreError(true), CATCH(e) );
</chain>
<chain name="chain2">
THEN( a, b, WHEN(c, d) );
</chain>
<chain name="chain3">
THEN( IF(x, d, a), CATCH(IF(x, d, a)) );
</chain>
<chain name="chain4">
SWITCH(f).TO(a, b).DEFAULT(d);
</chain>
<chain name="chain5">
FOR(g).DO(THEN(b, c)).BREAK(h);;
</chain>
<chain name="chain6">
WHILE(w).DO(THEN(a, b, d));
</chain>
<chain name="chain7">
ITERATOR(i).DO(THEN(a, b, d));
</chain>
<chain name="chain8">
CATCH( THEN(b, c, d) ).DO(a);
</chain>
</flow>