condvar_timedwait_tc.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. * 2026-03-19 cl2t Add standardized utest documentation block
  10. */
  11. /**
  12. * Test Case Name: Condition Variable Timed Wait Test
  13. *
  14. * Test Objectives:
  15. * - Verify that rt_condvar_timedwait() correctly times out when no signal
  16. * is received within the specified timeout period.
  17. * - Test core APIs: rt_condvar_timedwait(), rt_mutex_take(),
  18. * rt_mutex_init(), rt_condvar_init().
  19. *
  20. * Test Scenarios:
  21. * - The main thread acquires a mutex and calls rt_condvar_timedwait()
  22. * with a 100-tick timeout.
  23. * - Since no other thread signals the condition variable, the call is
  24. * expected to time out with -ETIMEDOUT or be interrupted with -EINTR.
  25. *
  26. * Verification Metrics:
  27. * - rt_condvar_timedwait() must return -ETIMEDOUT or -EINTR when no
  28. * signal is received.
  29. * - Any other non-zero return value indicates a test failure.
  30. * - Mutex initialization and acquisition must succeed.
  31. *
  32. * Dependencies:
  33. * - Software configuration: RT_USING_SMART must be enabled.
  34. * - Environmental assumptions: The platform must support condition
  35. * variables and mutexes.
  36. *
  37. * Expected Results:
  38. * - Final output: "[ PASSED ] [ result ] testcase (testcases.ipc.condvar.timedwait)"
  39. * - No assertion failures during test execution.
  40. */
  41. #include "common.h"
  42. #include "utest_assert.h"
  43. #include <rtdevice.h>
  44. #include <rtdef.h>
  45. static struct rt_mutex _local_mtx;
  46. static struct rt_condvar _local_cv;
  47. static void condvar_timedwait_tc(void)
  48. {
  49. rt_mutex_t mutex = &_local_mtx;
  50. rt_condvar_t cond = &_local_cv;
  51. int err;
  52. if (rt_mutex_take(mutex, RT_WAITING_FOREVER) != 0)
  53. {
  54. LOG_E("pthread_mutex_lock() error");
  55. uassert_false(1);
  56. return;
  57. }
  58. err = rt_condvar_timedwait(cond, mutex, RT_KILLABLE, 100);
  59. if (err != 0)
  60. {
  61. if (err == -EINTR || err == -ETIMEDOUT)
  62. {
  63. puts("wait timed out");
  64. uassert_false(0);
  65. }
  66. else
  67. {
  68. LOG_E("errno=%d, ret=%d\n", errno, err);
  69. LOG_E("pthread_cond_timedwait() error");
  70. uassert_false(1);
  71. }
  72. }
  73. return ;
  74. }
  75. static rt_err_t utest_tc_init(void)
  76. {
  77. if (rt_mutex_init(&_local_mtx, "utest", RT_IPC_FLAG_PRIO) != 0)
  78. {
  79. perror("pthread_mutex_init() error");
  80. uassert_false(1);
  81. return -1;
  82. }
  83. rt_condvar_init(&_local_cv, NULL);
  84. return RT_EOK;
  85. }
  86. static rt_err_t utest_tc_cleanup(void)
  87. {
  88. rt_mutex_detach(&_local_mtx);
  89. rt_condvar_detach(&_local_cv);
  90. return RT_EOK;
  91. }
  92. static void testcase(void)
  93. {
  94. UTEST_UNIT_RUN(condvar_timedwait_tc);
  95. }
  96. UTEST_TC_EXPORT(testcase, "testcases.ipc.condvar.timedwait", utest_tc_init, utest_tc_cleanup, 10);