Back to Repositories

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

The test suite provides comprehensive coverage of task execution patterns in both async and sync modes.

Key areas tested include:
  • Asynchronous task execution with Future return types
  • Synchronous sequential task execution
  • Performance timing measurements
  • Thread blocking behavior with Future.get()

Implementation Analysis

The testing approach utilizes Spring Boot’s testing framework with JUnit to validate task execution behaviors. The implementation leverages @Autowired for dependency injection and employs Future objects for async task management. The code demonstrates proper exception handling for InterruptedException and ExecutionException.

Technical Details

Testing tools and configuration:
  • JUnit test framework
  • Spring Boot test annotations
  • Lombok @Slf4j for logging
  • TaskFactory autowired component
  • System.currentTimeMillis() for performance measurement

Best Practices Demonstrated

The test suite exemplifies several testing best practices including proper separation of async and sync test methods, clear performance measurement patterns, and appropriate error handling. The code organization follows clean testing principles with well-documented test methods and clear assertions of expected behaviors.

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));
    }
}