Timed task testing method and computer device

By using a time interception library and virtual time context technology in the process under test, the problem of low efficiency in scheduled task testing is solved, enabling fast, non-intrusive automated testing of scheduled tasks and supporting multi-task concurrency.

CN123138121APending Publication Date: 2026-11-20BEIJING JIZHI DIGITAL TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202611381731.4
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-09-07
Publication Date
2026-11-20

AI Technical Summary

Technical Problem

Existing timed task testing methods suffer from problems such as long test execution time, low test efficiency, high intrusion into the runtime environment, and difficulty in supporting the concurrent execution of multiple test tasks.

Method used

By intercepting time calls through a time interception library running in the process under test, a virtual time context is created. The virtual time is determined based on the real time and time offset. The timing expression of the timed task is parsed, the virtual time is instantly adjusted to the trigger time of the timed task, and the task is triggered. The execution result is then obtained to determine the test result.

Benefits of technology

It significantly shortens the test duration of scheduled tasks, improves the efficiency of automated testing, and supports the concurrent execution of multiple test tasks, avoiding intrusive impacts on the runtime environment.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN123138121A_ABST
    Figure CN123138121A_ABST
Patent Text Reader

Abstract

The embodiment of the present specification provides a timing task test method and computer equipment, and relates to the technical field of task test. The method comprises the following steps: in a to-be-tested process, a time call of a current time is intercepted; in response to a test request for the to-be-tested process, a virtual time context including a time offset is created; when a thread initiating the time call is associated with the virtual time context, a virtual time is determined according to a real time and the time offset in the virtual time context, and the virtual time is returned to the to-be-tested process as a return result of the time call; a timing expression of a timing task in the to-be-tested process is acquired, and a next triggering moment is obtained by parsing; in response to a time control instruction, the virtual time is adjusted to an earliest triggering moment among the next triggering moments of the timing tasks, and the timing task is triggered; an execution result of the triggered timing task is acquired, and a test result is determined. In this way, the automation test efficiency of the timing task can be improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The embodiments described in this specification relate to the field of task testing technology, specifically to a timed task testing method and a computer device. Background Technology

[0002] As software systems continue to grow in scale, various business systems widely rely on scheduled tasks to carry out periodic business logic. For example, accounting systems typically generate monthly invoices at midnight on a fixed date each month, transaction systems typically execute timeout cancellations after a fixed period following order creation, and messaging systems typically clean up expired data according to preset cycles. The time span of these scheduled tasks often extends to several hours, days, or even months. How to efficiently automate the testing of scheduled tasks is a problem that needs to be addressed in the software quality assurance process.

[0003] In related technologies, testing scheduled tasks typically employs several methods: hard waiting, modifying the operating system time, and replacing the time function at the application layer. Hard waiting involves inserting a wait instruction into the test script before triggering the verification logic. For example, setting a 30-minute wait time in the script, and then querying the execution result of the scheduled task after the actual trigger time arrives. In this method, the test execution time is directly proportional to the time span of the scheduled task. For long-term scheduled tasks spanning months or years, a single test requires waiting for an extremely long real time, resulting in low testing efficiency and difficulty meeting the needs of high-frequency integration testing. Modifying the operating system time involves directly modifying the server's global time through system commands or system-provided time setting interfaces, causing the entire machine's time to jump to the trigger time of the scheduled task. However, the operating system time is globally unique; modifying the global time will affect all processes running on the same machine, causing problems such as incorrect log timestamps, database connection timeouts, and digital certificate verification failures. Furthermore, modifying the system time usually requires high system privileges, making it difficult to implement in containerized or restricted operating environments. In the testing method of replacing time functions at the application layer, the intrusion is relatively strong; and the replaced time function only takes effect within the single process where the application is located, which cannot cover the time perception of external components such as databases and message queues, and is difficult to apply to time-dependent scenarios across processes and services.

[0004] In summary, the scheduled task testing method in related technologies has problems such as long test execution time, low test efficiency, high intrusion into the operating environment, and difficulty in supporting the concurrent execution of multiple test tasks. Summary of the Invention

[0005] In view of this, various embodiments of this specification aim to provide a method and computer device for testing scheduled tasks, so as to at least improve the efficiency of automated testing of scheduled tasks.

[0006] This specification provides a method for testing scheduled tasks. The method includes: intercepting a time call initiated by the process under test to obtain the current time using a time interception library running in the process under test; creating a virtual time context corresponding to the test request in response to a test request for the process under test, the virtual time context including a time offset; when the thread initiating the time call is associated with the virtual time context, determining a virtual time based on the obtained real time and the time offset in the associated virtual time context, and returning the virtual time as the return result of the time call to the process under test; obtaining the timing configuration information of the scheduled tasks registered in the process under test, the timing configuration information including the timing expression of the scheduled task; parsing the timing expression to obtain the next trigger time of the scheduled task; adjusting the virtual time to the earliest trigger time among the next trigger times of each scheduled task in response to a time control command, and triggering the scheduled task corresponding to the earliest trigger time; obtaining the execution result of the triggered scheduled task, and determining the test result of the scheduled task based on the execution result.

[0007] This specification provides a computer device including a memory and a processor. The memory stores a computer program, and the processor executes the computer program to implement the timed task testing method described in any of the above embodiments.

