对异步线程的测试用例进行了重写

This commit is contained in:
bryan31
2021-11-10 00:42:26 +08:00
parent b57284e350
commit 448fa82023
7 changed files with 74 additions and 3 deletions

View File

@@ -105,12 +105,13 @@ public class AsyncNodeSpringbootTest extends BaseTest {
Assert.assertEquals(TestException.class, response.getCause().getClass());
}
//d g h并行配置了any=true其中d耗时3g耗时1秒,其他都不设耗时
//d g h并行配置了any=true其中d耗时1g耗时0.5秒,其他都不设耗时
//最终执行效果应该是h先返回然后执行abc,最后gd
//这里要注意的是由于step是先加入所以step的打印顺序并不是这样的。但是实际执行是正确的
@Test
public void testAsyncFlow8() throws Exception {
LiteflowResponse<DefaultSlot> response = flowExecutor.execute2Resp("chain8", "it's a base request");
Assert.assertTrue(response.isSuccess());
Assert.assertTrue(response.getSlot().getData("check").toString().startsWith("habc"));
}
}

View File

@@ -1,6 +1,7 @@
package com.yomahub.liteflow.test.asyncNode.cmp;
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.entity.data.Slot;
import org.springframework.stereotype.Component;
@@ -8,6 +9,16 @@ import org.springframework.stereotype.Component;
public class ACmp extends NodeComponent {
@Override
public void process() {
Slot slot = this.getSlot();
synchronized (NodeComponent.class){
if (slot.hasData("check")){
String str = slot.getData("check");
str += this.getNodeId();
slot.setData("check", str);
}else{
slot.setData("check", this.getNodeId());
}
}
System.out.println("Acomp executed!");
}
}

View File

@@ -1,6 +1,7 @@
package com.yomahub.liteflow.test.asyncNode.cmp;
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.entity.data.Slot;
import org.springframework.stereotype.Component;
@@ -8,6 +9,16 @@ import org.springframework.stereotype.Component;
public class BCmp extends NodeComponent {
@Override
public void process() {
Slot slot = this.getSlot();
synchronized (NodeComponent.class){
if (slot.hasData("check")){
String str = slot.getData("check");
str += this.getNodeId();
slot.setData("check", str);
}else{
slot.setData("check", this.getNodeId());
}
}
System.out.println("Bcomp executed!");
}
}

View File

@@ -1,6 +1,7 @@
package com.yomahub.liteflow.test.asyncNode.cmp;
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.entity.data.Slot;
import org.springframework.stereotype.Component;
@@ -8,6 +9,16 @@ import org.springframework.stereotype.Component;
public class CCmp extends NodeComponent {
@Override
public void process() throws Exception {
Slot slot = this.getSlot();
synchronized (NodeComponent.class){
if (slot.hasData("check")){
String str = slot.getData("check");
str += this.getNodeId();
slot.setData("check", str);
}else{
slot.setData("check", this.getNodeId());
}
}
System.out.println("Ccomp executed!");
}
}

View File

@@ -1,6 +1,7 @@
package com.yomahub.liteflow.test.asyncNode.cmp;
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.entity.data.Slot;
import org.springframework.stereotype.Component;
@@ -8,7 +9,17 @@ import org.springframework.stereotype.Component;
public class DCmp extends NodeComponent {
@Override
public void process() throws Exception {
Thread.sleep(3000);
Thread.sleep(1000);
Slot slot = this.getSlot();
synchronized (NodeComponent.class){
if (slot.hasData("check")){
String str = slot.getData("check");
str += this.getNodeId();
slot.setData("check", str);
}else{
slot.setData("check", this.getNodeId());
}
}
System.out.println("Dcomp executed!");
}
}

View File

@@ -1,6 +1,7 @@
package com.yomahub.liteflow.test.asyncNode.cmp;
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.entity.data.Slot;
import org.springframework.stereotype.Component;
@@ -9,7 +10,17 @@ public class GCmp extends NodeComponent {
@Override
public void process() throws Exception {
Thread.sleep(1000);
Thread.sleep(500);
Slot slot = this.getSlot();
synchronized (NodeComponent.class){
if (slot.hasData("check")){
String str = slot.getData("check");
str += this.getNodeId();
slot.setData("check", str);
}else{
slot.setData("check", this.getNodeId());
}
}
System.out.println("Gcomp executed!");
}
}

View File

@@ -1,14 +1,29 @@
package com.yomahub.liteflow.test.asyncNode.cmp;
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.core.NodeCondComponent;
import com.yomahub.liteflow.entity.data.Slot;
import org.springframework.stereotype.Component;
import java.util.concurrent.atomic.AtomicReference;
@Component("h")
public class HCmp extends NodeComponent {
@Override
public void process() throws Exception {
Slot slot = this.getSlot();
synchronized (NodeComponent.class){
if (slot.hasData("check")){
String str = slot.getData("check");
str += this.getNodeId();
slot.setData("check", str);
}else{
slot.setData("check", this.getNodeId());
}
}
System.out.println("Hcomp executed!");
}
}