Skip to content

Commit 9f8d745

Browse files
mengxianglinxcarlescufi
authored andcommitted
test: msgq: Add two corner cases in message queue
Put message to a full queue or get message from an empty queue with different timeout: K_NO_WAIT, a period of time, K_FOREVER. Signed-off-by: Meng xianglin <[email protected]>
1 parent b0d8758 commit 9f8d745

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

tests/kernel/msgq/msgq_api/src/main.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ extern void test_msgq_purge_when_put(void);
2121
extern void test_msgq_attrs_get(void);
2222
extern void test_msgq_alloc(void);
2323
extern void test_msgq_pend_thread(void);
24+
extern void test_msgq_empty(void);
25+
extern void test_msgq_full(void);
2426
#ifdef CONFIG_USERSPACE
2527
extern void test_msgq_user_thread(void);
2628
extern void test_msgq_user_thread_overflow(void);
@@ -79,6 +81,8 @@ void test_main(void)
7981
ztest_1cpu_unit_test(test_msgq_purge_when_put),
8082
ztest_user_unit_test(test_msgq_user_purge_when_put),
8183
ztest_1cpu_unit_test(test_msgq_pend_thread),
84+
ztest_1cpu_unit_test(test_msgq_empty),
85+
ztest_1cpu_unit_test(test_msgq_full),
8286
ztest_unit_test(test_msgq_alloc));
8387
ztest_run_test_suite(msgq_api);
8488
}

tests/kernel/msgq/msgq_api/src/test_msgq_contexts.c

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,47 @@ static void msgq_thread_data_passing(struct k_msgq *pmsgq)
198198
k_msgq_purge(pmsgq);
199199
}
200200

201+
static void get_empty_entry(void *p1, void *p2, void *p3)
202+
{
203+
int ret;
204+
uint32_t rx_buf[MSGQ_LEN];
205+
206+
/* make sure there is no message in the queue */
207+
ret = k_msgq_peek(p1, rx_buf);
208+
zassert_equal(ret, -ENOMSG, "Peek message from empty queue");
209+
210+
ret = k_msgq_get(p1, rx_buf, K_NO_WAIT);
211+
zassert_equal(ret, -ENOMSG, "Got message from empty queue");
212+
213+
/* blocked to TIMEOUT */
214+
ret = k_msgq_get(p1, rx_buf, TIMEOUT);
215+
zassert_equal(ret, -EAGAIN, "Got message from empty queue");
216+
217+
k_sem_give(&end_sema);
218+
/* blocked forever */
219+
k_msgq_get(p1, rx_buf, K_FOREVER);
220+
}
221+
222+
static void put_full_entry(void *p1, void *p2, void *p3)
223+
{
224+
int ret;
225+
226+
/* make sure the queue is full */
227+
zassert_equal(k_msgq_num_free_get(p1), 0, NULL);
228+
zassert_equal(k_msgq_num_used_get(p1), 1, NULL);
229+
230+
ret = k_msgq_put(p1, &data[1], K_NO_WAIT);
231+
zassert_equal(ret, -ENOMSG, "Put message to full queue");
232+
233+
/* blocked to TIMEOUT */
234+
ret = k_msgq_put(p1, &data[1], TIMEOUT);
235+
zassert_equal(ret, -EAGAIN, "Put message to full queue");
236+
237+
k_sem_give(&end_sema);
238+
/* blocked forever */
239+
k_msgq_put(p1, &data[1], K_FOREVER);
240+
}
241+
201242
/**
202243
* @addtogroup kernel_message_queue_tests
203244
* @{
@@ -316,6 +357,62 @@ void test_msgq_alloc(void)
316357
zassert_true(ret == -EINVAL, "Invalid request");
317358
}
318359

360+
/**
361+
* @brief Get message from an empty queue
362+
*
363+
* @details
364+
* - A thread get message from an empty message queue will get a -ENOMSG if
365+
* timeout is set to K_NO_WAIT
366+
* - A thread get message from an empty message queue will be blocked if timeout
367+
* is set to a positive value or K_FOREVER
368+
*
369+
* @see k_msgq_get()
370+
*/
371+
void test_msgq_empty(void)
372+
{
373+
int pri = k_thread_priority_get(k_current_get()) - 1;
374+
375+
k_msgq_init(&msgq1, tbuffer1, MSG_SIZE, 1);
376+
k_sem_init(&end_sema, 0, 1);
377+
378+
k_tid_t tid = k_thread_create(&tdata2, tstack2, STACK_SIZE,
379+
get_empty_entry, &msgq1, NULL,
380+
NULL, pri, 0, K_NO_WAIT);
381+
382+
k_sem_take(&end_sema, K_FOREVER);
383+
/* that getting thread is being blocked now */
384+
zassert_equal(tid->base.thread_state, _THREAD_PENDING, NULL);
385+
k_thread_abort(tid);
386+
}
387+
388+
/**
389+
* @brief Put message to a full queue
390+
*
391+
* @details
392+
* - A thread put message to a full message queue will get a -ENOMSG if
393+
* timeout is set to K_NO_WAIT
394+
* - A thread put message to a full message queue will be blocked if timeout
395+
* is set to a positive value or K_FOREVER
396+
*
397+
* @see k_msgq_put()
398+
*/
399+
void test_msgq_full(void)
400+
{
401+
int pri = k_thread_priority_get(k_current_get()) - 1;
402+
403+
k_msgq_init(&msgq1, tbuffer1, MSG_SIZE, 1);
404+
k_sem_init(&end_sema, 0, 1);
405+
406+
k_msgq_put(&msgq1, &data[0], K_NO_WAIT);
407+
k_tid_t tid = k_thread_create(&tdata2, tstack2, STACK_SIZE,
408+
put_full_entry, &msgq1, NULL,
409+
NULL, pri, 0, K_NO_WAIT);
410+
k_sem_take(&end_sema, K_FOREVER);
411+
/* that putting thread is being blocked now */
412+
zassert_equal(tid->base.thread_state, _THREAD_PENDING, NULL);
413+
k_thread_abort(tid);
414+
}
415+
319416
/**
320417
* @}
321418
*/

0 commit comments

Comments
 (0)