[0008] In the various implementations provided in this specification, firstly, a time interception library running in the process under test intercepts time calls initiated by the process under test to obtain the current time. Next, in response to a test request for the process under test, a virtual time context corresponding to the test request is created, including a time offset. Then, when the thread initiating the time call is associated with a virtual time context, the virtual time is determined based on the obtained real time and the time offset in the associated virtual time context, and this virtual time is returned to the process under test as the return result of the time call. Next, the timing configuration information of the registered scheduled tasks in the process under test is obtained, including the timing expressions of the scheduled tasks. Then, the timing expressions are parsed to obtain the next trigger time of the scheduled tasks. Next, in response to a time control command, the virtual time is adjusted to the earliest trigger time among the next trigger times of each scheduled task, and the scheduled task corresponding to the earliest trigger time is triggered. Finally, the execution result of the triggered scheduled task is obtained, and the test result of the scheduled task is determined based on the execution result. This improves the efficiency of automated testing of scheduled tasks. Attached Figure Description

[0009] Figure 1This is a schematic diagram of the timed task testing system provided in the embodiments of this specification; Figure 2 This is a flowchart illustrating the timed task testing method provided in the embodiments of this specification; Figure 3 A schematic diagram of a computer device provided for an embodiment of this specification. Detailed Implementation

[0010] To enable those skilled in the art to better understand the solutions described in this specification, the technical solutions in the embodiments of this specification will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only a part of the embodiments of this specification, and not all of them. Based on the embodiments in this specification, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this specification.

[0011] This specification provides an example application scenario for a scheduled task testing method. This application scenario can be a testing system or a scheduled task testing system. Figure 1 A schematic diagram of the test system used in the timed task testing method provided in the embodiments of this specification is shown. Figure 1 As shown, the test system may include a test control terminal 110, a device under test 120, and downstream services 130 and a database 140 that are communicatively connected to the device under test 120.

[0012] Test control terminal 110 is a computer device that runs test scripts. Test control terminal 110 can be a desktop computer, laptop computer, physical machine or virtual machine in a server cluster, etc. Test control terminal 110 is used to initiate test requests for the process under test to device under test 120, send time control instructions to device under test 120 during the test, and receive the execution results of the timed task returned by device under test 120, and determine the test result of the timed task based on the execution results.

[0013] The device under test (DUT) 120 is a computer device that hosts the process under test. The process under test refers to a process running on the DUT 120 that has registered at least one timed task. The DUT 120 runs a time interception library 122, which operates within the process space corresponding to the process under test and is used to intercept time calls initiated by the process under test to obtain the current time. The DUT 120 also maintains a virtual time context 126, which records time offsets. The virtual time context 126 is associated with the thread that initiates the time call. The DUT 120 can be a desktop computer, server, container, or virtual machine, etc. The operating system of the DUT 120 can be Linux, Unix-like operating systems, or other operating systems that support dynamic linking or bytecode enhancement mechanisms; this embodiment does not limit this.

[0014] Downstream service 130 is a service process located downstream in the call chain of the process under test (DUT) in the microservice architecture. When the DUT calls downstream service 130 through a service call request, the DUT can send the time offset from the virtual time context 126 in the request header of the service call request to downstream service 130, so that downstream service 130 processes the service call request according to the virtual time determined by the time offset. Database 140 is used to store business data generated after the execution of scheduled tasks, such as monthly billing data and order status data. The device under test (DUT) 120 or the test control terminal 110 can obtain the execution results of the scheduled tasks from database 140.

[0015] The device under test (DUT) 120 includes a processor, memory, network interface, and system clock connected via a system bus. The memory includes non-volatile storage media and internal memory. The non-volatile storage media stores the operating system, program files for the process under test (DUT), and program files for the time interception library 122. The internal memory contains process instances of the DUT, and timed tasks 124 are registered in the process space of the DUT. The processor executes the computer programs in memory, and the system clock provides real-time information to the DUT 120. It is understood that the test control terminal 110 may also have a similar or identical hardware structure to the DUT 120.

[0016] For ease of understanding, the terminology used in the various embodiments of this invention will be explained uniformly.

[0017] Scheduled tasks are business tasks that are automatically executed according to a pre-configured trigger time or trigger period. Scheduled tasks can be registered and managed by various scheduling frameworks, such as the Quartz and SpringTask frameworks in the Java ecosystem, the crontab tool at the operating system level, and delayed queues provided by message brokers. Scheduled tasks are registered in the memory of the process under test as scheduled task objects, such as ScheduledFuture objects and CronTrigger objects.

[0018] A timing expression is an expression that describes the trigger time or trigger period of a scheduled task. The timing configuration information includes the timing expression for the scheduled task. For example, a Cron expression uses several fields to describe the value rules for time dimensions such as seconds, minutes, hours, days, months, and weeks, such as the expression "0021". The question mark indicates that it will be triggered at 2:00 AM on the 1st of each month.

[0019] A time call can refer to a call initiated by the process under test to retrieve the current time. Examples include system calls like gettimeofday(), clock_gettime(), and time() in Linux, as well as similar time-retrieval interfaces in other operating systems. A sleep call is a call initiated by the process under test to request sleep for a specified duration, such as sleep() or nanosleep().

[0020] Real time refers to the physical time indicated by the operating system's system clock. Virtual time refers to the time calculated based on real time and time offset, used by the process under test during the test. Time offset is the difference between virtual time and real time. Time flow rate multiplier indicates the rate at which virtual time increases as real time passes.

