Back to Repositories

Testing ExecutorBiz Operations Implementation in xxl-job

This test suite validates the core functionality of the ExecutorBiz component in XXL-JOB, focusing on job execution control and monitoring capabilities. The tests verify essential executor operations including heartbeat mechanisms, job execution, termination, and logging functionality.

Test Coverage Overview

The test suite provides comprehensive coverage of ExecutorBiz operations:
  • Heartbeat monitoring through beat() and idleBeat() tests
  • Job execution lifecycle management with run() and kill() operations
  • Log retrieval and monitoring capabilities
  • Error handling and response validation
Integration points include HTTP client communication and executor state management.

Implementation Analysis

The testing approach utilizes JUnit Jupiter for structured unit testing, implementing a client-server interaction model. Each test follows a clear arrange-act-assert pattern, with ExecutorBizClient serving as the test subject. The implementation leverages framework-specific features like @Test annotations and Assertions class for validation.

Technical Details

Testing tools and configuration:
  • JUnit Jupiter test framework
  • ExecutorBizClient for API interaction
  • Local test endpoint (http://127.0.0.1:9999/)
  • Configurable timeout and access token parameters
  • ReturnT wrapper class for response handling

Best Practices Demonstrated

The test suite exemplifies strong testing practices through isolated test methods, comprehensive assertion checks, and clear test organization. Each test method focuses on a specific executor operation with proper setup and verification. The code demonstrates proper error handling validation and response code checking, ensuring robust executor behavior.

xuxueli/xxl-job

xxl-job-admin/src/test/java/com/xxl/job/executorbiz/ExecutorBizTest.java

            
package com.xxl.job.executorbiz;

import com.xxl.job.core.biz.ExecutorBiz;
import com.xxl.job.core.biz.client.ExecutorBizClient;
import com.xxl.job.core.biz.model.*;
import com.xxl.job.core.enums.ExecutorBlockStrategyEnum;
import com.xxl.job.core.glue.GlueTypeEnum;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
 * executor api test
 *
 * Created by xuxueli on 17/5/12.
 */
public class ExecutorBizTest {

    // admin-client
    private static String addressUrl = "http://127.0.0.1:9999/";
    private static String accessToken = null;
    private static int timeout = 3;

    @Test
    public void beat() throws Exception {
        ExecutorBiz executorBiz = new ExecutorBizClient(addressUrl, accessToken, timeout);
        // Act
        final ReturnT<String> retval = executorBiz.beat();

        // Assert result
        Assertions.assertNotNull(retval);
        Assertions.assertNull(((ReturnT<String>) retval).getContent());
        Assertions.assertEquals(200, retval.getCode());
        Assertions.assertNull(retval.getMsg());
    }

    @Test
    public void idleBeat(){
        ExecutorBiz executorBiz = new ExecutorBizClient(addressUrl, accessToken, timeout);

        final int jobId = 0;

        // Act
        final ReturnT<String> retval = executorBiz.idleBeat(new IdleBeatParam(jobId));

        // Assert result
        Assertions.assertNotNull(retval);
        Assertions.assertNull(((ReturnT<String>) retval).getContent());
        Assertions.assertEquals(500, retval.getCode());
        Assertions.assertEquals("job thread is running or has trigger queue.", retval.getMsg());
    }

    @Test
    public void run(){
        ExecutorBiz executorBiz = new ExecutorBizClient(addressUrl, accessToken, timeout);

        // trigger data
        final TriggerParam triggerParam = new TriggerParam();
        triggerParam.setJobId(1);
        triggerParam.setExecutorHandler("demoJobHandler");
        triggerParam.setExecutorParams(null);
        triggerParam.setExecutorBlockStrategy(ExecutorBlockStrategyEnum.COVER_EARLY.name());
        triggerParam.setGlueType(GlueTypeEnum.BEAN.name());
        triggerParam.setGlueSource(null);
        triggerParam.setGlueUpdatetime(System.currentTimeMillis());
        triggerParam.setLogId(1);
        triggerParam.setLogDateTime(System.currentTimeMillis());

        // Act
        final ReturnT<String> retval = executorBiz.run(triggerParam);

        // Assert result
        Assertions.assertNotNull(retval);
    }

    @Test
    public void kill(){
        ExecutorBiz executorBiz = new ExecutorBizClient(addressUrl, accessToken, timeout);

        final int jobId = 0;

        // Act
        final ReturnT<String> retval = executorBiz.kill(new KillParam(jobId));

        // Assert result
        Assertions.assertNotNull(retval);
        Assertions.assertNull(((ReturnT<String>) retval).getContent());
        Assertions.assertEquals(200, retval.getCode());
        Assertions.assertNull(retval.getMsg());
    }

    @Test
    public void log(){
        ExecutorBiz executorBiz = new ExecutorBizClient(addressUrl, accessToken, timeout);

        final long logDateTim = 0L;
        final long logId = 0;
        final int fromLineNum = 0;

        // Act
        final ReturnT<LogResult> retval = executorBiz.log(new LogParam(logDateTim, logId, fromLineNum));

        // Assert result
        Assertions.assertNotNull(retval);
    }

}