修正单元测试

This commit is contained in:
dq-open-cloud
2022-01-24 10:05:53 +08:00
parent 5713431caf
commit 87cae4a23e
5 changed files with 73 additions and 93 deletions

View File

@@ -38,7 +38,7 @@ public class ExecutorHelper {
* 此处使用Map缓存线程池信息 * 此处使用Map缓存线程池信息
* key - 线程池构建者的Class全类名 * key - 线程池构建者的Class全类名
* value - 线程池对象 * value - 线程池对象
* */ */
private final Map<String, ExecutorService> executorServiceMap; private final Map<String, ExecutorService> executorServiceMap;
private ExecutorHelper() { private ExecutorHelper() {
@@ -85,12 +85,14 @@ public class ExecutorHelper {
} }
} }
/** 构建全局默认线程池 */ /**
* 构建全局默认线程池
*/
public ExecutorService buildExecutor() { public ExecutorService buildExecutor() {
if (ObjectUtil.isNull(executorService)) { if (ObjectUtil.isNull(executorService)) {
LiteflowConfig liteflowConfig = SpringAware.getBean(LiteflowConfig.class); LiteflowConfig liteflowConfig = SpringAware.getBean(LiteflowConfig.class);
assert liteflowConfig != null; assert liteflowConfig != null;
executorService = buildExecutor(liteflowConfig.getThreadExecutorClass()); executorService = getExecutorBuilder(liteflowConfig.getThreadExecutorClass()).buildExecutor();
} }
return executorService; return executorService;
} }
@@ -99,27 +101,23 @@ public class ExecutorHelper {
* <p> * <p>
* 构建线程池执行器 - 支持多个when公用一个线程池 * 构建线程池执行器 - 支持多个when公用一个线程池
* </p> * </p>
* @author sikadai *
* @date 2022/1/21 23:00
* @param threadExecutorClass : 线程池构建者的Class全类名 * @param threadExecutorClass : 线程池构建者的Class全类名
* @return java.util.concurrent.ExecutorService * @return java.util.concurrent.ExecutorService
* @author sikadai
* @date 2022/1/21 23:00
*/ */
public ExecutorService buildExecutor(String threadExecutorClass) { public ExecutorService buildExecutor(String threadExecutorClass) {
try { if (StrUtil.isBlank(threadExecutorClass)) {
if (StrUtil.isBlank(threadExecutorClass)) { return buildExecutor();
return buildExecutor(); }
} ExecutorService executorServiceFromCache = executorServiceMap.get(threadExecutorClass);
ExecutorService executorServiceFromCache = executorServiceMap.get(threadExecutorClass); if (executorServiceFromCache != null) {
if (executorServiceFromCache != null) { return executorServiceFromCache;
return executorServiceFromCache; } else {
} else { ExecutorService executorService = getExecutorBuilder(threadExecutorClass).buildExecutor();
ExecutorService executorService = getExecutorBuilder(threadExecutorClass).buildExecutor(); executorServiceMap.put(threadExecutorClass, executorService);
executorServiceMap.put(threadExecutorClass, executorService); return executorService;
return executorService;
}
} catch (Exception e) {
LOG.error(e.getMessage(), e);
throw new ThreadExecutorServiceCreateException(e.getMessage());
} }
} }
@@ -128,13 +126,19 @@ public class ExecutorHelper {
* 根据线程执行构建者Class类名获取ExecutorBuilder实例 * 根据线程执行构建者Class类名获取ExecutorBuilder实例
* </p> * </p>
* *
* @author sikadai
* @date 2022/1/21 23:04
* @param threadExecutorClass * @param threadExecutorClass
* @return com.yomahub.liteflow.thread.ExecutorBuilder * @return com.yomahub.liteflow.thread.ExecutorBuilder
* @author sikadai
* @date 2022/1/21 23:04
*/ */
private ExecutorBuilder getExecutorBuilder(String threadExecutorClass) throws Exception { private ExecutorBuilder getExecutorBuilder(String threadExecutorClass) {
return (ExecutorBuilder) Class.forName(threadExecutorClass).newInstance(); try {
return (ExecutorBuilder) Class.forName(threadExecutorClass).newInstance();
} catch (Exception e) {
LOG.error(e.getMessage(), e);
throw new ThreadExecutorServiceCreateException(e.getMessage());
}
} }
public ExecutorService getExecutorService() { public ExecutorService getExecutorService() {

View File

@@ -1,12 +1,8 @@
package com.yomahub.liteflow.test.customWhenThreadPool; package com.yomahub.liteflow.test.customWhenThreadPool;
import com.yomahub.liteflow.builder.LiteFlowChainBuilder;
import com.yomahub.liteflow.builder.LiteFlowConditionBuilder;
import com.yomahub.liteflow.builder.LiteFlowNodeBuilder;
import com.yomahub.liteflow.core.FlowExecutor; import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.entity.data.DefaultSlot; import com.yomahub.liteflow.entity.data.DefaultSlot;
import com.yomahub.liteflow.entity.data.LiteflowResponse; import com.yomahub.liteflow.entity.data.LiteflowResponse;
import com.yomahub.liteflow.enums.NodeTypeEnum;
import com.yomahub.liteflow.test.BaseTest; import com.yomahub.liteflow.test.BaseTest;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
@@ -39,48 +35,38 @@ public class CustomWhenThreadPoolSpringbootTest extends BaseTest {
@Resource @Resource
private FlowExecutor flowExecutor; private FlowExecutor flowExecutor;
/**
* 测试全局线程池配置
*/
@Test @Test
public void testCustomThreadPool() throws Exception { public void testGlobalThreadPool() {
LiteflowResponse<DefaultSlot> response = flowExecutor.execute2Resp("chain", "arg"); LiteflowResponse<DefaultSlot> response = flowExecutor.execute2Resp("chain", "arg");
Assert.assertTrue(response.isSuccess()); Assert.assertTrue(response.isSuccess());
Assert.assertTrue(response.getSlot().getData("threadName").toString().startsWith("lf-when-thead")); Assert.assertTrue(response.getSlot().getData("threadName").toString().startsWith("lf-when-thead"));
}
/**
* 测试全局和when上自定义线程池-优先以when上为准
*/
@Test
public void testGlobalAndCustomWhenThreadPool() {
LiteflowResponse<DefaultSlot> response1 = flowExecutor.execute2Resp("chain1", "arg"); LiteflowResponse<DefaultSlot> response1 = flowExecutor.execute2Resp("chain1", "arg");
Assert.assertTrue(response1.isSuccess()); Assert.assertTrue(response1.isSuccess());
Assert.assertTrue(response1.getSlot().getData("threadName").toString().startsWith("customer-when-1-thead")); Assert.assertTrue(response1.getSlot().getData("threadName").toString().startsWith("customer-when-1-thead"));
}
/**
* when配置的线程池可以共用
*/
@Test
public void testCustomWhenThreadPool() {
// 使用when - thread1
testGlobalAndCustomWhenThreadPool();
// chain配置同一个thead1
LiteflowResponse<DefaultSlot> response2 = flowExecutor.execute2Resp("chain2", "arg"); LiteflowResponse<DefaultSlot> response2 = flowExecutor.execute2Resp("chain2", "arg");
Assert.assertTrue(response2.isSuccess()); Assert.assertTrue(response2.isSuccess());
Assert.assertTrue(response2.getSlot().getData("threadName").toString().startsWith("customer-when-2-thead")); Assert.assertTrue(response2.getSlot().getData("threadName").toString().startsWith("customer-when-1-thead"));
// 使用build模式构建chain测试when条件的多线程
LiteFlowNodeBuilder.createNode().setId("a")
.setName("组件A")
.setType(NodeTypeEnum.COMMON)
.setClazz("com.yomahub.liteflow.test.customWhenThreadPool.cmp.ACmp")
.build();
LiteFlowNodeBuilder.createNode().setId("b")
.setName("组件B")
.setType(NodeTypeEnum.COMMON)
.setClazz("com.yomahub.liteflow.test.customWhenThreadPool.cmp.BCmp")
.build();
LiteFlowNodeBuilder.createNode().setId("c")
.setName("组件C")
.setType(NodeTypeEnum.COMMON)
.setClazz("com.yomahub.liteflow.test.customWhenThreadPool.cmp.CCmp")
.build();
LiteFlowChainBuilder.createChain().setChainName("chain3").setCondition(
LiteFlowConditionBuilder
.createWhenCondition()
.setThreadExecutorClass(CustomThreadExecutor3.class.getName())
.setValue("a,b,c,d")
.build()
).build();
LiteflowResponse<DefaultSlot> response3 = flowExecutor.execute2Resp("chain3", "arg");
Assert.assertTrue(response3.isSuccess());
Assert.assertTrue(response3.getSlot().getData("threadName").toString().startsWith("customer-when-3-thead"));
} }
} }

View File

@@ -7,6 +7,6 @@
<when value="c,d" threadExecutorClass="com.yomahub.liteflow.test.customWhenThreadPool.CustomThreadExecutor1"/> <when value="c,d" threadExecutorClass="com.yomahub.liteflow.test.customWhenThreadPool.CustomThreadExecutor1"/>
</chain> </chain>
<chain name="chain2"> <chain name="chain2">
<when value="e,f" threadExecutorClass="com.yomahub.liteflow.test.customWhenThreadPool.CustomThreadExecutor2"/> <when value="e,f" threadExecutorClass="com.yomahub.liteflow.test.customWhenThreadPool.CustomThreadExecutor1"/>
</chain> </chain>
</flow> </flow>

View File

@@ -26,55 +26,45 @@ import javax.annotation.Resource;
*/ */
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@ContextConfiguration("classpath:/customWhenThreadPool/application.xml") @ContextConfiguration("classpath:/customWhenThreadPool/application.xml")
public class CustomWhenThreadPoolSpringbootTest extends BaseTest { public class CustomWhenThreadPoolSpringTest extends BaseTest {
private final Logger log = LoggerFactory.getLogger(this.getClass()); private final Logger log = LoggerFactory.getLogger(this.getClass());
@Resource @Resource
private FlowExecutor flowExecutor; private FlowExecutor flowExecutor;
/**
* 测试全局线程池配置
*/
@Test @Test
public void testCustomThreadPool() throws Exception { public void testGlobalThreadPool() {
LiteflowResponse<DefaultSlot> response = flowExecutor.execute2Resp("chain", "arg"); LiteflowResponse<DefaultSlot> response = flowExecutor.execute2Resp("chain", "arg");
Assert.assertTrue(response.isSuccess()); Assert.assertTrue(response.isSuccess());
Assert.assertTrue(response.getSlot().getData("threadName").toString().startsWith("lf-when-thead")); Assert.assertTrue(response.getSlot().getData("threadName").toString().startsWith("lf-when-thead"));
}
/**
* 测试全局和when上自定义线程池-优先以when上为准
*/
@Test
public void testGlobalAndCustomWhenThreadPool() {
LiteflowResponse<DefaultSlot> response1 = flowExecutor.execute2Resp("chain1", "arg"); LiteflowResponse<DefaultSlot> response1 = flowExecutor.execute2Resp("chain1", "arg");
Assert.assertTrue(response1.isSuccess()); Assert.assertTrue(response1.isSuccess());
Assert.assertTrue(response1.getSlot().getData("threadName").toString().startsWith("customer-when-1-thead")); Assert.assertTrue(response1.getSlot().getData("threadName").toString().startsWith("customer-when-1-thead"));
}
/**
* when配置的线程池可以共用
*/
@Test
public void testCustomWhenThreadPool() {
// 使用when - thread1
testGlobalAndCustomWhenThreadPool();
// chain配置同一个thead1
LiteflowResponse<DefaultSlot> response2 = flowExecutor.execute2Resp("chain2", "arg"); LiteflowResponse<DefaultSlot> response2 = flowExecutor.execute2Resp("chain2", "arg");
Assert.assertTrue(response2.isSuccess()); Assert.assertTrue(response2.isSuccess());
Assert.assertTrue(response2.getSlot().getData("threadName").toString().startsWith("customer-when-2-thead")); Assert.assertTrue(response2.getSlot().getData("threadName").toString().startsWith("customer-when-1-thead"));
// 使用build模式构建chain测试when条件的多线程
LiteFlowNodeBuilder.createNode().setId("a")
.setName("组件A")
.setType(NodeTypeEnum.COMMON)
.setClazz("com.yomahub.liteflow.test.customWhenThreadPool.cmp.ACmp")
.build();
LiteFlowNodeBuilder.createNode().setId("b")
.setName("组件B")
.setType(NodeTypeEnum.COMMON)
.setClazz("com.yomahub.liteflow.test.customWhenThreadPool.cmp.BCmp")
.build();
LiteFlowNodeBuilder.createNode().setId("c")
.setName("组件C")
.setType(NodeTypeEnum.COMMON)
.setClazz("com.yomahub.liteflow.test.customWhenThreadPool.cmp.CCmp")
.build();
LiteFlowChainBuilder.createChain().setChainName("chain3").setCondition(
LiteFlowConditionBuilder
.createWhenCondition()
.setThreadExecutorClass(CustomThreadExecutor3.class.getName())
.setValue("a,b,c,d")
.build()
).build();
LiteflowResponse<DefaultSlot> response3 = flowExecutor.execute2Resp("chain3", "arg");
Assert.assertTrue(response3.isSuccess());
Assert.assertTrue(response3.getSlot().getData("threadName").toString().startsWith("customer-when-3-thead"));
} }
} }

View File

@@ -7,6 +7,6 @@
<when value="c,d" threadExecutorClass="com.yomahub.liteflow.test.customWhenThreadPool.CustomThreadExecutor1"/> <when value="c,d" threadExecutorClass="com.yomahub.liteflow.test.customWhenThreadPool.CustomThreadExecutor1"/>
</chain> </chain>
<chain name="chain2"> <chain name="chain2">
<when value="e,f" threadExecutorClass="com.yomahub.liteflow.test.customWhenThreadPool.CustomThreadExecutor2"/> <when value="e,f" threadExecutorClass="com.yomahub.liteflow.test.customWhenThreadPool.CustomThreadExecutor1"/>
</chain> </chain>
</flow> </flow>