1 /*
2  * Copyright (c) 2006-2023, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2023-11-20     Shell        add test suites
9  */
10 
11 #include "common.h"
12 #include "utest_assert.h"
13 
14 #include <rtdevice.h>
15 #include <rtdef.h>
16 
17 static struct rt_mutex _local_mtx;
18 static struct rt_condvar _local_cv;
19 
condvar_timedwait_tc(void)20 static void condvar_timedwait_tc(void)
21 {
22     rt_mutex_t mutex = &_local_mtx;
23     rt_condvar_t cond = &_local_cv;
24     int err;
25 
26     if (rt_mutex_take(mutex, RT_WAITING_FOREVER) != 0)
27     {
28         LOG_E("pthread_mutex_lock() error");
29         uassert_false(1);
30         return;
31     }
32 
33     err = rt_condvar_timedwait(cond, mutex, RT_KILLABLE, 100);
34     if (err != 0)
35     {
36         if (err == -EINTR || err == -ETIMEDOUT)
37         {
38             puts("wait timed out");
39             uassert_false(0);
40         }
41         else
42         {
43             LOG_E("errno=%d, ret=%d\n", errno, err);
44             LOG_E("pthread_cond_timedwait() error");
45             uassert_false(1);
46         }
47     }
48 
49     return ;
50 }
51 
utest_tc_init(void)52 static rt_err_t utest_tc_init(void)
53 {
54     if (rt_mutex_init(&_local_mtx, "utest", RT_IPC_FLAG_PRIO) != 0)
55     {
56         perror("pthread_mutex_init() error");
57         uassert_false(1);
58         return -1;
59     }
60 
61     rt_condvar_init(&_local_cv, NULL);
62     return RT_EOK;
63 }
64 
utest_tc_cleanup(void)65 static rt_err_t utest_tc_cleanup(void)
66 {
67     rt_mutex_detach(&_local_mtx);
68     rt_condvar_detach(&_local_cv);
69     return RT_EOK;
70 }
71 
testcase(void)72 static void testcase(void)
73 {
74     UTEST_UNIT_RUN(condvar_timedwait_tc);
75 }
76 UTEST_TC_EXPORT(testcase, "testcases.ipc.condvar.timedwait", utest_tc_init, utest_tc_cleanup, 10);
77