[0021] Time control commands are instructions sent by the test control terminal 110 to the device under test 120 during the test process to control the virtual time. Time control commands include time advance commands, time setting commands, and event jump commands. The time advance command instructs the virtual time to be advanced by a specified duration, the time setting command instructs the virtual time to be set to a specified moment, and the event jump command instructs the virtual time to be adjusted to the earliest trigger moment among the next trigger moments of each timed task. Time control commands can be transmitted through inter-process communication (IPC) channels, such as shared memory, message queues, and Unix domain sockets; this embodiment does not limit the specific IPC channels used.

[0022] This specification provides a method for testing timed tasks. Please refer to [link / reference].Figure 2 , Figure 2 This is a flowchart illustrating a scheduled task testing method provided in this specification. This embodiment provides the method operation steps as shown in the flowchart, but based on conventional or non-creative labor, more or fewer operation steps may be included. The order of steps listed in the embodiment is merely one possible execution order among many, and does not represent the only possible execution order. In actual system or server product execution, the method can be executed sequentially as shown in the embodiment or in parallel (e.g., in a parallel processor or multi-threaded processing environment). This scheduled task testing method can be applied to the device under test in a testing system, specifically as follows... Figure 2 As shown, the timed task testing method may include the following steps S102-S114.

[0023] Step S102: By using a time interception library running in the process under test, intercept the time call initiated by the process under test to obtain the current time.

[0024] The time interception library is a program library pre-configured in the device under test (DUT) 120. The time interception library can be loaded into the process space corresponding to the process under test before the process under test starts, or it can be injected into the process under test during its execution. The specific loading method will be described in detail in subsequent embodiments. After the time interception library runs in the process space of the process under test, time calls initiated by the process under test no longer directly reach the operating system, but first pass through the time interception library. The time interception library determines what time to return to the process under test, thereby achieving non-intrusive control of the time awareness of the process under test without modifying its business logic code.

[0025] Step S104: In response to a test request for the process under test, create a virtual time context corresponding to the test request. The virtual time context includes a time offset.

[0026] When starting a test, the test control terminal 110 sends a test request to the device under test 120 for the process under test. In response to the test request, the device under test 120 creates a virtual time context corresponding to the test request. The virtual time context carries the time base information for this test, recording the time offset. In one embodiment, the virtual time context also records the initial real time and the initial virtual time for this test. For example, when the virtual time context is created, the current real time T0 and the initial virtual time V0 are recorded, and the time offset is the difference between the initial virtual time V0 and the initial real time T0. The initial virtual time can be specified by the test request or can default to being equal to the initial real time; this embodiment does not limit this.

[0027] An association is established between the virtual time context and the thread that initiates the time call. In one embodiment, the virtual time context is stored as a thread-local variable (ThreadLocal), and the virtual time context is associated with the current thread when the current thread processes the test request. In another embodiment, a test flag can be set in the thread context to indicate that the current thread is in the virtual time context. Through the thread-level association mechanism, different threads within the same process under test can be associated with different virtual time contexts, thereby supporting the concurrent execution of multiple test requests, which will be described in detail in subsequent embodiments.

[0028] Step S106: When the thread that initiates the time call is associated with a virtual time context, the virtual time is determined based on the obtained real time and the time offset in the associated virtual time context, and the virtual time is returned to the process under test as the return result of the time call.

[0029] After intercepting a time call, the time interception library first obtains the real time through the operating system's raw time interface, such as calling the operating system's `gettimeofday()` function to get the current physical time. Then, the time interception library checks whether the thread initiating the time call is associated with a virtual time context. If the thread is associated with a virtual time context, it adds the obtained real time to the time offset in the virtual time context to obtain the virtual time, and returns this virtual time as the return result of the time call to the process under test. Thus, the time perceived by the thread during the test is the virtual time adjusted for the time offset, rather than the real time.

[0030] When the thread initiating the time call is not associated with a virtual time context, the handling method of the time interception library will be described in subsequent embodiments.

[0031] Step S108: Obtain the timing configuration information of the timed tasks registered in the process under test. The timing configuration information includes the timing expression of the timed tasks.

[0032] The process under test has at least one timed task registered in its memory. Each timed task has corresponding timed configuration information, which includes the timed expression of the timed task. The device under test 120 can obtain the timed configuration information by scanning the registered timed task objects in the memory of the process under test, or by reading the configuration file of the timed task framework. This embodiment does not limit the method of obtaining the timed configuration information, as long as the timed expression of the timed task in the process under test can be obtained.

[0033] Step S110: Parse the timing expression to obtain the next trigger time of the timed task.

[0034] A timer expression describes the trigger time or trigger period of a timed task. By parsing the timer expression, the next trigger time of the timed task after the current time can be calculated. For example, the timer expression for a timed task is: 0021 If the current virtual time is the 20th of a certain month, then the next trigger time for this scheduled task is calculated to be 2:00 AM on the 1st of the following month. For each registered scheduled task, the next trigger time is calculated in the same way.

[0035] Step S112: In response to the time control command, adjust the virtual time to the earliest trigger time among the next trigger times of each timed task, and trigger the timed task corresponding to the earliest trigger time.

