completion_timeout_tc.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright (c) 2006-2024, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2024-04-30 Shell init ver.
  9. * 2025-11-16 ChuanN-sudo add standardized utest documentation block
  10. */
  11. /**
  12. * Test Case Name: IPC Completion Timeout Test
  13. *
  14. * Test Objectives:
  15. * - Validate rt_completion initialization, wait with timeout, and wake-up mechanisms.
  16. * - Verify thread synchronization resilience in producer-consumer model under timing constraints.
  17. * - Test core APIs: rt_completion_init(), rt_completion_wait_flags(), rt_completion_wakeup()
  18. * - Verify proper handling of timeout and interrupt scenarios during synchronization.
  19. *
  20. * Test Scenarios:
  21. * - Producer thread generates incrementing data with small delays between productions.
  22. * - Consumer thread waits for data with timeout flags (RT_INTERRUPTIBLE) to simulate real-world interruptions.
  23. * - Test deliberately introduces random thread yields to simulate scheduling variations.
  24. * - Producer-consumer synchronization loop runs for extended duration to expose timing issues.
  25. * - System handles asynchronous interruptions during wait operations.
  26. *
  27. * Verification Metrics:
  28. * - The producer produces data correctly and notifies the consumer thread.
  29. * - The consumer receives data correctly and acknowledges receipt to the producer.
  30. * - The producer and consumer threads synchronize their operations effectively.
  31. * - Verify the correctness of data production and consumption between producer and consumer threads.
  32. * - The asynchronous wakeup of consumer thread was handled properly so the consumer don't lose wakeup from producer.
  33. *
  34. * Dependencies:
  35. * - Hardware requirements: QEMU emulator or any hardware platform that supports RT-Thread.
  36. * - Software configuration:
  37. * - RT_USING_UTEST must be enabled (select "RT-Thread Utestcases" in menuconfig).
  38. * - RT_UTEST_COMPLETION must be enabled (enable via: RT-Thread Utestcases -> Kernel Components -> Drivers -> IPC Test -> IPC Completion Test).
  39. * - Environmental Assumptions: System clock interrupts and scheduler working normally.
  40. *
  41. * Expected Results:
  42. * - Progress logs: "[ INFO ] components.drivers.ipc.rt_completion_timeout: Summary:...Test times:...Async interruption count:...".
  43. * - Final output: "[ PASSED ] [ result ] testcase (components.drivers.ipc.rt_completion_timeout)".
  44. * - No assertions triggered.
  45. */
  46. #define TEST_LATENCY_TICK (1)
  47. #define TEST_LOOP_TIMES (60 * RT_TICK_PER_SECOND)
  48. #define TEST_PROGRESS_ON (RT_TICK_PER_SECOND)
  49. #include "utest.h"
  50. #include <ipc/completion.h>
  51. #include <rtthread.h>
  52. #include <stdlib.h>
  53. static struct rt_completion _prod_completion;
  54. static struct rt_completion _cons_completion;
  55. static int _test_data;
  56. static int _async_intr_count;
  57. static rt_atomic_t _progress_counter;
  58. static struct rt_semaphore _thr_exit_sem;
  59. static void _test_thread_exit_failure(char *string)
  60. {
  61. LOG_E("\t[TEST failed] %s", string);
  62. rt_sem_release(&_thr_exit_sem);
  63. rt_thread_delete(rt_thread_self());
  64. }
  65. static void done_safely(struct rt_completion *completion)
  66. {
  67. rt_err_t error;
  68. /* Signal completion */
  69. error = rt_completion_wakeup(completion);
  70. /* try again if failed to produce */
  71. if (error == -RT_EEMPTY)
  72. {
  73. rt_thread_yield();
  74. }
  75. else if (error)
  76. {
  77. uassert_true(error == RT_EOK);
  78. _test_thread_exit_failure("unexpected error");
  79. }
  80. }
  81. static void wait_safely(struct rt_completion *completion)
  82. {
  83. int try_times = 3;
  84. rt_err_t error;
  85. do
  86. {
  87. /* wait for one tick, to add more random */
  88. error = rt_completion_wait_flags(completion, 1, RT_INTERRUPTIBLE);
  89. if (error)
  90. {
  91. if (error == -RT_ETIMEOUT || error == -RT_EINTR)
  92. {
  93. _async_intr_count++;
  94. }
  95. else
  96. {
  97. LOG_I("Async event %d\n", error);
  98. uassert_true(0);
  99. }
  100. rt_thread_yield();
  101. }
  102. else
  103. {
  104. break;
  105. }
  106. } while (try_times--);
  107. if (error != RT_EOK)
  108. {
  109. uassert_true(error == RT_EOK);
  110. _test_thread_exit_failure("wait failed");
  111. }
  112. }
  113. static void producer_thread_entry(void *parameter)
  114. {
  115. for (size_t i = 0; i < TEST_LOOP_TIMES; i++)
  116. {
  117. /* Produce data */
  118. _test_data++;
  119. /* Delay before producing next data */
  120. rt_thread_delay(TEST_LATENCY_TICK);
  121. /* notify consumer */
  122. done_safely(&_prod_completion);
  123. /* sync with consumer */
  124. wait_safely(&_cons_completion);
  125. }
  126. rt_sem_release(&_thr_exit_sem);
  127. }
  128. static void consumer_thread_entry(void *parameter)
  129. {
  130. int local_test_data = 0;
  131. rt_thread_startup(parameter);
  132. for (size_t i = 0; i < TEST_LOOP_TIMES; i++)
  133. {
  134. /* Wait for data update */
  135. wait_safely(&_prod_completion);
  136. /* Consume data */
  137. if (local_test_data + 1 != _test_data)
  138. {
  139. LOG_I("local test data is %d, shared test data is %d",
  140. local_test_data, _test_data);
  141. uassert_true(0);
  142. }
  143. else if (rt_atomic_add(&_progress_counter, 1) % TEST_PROGRESS_ON == 0)
  144. {
  145. uassert_true(1);
  146. }
  147. local_test_data = _test_data;
  148. done_safely(&_cons_completion);
  149. }
  150. rt_sem_release(&_thr_exit_sem);
  151. }
  152. rt_thread_t _watching_thread1;
  153. rt_thread_t _watching_thread2;
  154. static void testcase(void)
  155. {
  156. /* Initialize completion object */
  157. rt_completion_init(&_prod_completion);
  158. rt_completion_init(&_cons_completion);
  159. /* Create producer and consumer threads */
  160. rt_thread_t producer_thread =
  161. rt_thread_create("producer", producer_thread_entry, RT_NULL,
  162. UTEST_THR_STACK_SIZE, UTEST_THR_PRIORITY, 100);
  163. rt_thread_t consumer_thread =
  164. rt_thread_create("consumer", consumer_thread_entry, producer_thread,
  165. UTEST_THR_STACK_SIZE, UTEST_THR_PRIORITY, 100);
  166. uassert_true(producer_thread != RT_NULL);
  167. uassert_true(consumer_thread != RT_NULL);
  168. _watching_thread1 = consumer_thread;
  169. _watching_thread2 = producer_thread;
  170. rt_thread_startup(consumer_thread);
  171. for (size_t i = 0; i < 2; i++)
  172. {
  173. rt_sem_take(&_thr_exit_sem, RT_WAITING_FOREVER);
  174. }
  175. LOG_I("Summary:\n"
  176. "\tTest times: %ds(%d times)\n"
  177. "\tAsync interruption count: %d\n",
  178. TEST_LOOP_TIMES / RT_TICK_PER_SECOND, TEST_LOOP_TIMES,
  179. _async_intr_count);
  180. }
  181. static rt_err_t utest_tc_init(void)
  182. {
  183. _test_data = 0;
  184. _progress_counter = 0;
  185. _async_intr_count = 0;
  186. rt_sem_init(&_thr_exit_sem, "test", 0, RT_IPC_FLAG_PRIO);
  187. return RT_EOK;
  188. }
  189. static rt_err_t utest_tc_cleanup(void)
  190. {
  191. rt_sem_detach(&_thr_exit_sem);
  192. return RT_EOK;
  193. }
  194. UTEST_TC_EXPORT(testcase, "components.drivers.ipc.rt_completion_timeout",
  195. utest_tc_init, utest_tc_cleanup, 1000);