Skip to content

Commit ea8f71a

Browse files
committed
Most test cases in src/utest lack standardized functional documentation, as tracked in issue [#10895](#10895). This leads to high maintenance costs, difficulty for new contributors, and inefficient code reviews.
Solution: This patch adds the full, standardized documentation block to `sched_mtx_tc.c`, `sched_sem_tc.c`, `sched_thread_tc.c`, `sched_timed_mtx_tc.c` and `sched_timed_sem_tc.c` following the approved template. The documentation details: - Test Objectives and tested APIs - Test Scenarios - Verification Metrics - Dependencies - Test Execution command and Expected Results This makes the test case's purpose and behavior immediately clear to future maintainers and reviewers. Relates to [#10895](#10895) Signed-off-by: lhxj <[email protected]>
1 parent 7330df6 commit ea8f71a

File tree

5 files changed

+249
-0
lines changed

5 files changed

+249
-0
lines changed

src/utest/sched_mtx_tc.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,56 @@
66
* Change Logs:
77
* Date Author Notes
88
* 2024-01-17 Shell the first version
9+
* 2025-12-12 lhxj Add standardized utest documentation block
910
*/
11+
12+
/**
13+
* Test Case Name: Scheduler Mutex Stress Test (core.scheduler_mutex)
14+
*
15+
* Test Objectives:
16+
* - Validate the stability of the Mutex subsystem under high contention (Stress Test).
17+
* - Ensure priority inheritance (if supported) or basic blocking/waking works correctly.
18+
* - In multi-core systems, verify data consistency and spinlock mechanisms when multiple cores contend for a single kernel object simultaneously.
19+
* - List specific functions or APIs to be tested:
20+
* - rt_mutex_take()
21+
* - rt_mutex_release()
22+
* - rt_thread_create()
23+
*
24+
* Test Scenarios:
25+
* - **Stress Test (mutex_stress_tc):**
26+
* 1. Create `RT_CPUS_NR` threads (e.g., 4 threads in a Quad-Core setup).
27+
* 2. Assign **staggered priorities** to these threads (`priority_base + i % range`) to simulate contention between high and low priority tasks.
28+
* 3. All tester threads execute a tight loop: attempting to take and immediately release the *same* global mutex (`_racing_lock`).
29+
* - In SMP, this simulates true parallel contention.
30+
* 4. The main test thread sleeps for `TEST_SECONDS` (30s), periodically printing progress.
31+
* 5. After time is up, signal threads to exit (`_exit_flag`) and wait for them using a semaphore (`_thr_exit_sem`).
32+
*
33+
* Verification Metrics:
34+
* - **Pass:** The system must remain responsive (no deadlocks, hard faults, or RCU stalls) during the 30-second run.
35+
* - **Pass:** The main thread must successfully wait for all tester threads to exit (`rt_sem_take` returns `RT_EOK`).
36+
* - **Pass:** `uassert_true(1)` is executed periodically, confirming the main loop is alive.
37+
*
38+
* Dependencies:
39+
* - Hardware requirements (e.g., specific peripherals)
40+
* - No specific peripherals required, but Multi-core CPU recommended for SMP testing.
41+
* (This is met by the qemu-virt64-riscv BSP).
42+
* - Software configuration (e.g., kernel options, driver initialization)
43+
* - `RT_USING_UTEST` must be enabled (`RT-Thread Utestcases`).
44+
* - `Scheduler Test` must be enabled (`RT-Thread Utestcases` -> `Kernel Core` -> 'Scheduler Test').
45+
* - (Optional) Enable SMP for parallel testing:
46+
* - Go to `RT-Thread Kernel` -> `Enable SMP (Symmetric multiprocessing)`.
47+
* - Set `Number of CPUs` to > 1 (e.g., 4).
48+
* - Environmental assumptions
49+
* - Requires sufficient heap memory to allocate stacks for `RT_CPUS_NR` threads.
50+
* - Run the test case from the msh prompt:
51+
* `utest_run core.scheduler_mutex`
52+
*
53+
* Expected Results:
54+
* - The test continues for approximately 30 seconds.
55+
* - The console logs periodic success assertions.
56+
* - Final Output: `[ PASSED ] [ result ] testcase (core.scheduler_mutex)`
57+
*/
58+
1059
#include <rtthread.h>
1160
#include <stdlib.h>
1261
#include "utest.h"

src/utest/sched_sem_tc.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,60 @@
66
* Change Logs:
77
* Date Author Notes
88
* 2024-01-17 Shell the first version
9+
* 2025-12-12 lhxj Add standardized utest documentation block
910
*/
11+
12+
/**
13+
* Test Case Name: Priority Based Semaphore Synchronization Test (core.scheduler_sem)
14+
*
15+
* Test Objectives:
16+
* - Verify the stability and correctness of the scheduler under high concurrency.
17+
* - Verify thread synchronization and execution order using Semaphore chains across different priority levels.
18+
* - Verify SMP (Symmetric Multiprocessing) load balancing and atomic operations in a multi-core environment.
19+
* - List specific functions or APIs to be tested:
20+
* - rt_sem_init
21+
* - rt_sem_take
22+
* - rt_sem_release
23+
* - rt_thread_create
24+
* - rt_thread_startup
25+
* - rt_atomic_add
26+
*
27+
* Test Scenarios:
28+
* - **Semaphore Chained Scheduling:**
29+
* 1. Initialize a "thread matrix" where threads are created across multiple priority levels (`TEST_LEVEL_COUNTS`).
30+
* 2. For each priority level, create multiple concurrent threads (`RT_CPUS_NR * 2`).
31+
* 3. Establish a dependency chain (Ring Topology):
32+
* - **Level 0 threads:** Notify Level 1, then wait for their own resource.
33+
* - **Middle Level threads:** Wait for their resource (notified by Level N-1), then notify Level N+1.
34+
* - **Last Level threads:** Wait for their resource, print status (CPU ID), delay, then notify Level 0.
35+
* 4. Each thread increments an atomic load counter for the specific CPU it is running on.
36+
* 5. The main test thread waits for all sub-threads to signal completion via `_thr_exit_sem`.
37+
*
38+
* Verification Metrics:
39+
* - **Pass:** All created threads must complete their execution loops without deadlocking.
40+
* - **Pass:** The sum of execution counts across all CPUs (`_load_average`) must equal the calculated expected total (`KERN_TEST_CONFIG_LOOP_TIMES * TEST_LEVEL_COUNTS * KERN_TEST_CONCURRENT_THREADS`).
41+
*
42+
* Dependencies:
43+
* - Hardware requirements
44+
* - No specific peripherals required, but multi-core CPU is recommended for SMP verification.
45+
* (This is met by the qemu-virt64-riscv BSP).
46+
* - Software configuration
47+
* - `RT_USING_UTEST` must be enabled (`RT-Thread Utestcases`).
48+
* - `Scheduler Test` must be enabled (`RT-Thread Utestcases` -> `Kernel Core` -> 'Scheduler Test').
49+
* - (Optional) Enable SMP for parallel testing (Highly Recommended):
50+
* - Go to `RT-Thread Kernel` -> `Enable SMP (Symmetric multiprocessing)`.
51+
* - Set `Number of CPUs` to > 1 (e.g., 2 or 4).
52+
* - Environmental assumptions
53+
* - The system must support enough valid priority levels (`RT_THREAD_PRIORITY_MAX`) to accommodate `TEST_LEVEL_COUNTS`.
54+
* - Run the test case from the msh prompt:
55+
* `utest_run core.scheduler_sem`
56+
*
57+
* Expected Results:
58+
* - The console should print character patterns (e.g., `*0*1...`) indicating thread activity on specific CPUs.
59+
* - The final load statistics per CPU should be printed.
60+
* - Final Output: `[ PASSED ] [ result ] testcase (core.scheduler_sem)`
61+
*/
62+
1063
#define __RT_IPC_SOURCE__
1164

1265
#include <rtthread.h>

src/utest/sched_thread_tc.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,58 @@
66
* Change Logs:
77
* Date Author Notes
88
* 2024-01-25 Shell init ver.
9+
* 2025-12-12 lhxj Add standardized utest documentation block
910
*/
11+
12+
/**
13+
* Test Case Name: Scheduler Thread Stability Test (core.scheduler_thread)
14+
*
15+
* Test Objectives:
16+
* - Verify the stability of the scheduler during intensive context switching.
17+
* - Test the interaction between `rt_thread_suspend` and `rt_thread_resume` within critical sections.
18+
* - Verify scheduler robustness in multi-core environments (using `RT_CPUS_NR`) ensuring no deadlocks or race conditions occur during thread state transitions.
19+
* - List specific functions or APIs to be tested:
20+
* - rt_thread_create
21+
* - rt_thread_startup
22+
* - rt_thread_suspend
23+
* - rt_thread_resume
24+
* - rt_enter_critical / rt_exit_critical_safe
25+
* - rt_atomic_add
26+
*
27+
* Test Scenarios:
28+
* - **Multi-threaded Ping-Pong Context Switching:**
29+
* 1. Initialize a semaphore `_thr_exit_sem` for completion synchronization.
30+
* 2. Create `TEST_THREAD_COUNT` pairs of threads (based on `RT_CPUS_NR`).
31+
* 3. Each thread pair performs a "ping-pong" operation in a loop (100,000 iterations):
32+
* - Thread A enters critical section, suspends self, resumes Thread B, exits critical section.
33+
* - Thread B enters critical section, suspends self, resumes Thread A, exits critical section.
34+
* 4. An atomic counter `_progress_counter` tracks execution progress, triggering `uassert_true` at intervals.
35+
* 5. The main test thread waits for all worker threads to signal completion via the semaphore.
36+
*
37+
* Verification Metrics:
38+
* - **Pass:** All created threads complete their execution loops without system hangs or crashes.
39+
* - **Pass:** The progress counter increments as expected, validating thread execution flow.
40+
*
41+
* Dependencies:
42+
* - Hardware requirements (e.g., specific peripherals)
43+
* - No specific peripherals required.
44+
* (This is met by the qemu-virt64-riscv BSP).
45+
* - Software configuration (e.g., kernel options, driver initialization)
46+
* - `RT_USING_UTEST` must be enabled (`RT-Thread Utestcases`).
47+
* - `Scheduler Test` must be enabled (`RT-Thread Utestcases` -> `Kernel Core` -> 'Scheduler Test').
48+
* - (Optional) Enable SMP for parallel testing:
49+
* - Go to `RT-Thread Kernel` -> `Enable SMP (Symmetric multiprocessing)`.
50+
* - Set `Number of CPUs` to > 1 (e.g., 4).
51+
* - Environmental assumptions
52+
* - `UTEST_THR_STACK_SIZE` is sufficient for the test threads.
53+
* - Run the test case from the msh prompt:
54+
* `utest_run core.scheduler_thread`
55+
*
56+
* Expected Results:
57+
* - The test proceeds through multiple loops of thread suspension and resumption.
58+
* - Final Output: `[ PASSED ] [ result ] testcase (core.scheduler_thread)`
59+
*/
60+
1061
#define __RT_KERNEL_SOURCE__
1162
#include <rtthread.h>
1263
#include "utest.h"

src/utest/sched_timed_mtx_tc.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,54 @@
66
* Change Logs:
77
* Date Author Notes
88
* 2024-01-25 Shell init ver.
9+
* 2025-12-12 lhxj Add standardized utest documentation block
910
*/
11+
12+
/**
13+
* Test Case Name: Timed Mutex Race Condition Test (core.scheduler_timed_mtx)
14+
*
15+
* Test Objectives:
16+
* - Verify mutex behavior when a timeout race condition occurs between the timeout timer (scheduler) and mutex release.
17+
* - Ensure strict round-robin ownership (Producer <-> Consumer) is maintained despite timeouts.
18+
* - Validate that `rt_mutex_take_interruptible` correctly handles timeouts returning `-RT_ETIMEOUT`.
19+
* - Ensure a thread does not hold the mutex if it reports a timeout.
20+
* - List specific functions or APIs to be tested:
21+
* - rt_mutex_take_interruptible
22+
* - rt_mutex_take
23+
* - rt_mutex_release
24+
* - rt_tick_get
25+
*
26+
* Test Scenarios:
27+
* - **Timeout vs Release Race:**
28+
* 1. Create a Producer thread and a Consumer thread.
29+
* 2. Producer acquires the mutex, aligns execution to the system tick edge (`_wait_until_edge`) with random latency, and releases the mutex.
30+
* 3. Consumer attempts to acquire the mutex with a short timeout (1 tick) using `rt_mutex_take_interruptible`.
31+
* 4. Verify that if Consumer times out, it does not hold the mutex.
32+
* 5. Verify that if Consumer acquires the mutex, strict ownership order (Producer -> Consumer) was followed using magic flags.
33+
* 6. Repeat for `TEST_LOOP_TICKS`.
34+
*
35+
* Verification Metrics:
36+
* - **Pass:** The mutex ownership sequence (Consumer -> Producer -> Consumer) is never violated.
37+
* - **Pass:** `rt_mutex_get_owner` returns NULL or not the current thread if `rt_mutex_take_interruptible` returns `-RT_ETIMEOUT`.
38+
* - **Pass:** Both threads complete their loops and signal exit without asserting failure.
39+
*
40+
* Dependencies:
41+
* - Hardware requirements (e.g., specific peripherals)
42+
* - No specific hardware requirements.
43+
* (This is met by the qemu-virt64-riscv BSP).
44+
* - Software configuration (e.g., kernel options, driver initialization)
45+
* - `RT_USING_UTEST` must be enabled (`RT-Thread Utestcases`).
46+
* - `Scheduler Test` must be enabled (`RT-Thread Utestcases` -> `Kernel Core` -> 'Scheduler Test').
47+
* - Environmental assumptions
48+
* - No specific environmental assumptions.
49+
* - Run the test case from the msh prompt:
50+
* `utest_run core.scheduler_timed_mtx`
51+
*
52+
* Expected Results:
53+
* - The test logs "Total failed times: X(in Y)" indicating valid timeouts handled correctly.
54+
* - Final Output: `[ PASSED ] [ result ] testcase (core.scheduler_timed_mtx)`
55+
*/
56+
1057
#define __RT_KERNEL_SOURCE__
1158
#include <rtthread.h>
1259
#include <stdlib.h>

src/utest/sched_timed_sem_tc.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,56 @@
66
* Change Logs:
77
* Date Author Notes
88
* 2024-01-25 Shell init ver.
9+
* 2025-12-12 lhxj Add standardized utest documentation block
910
*/
11+
12+
/**
13+
* Test Case Name: Scheduler Timed Semaphore Race Test (core.scheduler_timed_sem)
14+
*
15+
* Test Objectives:
16+
* - Verify IPC (Semaphore) behavior under tight timing conditions (tick edge).
17+
* - Stress test the race condition where a timeout routine and a producer thread
18+
* race to wake up a sleeping consumer.
19+
* - Ensure the scheduler handles interruptible semaphore takes correctly without
20+
* returning unexpected error codes during high-contention/edge-case timing.
21+
* - List specific functions or APIs to be tested:
22+
* - rt_sem_take_interruptible
23+
* - rt_sem_release
24+
* - rt_tick_get
25+
* - rt_thread_create
26+
*
27+
* Test Scenarios:
28+
* - **Producer-Consumer Tick Edge Race:**
29+
* 1. Initialize two semaphores (`_ipc_sem`, `_thr_exit_sem`).
30+
* 2. Create two threads: a Producer (priority +1) and a Consumer (priority +1).
31+
* 3. **Producer Loop:** Wait specifically for the RT-Thread tick count to change (tick edge),
32+
* add a small random latency, and then release `_ipc_sem`.
33+
* 4. **Consumer Loop:** Attempt to take `_ipc_sem` with a timeout of exactly 1 tick.
34+
* 5. Track "failed times" (valid timeouts) versus "unexpected errors" (assert failure).
35+
* 6. Run this loop for `TEST_SECONDS` (10 seconds).
36+
*
37+
* Verification Metrics:
38+
* - **Pass:** The test completes the duration without triggering `uassert_true(0)`.
39+
* - **Pass:** Consumer receives either `RT_EOK` (success) or `-RT_ETIMEOUT` (expected race loss).
40+
* - **Fail:** Consumer receives any error code other than `RT_EOK` or `-RT_ETIMEOUT`.
41+
*
42+
* Dependencies:
43+
* - Hardware requirements
44+
* - No specific peripheral required.
45+
* (This is met by the qemu-virt64-riscv BSP).
46+
* - Software configuration
47+
* - `RT_USING_UTEST` must be enabled (`RT-Thread Utestcases`).
48+
* - `Scheduler Test` must be enabled (`RT-Thread Utestcases` -> `Kernel Core` -> 'Scheduler Test').
49+
* - Environmental assumptions
50+
* - System tick must be running.
51+
* - Run the test case from the msh prompt:
52+
* `utest_run core.scheduler_timed_sem`
53+
*
54+
* Expected Results:
55+
* - The system logs "Total failed times: X(in Y)" (Timeouts are allowed/counted, not fatal).
56+
* - Final Output: `[ PASSED ] [ result ] testcase (core.scheduler_timed_sem)`
57+
*/
58+
1059
#define __RT_KERNEL_SOURCE__
1160
#include <rtthread.h>
1261
#include <stdlib.h>

0 commit comments

Comments
 (0)