[0036] After completing a test step, the test control terminal 110 sends a time control command to the device under test 120. In response to this command, the device under test 120 determines the earliest trigger time among the next trigger times of each scheduled task and directly adjusts its virtual time to that earliest trigger time. Because the virtual time is instantly adjusted to the trigger time of the scheduled task, the scheduled task at that trigger time is executed. In this way, the test process does not need to wait for real time to elapse to the trigger time; instead, it instantly reaches the business trigger point of the scheduled task through a jump in virtual time, compressing the wait that would originally take hours or even days into milliseconds.

[0037] Step S114: Obtain the execution result of the triggered scheduled task, and determine the test result of the scheduled task based on the execution result.

[0038] Once the scheduled task is triggered, it will generate corresponding execution results. These results may include, for example, business data written to database 140 after the scheduled task is executed, or status return values ​​and log records. The device under test 120 or the test control terminal 110 obtains these results and determines the test result of the scheduled task based on them. In one embodiment, the execution result is compared with the expected result; if the execution result matches the expected result, the scheduled task test is considered passed.

[0039] In the above embodiments, by intercepting time calls using a time interception library running in the process under test, virtual time is returned at the granularity of the virtual time context associated with the thread, and the next trigger time is parsed based on the timer expression of the timer task registered in the process under test. The virtual time is then jump to the earliest trigger time and the timer task is triggered to execute. The test result is determined based on the execution result. On the one hand, the time perception of the process under test can be controlled without modifying the business code of the process under test. On the other hand, the time jump to the business trigger point of the timer task can be automatically performed without manually calculating the time span, avoiding real time waiting, significantly shortening the test duration of the timer task, and improving the efficiency of automated testing of the timer task.

[0040] Regarding the loading method of the time interception library, in one embodiment, before the process under test starts, the time interception library can be loaded into the process space corresponding to the process under test through the preloading mechanism of the dynamic linker. Taking the Linux operating system as an example, the dynamic linker supports specifying preloaded shared libraries through environment variables. When the process under test starts, this environment variable is set and points to the time interception library. The dynamic linker will load the time interception library first. The time functions with the same name defined in the time interception library will override the time functions in the standard library. Subsequent time calls initiated by the process under test will enter the processing logic of the time interception library. In one embodiment, the time interception library can be preloaded through the environment variable LD_PRELOAD; in other operating systems, a dynamic library insertion mechanism with equivalent functionality can also be used, and this embodiment does not limit this.

[0041] In another embodiment, during the execution of the process under test, the time interception library can be injected into the process under test through bytecode enhancement. For example, for a process under test running on a Java Virtual Machine, the time interception library can be attached to the process under test as an agent during runtime, enhancing the bytecode for obtaining time in the process under test, so that the calls for obtaining time are redirected to the time interception library.

[0042] After intercepting a time call, the time interception library can perform the following processing: First, it calls the operating system's original time function to obtain the real time. During the initialization phase, the time interception library records the entry address of the real time function in the operating system through dynamic symbol lookup and other methods. After intercepting a time call, it first calls the original time function through this entry address to obtain the current real time. Then, it obtains the virtual time context associated with the thread that initiated the time call. When the thread is associated with a virtual time context, it adds the real time to the time offset in the virtual time context to obtain the virtual time, and returns the virtual time as the return result of the time call to the process under test. Taking the gettimeofday() call as an example, the interception function first calls the real gettimeofday() to obtain the real time, then adds the second field of the real time to the time offset by the corresponding number of seconds, and returns the adjusted time as the result.

[0043] In one embodiment, the virtual time context also records a time flow rate multiplier. The virtual time is determined based on the real time, time offset, and time flow rate multiplier. The rate at which the virtual time increases with the passage of real time is related to the time flow rate multiplier. Specifically, if the initial real time at the time of virtual time context creation is T0, the initial virtual time is V0, and the time flow rate multiplier is K, then after a real time of duration t, the virtual time is V0 + t × K. When the time flow rate multiplier K is greater than 1, the virtual time passes faster than the real time. For example, when K is 3600, for every second of real time, 3600 seconds of virtual time have passed, which is equivalent to compressing one hour of business waiting into one second. When K equals 1, the virtual time passes at the same speed as the real time, only maintaining a fixed offset. The time flow rate multiplier can be specified by the test request or adjusted by the time control command during the test; this embodiment does not limit this.

[0044] When the thread initiating the time call is not associated with a virtual time context, the time interception library directly returns the obtained real time as the result of the time call to the process under test. In other words, threads within the process under test that are not involved in the test (such as log reporting threads, monitoring and data collection threads, and other infrastructure threads) still perceive the real time. Infrastructure such as log timestamps, monitoring data, and certificate verification are not affected by virtual time, ensuring the non-intrusiveness of the test. The process under test can handle non-test traffic normally while carrying test traffic.

[0045] In one embodiment, the time interception library can also intercept sleep calls initiated by the process under test. When the thread initiating the sleep call is associated with a virtual time context, the sleep duration requested by the sleep call is shortened according to the time flow rate multiplier, and the sleep is executed according to the shortened sleep duration. For example, if the sleep call requests a sleep of 3600 seconds and the time flow rate multiplier is 3600, the sleep duration is shortened to 1 second before the actual sleep is executed. The process under test is awakened after 1 second in real time, while it is equivalent to sleeping for 3600 seconds in the virtual time dimension. In another embodiment, when the thread initiating the sleep call is associated with a virtual time context, the sleep completion result can also be directly returned to the process under test, realizing instant wake-up from sleep and further eliminating real waiting during the test. In addition, in one embodiment, the time interception library can also intercept timer-related system calls (such as timer_settime, timer_gettime, and timerfd-related calls), and adjust the timer expiration time according to the time offset or time flow rate multiplier to cover timed tasks implemented through kernel timers.

