bh_queue_test.cc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "test_helper.h"
  6. #include "gtest/gtest.h"
  7. #include "bh_platform.h"
  8. // FIXME: Resolve memory leak in bh_queue_test_suite.
  9. // It includes release created queue and messages.
  10. class bh_queue_test_suite : public testing::Test
  11. {
  12. protected:
  13. // You should make the members protected s.t. they can be
  14. // accessed from sub-classes.
  15. // virtual void SetUp() will be called before each test is run. You
  16. // should define it if you need to initialize the variables.
  17. // Otherwise, this can be skipped.
  18. virtual void SetUp() {}
  19. // virtual void TearDown() will be called after each test is run.
  20. // You should define it if there is cleanup work to do. Otherwise,
  21. // you don't have to provide it.
  22. //
  23. virtual void TearDown() {}
  24. public:
  25. WAMRRuntimeRAII<512 * 1024> runtime;
  26. };
  27. typedef struct bh_queue_node {
  28. struct bh_queue_node *next;
  29. struct bh_queue_node *prev;
  30. unsigned short tag;
  31. unsigned int len;
  32. void *body;
  33. bh_msg_cleaner msg_cleaner;
  34. } bh_queue_node;
  35. struct bh_queue {
  36. bh_queue_mutex queue_lock;
  37. bh_queue_cond queue_wait_cond;
  38. unsigned int cnt;
  39. unsigned int max;
  40. unsigned int drops;
  41. bh_queue_node *head;
  42. bh_queue_node *tail;
  43. bool exit_loop_run;
  44. };
  45. typedef enum LINK_MSG_TYPE {
  46. COAP_TCP_RAW = 0,
  47. COAP_UDP_RAW = 1,
  48. REQUEST_PACKET,
  49. RESPONSE_PACKET,
  50. INSTALL_WASM_APP,
  51. CBOR_GENERIC = 30,
  52. LINK_MSG_TYPE_MAX = 50
  53. } LINK_MSG_TYPE;
  54. typedef enum QUEUE_MSG_TYPE {
  55. COAP_PARSED = LINK_MSG_TYPE_MAX + 1,
  56. RESTFUL_REQUEST,
  57. RESTFUL_RESPONSE,
  58. TIMER_EVENT = 5,
  59. SENSOR_EVENT = 6,
  60. GPIO_INTERRUPT_EVENT = 7,
  61. BLE_EVENT = 8,
  62. JDWP_REQUEST = 9,
  63. WD_TIMEOUT = 10,
  64. BASE_EVENT_MAX = 100
  65. } QUEUE_MSG_TYPE;
  66. enum {
  67. WASM_Msg_Start = BASE_EVENT_MAX,
  68. TIMER_EVENT_WASM,
  69. SENSOR_EVENT_WASM,
  70. CONNECTION_EVENT_WASM,
  71. WIDGET_EVENT_WASM,
  72. WASM_Msg_End = WASM_Msg_Start + 100
  73. };
  74. // If RES_CMP == 1, the function bh_queue_enter_loop_run run error.
  75. int RES_CMP = 0;
  76. /* Don't touch .ro data in msg body */
  77. static void
  78. local_ro_msg_body_cleaner(void *body)
  79. {
  80. (void)body;
  81. return;
  82. }
  83. TEST_F(bh_queue_test_suite, bh_queue_create)
  84. {
  85. EXPECT_NE(nullptr, bh_queue_create());
  86. }
  87. TEST_F(bh_queue_test_suite, bh_queue_destroy)
  88. {
  89. bh_message_t msg_ptr;
  90. bh_queue *queue_ptr = bh_queue_create();
  91. // Normally.
  92. msg_ptr = bh_new_msg(RESTFUL_REQUEST, nullptr, 0, nullptr);
  93. bh_post_msg2(queue_ptr, msg_ptr);
  94. bh_queue_destroy(queue_ptr);
  95. EXPECT_EQ(nullptr, queue_ptr->head);
  96. // Illegal parameters.
  97. bh_queue_destroy(nullptr);
  98. }
  99. TEST_F(bh_queue_test_suite, bh_message_payload)
  100. {
  101. bh_message_t msg_ptr;
  102. msg_ptr =
  103. bh_new_msg(RESTFUL_REQUEST, (void *)"test_msg_body",
  104. sizeof("test_msg_body"), (void *)local_ro_msg_body_cleaner);
  105. EXPECT_EQ("test_msg_body", bh_message_payload(msg_ptr));
  106. bh_free_msg(msg_ptr);
  107. }
  108. TEST_F(bh_queue_test_suite, bh_message_payload_len)
  109. {
  110. bh_message_t msg_ptr;
  111. msg_ptr =
  112. bh_new_msg(RESTFUL_REQUEST, (void *)"test_msg_body",
  113. sizeof("test_msg_body"), (void *)local_ro_msg_body_cleaner);
  114. EXPECT_EQ(sizeof("test_msg_body"), bh_message_payload_len(msg_ptr));
  115. bh_free_msg(msg_ptr);
  116. }
  117. TEST_F(bh_queue_test_suite, bh_message_type)
  118. {
  119. bh_message_t msg_ptr;
  120. msg_ptr =
  121. bh_new_msg(RESTFUL_REQUEST, (void *)"test_msg_body",
  122. sizeof("test_msg_body"), (void *)local_ro_msg_body_cleaner);
  123. EXPECT_EQ(RESTFUL_REQUEST, bh_message_type(msg_ptr));
  124. bh_free_msg(msg_ptr);
  125. }
  126. TEST_F(bh_queue_test_suite, bh_new_msg)
  127. {
  128. bh_message_t msg_ptr =
  129. bh_new_msg(RESTFUL_REQUEST, (void *)"test_msg_body",
  130. sizeof("test_msg_body"), (void *)local_ro_msg_body_cleaner);
  131. EXPECT_NE(nullptr, msg_ptr);
  132. bh_free_msg(msg_ptr);
  133. }
  134. void
  135. msg_cleaner_test(void *)
  136. {
  137. return;
  138. }
  139. TEST_F(bh_queue_test_suite, bh_free_msg)
  140. {
  141. bh_message_t msg_ptr;
  142. msg_ptr = bh_new_msg(RESTFUL_REQUEST, (void *)"test_msg_body",
  143. sizeof("test_msg_body"), (void *)msg_cleaner_test);
  144. bh_free_msg(msg_ptr);
  145. msg_ptr = bh_new_msg(RESTFUL_REQUEST, (void *)"test_msg_body",
  146. sizeof("test_msg_body"), nullptr);
  147. bh_free_msg(msg_ptr);
  148. msg_ptr = bh_new_msg(RESTFUL_REQUEST, (void *)(uintptr_t)100, 0, nullptr);
  149. bh_free_msg(msg_ptr);
  150. }
  151. TEST_F(bh_queue_test_suite, bh_post_msg)
  152. {
  153. int i = 0;
  154. bh_queue *queue_ptr = bh_queue_create();
  155. bh_message_t msg_ptr;
  156. EXPECT_EQ(true, bh_post_msg(queue_ptr, TIMER_EVENT_WASM, nullptr, 0));
  157. EXPECT_EQ(1, queue_ptr->cnt);
  158. // queue_ptr->cnt >= queue_ptr->max.
  159. msg_ptr = bh_new_msg(RESTFUL_REQUEST, (void *)"test_msg_body",
  160. sizeof("test_msg_body"), nullptr);
  161. for (i = 1; i <= 50; i++) {
  162. bh_post_msg2(queue_ptr, msg_ptr);
  163. }
  164. EXPECT_EQ(false, bh_post_msg(queue_ptr, TIMER_EVENT_WASM, nullptr, 0));
  165. }
  166. TEST_F(bh_queue_test_suite, bh_post_msg2)
  167. {
  168. bh_message_t msg_ptr;
  169. bh_queue *queue_ptr = bh_queue_create();
  170. msg_ptr = bh_new_msg(RESTFUL_REQUEST, nullptr, 0, nullptr);
  171. EXPECT_EQ(true, bh_post_msg2(queue_ptr, msg_ptr));
  172. EXPECT_EQ(1, queue_ptr->cnt);
  173. }
  174. TEST_F(bh_queue_test_suite, bh_get_msg)
  175. {
  176. bh_message_t msg_ptr;
  177. bh_queue *queue_ptr = bh_queue_create();
  178. msg_ptr = bh_new_msg(RESTFUL_REQUEST, (void *)"test_msg_body",
  179. sizeof("test_msg_body"), nullptr);
  180. // queue->cnt == 0, timeout_us == 0.
  181. EXPECT_EQ(nullptr, bh_get_msg(queue_ptr, 0));
  182. // queue->cnt == 0, timeout_us != 0.
  183. bh_get_msg(queue_ptr, 1);
  184. bh_post_msg2(queue_ptr, msg_ptr);
  185. EXPECT_EQ(1, queue_ptr->cnt);
  186. bh_get_msg(queue_ptr, -1);
  187. EXPECT_EQ(0, queue_ptr->cnt);
  188. }
  189. TEST_F(bh_queue_test_suite, bh_queue_get_message_count)
  190. {
  191. int i = 0, j = 0;
  192. bh_message_t msg_ptr;
  193. bh_queue *queue_ptr = bh_queue_create();
  194. for (i = 1; i <= 20; i++) {
  195. msg_ptr = bh_new_msg(RESTFUL_REQUEST, (void *)"test_msg_body",
  196. sizeof("test_msg_body"),
  197. (void *)local_ro_msg_body_cleaner);
  198. EXPECT_NE(nullptr, msg_ptr);
  199. bool post_result = bh_post_msg2(queue_ptr, msg_ptr);
  200. EXPECT_EQ(true, post_result);
  201. }
  202. i = i - 1;
  203. // The count of msg is less than queue_ptr->max.
  204. EXPECT_EQ(i, bh_queue_get_message_count(queue_ptr));
  205. // The count of msg is more than queue_ptr->max.
  206. for (j = 1; j <= 60; j++) {
  207. msg_ptr = bh_new_msg(RESTFUL_REQUEST, (void *)"test_msg_body",
  208. sizeof("test_msg_body"),
  209. (void *)local_ro_msg_body_cleaner);
  210. EXPECT_NE(nullptr, msg_ptr);
  211. bool post_result = bh_post_msg2(queue_ptr, msg_ptr);
  212. // The first 30 messages should be posted successfully, and the rest
  213. // should be dropped.
  214. if (j <= 30) {
  215. EXPECT_EQ(true, post_result);
  216. }
  217. else {
  218. EXPECT_EQ(false, post_result);
  219. }
  220. }
  221. j = j - 1;
  222. EXPECT_EQ(queue_ptr->max, bh_queue_get_message_count(queue_ptr));
  223. EXPECT_EQ(j + i - queue_ptr->max, queue_ptr->drops);
  224. // Illegal parameters.
  225. EXPECT_EQ(0, bh_queue_get_message_count(nullptr));
  226. bh_queue_destroy(queue_ptr);
  227. }
  228. void
  229. bh_queue_enter_loop_run_test_fun(void *message, void *arg)
  230. {
  231. static int count = 0;
  232. RES_CMP =
  233. strncmp("test_queue_loop", (char *)((bh_message_t)message)->body, 15);
  234. count++;
  235. if (2 == count) {
  236. bh_queue_exit_loop_run((bh_queue *)arg);
  237. }
  238. }
  239. TEST_F(bh_queue_test_suite, bh_queue_enter_loop_run)
  240. {
  241. bh_queue *queue_ptr = bh_queue_create();
  242. bh_message_t msg_ptr1 =
  243. bh_new_msg(RESTFUL_REQUEST, (void *)"test_queue_loop",
  244. sizeof("test_queue_loop"), nullptr);
  245. bh_message_t msg_ptr2 =
  246. bh_new_msg(RESTFUL_REQUEST, (void *)"test_queue_loop",
  247. sizeof("test_queue_loop"), nullptr);
  248. bh_post_msg2(queue_ptr, msg_ptr1);
  249. bh_post_msg2(queue_ptr, msg_ptr2);
  250. bh_queue_enter_loop_run(queue_ptr, bh_queue_enter_loop_run_test_fun,
  251. queue_ptr);
  252. EXPECT_EQ(0, RES_CMP);
  253. // Illegal parameters.
  254. bh_queue_enter_loop_run(nullptr, bh_queue_enter_loop_run_test_fun,
  255. queue_ptr);
  256. }
  257. TEST_F(bh_queue_test_suite, bh_queue_exit_loop_run)
  258. {
  259. // Illegal parameters.
  260. bh_queue_exit_loop_run(nullptr);
  261. }