Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修改测试用例显示创建线程, 改为使用线程池来创建 (#194) #200

Merged
merged 1 commit into from
Apr 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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