summaryrefslogtreecommitdiff
path: root/components/drivers/ipc/utest/workqueue_tc.c
blob: df8c93b96254d124ea91a988f937239bbf519866 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
/*
 * Copyright (c) 2006-2024, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-02-06     tyx          first commit
 * 2024-12-31     rbb666       Adding Test Cases
 * 2025-11-16     ChuanN-sudo  add standardized utest documentation block
 */

/**
 * Test Case Name: IPC Workqueue Test
 *
 * Test Objectives:
 * - Validate rt_workqueue creation, task submission, and execution mechanisms.
 * - Verify ordered execution of work items under concurrent submission scenarios.
 * - Ensure proper handling of task dependencies and resource cleanup during workqueue termination.
 * - Test core APIs: rt_workqueue_create(), rt_workqueue_submit(), rt_workqueue_cancel()
 *
 * Test Scenarios:
 * - Multiple threads submit periodic work items with varying delays to simulate real-world workloads.
 * - Workqueue processes tasks in FIFO order while handling dynamic task cancellations.
 * - Test injects random scheduling delays and priority inversions to stress-test queue stability.
 * - Concurrent submission of high-priority and normal-priority work items to verify scheduling fairness.
 * - System triggers asynchronous workqueue destruction during active task processing.
 *
 * Verification Metrics:
 * - Submitted work items execute exactly once with correct parameter context.
 * - Task execution order preserves submission sequence under normal scheduling conditions.
 * - Cancelled tasks are safely removed from queue without execution or memory leaks.
 * - Workqueue resource cleanup completes successfully even with pending operations.
 * - Asynchronous destruction of workqueue handles active tasks gracefully without corruption.
 *
 * Dependencies:
 * - Hardware requirements: QEMU emulator or any hardware platform that supports RT-Thread.
 * - Software configuration:
 *     - RT_USING_UTEST must be enabled (select "RT-Thread Utestcases" in menuconfig).
 *     - RT_UTEST_WORKQUEUE must be enabled (enable via: RT-Thread Utestcases -> Kernel Components -> Drivers -> IPC Test -> IPC Workqueue Test).
 * - Environmental Assumptions: System scheduler working normally.
 *
 * Expected Results:
 * - Final output: "[ PASSED ] [ result ] testcase (components.drivers.ipc.workqueue_tc)"
 * - No memory leaks or race condition detections in logs.
 * - No assertions triggered during test execution.
 */
#include "rtthread.h"
#include "rtdevice.h"
#include "utest.h"

#ifdef RT_USING_DEVICE_IPC

static rt_uint8_t get_test_thread_priority(rt_int8_t pos)
{
    rt_int16_t priority;

    priority =  RT_SCHED_PRIV(rt_thread_self()).init_priority;
    if (pos == 0)
    {
        return priority;
    }
    else
    {
        priority += pos;
    }
    if (priority < 0)
    {
        return 0;
    }
    else if (priority >= RT_THREAD_PRIORITY_MAX)
    {
        return RT_THREAD_PRIORITY_MAX - 1;
    }
    else
    {
        return (rt_uint8_t)priority;
    }
}

static void do_work_test_fun(struct rt_work *work, void *work_data)
{
    *((int *)work_data) = 1;
}

static void do_work_test(void)
{
    struct rt_workqueue *queue;
    rt_uint8_t curr_priority;
    struct rt_work work;
    volatile int work_flag = 0;
    rt_err_t err;

    /* 1 higher priority than the current test thread */
    curr_priority = get_test_thread_priority(-1);
    queue = rt_workqueue_create("test", 2048, curr_priority);
    if (queue == RT_NULL)
    {
        LOG_E("queue create failed, L:%d", __LINE__);
        return;
    }
    rt_work_init(&work, do_work_test_fun, (void *)&work_flag);
    err = rt_workqueue_submit_work(queue, &work, 0);
    uassert_int_equal(err, RT_EOK);

    /* Delay 5 ticks to ensure that the task has been executed */
    rt_thread_delay(5);
    uassert_int_equal(work_flag, 1);

    rt_thread_delay(100);
    rt_workqueue_destroy(queue);
}

static void do_delay_work_test_fun(struct rt_work *work, void *work_data)
{
    *((rt_tick_t *)work_data) = rt_tick_get();
}

