Testing Async Task Execution Patterns in spring-boot-demo
This test suite evaluates the performance and functionality of asynchronous and synchronous task execution in a Spring Boot application. It compares execution times and validates the proper handling of concurrent operations using Spring’s async capabilities.
Test Coverage Overview
Implementation Analysis
Technical Details
Best Practices Demonstrated
xkcoding/spring-boot-demo
demo-async/src/test/java/com/xkcoding/async/task/TaskFactoryTest.java
package com.xkcoding.async.task;
import com.xkcoding.async.SpringBootDemoAsyncApplicationTests;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
/**
* <p>
* 测试任务
* </p>
*
* @author yangkai.shen
* @date Created in 2018-12-29 10:49
*/
@Slf4j
public class TaskFactoryTest extends SpringBootDemoAsyncApplicationTests {
@Autowired
private TaskFactory task;
/**
* 测试异步任务
*/
@Test
public void asyncTaskTest() throws InterruptedException, ExecutionException {
long start = System.currentTimeMillis();
Future<Boolean> asyncTask1 = task.asyncTask1();
Future<Boolean> asyncTask2 = task.asyncTask2();
Future<Boolean> asyncTask3 = task.asyncTask3();
// 调用 get() 阻塞主线程
asyncTask1.get();
asyncTask2.get();
asyncTask3.get();
long end = System.currentTimeMillis();
log.info("异步任务全部执行结束,总耗时:{} 毫秒", (end - start));
}
/**
* 测试同步任务
*/
@Test
public void taskTest() throws InterruptedException {
long start = System.currentTimeMillis();
task.task1();
task.task2();
task.task3();
long end = System.currentTimeMillis();
log.info("同步任务全部执行结束,总耗时:{} 毫秒", (end - start));
}
}