增加测试用例

This commit is contained in:
everywhere.z
2022-12-19 19:01:23 +08:00
parent 40560647e7
commit 16cfa3816f
5 changed files with 129 additions and 1 deletions

View File

@@ -1,6 +1,8 @@
package com.yomahub.liteflow.test.cmpData;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.flow.LiteflowResponse;
import com.yomahub.liteflow.slot.DefaultContext;
@@ -16,6 +18,8 @@ import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
/**
* springboot环境EL常规的例子测试
@@ -33,7 +37,7 @@ public class CmpDataELSpringbootTest extends BaseTest {
//最简单的情况
@Test
public void testCmpData() throws Exception{
public void testCmpData1() throws Exception{
LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg");
Assert.assertTrue(response.isSuccess());
DefaultContext context = response.getFirstContextBean();
@@ -42,4 +46,14 @@ public class CmpDataELSpringbootTest extends BaseTest {
Assert.assertEquals("jack", user.getName());
Assert.assertEquals(0, user.getBirth().compareTo(DateUtil.parseDate("1995-10-01").toJdkDate()));
}
@Test
public void testCmpData2() throws Exception{
LiteflowResponse response = flowExecutor.execute2Resp("chain2", "arg", TestContext.class);
Assert.assertTrue(response.isSuccess());
TestContext context = response.getFirstContextBean();
Assert.assertEquals(8, context.getSet().size());
String result = context.getSet().stream().sorted().collect(Collectors.joining());
Assert.assertEquals("12345678", result);
}
}

View File

@@ -0,0 +1,18 @@
package com.yomahub.liteflow.test.cmpData;
import cn.hutool.core.collection.ConcurrentHashSet;
import java.util.Set;
public class TestContext {
private Set<String> set = new ConcurrentHashSet<>();
public void add2Set(String str){
set.add(str);
}
public Set<String> getSet(){
return set;
}
}

View File

@@ -8,6 +8,7 @@
package com.yomahub.liteflow.test.cmpData.cmp;
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.test.cmpData.TestContext;
import org.springframework.stereotype.Component;
@Component("c")
@@ -15,6 +16,9 @@ public class CCmp extends NodeComponent {
@Override
public void process() {
String data = this.getCmpData(String.class);
TestContext context = this.getFirstContextBean();
context.add2Set(data);
System.out.println("CCmp executed!");
}

View File

@@ -0,0 +1,79 @@
package com.yomahub.liteflow.test.slotOffer;
import cn.hutool.core.collection.ConcurrentHashSet;
import cn.hutool.core.collection.ListUtil;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.flow.FlowBus;
import com.yomahub.liteflow.flow.LiteflowResponse;
import com.yomahub.liteflow.flow.parallel.ParallelSupplier;
import com.yomahub.liteflow.slot.DataBus;
import com.yomahub.liteflow.slot.DefaultContext;
import com.yomahub.liteflow.test.BaseTest;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
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.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;
/**
* springboot环境EL常规的例子测试
* @author Bryan.Zhang
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SlotOfferELSpringbootTest.class)
@EnableAutoConfiguration
public class SlotOfferELSpringbootTest extends BaseTest {
@Resource
private FlowExecutor flowExecutor;
//最简单的情况
@Test
public void testSlotOffer() throws Exception{
Set<Integer> set = new ConcurrentHashSet<>();
Set<CompletableFuture<Boolean>> futureSet = new ConcurrentHashSet<>();
Set<String> error = new ConcurrentHashSet<>();
for (int i = 0; i < 200; i++) {
futureSet.add(CompletableFuture.supplyAsync(() -> {
for (int j = 0; j < 300; j++) {
int index=0;
try{
index = DataBus.offerSlotByClass(ListUtil.toList(DefaultContext.class));
boolean flag = set.add(index);
if (!flag){
error.add(Integer.toString(index));
}
}catch (Exception e) {
error.add(e.getMessage());
}finally {
DataBus.releaseSlot(index);
boolean flag = set.remove(index);
if(!flag){
error.add(Integer.toString(index));
}
}
}
return Boolean.TRUE;
}));
}
CompletableFuture<Void> resultFuture = CompletableFuture.allOf(futureSet.toArray(new CompletableFuture[]{}));
resultFuture.get();
Assert.assertEquals(0, set.size());
Assert.assertEquals(0, error.size());
Assert.assertEquals(0, DataBus.OCCUPY_COUNT.get());
}
}

View File

@@ -11,4 +11,17 @@
c
);
</chain>
<chain name="chain2">
WHEN(
c.data("1"),
c.data("2"),
c.data("3"),
c.data("4"),
c.data("5"),
c.data("6"),
c.data("7"),
c.data("8")
);
</chain>
</flow>