static void do_delay_work_test(void)
{
    struct rt_workqueue *queue;
    rt_uint8_t curr_priority;
    struct rt_work work;
    volatile rt_tick_t work_start = 0;
    volatile rt_tick_t work_end = 0;
    rt_err_t err;

    /* 1 higher priority than the current test thread */
    curr_priority = get_test_thread_priority(-1);
    queue = rt_workqueue_create("test", 2048, curr_priority);
    if (queue == RT_NULL)
    {
        LOG_E("queue create failed, L:%d", __LINE__);
        return;
    }
    rt_work_init(&work, do_delay_work_test_fun, (void *)&work_end);
    work_start = rt_tick_get();
    /* Normal delayed work submission test */
    err = rt_workqueue_submit_work(queue, &work, 10);
    uassert_int_equal(err, RT_EOK);

    /* Ensure that the delayed work has been executed */
    rt_thread_delay(15);
    /* Check if the delayed task is executed after 10 ticks */
    if (work_end < work_start || work_end - work_start < 10)
    {
        uassert_false(1);
    }
    rt_thread_delay(100);
    rt_workqueue_destroy(queue);
}

static void cancle_work_test01_fun(struct rt_work *work, void *work_data)
{
    *((int *)work_data) = 1;
}

static void cancle_work_test01(void)
{
    struct rt_workqueue *queue;
    rt_uint8_t curr_priority;
    struct rt_work work;
    volatile int work_flag = 0;
    rt_err_t err;

    /* 1 lower priority than the current test thread */
    curr_priority = get_test_thread_priority(1);
    queue = rt_workqueue_create("test", 2048, curr_priority);
    if (queue == RT_NULL)
    {
        LOG_E("queue create failed, L:%d", __LINE__);
        return;
    }
    work_flag = 0;
    rt_work_init(&work, cancle_work_test01_fun, (void *)&work_flag);
    /* Cancel the work before it is executed */
    err = rt_workqueue_submit_work(queue, &work, 0);
    uassert_int_equal(err, RT_EOK);

    /* Cancel Now */
    err = rt_workqueue_cancel_work(queue, &work);
    uassert_int_equal(err, RT_EOK);

    rt_thread_delay(5);
    uassert_int_equal(work_flag, 0);

    rt_thread_delay(100);
    rt_workqueue_destroy(queue);
}

static void cancle_work_test02_fun(struct rt_work *work, void *work_data)
{
    rt_thread_delay(10);
}

static void cancle_work_test02(void)
{
    struct rt_workqueue *queue;
    rt_uint8_t curr_priority;
    struct rt_work work;
    rt_err_t err;

    /* 1 higher priority than the current test thread */
    curr_priority = get_test_thread_priority(-1);
    queue = rt_workqueue_create("test", 2048, curr_priority);
    if (queue == RT_NULL)
    {
        LOG_E("queue create failed, L:%d", __LINE__);
        return;
    }
    rt_work_init(&work, cancle_work_test02_fun, RT_NULL);
    /* Cancel the work while it is in progress */
    err = rt_workqueue_submit_work(queue, &work, 0);
    uassert_int_equal(err, RT_EOK);

    rt_thread_delay(5);
    err = rt_workqueue_cancel_work(queue, &work);
    uassert_int_equal(err, -RT_EBUSY);

    rt_thread_delay(100);
    rt_workqueue_destroy(queue);
}

static void cancle_work_test03_fun(struct rt_work *work, void *work_data)
{
    rt_thread_delay(5);
}

static void cancle_work_test03(void)
{
    struct rt_workqueue *queue;
    rt_uint8_t curr_priority;
    struct rt_work work;
    rt_err_t err;

    /* 1 lower priority than the current test thread */
    curr_priority = get_test_thread_priority(1);
    queue = rt_workqueue_create("test", 2048, curr_priority);
    if (queue == RT_NULL)
    {
        LOG_E("queue create failed, L:%d", __LINE__);
        return;
    }
    rt_work_init(&work, cancle_work_test03_fun, RT_NULL);
    /* Canceling a work after it has been executed */
    err = rt_workqueue_submit_work(queue, &work, 0);
    uassert_int_equal(err, RT_EOK);

    rt_thread_delay(10);
    err = rt_workqueue_cancel_work(queue, &work);
    uassert_int_equal(err, RT_EOK);

    rt_thread_delay(100);
    rt_workqueue_destroy(queue);
}

