@@ -135,6 +135,51 @@ void test_work_item_supplied_with_func(void)
135135 zassert_true (k_queue_is_empty (& workq .queue ), NULL );
136136}
137137
138+
139+ /**
140+ * @brief Test k_work_submit_to_user_queue API
141+ *
142+ * @details Funcion k_work_submit_to_user_queue() will return
143+ * -EBUSY: if the work item was already in some workqueue and
144+ * -ENOMEM: if no memory for thread resource pool allocation.
145+ * Create two situation to meet the error return value.
146+ *
147+ * @see k_work_submit_to_user_queue()
148+ * @ingroup kernel_workqueue_tests
149+ */
150+ void test_k_work_submit_to_user_queue_fail (void )
151+ {
152+ int ret = 0 ;
153+
154+ k_sem_reset (& sync_sema );
155+ k_work_init (& work [0 ], common_work_handler );
156+ k_work_init (& work [1 ], common_work_handler );
157+
158+ /* TESTPOINT: When a work item be added to a workqueue,
159+ * it's flag will be in pending state, before the work item be processed,
160+ * it cannot be append to a workqueue another time.
161+ */
162+ k_work_submit_to_user_queue (& user_workq , & work [0 ]);
163+ k_work_submit_to_user_queue (& user_workq , & work [0 ]);
164+
165+ /* Test the work item's callback function can only be invoked once */
166+ k_sem_take (& sync_sema , K_FOREVER );
167+ zassert_true (k_queue_is_empty (& user_workq .queue ), NULL );
168+
169+ /* use up the memory in resource pool */
170+ for (int i = 0 ; i < 100 ; i ++ ) {
171+ ret = k_queue_alloc_append (& user_workq .queue , & work [1 ]);
172+ if (ret == - ENOMEM ) {
173+ break ;
174+ }
175+ }
176+
177+ k_work_submit_to_user_queue (& user_workq , & work [0 ]);
178+ /* if memory is used up, the work cannot be append into the workqueue */
179+ zassert_false (k_work_pending (& work [0 ]), NULL );
180+ }
181+
182+
138183/* Two handler functions fifo_work_first() and fifo_work_second
139184 * are made for two work items to test first in, first out.
140185 * It tests workqueue thread process work items
@@ -1412,6 +1457,7 @@ void test_main(void)
14121457 ztest_1cpu_unit_test (test_triggered_work_cancel_thread ),
14131458 ztest_1cpu_unit_test (test_triggered_work_cancel_isr ),
14141459 ztest_unit_test (test_work_item_supplied_with_func ),
1460+ ztest_user_unit_test (test_k_work_submit_to_user_queue_fail ),
14151461 ztest_unit_test (test_process_work_items_fifo ),
14161462 ztest_unit_test (test_sched_delayed_work_item ),
14171463 ztest_unit_test (test_workqueue_max_number ),
0 commit comments