对大容量路由脚本增加测试用例

This commit is contained in:
everywhere.z
2026-01-22 10:54:04 +08:00
parent f289045046
commit 2faa6c58d9
9 changed files with 54216 additions and 3 deletions

View File

@@ -73,15 +73,91 @@ public class GraalJavaScriptExecutor extends ScriptExecutor {
});
Value value = context.eval(scriptMap.get(wrap.getNodeId()));
// 处理 null 值
if (value.isNull()) {
return null;
}
// 处理布尔类型
if (value.isBoolean()) {
return value.asBoolean();
}
else if (value.isNumber()) {
return value.asInt();
// 处理数值类型(按精度从高到低处理,避免精度丢失)
if (value.isNumber()) {
// 优先尝试转换为整数类型
if (value.fitsInInt()) {
return value.asInt();
} else if (value.fitsInLong()) {
return value.asLong();
}
// 浮点数类型
else if (value.fitsInFloat()) {
return value.asFloat();
} else if (value.fitsInDouble()) {
return value.asDouble();
}
// 默认返回 double兜底方案
return value.asDouble();
}
else if (value.isString()) {
// 处理字符串类型
if (value.isString()) {
return value.asString();
}
// 处理时间日期类型
if (value.isDate()) {
return value.asDate();
}
if (value.isTime()) {
return value.asTime();
}
if (value.isInstant()) {
return value.asInstant();
}
if (value.isDuration()) {
return value.asDuration();
}
if (value.isTimeZone()) {
return value.asTimeZone();
}
// 处理异常类型
if (value.isException()) {
try {
value.throwException();
} catch (Exception e) {
throw new RuntimeException("Script execution threw an exception", e);
}
}
// 处理 Java 主机对象(直接返回原始 Java 对象)
if (value.isHostObject()) {
return value.asHostObject();
}
// 处理数组类型(转换为 Java List
if (value.hasArrayElements()) {
long size = value.getArraySize();
List<Object> list = new ArrayList<>((int) size);
for (long i = 0; i < size; i++) {
list.add(value.getArrayElement(i));
}
return list;
}
// 处理对象类型(转换为 Java Map
if (value.hasMembers()) {
Map<String, Object> map = new java.util.HashMap<>();
for (String key : value.getMemberKeys()) {
map.put(key, value.getMember(key));
}
return map;
}
// 其他类型直接返回 Value 对象
return value;
}
catch (Exception e) {

View File

@@ -0,0 +1,52 @@
package com.yomahub.liteflow.test.largeNumRouteChain;
import cn.hutool.core.date.StopWatch;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.exception.NoMatchedRouteChainException;
import com.yomahub.liteflow.exception.RouteChainNotFoundException;
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.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.TestPropertySource;
import javax.annotation.Resource;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* springboot环境EL常规的例子测试
*
* @author Bryan.Zhang
*/
@TestPropertySource(value = "classpath:/largeNumRouteChain/application.properties")
@SpringBootTest(classes = LargeNumRouteChainTest.class)
@EnableAutoConfiguration
@ComponentScan({ "com.yomahub.liteflow.test.largeNumRouteChain.cmp" })
public class LargeNumRouteChainTest extends BaseTest {
@Resource
private FlowExecutor flowExecutor;
// 在大量的路由规则里只有3个匹配
@Test
public void test1() throws Exception {
StopWatch sw = new StopWatch();
sw.start();
List<LiteflowResponse> responseList = flowExecutor.executeRouteChain("n1", 15, DefaultContext.class);
List<LiteflowResponse> resultList = responseList.stream().filter(
LiteflowResponse::isSuccess
).collect(Collectors.toList());
Assertions.assertEquals(3, resultList.size());
sw.stop();
System.out.println("耗时:" + sw.getTotalTimeMillis());
}
}

View File

@@ -0,0 +1,20 @@
/**
* <p>Title: liteflow</p>
* <p>Description: 轻量级的组件式流程框架</p>
* @author Bryan.Zhang
* @email weenyc31@163.com
* @Date 2020/4/1
*/
package com.yomahub.liteflow.test.largeNumRouteChain.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!");
}
}

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.largeNumRouteChain.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!");
}
}

View File

@@ -0,0 +1,13 @@
package com.yomahub.liteflow.test.largeNumRouteChain.cmp;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeBooleanComponent;
@LiteflowComponent("r1")
public class R1 extends NodeBooleanComponent {
@Override
public boolean processBoolean() throws Exception {
int testInt = this.getRequestData();
return testInt >= 10 && testInt <= 20;
}
}

View File

@@ -0,0 +1,13 @@
package com.yomahub.liteflow.test.largeNumRouteChain.cmp;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeBooleanComponent;
@LiteflowComponent("r2")
public class R2 extends NodeBooleanComponent {
@Override
public boolean processBoolean() throws Exception {
int testInt = this.getRequestData();
return testInt == 18;
}
}

View File

@@ -0,0 +1,13 @@
package com.yomahub.liteflow.test.largeNumRouteChain.cmp;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeBooleanComponent;
@LiteflowComponent("r3")
public class R3 extends NodeBooleanComponent {
@Override
public boolean processBoolean() throws Exception {
int testInt = this.getRequestData();
return testInt < 100;
}
}

View File

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