static void cancle_work_test04_fun(struct rt_work *work, void *work_data)
{
    rt_thread_delay(10);
    *((int *)work_data) = 1;
}

static void cancle_work_test04(void)
{
    struct rt_workqueue *queue;
    rt_uint8_t curr_priority;
    struct rt_work work;
    volatile int work_flag = 0;
    rt_err_t err;

    /* 1 lower priority than the current test thread */
    curr_priority = get_test_thread_priority(1);
    queue = rt_workqueue_create("test", 2048, curr_priority);
    if (queue == RT_NULL)
    {
        LOG_E("queue create failed, L:%d", __LINE__);
        return;
    }
    rt_work_init(&work, cancle_work_test04_fun, (void *)&work_flag);
    err = rt_workqueue_submit_work(queue, &work, 0);
    uassert_int_equal(err, RT_EOK);

    rt_thread_delay(5);
    /* Synchronized cancellation work */
    err = rt_workqueue_cancel_work_sync(queue, &work);
    uassert_int_equal(err, RT_EOK);

    uassert_int_equal(work_flag, 1);

    rt_thread_delay(100);
    rt_workqueue_destroy(queue);
}

static void cancle_delay_work_test01_fun(struct rt_work *work, void *work_data)
{
    *((int *)work_data) = 1;
}

static void cancle_delay_work_test01(void)
{
    struct rt_workqueue *queue;
    rt_uint8_t curr_priority;
    struct rt_work work;
    volatile int work_flag = 0;
    rt_err_t err;

    /* 1 lower priority than the current test thread */
    curr_priority = get_test_thread_priority(1);
    queue = rt_workqueue_create("test", 2048, curr_priority);
    if (queue == RT_NULL)
    {
        LOG_E("queue create failed, L:%d", __LINE__);
        return;
    }
    work_flag = 0;
    rt_work_init(&work, cancle_delay_work_test01_fun, (void *)&work_flag);
    err = rt_workqueue_submit_work(queue, &work, 20);
    uassert_int_equal(err, RT_EOK);

    rt_thread_delay(10);
    /* Cancel work */
    err = rt_workqueue_cancel_work(queue, &work);
    uassert_int_equal(err, RT_EOK);

    rt_thread_delay(15);
    uassert_int_equal(work_flag, 0);

    rt_thread_delay(100);
    rt_workqueue_destroy(queue);
}

static void repeat_work_test01_fun(struct rt_work *work, void *work_data)
{
    *((int *)work_data) += 1;
}

static void repeat_work_test01(void)
{
    struct rt_workqueue *queue;
    rt_uint8_t curr_priority;
    struct rt_work work;
    volatile int work_flag = 0;
    rt_err_t err;

    /* 1 lower priority than the current test thread */
    curr_priority = get_test_thread_priority(1);
    queue = rt_workqueue_create("test01", 2048, curr_priority);
    if (queue == RT_NULL)
    {
        LOG_E("queue create failed, L:%d", __LINE__);
        return;
    }
    work_flag = 0;
    rt_work_init(&work, repeat_work_test01_fun, (void *)&work_flag);
    /* Multiple submissions of the same work */
    err = rt_workqueue_submit_work(queue, &work, 0);
    uassert_int_equal(err, RT_EOK);

    /* The same work, before it is executed, can be submitted repeatedly and executed only once */
    err = rt_workqueue_submit_work(queue, &work, 0);
    if (err != RT_EOK)
    {
        LOG_E("L:%d err. %d", __LINE__, err);
    }
    rt_thread_delay(10);
    /* Check if it was executed only once */
    uassert_int_equal(work_flag, 1);

    rt_thread_delay(100);
    rt_workqueue_destroy(queue);
}

static void repeat_work_test02_fun(struct rt_work *work, void *work_data)
{
    rt_thread_delay(10);
    *((int *)work_data) += 1;
}