[0046] In the above embodiments, the time interception library is loaded by preloading or bytecode enhancement, the real time is passed through to threads that are not associated with virtual time context, and the sleep duration of sleep calls is shortened according to the time flow rate multiple. While achieving isolation between test traffic and non-test traffic, unnecessary real waiting during the test process is eliminated.

[0047] Regarding the acquisition of timing configuration information, in one embodiment, acquiring the timing configuration information of the timing tasks registered in the process under test may include the following steps S202-204.

[0048] Step S202: Scan the memory of the process under test for registered timed task objects.

[0049] Step S204: Read the timing expression of the timer task object from the scanned timer task object.

[0050] Scanning can be implemented using reflection. For example, in a Java-based process under test, reflection can be used to obtain all registered ScheduledFuture and / or CronTrigger objects. Scanning can also be achieved using a probe mechanism, where the probe records the scheduled task objects when the scheduled task framework registers them. By scanning the memory of the process under test, all scheduled tasks actually in effect at runtime can be obtained, avoiding omissions caused by inconsistencies between configuration files and runtime status.

[0051] Regarding the parsing of timing expressions, in one embodiment, parsing the timing expression to obtain the next trigger time of the timing task may include the following steps S302-304.

[0052] Step S302: Parse the timing expression and generate an abstract syntax tree.

[0053] Step S304: Calculate the next trigger time of the scheduled task based on the abstract syntax tree.

[0054] An Abstract Syntax Tree (AST) is a tree-like representation of an expression structure. Taking a Cron expression as an example, a Cron expression consists of multiple fields such as seconds, minutes, hours, days, months, and weeks. Each field can contain syntactic elements such as wildcards, value ranges, step sizes, and enumerations. When parsing a Cron expression, it is broken down into multiple syntactic units according to the fields, constructing an AST. Then, starting from the current virtual time, the AST searches for the minimum time point that satisfies the value rules of each field. This minimum time point is the next trigger time for the scheduled task. For example, if the current virtual time is January 20, 2024, the scheduled task "0021" would trigger on the AST. If the next time point that satisfies the rule "2:00 AM on the 1st of every month" is February 1, 2024, then the next trigger time for this scheduled task will be February 1, 2024, at 2:00:00 AM.

[0055] Regarding the construction of the virtual event queue, in one embodiment, the timed task testing method may further include: constructing each timed task into a virtual event queue according to the time sequence of the next trigger time of each timed task.

[0056] The next trigger time of each scheduled task constitutes an event node on the virtual timeline. Arranging all event nodes in order of their trigger times from earliest to latest yields the virtual event queue.

[0057] Accordingly, adjusting the virtual time to the earliest trigger time among the next trigger times of each scheduled task can include: adjusting the virtual time to the trigger time corresponding to the head event in the virtual event queue, and triggering the scheduled task corresponding to the head event. For example, if the current virtual time is T_now, and the scanned scheduled tasks include task A and task B, with task A's next trigger time being T_A and task B's next trigger time being T_B, and T_A being earlier than T_B, then task A is the head event in the virtual event queue; in response to the time control command, the virtual clock is set to T_A, task A is triggered to execute, and the event node corresponding to task A is removed from the virtual event queue.

[0058] Regarding time control instructions, in one embodiment, the timed task testing method may further include the following steps S402-404.

[0059] Step S402: In response to the time advance command, advance the virtual time by the duration indicated by the time advance command; For example, if a time advance command carries a specified duration of 20 seconds, then the virtual time will be advanced by 20 seconds from the current value.

[0060] Step S404: In response to the time setting command, set the virtual time to the time indicated by the time setting command.

[0061] For example, if the time setting command carries the specified time "02:00:00", the virtual time will be directly set to 02:00:00 of the current day. The time control commands also include event jump commands, which are the aforementioned commands that adjust the virtual time to the earliest trigger time. The test control terminal 110 and the device under test 120 transmit time control commands through an inter-process communication channel. Testers can arrange interactive test processes in the test script according to the rhythm of "execute one test step, send one time control command, and verify one result".

[0062] In the above embodiments, by scanning the timed task objects in the memory of the process under test and parsing the timed expressions to construct a virtual event queue, testers do not need to manually calculate the time span. The system can automatically determine the next business trigger point on the virtual time axis and jump to that trigger point, realizing the improvement from fast forward based on fixed duration to fast forward based on business events.

[0063] The following description uses a specific cross-month billing test scenario. This scenario is used to verify the scheduled task of the accounting system generating monthly bills at 2:00 AM on the 1st of each month. The process of this test scenario may include the following steps S502-510.

[0064] Step S502: Initialization. The test control terminal 110 starts the test script and sends a test request to the device under test 120 for the process under test. The test request specifies the initial virtual time as January 31, 2024, 23:59:50. The device under test 120 creates a virtual time context corresponding to the test request, records the initial real time and the initial virtual time, and the time offset is the difference between the initial virtual time and the initial real time.

