123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603 |
- /*
- * 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
- */
- #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
|