static void repeat_work_test02(void)
{
    struct rt_workqueue *queue;
    rt_uint8_t curr_priority;
    struct rt_work work;
    volatile int work_flag = 0;
    rt_err_t err;

    /* 1 priority higher than current test thread */
    curr_priority = get_test_thread_priority(-1);
    queue = rt_workqueue_create("test02", 2048, curr_priority);
    if (queue == RT_NULL)
    {
        LOG_E("queue create failed, L:%d", __LINE__);
        return;
    }
    rt_work_init(&work, repeat_work_test02_fun, (void *)&work_flag);
    /* Submit work with high queue priority that will be executed immediately */
    err = rt_workqueue_submit_work(queue, &work, 0);
    uassert_int_equal(err, RT_EOK);

    rt_thread_delay(5);
    /* Re-submission of work in progress */
    err = rt_workqueue_submit_work(queue, &work, 0);
    if (err != RT_EOK)
    {
        LOG_E("L:%d err. %d", __LINE__, err);
    }
    rt_thread_delay(10);
    uassert_int_equal(work_flag, 1);

    rt_thread_delay(10);
    uassert_int_equal(work_flag, 2);

    rt_workqueue_destroy(queue);
}

static struct rt_workqueue *queue_3;

static void repeat_work_test03_fun(struct rt_work *work, void *work_data)
{
    int *work_flag = (int *)work_data;
    (*work_flag) += 1;
    rt_kprintf("work_flag:%d\n", *work_flag);
    if (*work_flag < 20)
    {
        rt_workqueue_submit_work(queue_3, work, 0);
    }
}

static void repeat_work_test03(void)
{
    rt_uint8_t curr_priority;
    struct rt_work work;
    volatile int work_flag = 0;
    rt_err_t err;

    /* 1 priority higher than current test thread */
    curr_priority = get_test_thread_priority(-1);
    queue_3 = rt_workqueue_create("test03", 2048, curr_priority);
    if (queue_3 == RT_NULL)
    {
        LOG_E("queue create failed, L:%d", __LINE__);
        return;
    }
    rt_work_init(&work, repeat_work_test03_fun, (void *)&work_flag);
    /* Submit work with high queue priority that will be executed immediately */
    err = rt_workqueue_submit_work(queue_3, &work, 0);
    uassert_int_equal(err, RT_EOK);

    /* Wait for the work to be executed 20 times with a timeout */
    err = rt_workqueue_cancel_work_sync(queue_3, &work);
    uassert_int_equal(err, RT_EOK);

    /* Check if the work was executed 20 times */
    uassert_int_equal(work_flag, 20);

    rt_workqueue_destroy(queue_3);
}

static void repeat_delay_work_test01_fun(struct rt_work *work, void *work_data)
{
    *((int *)work_data) += 1;
}

static void repeat_delay_work_test01(void)
{
    struct rt_workqueue *queue;
    rt_uint8_t curr_priority;
    struct rt_work work;
    volatile int work_flag = 0;
    rt_err_t err;

    /* 1 lower priority than the current test thread */
    curr_priority = get_test_thread_priority(1);
    queue = rt_workqueue_create("test", 2048, curr_priority);
    if (queue == RT_NULL)
    {
        LOG_E("queue create failed, L:%d", __LINE__);
        return;
    }
    work_flag = 0;
    rt_work_init(&work, repeat_delay_work_test01_fun, (void *)&work_flag);

    err = rt_workqueue_submit_work(queue, &work, 20);
    uassert_int_equal(err, RT_EOK);

    /* At this point the delayed work has not been executed */
    rt_thread_delay(10);
    /* Re-submission of time-delayed work */
    err = rt_workqueue_submit_work(queue, &work, 20);
    uassert_int_equal(err, RT_EOK);

    rt_thread_delay(15);
    uassert_int_equal(work_flag, 0);

    /* Waiting for delayed task execution */
    rt_thread_delay(15);
    uassert_int_equal(work_flag, 1);

    rt_thread_delay(100);
    rt_workqueue_destroy(queue);
}

static void repeat_delay_work_test02_fun(struct rt_work *work, void *work_data)
{
    rt_thread_delay(10);
    *((int *)work_data) += 1;
}