[0065] Step S504: Fast forward to the next month. The test control terminal 110 sends a time advance command, indicating an advance of 20 seconds. The device under test 120 advances the virtual time from 23:59:50 on January 31, 2024 to 00:00:10 on February 1, 2024. After crossing midnight, the virtual time reaches the trigger time of all scheduled tasks that are triggered at 00:00:00 every day. The system automatically triggers these scheduled tasks, such as the task of resetting the serial number.

[0066] Step S506: Fast forward to the task point. The test control terminal 110 sends a time setting command, which indicates the time as 02:00:00 of the current day. The device under test 120 instantly sets the virtual time to 02:00:00 on February 1, 2024, triggering the execution of the bill generation task. In another implementation of this scenario, the test control terminal 110 can also directly send an event jump command, which the device under test 120 parses the timing expression "0021" of the bill generation task. The function calculates the next trigger time and automatically jumps to that time, without requiring testers to input the time value.

[0067] Step S508: Verification. Query database 140 to retrieve the business data written to database 140 after the bill generation task was executed, confirming that the bill for January 2024 has been generated and the amount is correct. Specifically, compare the bill business data in database 140 with the expected business data. If the bill business data matches the expected business data, the bill generation task test is considered successful.

[0068] Step S510: Destruction. After the test is completed, delete the virtual time context corresponding to the test request. After the virtual time context is deleted, time calls initiated by the process under test return the real time, and subsequent requests arriving at the process under test resume processing under normal physical time.

[0069] In the aforementioned test scenario of cross-month billing, the test process, which originally required waiting for an entire month, was compressed to be completed in seconds, and the entire test process did not require any modification to the business code of the accounting system.

[0070] In one embodiment, there can be multiple test requests, which are executed concurrently. Different test requests have different time offsets in their corresponding virtual time contexts. Since each test request creates its own virtual time context, and each virtual time context is associated with a thread, threads within the same process under test that process different test requests will perceive different virtual times. For example, the time offset in the virtual time context corresponding to test request A might cause the thread processing test request A to perceive the year 2099, while the time offset in the virtual time context corresponding to test request B is zero or extremely small, causing the thread processing test request B to perceive the current real time. The two test requests run on parallel timelines that do not interfere with each other.

[0071] Thus, based on the above thread-level virtual time isolation mechanism, multiple test cases can be executed concurrently within the same process under test. Compared with modifying the global time of the operating system (global time is unique and cannot be executed concurrently) and replacing the time function at the application layer (which takes effect uniformly across the entire process), this method can significantly improve concurrent testing capabilities and resource utilization.

[0072] In a microservice architecture, the process under test often needs to call downstream service 130 to complete business processing. Downstream service 130 also needs to be aware of time when processing business, such as determining whether the business has timed out or whether the data has expired. If virtual time is only controlled on the side of the process under test, the downstream service 130 may still perceive the real time, which will lead to inconsistencies in time between upstream and downstream processes in the call chain.

[0073] Therefore, in one embodiment, the timed task testing method may further include: when the tested process sends a service call request to the downstream service 130, sending the time offset from the virtual time context in the request header of the service call request to the downstream service 130, so that the downstream service 130 can determine the virtual time used to process the service call request based on the time offset carried in the request header. Specifically, when the tested process initiates a cross-service call, if the time interception library or service call framework detects that the service call request comes from a thread associated with a virtual time context, it injects the time offset into the request header of the service call request (e.g., an HTTP request header or a context field of an RPC call). After receiving the service call request, the downstream service 130 reads the time offset in the request header and initializes its local virtual time context, so that the downstream service 130 uses the same virtual time as the tested process when processing the service call request. This transmission method is similar to the practice of passing the link identifier through the request header in distributed tracing, that is, propagating the time offset as metadata along the call chain at the network protocol layer.

[0074] In one embodiment, the time offset is passed through level by level along the call chain. That is, when the downstream service 130 calls the next downstream service, it also carries the time offset in the request header, so that the entire distributed call chain shares the same virtual time when processing the same test request, thereby supporting automated testing of long-cycle distributed businesses such as distributed delay queues and cross-service timed scheduling.

[0075] This specification provides a timed task testing method applied to a test system including a test control terminal 110 and a device under test 120. The device under test 120 runs the process under test. The timed task testing method may include the following steps S602-608.

[0076] Step S602: The test control terminal 110 sends a time control command to the device under test 120.

[0077] Step S604: In response to a test request for the process under test, the device under test 120 creates a virtual time context corresponding to the test request. The virtual time context includes a time offset. By using a time interception library running in the process under test, it intercepts the time call initiated by the process under test to obtain the current time. When the thread initiating the time call is associated with a virtual time context, it determines the virtual time based on the obtained real time and time offset, and returns the virtual time as the return result of the time call to the process under test. It obtains the timer expression of the timer task registered in the process under test, parses the timer expression to obtain the next trigger time of the timer task, and in response to a time control command, adjusts the virtual time to the earliest trigger time among the next trigger times of each timer task, and triggers the timer task corresponding to the earliest trigger time.

[0078] Step S606: The device under test 120 returns the execution result of the triggered timed task to the test control terminal 110.

[0079] Step S608: The test control terminal 110 determines the test result of the timed task based on the execution result.

