completion_timeout_tc.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. */
  10. /**
  11. * Test Case for rt_completion API
  12. *
  13. * The test simulates a producer-consumer interaction where a producer thread
  14. * generates data, and a consumer thread consumes the data after waiting for its
  15. * availability using rt_completion synchronization primitives.
  16. *
  17. * Test Criteria:
  18. * - The producer produces data correctly and notifies the consumer thread.
  19. * - The consumer receives data correctly and acknowledges receipt to the
  20. * producer.
  21. * - The producer and consumer threads synchronize their operations effectively.
  22. * - Verify the correctness of data production and consumption between producer
  23. * and consumer threads.
  24. * - The asynchronous woken of consumer thread was handled properly so the
  25. * consumer don't lose woken from producer.
  26. *
  27. * Test APIs:
  28. * - rt_completion_init()
  29. * - rt_completion_wakeup()
  30. * - rt_completion_wait_flags()
  31. */
  32. #define TEST_LATENCY_TICK (1)
  33. #define TEST_LOOP_TIMES (60 * RT_TICK_PER_SECOND)
  34. #define TEST_PROGRESS_ON (RT_TICK_PER_SECOND)
  35. #include "utest.h"
  36. #include <ipc/completion.h>
  37. #include <rtthread.h>
  38. #include <stdlib.h>
  39. static struct rt_completion _prod_completion;
  40. static struct rt_completion _cons_completion;
  41. static int _test_data;
  42. static int _async_intr_count;
  43. static rt_atomic_t _progress_counter;
  44. static struct rt_semaphore _thr_exit_sem;
  45. static void _test_thread_exit_failure(char *string)
  46. {
  47. LOG_E("\t[TEST failed] %s", string);
  48. rt_sem_release(&_thr_exit_sem);
  49. rt_thread_delete(rt_thread_self());
  50. }
  51. static void done_safely(struct rt_completion *completion)
  52. {
  53. rt_err_t error;
  54. /* Signal completion */
  55. error = rt_completion_wakeup(completion);
  56. /* try again if failed to produce */
  57. if (error == -RT_EEMPTY)
  58. {
  59. rt_thread_yield();
  60. }
  61. else if (error)
  62. {
  63. uassert_true(error == RT_EOK);
  64. _test_thread_exit_failure("unexpected error");
  65. }
  66. }
  67. static void wait_safely(struct rt_completion *completion)
  68. {
  69. int try_times = 3;
  70. rt_err_t error;
  71. do
  72. {
  73. /* wait for one tick, to add more random */
  74. error = rt_completion_wait_flags(completion, 1, RT_INTERRUPTIBLE);
  75. if (error)
  76. {
  77. if (error == -RT_ETIMEOUT || error == -RT_EINTR)
  78. {
  79. _async_intr_count++;
  80. }
  81. else
  82. {
  83. LOG_I("Async event %d\n", error);
  84. uassert_true(0);
  85. }
  86. rt_thread_yield();
  87. }
  88. else
  89. {
  90. break;
  91. }
  92. } while (try_times--);
  93. if (error != RT_EOK)
  94. {
  95. uassert_true(error == RT_EOK);
  96. _test_thread_exit_failure("wait failed");
  97. }
  98. }
  99. static void producer_thread_entry(void *parameter)
  100. {
  101. for (size_t i = 0; i < TEST_LOOP_TIMES; i++)
  102. {
  103. /* Produce data */
  104. _test_data++;
  105. /* Delay before producing next data */
  106. rt_thread_delay(TEST_LATENCY_TICK);
  107. /* notify consumer */
  108. done_safely(&_prod_completion);
  109. /* sync with consumer */
  110. wait_safely(&_cons_completion);
  111. }
  112. rt_sem_release(&_thr_exit_sem);
  113. }
  114. static void consumer_thread_entry(void *parameter)
  115. {
  116. int local_test_data = 0;
  117. rt_thread_startup(parameter);
  118. for (size_t i = 0; i < TEST_LOOP_TIMES; i++)
  119. {
  120. /* Wait for data update */
  121. wait_safely(&_prod_completion);
  122. /* Consume data */
  123. if (local_test_data + 1 != _test_data)
  124. {
  125. LOG_I("local test data is %d, shared test data is %d",
  126. local_test_data, _test_data);
  127. uassert_true(0);
  128. }
  129. else if (rt_atomic_add(&_progress_counter, 1) % TEST_PROGRESS_ON == 0)
  130. {
  131. uassert_true(1);
  132. }
  133. local_test_data = _test_data;
  134. done_safely(&_cons_completion);
  135. }
  136. rt_sem_release(&_thr_exit_sem);
  137. }
  138. rt_thread_t _watching_thread1;
  139. rt_thread_t _watching_thread2;
  140. static void testcase(void)
  141. {
  142. /* Initialize completion object */
  143. rt_completion_init(&_prod_completion);
  144. rt_completion_init(&_cons_completion);
  145. /* Create producer and consumer threads */
  146. rt_thread_t producer_thread =
  147. rt_thread_create("producer", producer_thread_entry, RT_NULL,
  148. UTEST_THR_STACK_SIZE, UTEST_THR_PRIORITY, 100);
  149. rt_thread_t consumer_thread =
  150. rt_thread_create("consumer", consumer_thread_entry, producer_thread,
  151. UTEST_THR_STACK_SIZE, UTEST_THR_PRIORITY, 100);
  152. uassert_true(producer_thread != RT_NULL);
  153. uassert_true(consumer_thread != RT_NULL);
  154. _watching_thread1 = consumer_thread;
  155. _watching_thread2 = producer_thread;
  156. rt_thread_startup(consumer_thread);
  157. for (size_t i = 0; i < 2; i++)
  158. {
  159. rt_sem_take(&_thr_exit_sem, RT_WAITING_FOREVER);
  160. }
  161. LOG_I("Summary:\n"
  162. "\tTest times: %ds(%d times)\n"
  163. "\tAsync interruption count: %d\n",
  164. TEST_LOOP_TIMES / RT_TICK_PER_SECOND, TEST_LOOP_TIMES,
  165. _async_intr_count);
  166. }
  167. static rt_err_t utest_tc_init(void)
  168. {
  169. _test_data = 0;
  170. _progress_counter = 0;
  171. _async_intr_count = 0;
  172. rt_sem_init(&_thr_exit_sem, "test", 0, RT_IPC_FLAG_PRIO);
  173. return RT_EOK;
  174. }
  175. static rt_err_t utest_tc_cleanup(void)
  176. {
  177. rt_sem_detach(&_thr_exit_sem);
  178. return RT_EOK;
  179. }
  180. UTEST_TC_EXPORT(testcase, "testcases.drivers.ipc.rt_completion.timeout",
  181. utest_tc_init, utest_tc_cleanup, 1000);