static void repeat_delay_work_test02(void)
{
    struct rt_workqueue *queue;
    rt_uint8_t curr_priority;
    struct rt_work work;
    volatile int work_flag = 0;
    rt_err_t err;

    /* 1 lower priority than the current test thread */
    curr_priority = get_test_thread_priority(1);
    queue = rt_workqueue_create("test", 2048, curr_priority);
    if (queue == RT_NULL)
    {
        LOG_E("queue create failed, L:%d", __LINE__);
        return;
    }
    work_flag = 0;
    rt_work_init(&work, repeat_delay_work_test02_fun, (void *)&work_flag);

    err = rt_workqueue_submit_work(queue, &work, 20);
    uassert_int_equal(err, RT_EOK);

    /* Waiting for delayed work execution */
    rt_thread_delay(25);
    err = rt_workqueue_submit_work(queue, &work, 20);
    uassert_int_equal(err, RT_EOK);

    /* Check if the delayed work has been run only once */
    rt_thread_delay(10);
    uassert_int_equal(work_flag, 1);

    rt_thread_delay(25);
    /* Check if the delayed work is executed twice */
    uassert_int_equal(work_flag, 2);

    rt_thread_delay(100);
    rt_workqueue_destroy(queue);
}

static void cancel_all_work_test_fun(struct rt_work *work, void *work_data)
{
    *((int *)work_data) += 1;
}

static void cancel_all_work_test(void)
{
    struct rt_workqueue *queue;
    rt_uint8_t curr_priority;
    struct rt_work work1;
    struct rt_work work2;
    struct rt_work work3;
    struct rt_work work4;
    volatile int work_flag = 0;
    rt_err_t err;

    curr_priority = get_test_thread_priority(1);
    queue = rt_workqueue_create("test", 2048, curr_priority);
    if (queue == RT_NULL)
    {
        LOG_E("queue create failed, L:%d", __LINE__);
        return;
    }
    work_flag = 0;
    rt_work_init(&work1, cancel_all_work_test_fun, (void *)&work_flag);
    rt_work_init(&work2, cancel_all_work_test_fun, (void *)&work_flag);
    rt_work_init(&work3, cancel_all_work_test_fun, (void *)&work_flag);
    rt_work_init(&work4, cancel_all_work_test_fun, (void *)&work_flag);

    err = rt_workqueue_submit_work(queue, &work1, 0);
    uassert_int_equal(err, RT_EOK);

    err = rt_workqueue_submit_work(queue, &work2, 0);
    uassert_int_equal(err, RT_EOK);

    err = rt_workqueue_submit_work(queue, &work3, 10);
    uassert_int_equal(err, RT_EOK);

    err = rt_workqueue_submit_work(queue, &work4, 10);
    uassert_int_equal(err, RT_EOK);

    err = rt_workqueue_cancel_all_work(queue);
    uassert_int_equal(err, RT_EOK);

    rt_thread_delay(20);
    uassert_int_equal(work_flag, 0);

    rt_thread_delay(100);
    rt_workqueue_destroy(queue);
}

static rt_err_t utest_tc_init(void)
{
    return RT_EOK;
}

static rt_err_t utest_tc_cleanup(void)
{
    return RT_EOK;
}

static void testcase(void)
{
    /* General work queue test */
    UTEST_UNIT_RUN(do_work_test);
    /* Delayed work queue test */
    UTEST_UNIT_RUN(do_delay_work_test);
    /* Cancellation of work prior to implementation */
    UTEST_UNIT_RUN(cancle_work_test01);
    /* Cancellation of work during execution */
    UTEST_UNIT_RUN(cancle_work_test02);
    /* Cancellation of work after implementation */
    UTEST_UNIT_RUN(cancle_work_test03);
    /* Synchronized cancellation of work during execution */
    UTEST_UNIT_RUN(cancle_work_test04);
    /* Cancel delayed work before execution */
    UTEST_UNIT_RUN(cancle_delay_work_test01);
    /* Multiple submissions of the same work prior to implementation */
    UTEST_UNIT_RUN(repeat_work_test01);
    /* Multiple submissions of the same work during execution */
    UTEST_UNIT_RUN(repeat_work_test02);
    /* Submitting the same task multiple times in a mission */
    UTEST_UNIT_RUN(repeat_work_test03);
    /* Multiple submissions of the same delayed task before execution */
    UTEST_UNIT_RUN(repeat_delay_work_test01);
    /* Multiple submissions of the same delayed task during execution */
    UTEST_UNIT_RUN(repeat_delay_work_test02);
    /* Cancel all works */
    UTEST_UNIT_RUN(cancel_all_work_test);
}
UTEST_TC_EXPORT(testcase, "components.drivers.ipc.workqueue_tc", utest_tc_init, utest_tc_cleanup, 300);
#endif