[0080] For example, in the timed task test interaction process between the test control terminal 110 and the device under test 120, the test control terminal 110 can alternately execute business operations, send time control instructions and receive verification data according to the arrangement of test cases, so as to realize the interactive test process of "execute one test, span one time, and verify one result".

[0081] This specification provides a timed task testing device, which includes a context creation module, a time interception module, a virtual time determination module, a task parsing module, a time adjustment module, and a test result determination module.

[0082] The context creation module is used to respond to test requests for the process under test by creating a virtual time context corresponding to the test request. The virtual time context includes a time offset. The context creation module can also record the initial real time, initial virtual time, and time flow rate multiplier in the virtual time context, and create separate virtual time contexts for multiple concurrent test requests.

[0083] The time interception module is used to intercept time calls initiated by the process under test to obtain the current time by running a time interception library within the process under test. The time interception module can also intercept sleep calls initiated by the process under test, and can load the time interception library before the process under test starts via the dynamic linker's preloading mechanism, or inject the time interception library during the execution of the process under test via bytecode enhancement.

[0084] The virtual time determination module is used to determine the virtual time based on the obtained real time and the time offset in the associated virtual time context when the thread initiating the time call is associated with a virtual time context. This virtual time is then returned to the process under test as the return result of the time call. The virtual time determination module can also return the obtained real time as the return result when the thread initiating the time call is not associated with a virtual time context. Furthermore, it can determine the virtual time based on the real time, time offset, and time flow rate multiplier, and shorten the sleep duration requested by the sleep call according to the time flow rate multiplier.

[0085] The task parsing module is used to obtain the timing configuration information of the timed tasks registered in the process under test, parse the timing expressions in the timing configuration information, and obtain the next trigger time of the timed tasks. The task parsing module can also scan the registered timed task objects in the memory of the process under test, read the timing expressions in the timed task objects, parse the timing expressions to generate an abstract syntax tree, calculate the next trigger time based on the abstract syntax tree, and construct a virtual event queue according to the time sequence of each next trigger time.

[0086] The time adjustment module, in response to time control commands, adjusts the virtual time to the earliest trigger time among the next trigger times of each scheduled task, and triggers the scheduled task corresponding to that earliest trigger time. The time adjustment module can also advance the virtual time by a specified duration in response to time advance commands, set the virtual time to a specified time in response to time setting commands, and, when it detects that the tested process is sending a service call request to a downstream service, send the time offset in the request header of the service call request to the downstream service.

[0087] The test result determination module is used to obtain the execution result of the triggered scheduled task and determine the test result of the scheduled task based on the execution result. The test result determination module can also compare the business data in the execution result with the expected business data. If the business data matches the expected business data, the scheduled task test is determined to have passed. Additionally, the virtual time context is deleted when the test ends.

[0088] The specific functions and effects of the timed task testing device can be explained by referring to other embodiments in this manual, and will not be repeated here. Each module in the timed task testing device can be implemented entirely or partially through software, hardware, or a combination thereof. Each module can be embedded in the processor of the computer device in hardware form or independent of it, or it can be stored in the memory of the computer device in software form, so that the processor can call and execute the operations corresponding to each module.

[0089] This specification also provides a computer-readable storage medium storing a computer program that, when executed by a computer, implements the timed task testing method in any of the above embodiments.

[0090] This specification also provides a computer program product containing instructions that, when executed by a computer, cause the computer to implement the timed task testing method in any of the above embodiments.

[0091] This specification also provides a computer device, including a memory and a processor, wherein the memory stores a computer program, which, when executed by the processor, implements the timed task testing method in any of the above embodiments.

[0092] In some implementations, please refer to Figure 3 The computer device can be a terminal, and its internal structure diagram can be as follows: Figure 3 As shown. The computer device includes a processor, memory, and communication interface connected via a system bus. The processor provides computing and control capabilities. The memory includes non-volatile storage media and internal memory. The non-volatile storage media stores the operating system and computer programs. The internal memory provides an environment for the operation of the operating system and computer programs stored in the non-volatile storage media. The communication interface is used for wired or wireless communication with external terminals; wireless communication can be achieved through Wi-Fi, carrier networks, NFC (Near Field Communication), or other technologies. When the computer program is executed by the processor, it implements a timed task testing method.

[0093] It is understood that the specific examples in this document are only intended to help those skilled in the art better understand the embodiments described herein, and are not intended to limit the scope of the invention.

[0094] It is understood that in the various embodiments described in this specification, the sequence number of each process does not imply the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments described in this specification.

[0095] It is understood that the various implementation methods described in this specification can be implemented individually or in combination, and the implementation methods in this specification are not limited in this respect.

[0096] Unless otherwise stated, all technical and scientific terms used in the embodiments of this specification have the same meaning as commonly understood by one of ordinary skill in the art. The terminology used in this specification is for the purpose of describing particular embodiments only and is not intended to limit the scope of this specification. The term "and / or" as used in this specification includes any and all combinations of one or more of the associated listed items. The singular forms "a," "the," and "the" as used in the embodiments of this specification and the appended claims are also intended to include the plural forms unless the context clearly indicates otherwise.

