Back to Repositories

Testing XxlJobRegistry DAO Operations in xxl-job

This test suite evaluates the XxlJobRegistry data access functionality in the xxl-job scheduler system. It focuses on registry operations including save, update, and cleanup of job registry entries, with specific attention to concurrent access scenarios.

Test Coverage Overview

The test suite covers core registry operations with two main test cases:
  • Basic registry operations including save/update and retrieval
  • Concurrent access testing with 100 simultaneous registry operations
  • Dead entry removal functionality
  • Data persistence verification

Implementation Analysis

The testing approach utilizes Spring Boot’s test framework with JUnit Jupiter. It implements both single-threaded and multi-threaded test scenarios to validate registry operations under different conditions. The tests use @SpringBootTest for full context loading and dependency injection of the DAO layer.

Technical Details

Testing infrastructure includes:
  • Spring Boot Test framework with RANDOM_PORT configuration
  • JUnit Jupiter test runner
  • Resource injection for DAO access
  • Thread management for concurrent testing
  • Date utilities for timestamp handling

Best Practices Demonstrated

The test suite demonstrates several testing best practices:
  • Isolation of database operations
  • Concurrent access testing
  • Resource cleanup verification
  • Clear test method organization
  • Proper dependency injection usage

xuxueli/xxl-job

xxl-job-admin/src/test/java/com/xxl/job/admin/dao/XxlJobRegistryDaoTest.java

            
package com.xxl.job.admin.dao;

import com.xxl.job.admin.core.model.XxlJobRegistry;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class XxlJobRegistryDaoTest {

    @Resource
    private XxlJobRegistryDao xxlJobRegistryDao;

    @Test
    public void test(){
        int ret = xxlJobRegistryDao.registrySaveOrUpdate("g1", "k1", "v1", new Date());
        /*int ret = xxlJobRegistryDao.registryUpdate("g1", "k1", "v1", new Date());
        if (ret < 1) {
            ret = xxlJobRegistryDao.registrySave("g1", "k1", "v1", new Date());
        }*/

        List<XxlJobRegistry> list = xxlJobRegistryDao.findAll(1, new Date());

        int ret2 = xxlJobRegistryDao.removeDead(Arrays.asList(1));
    }

    @Test
    public void test2() throws InterruptedException {
        for (int i = 0; i < 100; i++) {
            new Thread(()->{
                int ret = xxlJobRegistryDao.registrySaveOrUpdate("g1", "k1", "v1", new Date());
                System.out.println(ret);

                /*int ret = xxlJobRegistryDao.registryUpdate("g1", "k1", "v1", new Date());
                if (ret < 1) {
                    ret = xxlJobRegistryDao.registrySave("g1", "k1", "v1", new Date());
                }*/
            }).start();
        }

        TimeUnit.SECONDS.sleep(10);
    }

}