summaryrefslogtreecommitdiff
path: root/components
diff options
context:
space:
mode:
authorChen Wang <[email protected]>2025-10-13 08:47:37 +0800
committerR b b666 <[email protected]>2025-10-13 10:44:16 +0800
commitd14daa6cb1a3ef6698862bb86822ba64a21e13dc (patch)
tree408ecf3c9313e06223b9513f9dccd41cb135cf49 /components
parent74997901401df476d4b73b34ea3de945916a904f (diff)
utest: ipc: move from examples to components/drivers/ipc
Also: - Add "RT_" prefix for utest config options. - Rename the case names to following the naming rule. Signed-off-by: Chen Wang <[email protected]>
Diffstat (limited to 'components')
-rw-r--r--components/drivers/ipc/SConscript5
-rw-r--r--components/drivers/ipc/utest/Kconfig12
-rw-r--r--components/drivers/ipc/utest/SConscript16
-rw-r--r--components/drivers/ipc/utest/completion_tc.c199
-rw-r--r--components/drivers/ipc/utest/completion_timeout_tc.c213
-rw-r--r--components/drivers/ipc/utest/workqueue_tc.c603
6 files changed, 1048 insertions, 0 deletions
diff --git a/components/drivers/ipc/SConscript b/components/drivers/ipc/SConscript
index d8a18939f3..892701733c 100644
--- a/components/drivers/ipc/SConscript
+++ b/components/drivers/ipc/SConscript
@@ -15,4 +15,9 @@ else:
group = DefineGroup('DeviceDrivers', src, depend = ['RT_USING_DEVICE_IPC'], CPPPATH = CPPPATH, LOCAL_CPPDEFINES=['__RT_IPC_SOURCE__'])
+list = os.listdir(cwd)
+for item in list:
+ if os.path.isfile(os.path.join(cwd, item, 'SConscript')):
+ group = group + SConscript(os.path.join(item, 'SConscript'))
+
Return('group')
diff --git a/components/drivers/ipc/utest/Kconfig b/components/drivers/ipc/utest/Kconfig
new file mode 100644
index 0000000000..86c4c5dc7f
--- /dev/null
+++ b/components/drivers/ipc/utest/Kconfig
@@ -0,0 +1,12 @@
+menu "IPC Unit Testcases"
+ depends on RT_USING_DEVICE_IPC
+
+config RT_UTEST_COMPLETION
+ bool "rt_completion testcase"
+ default n
+
+config RT_UTEST_WORKQUEUE
+ bool "rt_workqueue testcase"
+ default n
+
+endmenu
diff --git a/components/drivers/ipc/utest/SConscript b/components/drivers/ipc/utest/SConscript
new file mode 100644
index 0000000000..12a67b6fd6
--- /dev/null
+++ b/components/drivers/ipc/utest/SConscript
@@ -0,0 +1,16 @@
+Import('rtconfig')
+from building import *
+
+cwd = GetCurrentDir()
+src = []
+CPPPATH = [cwd]
+
+if GetDepend(['RT_UTEST_COMPLETION']):
+ src += ['completion_tc.c', 'completion_timeout_tc.c']
+
+if GetDepend(['RT_UTEST_WORKQUEUE']):
+ src += ['workqueue_tc.c']
+
+group = DefineGroup('utestcases', src, depend = ['RT_USING_UTESTCASES'], CPPPATH = CPPPATH)
+
+Return('group')
diff --git a/components/drivers/ipc/utest/completion_tc.c b/components/drivers/ipc/utest/completion_tc.c
new file mode 100644
index 0000000000..4406153bdc
--- /dev/null
+++ b/components/drivers/ipc/utest/completion_tc.c
@@ -0,0 +1,199 @@
+/*
+ * Copyright (c) 2006-2024, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2024-04-30 Shell init ver.
+ */
+
+/**
+ * Test Case for rt_completion API
+ *
+ * The test simulates a producer-consumer interaction where a producer thread
+ * generates data, and a consumer thread consumes the data after waiting for its
+ * availability using rt_completion synchronization primitives.
+ *
+ * Test Criteria:
+ * - The producer should correctly increment the test data and signal
+ * completion.
+ * - The consumer should correctly wait for data update, consume it, and signal
+ * completion.
+ * - Data integrity should be maintained between producer and consumer.
+ * - Synchronization is properly done so both can see consistent data.
+ * - Random latency is introduced to simulate racing scenarios.
+ */
+
+#define TEST_LATENCY_TICK (1)
+#define TEST_LOOP_TIMES (60 * RT_TICK_PER_SECOND)
+#define TEST_PROGRESS_ON (RT_TICK_PER_SECOND)
+
+#include "utest.h"
+
+#include <ipc/completion.h>
+#include <rtthread.h>
+#include <stdlib.h>
+
+static struct rt_completion _prod_completion;
+static struct rt_completion _cons_completion;
+static int _test_data = 0;
+static rt_atomic_t _progress_counter;
+static struct rt_semaphore _thr_exit_sem;
+
+static void done_safely(struct rt_completion *completion)
+{
+ rt_err_t error;
+
+ /* Signal completion */
+ error = rt_completion_wakeup(completion);
+
+ /* try again if failed to produce */
+ if (error == -RT_EEMPTY)
+ {
+ rt_thread_yield();
+ }
+ else if (error)
+ {
+ uassert_false(0);
+ rt_thread_delete(rt_thread_self());
+ }
+}
+
+static void wait_safely(struct rt_completion *completion)
+{
+ rt_err_t error;
+ do
+ {
+ error = rt_completion_wait_flags(completion, RT_WAITING_FOREVER,
+ RT_INTERRUPTIBLE);
+ if (error)
+ {
+ uassert_true(error == -RT_EINTR);
+ rt_thread_yield();
+ }
+ else
+ {
+ break;
+ }
+ } while (1);
+}
+
+static void producer_thread_entry(void *parameter)
+{
+ for (size_t i = 0; i < TEST_LOOP_TIMES; i++)
+ {
+ /* Produce data */
+ _test_data++;
+
+ /* notify consumer */
+ done_safely(&_prod_completion);
+
+ /* Delay before producing next data */
+ rt_thread_delay(TEST_LATENCY_TICK);
+
+ /* sync with consumer */
+ wait_safely(&_cons_completion);
+ }
+
+ rt_sem_release(&_thr_exit_sem);
+}
+
+static void _wait_until_edge(void)
+{
+ rt_tick_t entry_level, current;
+ rt_base_t random_latency;
+
+ entry_level = rt_tick_get();
+ do
+ {
+ current = rt_tick_get();
+ } while (current == entry_level);
+
+ /* give a random latency for test */
+ random_latency = rand();
+ entry_level = current;
+ for (size_t i = 0; i < random_latency; i++)
+ {
+ current = rt_tick_get();
+ if (current != entry_level) break;
+ }
+}
+
+static void consumer_thread_entry(void *parameter)
+{
+ int local_test_data = 0;
+
+ rt_thread_startup(parameter);
+
+ for (size_t i = 0; i < TEST_LOOP_TIMES; i++)
+ {
+ /* add more random case for test */
+ _wait_until_edge();
+
+ /* Wait for data update */
+ wait_safely(&_prod_completion);
+
+ /* Consume data */
+ if (local_test_data + 1 != _test_data)
+ {
+ LOG_I("local test data is %d, shared test data is %d",
+ local_test_data, _test_data);
+ uassert_true(0);
+ }
+ else if (rt_atomic_add(&_progress_counter, 1) % TEST_PROGRESS_ON == 0)
+ {
+ uassert_true(1);
+ }
+
+ local_test_data = _test_data;
+ done_safely(&_cons_completion);
+ }
+
+ rt_sem_release(&_thr_exit_sem);
+}
+
+static void testcase(void)
+{
+ /* Initialize completion object */
+ rt_completion_init(&_prod_completion);
+ rt_completion_init(&_cons_completion);
+
+ /* Create producer and consumer threads */
+ rt_thread_t producer_thread =
+ rt_thread_create("producer", producer_thread_entry, RT_NULL,
+ UTEST_THR_STACK_SIZE, UTEST_THR_PRIORITY, 100);
+ rt_thread_t consumer_thread =
+ rt_thread_create("consumer", consumer_thread_entry, producer_thread,
+ UTEST_THR_STACK_SIZE, UTEST_THR_PRIORITY, 100);
+ uassert_true(producer_thread != RT_NULL);
+ uassert_true(consumer_thread != RT_NULL);
+
+ LOG_I("Summary:\n"
+ "\tTest times: %ds(%d)",
+ TEST_LOOP_TIMES / RT_TICK_PER_SECOND, TEST_LOOP_TIMES);
+
+ rt_thread_startup(consumer_thread);
+
+ for (size_t i = 0; i < 2; i++)
+ {
+ rt_sem_take(&_thr_exit_sem, RT_WAITING_FOREVER);
+ }
+}
+
+static rt_err_t utest_tc_init(void)
+{
+ _test_data = 0;
+ _progress_counter = 0;
+ rt_sem_init(&_thr_exit_sem, "test", 0, RT_IPC_FLAG_PRIO);
+ return RT_EOK;
+}
+
+static rt_err_t utest_tc_cleanup(void)
+{
+ rt_sem_detach(&_thr_exit_sem);
+ return RT_EOK;
+}
+
+UTEST_TC_EXPORT(testcase, "components.drivers.ipc.rt_completion_basic",
+ utest_tc_init, utest_tc_cleanup, 10);
diff --git a/components/drivers/ipc/utest/completion_timeout_tc.c b/components/drivers/ipc/utest/completion_timeout_tc.c
new file mode 100644
index 0000000000..b7f10959e0
--- /dev/null
+++ b/components/drivers/ipc/utest/completion_timeout_tc.c
@@ -0,0 +1,213 @@
+/*
+ * Copyright (c) 2006-2024, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2024-04-30 Shell init ver.
+ */
+
+/**
+ * Test Case for rt_completion API
+ *
+ * The test simulates a producer-consumer interaction where a producer thread
+ * generates data, and a consumer thread consumes the data after waiting for its
+ * availability using rt_completion synchronization primitives.
+ *
+ * Test Criteria:
+ * - The producer produces data correctly and notifies the consumer thread.
+ * - The consumer receives data correctly and acknowledges receipt to the
+ * producer.
+ * - The producer and consumer threads synchronize their operations effectively.
+ * - Verify the correctness of data production and consumption between producer
+ * and consumer threads.
+ * - The asynchronous woken of consumer thread was handled properly so the
+ * consumer don't lose woken from producer.
+ *
+ * Test APIs:
+ * - rt_completion_init()
+ * - rt_completion_wakeup()
+ * - rt_completion_wait_flags()
+ */
+
+#define TEST_LATENCY_TICK (1)
+#define TEST_LOOP_TIMES (60 * RT_TICK_PER_SECOND)
+#define TEST_PROGRESS_ON (RT_TICK_PER_SECOND)
+
+#include "utest.h"
+
+#include <ipc/completion.h>
+#include <rtthread.h>
+#include <stdlib.h>
+
+static struct rt_completion _prod_completion;
+static struct rt_completion _cons_completion;
+static int _test_data;
+static int _async_intr_count;
+static rt_atomic_t _progress_counter;
+static struct rt_semaphore _thr_exit_sem;
+
+static void _test_thread_exit_failure(char *string)
+{
+ LOG_E("\t[TEST failed] %s", string);
+
+ rt_sem_release(&_thr_exit_sem);
+ rt_thread_delete(rt_thread_self());
+}
+
+static void done_safely(struct rt_completion *completion)
+{
+ rt_err_t error;
+
+ /* Signal completion */
+ error = rt_completion_wakeup(completion);
+
+ /* try again if failed to produce */
+ if (error == -RT_EEMPTY)
+ {
+ rt_thread_yield();
+ }
+ else if (error)
+ {
+ uassert_true(error == RT_EOK);
+ _test_thread_exit_failure("unexpected error");
+ }
+}
+
+static void wait_safely(struct rt_completion *completion)
+{
+ int try_times = 3;
+ rt_err_t error;
+ do
+ {
+ /* wait for one tick, to add more random */
+ error = rt_completion_wait_flags(completion, 1, RT_INTERRUPTIBLE);
+ if (error)
+ {
+ if (error == -RT_ETIMEOUT || error == -RT_EINTR)
+ {
+ _async_intr_count++;
+ }
+ else
+ {
+ LOG_I("Async event %d\n", error);
+ uassert_true(0);
+ }
+ rt_thread_yield();
+ }
+ else
+ {
+ break;
+ }
+ } while (try_times--);
+
+ if (error != RT_EOK)
+ {
+ uassert_true(error == RT_EOK);
+ _test_thread_exit_failure("wait failed");
+ }
+}
+
+static void producer_thread_entry(void *parameter)
+{
+ for (size_t i = 0; i < TEST_LOOP_TIMES; i++)
+ {
+ /* Produce data */
+ _test_data++;
+
+ /* Delay before producing next data */
+ rt_thread_delay(TEST_LATENCY_TICK);
+
+ /* notify consumer */
+ done_safely(&_prod_completion);
+
+ /* sync with consumer */
+ wait_safely(&_cons_completion);
+ }
+
+ rt_sem_release(&_thr_exit_sem);
+}
+
+static void consumer_thread_entry(void *parameter)
+{
+ int local_test_data = 0;
+
+ rt_thread_startup(parameter);
+
+ for (size_t i = 0; i < TEST_LOOP_TIMES; i++)
+ {
+ /* Wait for data update */
+ wait_safely(&_prod_completion);
+
+ /* Consume data */
+ if (local_test_data + 1 != _test_data)
+ {
+ LOG_I("local test data is %d, shared test data is %d",
+ local_test_data, _test_data);
+ uassert_true(0);
+ }
+ else if (rt_atomic_add(&_progress_counter, 1) % TEST_PROGRESS_ON == 0)
+ {
+ uassert_true(1);
+ }
+
+ local_test_data = _test_data;
+ done_safely(&_cons_completion);
+ }
+
+ rt_sem_release(&_thr_exit_sem);
+}
+
+rt_thread_t _watching_thread1;
+rt_thread_t _watching_thread2;
+
+static void testcase(void)
+{
+ /* Initialize completion object */
+ rt_completion_init(&_prod_completion);
+ rt_completion_init(&_cons_completion);
+
+ /* Create producer and consumer threads */
+ rt_thread_t producer_thread =
+ rt_thread_create("producer", producer_thread_entry, RT_NULL,
+ UTEST_THR_STACK_SIZE, UTEST_THR_PRIORITY, 100);
+ rt_thread_t consumer_thread =
+ rt_thread_create("consumer", consumer_thread_entry, producer_thread,
+ UTEST_THR_STACK_SIZE, UTEST_THR_PRIORITY, 100);
+ uassert_true(producer_thread != RT_NULL);
+ uassert_true(consumer_thread != RT_NULL);
+ _watching_thread1 = consumer_thread;
+ _watching_thread2 = producer_thread;
+
+ rt_thread_startup(consumer_thread);
+
+ for (size_t i = 0; i < 2; i++)
+ {
+ rt_sem_take(&_thr_exit_sem, RT_WAITING_FOREVER);
+ }
+
+ LOG_I("Summary:\n"
+ "\tTest times: %ds(%d times)\n"
+ "\tAsync interruption count: %d\n",
+ TEST_LOOP_TIMES / RT_TICK_PER_SECOND, TEST_LOOP_TIMES,
+ _async_intr_count);
+}
+
+static rt_err_t utest_tc_init(void)
+{
+ _test_data = 0;
+ _progress_counter = 0;
+ _async_intr_count = 0;
+ rt_sem_init(&_thr_exit_sem, "test", 0, RT_IPC_FLAG_PRIO);
+ return RT_EOK;
+}
+
+static rt_err_t utest_tc_cleanup(void)
+{
+ rt_sem_detach(&_thr_exit_sem);
+ return RT_EOK;
+}
+
+UTEST_TC_EXPORT(testcase, "components.drivers.ipc.rt_completion_timeout",
+ utest_tc_init, utest_tc_cleanup, 1000);
diff --git a/components/drivers/ipc/utest/workqueue_tc.c b/components/drivers/ipc/utest/workqueue_tc.c
new file mode 100644
index 0000000000..3cae6657d3
--- /dev/null
+++ b/components/drivers/ipc/utest/workqueue_tc.c
@@ -0,0 +1,603 @@
+/*
+ * 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