[0097] It is understood that the processor in the embodiments of this specification can be an integrated circuit chip with signal processing capabilities. In implementation, each step of the above method embodiments can be completed by integrated logic circuits in the processor's hardware or by instructions in software form. The processor can be a general-purpose processor, a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components. It can implement or execute the methods, steps, and logic block diagrams disclosed in the embodiments of this specification. The general-purpose processor can be a microprocessor or any conventional processor. The steps of the methods disclosed in the embodiments of this specification can be directly implemented by a hardware decoding processor, or by a combination of hardware and software modules in the decoding processor. The software modules can reside in random access memory, flash memory, read-only memory, programmable read-only memory, electrically erasable programmable memory, registers, or other mature storage media in the art. This storage medium is located in memory; the processor reads information from the memory and, in conjunction with its hardware, completes the steps of the above methods.

[0098] It is understood that the memory in the embodiments of this specification may be volatile memory or non-volatile memory, or may include both volatile and non-volatile memory. Non-volatile memory may be read-only memory (ROM), programmable read-only memory (PROM), erasable programmable read-only memory (EPROM), electrically erasable programmable read-only memory (EEPROM), or flash memory. Volatile memory may be random access memory (RAM). It should be noted that the memory in the systems and methods described herein is intended to include, but is not limited to, these and any other suitable types of memory.

[0099] Those skilled in the art will recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this specification.

[0100] Those skilled in the art will clearly understand that, for the sake of convenience and brevity, the specific working processes of the systems, devices, and units described above can be referred to the corresponding processes in the aforementioned method implementations, and will not be repeated here.

[0101] The above description is merely a specific embodiment of this specification, but the scope of protection of this invention is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the technical scope disclosed in this specification should be included within the scope of protection of this specification. Therefore, the scope of protection of this invention should be determined by the scope of the claims.

Claims

1. A method for testing timed tasks, characterized in that, The method includes: By using a time interception library running in the process under test, the time call initiated by the process under test to obtain the current time is intercepted; In response to a test request for the process under test, a virtual time context corresponding to the test request is created, the virtual time context including a time offset; When the thread that initiates the time call is associated with the virtual time context, the virtual time is determined based on the obtained real time and the time offset in the associated virtual time context, and the virtual time is returned to the process under test as the return result of the time call. Obtain the timing configuration information of the timed tasks registered in the process under test, wherein the timing configuration information includes the timing expression of the timed tasks; Parse the timing expression to obtain the next trigger time of the timing task; In response to a time control command, the virtual time is adjusted to the earliest trigger time among the next trigger times of each of the timed tasks, and the timed task corresponding to the earliest trigger time is triggered. Obtain the execution result of the triggered scheduled task, and determine the test result of the scheduled task based on the execution result.

2. The timed task testing method according to claim 1, characterized in that, The method further includes: When the thread that initiates the time call is not associated with the virtual time context, the obtained real time is returned to the process under test as the return result of the time call.

3. The timed task testing method according to claim 1, characterized in that, The method further includes: Before the process under test starts, the time interception library is loaded into the process space corresponding to the process under test through the preloading mechanism of the dynamic linker.

4. The timed task testing method according to claim 1, characterized in that, The method further includes: During the execution of the process under test, the time interception library is injected into the process under test through bytecode enhancement.

5. The timed task testing method according to claim 1, characterized in that, The virtual time is determined based on the real time, the time offset, and the time flow rate multiplier; wherein, the rate at which the virtual time increases as the real time passes is related to the time flow rate multiplier.

6. The timed task testing method according to claim 5, characterized in that, The method further includes: The time interception library intercepts the sleep call initiated by the process under test; When the thread that initiates the sleep call is associated with the virtual time context, the sleep duration requested by the sleep call is shortened according to the time flow rate multiplier, and sleep is performed according to the shortened sleep duration.

7. The timed task testing method according to claim 1, characterized in that, The step of obtaining the timing configuration information of the timed tasks registered in the process under test includes: Scan the memory of the process under test for registered timed task objects; Read the timing expression of the timed task object from the scanned timed task object.

8. The timed task testing method according to claim 1, characterized in that, The method further includes: According to the time sequence of the next trigger time of each of the timed tasks, each timed task is constructed into a virtual event queue; The step of adjusting the virtual time to the earliest trigger time among the next trigger times of each of the timed tasks includes: adjusting the virtual time to the trigger time corresponding to the first event in the virtual event queue, and triggering the timed task corresponding to the first event.

9. A method for testing timed tasks, characterized in that, The method is applied to a testing system, which includes a test control terminal and a device under test (DUT), wherein the DUT runs a process under test. The test control terminal sends a time control command to the device under test; In response to a test request for the process under test, the device under test creates a virtual time context corresponding to the test request, the virtual time context including a time offset; it intercepts time calls initiated by the process under test to obtain the current time using a time interception library running in the process under test; when the thread initiating the time call is associated with the virtual time context, it determines the virtual time based on the obtained real time and the time offset, and returns the virtual time as the return result of the time call to the process under test; it obtains the timer expressions of the timer tasks registered in the process under test, parses the timer expressions to obtain the next trigger time of the timer tasks; in response to the time control command, it adjusts the virtual time to the earliest trigger time among the next trigger times of each timer task, and triggers the timer task corresponding to the earliest trigger time; The device under test returns the execution result of the triggered timed task to the test control terminal; The test control terminal determines the test result of the scheduled task based on the execution result.

10. A computer device comprising a memory and a processor, wherein the memory stores a computer program, characterized in that, When the processor executes the computer program, it implements the timed task testing method according to any one of claims 1 to 8.