Skip to content

Commit

Permalink
Merge pull request #200 from zhuanghaozhe/develop
Browse files Browse the repository at this point in the history
修改测试用例显示创建线程, 改为使用线程池来创建 (#194)
  • Loading branch information
magestacks authored Apr 24, 2022
2 parents 65d39ca + ae59658 commit f811240
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import javax.annotation.Resource;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import static cn.hippo4j.common.constant.Constants.EXECUTE_TIMEOUT_TRACE;

Expand All @@ -29,6 +31,20 @@ public class RunStateHandlerTest {
@Resource
private ThreadPoolExecutor messageProduceDynamicThreadPool;

private final ThreadPoolExecutor runStateHandlerTestExecutor = new ThreadPoolExecutor(
2,
2,
0L,
TimeUnit.MILLISECONDS,
new SynchronousQueue<>(),
r -> {
Thread t = new Thread(r);
t.setName("client.example.runStateHandler.test");
t.setDaemon(true);
return t;
},
new ThreadPoolExecutor.AbortPolicy());

@PostConstruct
@SuppressWarnings("all")
public void runStateHandlerTest() {
Expand All @@ -43,7 +59,7 @@ public void runStateHandlerTest() {

private void runTask(ExecutorService executorService) {
// 模拟任务运行
new Thread(() -> {
runStateHandlerTestExecutor.execute(() -> {
/**
* 当线程池任务执行超时, 向 MDC 放入 Trace 标识, 报警时打印出来.
*/
Expand Down Expand Up @@ -73,7 +89,7 @@ private void runTask(ExecutorService executorService) {
ThreadUtil.sleep(500);
}

}).start();
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import org.springframework.core.task.TaskDecorator;
import org.springframework.stereotype.Component;

import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
* TaskDecorator test.
Expand All @@ -23,12 +25,26 @@ public class TaskDecoratorTest {

public static final String PLACEHOLDER = "site";

private final ThreadPoolExecutor taskDecoratorTestExecutor = new ThreadPoolExecutor(
1,
1,
0L,
TimeUnit.MILLISECONDS,
new SynchronousQueue<>(),
r -> {
Thread t = new Thread(r);
t.setName("client.example.taskDecorator.test");
t.setDaemon(true);
return t;
},
new ThreadPoolExecutor.AbortPolicy());

/**
* 测试动态线程池传递 {@link TaskDecorator}
* 如果需要运行此单测, 方法上添加 @PostConstruct
*/
public void taskDecoratorTest() {
new Thread(() -> {
taskDecoratorTestExecutor.execute(() -> {
MDC.put(PLACEHOLDER, "查看官网: https://www.hippox.cn");
ThreadUtil.sleep(5000);
DynamicThreadPoolWrapper poolWrapper = GlobalThreadPoolManage.getExecutorService(GlobalTestConstant.MESSAGE_PRODUCE);
Expand All @@ -40,7 +56,7 @@ public void taskDecoratorTest() {
*/
log.info("通过 taskDecorator MDC 传递上下文 :: {}", MDC.get(PLACEHOLDER));
});
}).start();
});

}

Expand Down

0 comments on commit f811240

Please sign in to comment.