test_event.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. #include <stdbool.h>
  2. #include <string.h>
  3. #include "esp_event.h"
  4. #include "sdkconfig.h"
  5. #include "freertos/FreeRTOS.h"
  6. #include "esp_event_loop.h"
  7. #include "freertos/task.h"
  8. #include "esp_log.h"
  9. #include "esp_event.h"
  10. #include "esp_event_private.h"
  11. #include "esp_event_internal.h"
  12. #include "esp_heap_caps.h"
  13. #include "sdkconfig.h"
  14. #include "unity.h"
  15. #include "test_utils.h"
  16. static const char* TAG = "test_event";
  17. #define TEST_CONFIG_ITEMS_TO_REGISTER 5
  18. #define TEST_CONFIG_TASKS_TO_SPAWN 2
  19. #define TEST_CONFIG_WAIT_MULTIPLIER 5
  20. #define TEST_SETUP() \
  21. test_setup(); \
  22. s_test_core_id = xPortGetCoreID(); \
  23. s_test_priority = uxTaskPriorityGet(NULL); \
  24. #define TEST_TEARDOWN() \
  25. test_teardown(); \
  26. vTaskDelay(pdMS_TO_TICKS(CONFIG_INT_WDT_TIMEOUT_MS * TEST_CONFIG_WAIT_MULTIPLIER));
  27. typedef struct {
  28. void* data;
  29. SemaphoreHandle_t start;
  30. SemaphoreHandle_t done;
  31. } task_arg_t;
  32. typedef struct {
  33. esp_event_base_t base;
  34. int32_t id;
  35. esp_event_handler_t* handles;
  36. int32_t num;
  37. esp_event_loop_handle_t loop;
  38. bool is_registration;
  39. } handler_registration_data_t;
  40. typedef struct {
  41. esp_event_base_t base;
  42. int32_t id;
  43. esp_event_loop_handle_t loop;
  44. int32_t num;
  45. } post_event_data_t;
  46. typedef struct {
  47. int performed;
  48. int expected;
  49. SemaphoreHandle_t done;
  50. } performance_data_t;
  51. typedef struct {
  52. void* data;
  53. SemaphoreHandle_t mutex;
  54. } simple_arg_t;
  55. static BaseType_t s_test_core_id;
  56. static UBaseType_t s_test_priority;
  57. ESP_EVENT_DECLARE_BASE(s_test_base1);
  58. ESP_EVENT_DECLARE_BASE(s_test_base2);
  59. ESP_EVENT_DEFINE_BASE(s_test_base1);
  60. ESP_EVENT_DEFINE_BASE(s_test_base2);
  61. enum {
  62. TEST_EVENT_BASE1_EV1,
  63. TEST_EVENT_BASE1_EV2,
  64. TEST_EVENT_BASE1_MAX
  65. };
  66. enum {
  67. TEST_EVENT_BASE2_EV1,
  68. TEST_EVENT_BASE2_EV2,
  69. TEST_EVENT_BASE2_MAX
  70. };
  71. static BaseType_t test_event_get_core()
  72. {
  73. static int calls = 0;
  74. if (portNUM_PROCESSORS > 1) {
  75. return (s_test_core_id + calls++) % portNUM_PROCESSORS;
  76. } else {
  77. return s_test_core_id;
  78. }
  79. }
  80. static esp_event_loop_args_t test_event_get_default_loop_args()
  81. {
  82. esp_event_loop_args_t loop_config = {
  83. .queue_size = CONFIG_SYSTEM_EVENT_QUEUE_SIZE,
  84. .task_name = "loop",
  85. .task_priority = s_test_priority,
  86. .task_stack_size = 2048,
  87. .task_core_id = test_event_get_core()
  88. };
  89. return loop_config;
  90. }
  91. static void test_event_simple_handler(void* event_handler_arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
  92. {
  93. simple_arg_t* arg = (simple_arg_t*) event_handler_arg;
  94. xSemaphoreTake(arg->mutex, portMAX_DELAY);
  95. int* count = (int*) arg->data;
  96. if (event_data == NULL) {
  97. (*count)++;
  98. } else {
  99. (*count) += *((int*) event_data);
  100. }
  101. xSemaphoreGive(arg->mutex);
  102. }
  103. static void test_event_performance_handler(void* event_handler_arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
  104. {
  105. performance_data_t* data = (performance_data_t*) event_handler_arg;
  106. data->performed++;
  107. if (data->performed >= data->expected) {
  108. xSemaphoreGive(data->done);
  109. }
  110. }
  111. static void test_event_post_task(void* args)
  112. {
  113. task_arg_t* arg = (task_arg_t*) args;
  114. post_event_data_t* data = arg->data;
  115. xSemaphoreTake(arg->start, portMAX_DELAY);
  116. for (int i = 0; i < data->num; i++) {
  117. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(data->loop, data->base, data->id, NULL, 0, portMAX_DELAY));
  118. vTaskDelay(1);
  119. }
  120. xSemaphoreGive(arg->done);
  121. vTaskDelete(NULL);
  122. }
  123. static void test_event_simple_handler_registration_task(void* args)
  124. {
  125. task_arg_t* arg = (task_arg_t*) args;
  126. handler_registration_data_t* data = (handler_registration_data_t*) arg->data;
  127. xSemaphoreTake(arg->start, portMAX_DELAY);
  128. for(int i = 0; i < data->num; i++) {
  129. if (data->is_registration) {
  130. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(data->loop, data->base, data->id, data->handles[i], NULL));
  131. } else {
  132. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_unregister_with(data->loop, data->base, data->id, data->handles[i]));
  133. }
  134. vTaskDelay(1);
  135. }
  136. xSemaphoreGive(arg->done);
  137. vTaskDelete(NULL);
  138. }
  139. static void test_handler_post_w_task(void* event_handler_arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
  140. {
  141. simple_arg_t* arg = (simple_arg_t*) event_handler_arg;
  142. esp_event_loop_handle_t* loop = (esp_event_loop_handle_t*) event_data;
  143. int* count = (int*) arg->data;
  144. (*count)++;
  145. if (*count <= 2) {
  146. if (event_base == s_test_base1 && event_id == TEST_EVENT_BASE1_EV1) {
  147. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(*loop, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  148. } else{
  149. xSemaphoreGive((SemaphoreHandle_t) arg->mutex);
  150. }
  151. } else {
  152. // Test that once the queue is full and the handler attempts to post to the same loop,
  153. // posting does not block indefinitely.
  154. if (event_base == s_test_base1 && event_id == TEST_EVENT_BASE1_EV1) {
  155. xSemaphoreTake((SemaphoreHandle_t) arg->mutex, portMAX_DELAY);
  156. TEST_ASSERT_EQUAL(ESP_ERR_TIMEOUT, esp_event_post_to(*loop, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  157. }
  158. }
  159. }
  160. static void test_handler_post_wo_task(void* event_handler_arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
  161. {
  162. simple_arg_t* arg = (simple_arg_t*) event_handler_arg;
  163. esp_event_loop_handle_t* loop = (esp_event_loop_handle_t*) event_data;
  164. int* count = (int*) arg->data;
  165. (*count)++;
  166. if (*count <= 2) {
  167. if (event_base == s_test_base1 && event_id == TEST_EVENT_BASE1_EV1) {
  168. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(*loop, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  169. } else{
  170. xSemaphoreGive((SemaphoreHandle_t) arg->mutex);
  171. }
  172. } else {
  173. // Test that once the queue is full and the handler attempts to post to the same loop,
  174. // posting does not block indefinitely.
  175. if (event_base == s_test_base1 && event_id == TEST_EVENT_BASE1_EV1) {
  176. xSemaphoreTake((SemaphoreHandle_t) arg->mutex, portMAX_DELAY);
  177. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(*loop, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  178. TEST_ASSERT_EQUAL(ESP_ERR_TIMEOUT, esp_event_post_to(*loop, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  179. }
  180. }
  181. }
  182. static void test_post_from_handler_loop_task(void* args)
  183. {
  184. esp_event_loop_handle_t event_loop = (esp_event_loop_handle_t) args;
  185. while(1) {
  186. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_run(event_loop, portMAX_DELAY));
  187. }
  188. }
  189. static void test_setup()
  190. {
  191. TEST_ASSERT_TRUE(TEST_CONFIG_TASKS_TO_SPAWN >= 2);
  192. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create_default());
  193. }
  194. static void test_teardown()
  195. {
  196. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete_default());
  197. }
  198. TEST_CASE("can create and delete event loops", "[event]")
  199. {
  200. /* this test aims to verify that:
  201. * - creating loops with and without a task succeeds
  202. * - event queue can accomodate the set queue size, and drops the post when exceeded
  203. * - deleting loops with unconsumed posts and unregistered handlers (when unregistration is enabled) does not leak memory */
  204. TEST_SETUP();
  205. esp_event_loop_handle_t loop1; // with dedicated task
  206. esp_event_loop_handle_t loop2; // without dedicated task
  207. esp_event_loop_handle_t loop3; // with leftover post and handlers
  208. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  209. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create(&loop_args, &loop1));
  210. loop_args.task_name = NULL;
  211. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create(&loop_args, &loop2));
  212. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create(&loop_args, &loop3));
  213. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop3, s_test_base1, TEST_EVENT_BASE1_EV1, (void*) 0x00000001, NULL));
  214. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop3, s_test_base1, TEST_EVENT_BASE1_EV2, (void*) 0x00000002, NULL));
  215. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop3, s_test_base2, TEST_EVENT_BASE1_EV1, (void*) 0x00000003, NULL));
  216. for (int i = 0; i < loop_args.queue_size; i++) {
  217. int mod = i % 4;
  218. switch(mod) {
  219. case 0:
  220. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop3, s_test_base1, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  221. break;
  222. case 1:
  223. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop3, s_test_base2, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  224. break;
  225. case 2:
  226. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop3, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  227. break;
  228. case 3:
  229. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop3, s_test_base2, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  230. break;
  231. default:
  232. break;
  233. }
  234. }
  235. TEST_ASSERT_EQUAL(ESP_ERR_TIMEOUT, esp_event_post_to(loop3, s_test_base1, TEST_EVENT_BASE1_EV1, NULL, 0, pdMS_TO_TICKS(10)));
  236. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete(loop1));
  237. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete(loop2));
  238. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete(loop3));
  239. TEST_TEARDOWN();
  240. }
  241. TEST_CASE("can register/unregister handlers for all events/all events for a specific base", "[event]")
  242. {
  243. /* this test aims to verify that handlers can be registered to be called on all events
  244. * or for all events with specific bases */
  245. TEST_SETUP();
  246. esp_event_loop_handle_t loop;
  247. int count = 0;
  248. simple_arg_t arg = {
  249. .data = &count,
  250. .mutex = xSemaphoreCreateMutex()
  251. };
  252. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  253. loop_args.task_name = NULL;
  254. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create(&loop_args, &loop));
  255. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, ESP_EVENT_ANY_BASE, ESP_EVENT_ANY_ID, test_event_simple_handler, &arg));
  256. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base1, ESP_EVENT_ANY_ID, test_event_simple_handler, &arg));
  257. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_event_handler_register_with(loop, ESP_EVENT_ANY_BASE, TEST_EVENT_BASE1_EV1, test_event_simple_handler, &arg));
  258. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV1, test_event_simple_handler, &arg));
  259. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV2, test_event_simple_handler, &arg));
  260. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base2, TEST_EVENT_BASE1_EV1, test_event_simple_handler, &arg));
  261. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_event_post_to(loop, ESP_EVENT_ANY_BASE, ESP_EVENT_ANY_ID, NULL, 0, portMAX_DELAY));
  262. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_event_post_to(loop, s_test_base1, ESP_EVENT_ANY_ID, NULL, 0, portMAX_DELAY));
  263. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_event_post_to(loop, ESP_EVENT_ANY_BASE, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  264. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY)); // exec loop, base and id level (+3)
  265. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY)); // exec loop, base and id level (+3)
  266. // Post unknown events. Respective loop level and base level handlers should still execute.
  267. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_MAX, NULL, 0, portMAX_DELAY)); // exec loop and base level (+2)
  268. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE2_MAX, NULL, 0, portMAX_DELAY)); // exec loop level (+1)
  269. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  270. TEST_ASSERT_EQUAL(9, count); // 3 + 3 + 2 + 1
  271. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete(loop));
  272. vSemaphoreDelete(arg.mutex);
  273. TEST_TEARDOWN();
  274. }
  275. TEST_CASE("can unregister handler", "[event]")
  276. {
  277. /* this test aims to verify that unregistered handlers no longer execute when events are raised */
  278. TEST_SETUP();
  279. esp_event_loop_handle_t loop;
  280. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  281. loop_args.task_name = NULL;
  282. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create(&loop_args, &loop));
  283. int count = 0;
  284. simple_arg_t arg = {
  285. .data = &count,
  286. .mutex = xSemaphoreCreateMutex()
  287. };
  288. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV1, test_event_simple_handler, &arg));
  289. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base2, TEST_EVENT_BASE1_EV1, test_event_simple_handler, &arg));
  290. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  291. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  292. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  293. TEST_ASSERT_EQUAL(2, count);
  294. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_unregister_with(loop, s_test_base1, TEST_EVENT_BASE1_EV1, test_event_simple_handler));
  295. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  296. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  297. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  298. TEST_ASSERT_EQUAL(3, count);
  299. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete(loop));
  300. vSemaphoreDelete(arg.mutex);
  301. TEST_TEARDOWN();
  302. }
  303. TEST_CASE("can exit running loop at approximately the set amount of time", "[event]")
  304. {
  305. /* this test aims to verify that running loop does not block indefinitely in cases where
  306. * events are posted frequently */
  307. TEST_SETUP();
  308. esp_event_loop_handle_t loop;
  309. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  310. loop_args.task_name = NULL;
  311. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create(&loop_args, &loop));
  312. performance_data_t handler_data = {
  313. .performed = 0,
  314. .expected = INT32_MAX,
  315. .done = xSemaphoreCreateBinary()
  316. };
  317. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV1, test_event_performance_handler, &handler_data));
  318. post_event_data_t post_event_data = {
  319. .base = s_test_base1,
  320. .id = TEST_EVENT_BASE1_EV1,
  321. .loop = loop,
  322. .num = INT32_MAX
  323. };
  324. task_arg_t post_event_arg = {
  325. .data = &post_event_data,
  326. .done = xSemaphoreCreateBinary(),
  327. .start = xSemaphoreCreateBinary()
  328. };
  329. TaskHandle_t post_task;
  330. xTaskCreatePinnedToCore(test_event_post_task, "post", 2048, &post_event_arg, s_test_priority, &post_task, test_event_get_core());
  331. int runtime_ms = 10;
  332. int runtime_us = runtime_ms * 1000;
  333. int64_t start, diff;
  334. start = esp_timer_get_time();
  335. xSemaphoreGive(post_event_arg.start);
  336. // Run the loop for the runtime_ms set amount of time, regardless of whether events
  337. // are still being posted to the loop.
  338. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_run(loop, pdMS_TO_TICKS(runtime_ms)));
  339. diff = (esp_timer_get_time() - start);
  340. // Threshold is 25 percent.
  341. TEST_ASSERT(diff < runtime_us * 1.25f);
  342. // Verify that the post task still continues
  343. TEST_ASSERT_NOT_EQUAL(pdTRUE, xSemaphoreTake(post_event_arg.done, pdMS_TO_TICKS(10)));
  344. vSemaphoreDelete(post_event_arg.done);
  345. vSemaphoreDelete(post_event_arg.start);
  346. vSemaphoreDelete(handler_data.done);
  347. vTaskDelete(post_task);
  348. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete(loop));
  349. TEST_TEARDOWN();
  350. }
  351. TEST_CASE("can register/unregister handlers simultaneously", "[event]")
  352. {
  353. /* this test aims to verify that the event handlers list remains consistent despite
  354. * simultaneous access by differenct tasks */
  355. TEST_SETUP();
  356. const char* base = "base";
  357. int32_t id = 0;
  358. esp_event_loop_handle_t loop;
  359. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  360. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create(&loop_args, &loop));
  361. ESP_LOGI(TAG, "registering handlers");
  362. handler_registration_data_t* registration_data = calloc(TEST_CONFIG_TASKS_TO_SPAWN, sizeof(*registration_data));
  363. task_arg_t* registration_arg = calloc(TEST_CONFIG_TASKS_TO_SPAWN, sizeof(*registration_arg));
  364. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  365. registration_data[i].base = base;
  366. registration_data[i].id = id;
  367. registration_data[i].loop = loop;
  368. registration_data[i].handles = calloc(TEST_CONFIG_ITEMS_TO_REGISTER, sizeof(esp_event_handler_t));
  369. registration_data[i].num = TEST_CONFIG_ITEMS_TO_REGISTER;
  370. registration_data[i].is_registration = true;
  371. for (int j = 0; j < TEST_CONFIG_ITEMS_TO_REGISTER; j++) {
  372. registration_data[i].handles[j] = (void*) (i * TEST_CONFIG_ITEMS_TO_REGISTER) + (j + TEST_CONFIG_ITEMS_TO_REGISTER);
  373. }
  374. registration_arg[i].start = xSemaphoreCreateBinary();
  375. registration_arg[i].done = xSemaphoreCreateBinary();
  376. registration_arg[i].data = &registration_data[i];
  377. xTaskCreatePinnedToCore(test_event_simple_handler_registration_task, "register", 2048, &registration_arg[i], s_test_priority, NULL, test_event_get_core());
  378. }
  379. // Give the semaphores to the spawned registration task
  380. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  381. xSemaphoreGive(registration_arg[i].start);
  382. }
  383. // Take the same semaphores in order to proceed
  384. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  385. xSemaphoreTake(registration_arg[i].done, portMAX_DELAY);
  386. }
  387. ESP_LOGI(TAG, "checking consistency of handlers list");
  388. // Check consistency of events list
  389. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  390. for (int j = 0; j < TEST_CONFIG_ITEMS_TO_REGISTER; j++) {
  391. TEST_ASSERT_TRUE(esp_event_is_handler_registered(loop, base, id, registration_data[i].handles[j]));
  392. }
  393. }
  394. ESP_LOGI(TAG, "unregistering handlers");
  395. /* Test if tasks can unregister simultaneously */
  396. // Unregister registered events
  397. handler_registration_data_t* unregistration_data = calloc(TEST_CONFIG_TASKS_TO_SPAWN, sizeof(*unregistration_data));
  398. task_arg_t* unregistration_arg = calloc(TEST_CONFIG_TASKS_TO_SPAWN, sizeof(*unregistration_arg));
  399. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  400. unregistration_data[i].base = base;
  401. unregistration_data[i].id = id;
  402. unregistration_data[i].loop = loop;
  403. unregistration_data[i].handles = calloc(TEST_CONFIG_ITEMS_TO_REGISTER, sizeof(esp_event_handler_t));
  404. unregistration_data[i].num = TEST_CONFIG_ITEMS_TO_REGISTER;
  405. unregistration_data[i].is_registration = false;
  406. memcpy(unregistration_data[i].handles, registration_data[i].handles, TEST_CONFIG_ITEMS_TO_REGISTER * sizeof(esp_event_handler_t));
  407. unregistration_arg[i].data = &unregistration_data[i];
  408. unregistration_arg[i].start = xSemaphoreCreateBinary();
  409. unregistration_arg[i].done = xSemaphoreCreateBinary();
  410. xTaskCreatePinnedToCore(test_event_simple_handler_registration_task, "unregister", 2048, &unregistration_arg[i], s_test_priority, NULL, test_event_get_core());
  411. }
  412. // Give the semaphores to the spawned unregistration task
  413. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  414. xSemaphoreGive(unregistration_arg[i].start);
  415. }
  416. // Take the same semaphores in order to proceed
  417. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  418. xSemaphoreTake(unregistration_arg[i].done, portMAX_DELAY);
  419. }
  420. ESP_LOGI(TAG, "checking consistency of handlers list");
  421. // Check consistency of events list
  422. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  423. for (int j = 0; j < TEST_CONFIG_ITEMS_TO_REGISTER; j++) {
  424. TEST_ASSERT_FALSE(esp_event_is_handler_registered(loop, base, id, registration_data[i].handles[j]));
  425. }
  426. }
  427. // Do cleanup
  428. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  429. free(registration_data[i].handles);
  430. vSemaphoreDelete(registration_arg[i].start);
  431. vSemaphoreDelete(registration_arg[i].done);
  432. free(unregistration_data[i].handles);
  433. vSemaphoreDelete(unregistration_arg[i].start);
  434. vSemaphoreDelete(unregistration_arg[i].done);
  435. }
  436. free(registration_data);
  437. free(unregistration_data);
  438. free(registration_arg);
  439. free(unregistration_arg);
  440. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete(loop));
  441. TEST_TEARDOWN();
  442. }
  443. TEST_CASE("can post and run events", "[event]")
  444. {
  445. /* this test aims to verify that:
  446. * - multiple tasks can post to the queue simultaneously
  447. * - handlers recieve the appropriate handler arg and associated event data */
  448. TEST_SETUP();
  449. esp_event_loop_handle_t loop;
  450. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  451. loop_args.task_name = NULL;
  452. loop_args.queue_size = TEST_CONFIG_TASKS_TO_SPAWN * TEST_CONFIG_ITEMS_TO_REGISTER;
  453. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create(&loop_args, &loop));
  454. int count = 0;
  455. simple_arg_t arg = {
  456. .data = &count,
  457. .mutex = xSemaphoreCreateMutex()
  458. };
  459. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV1, test_event_simple_handler, &arg));
  460. post_event_data_t* post_event_data = calloc(TEST_CONFIG_TASKS_TO_SPAWN, sizeof(*post_event_data));
  461. task_arg_t* post_event_arg = calloc(TEST_CONFIG_TASKS_TO_SPAWN, sizeof(*post_event_arg));
  462. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++)
  463. {
  464. post_event_data[i].base = s_test_base1;
  465. post_event_data[i].id = TEST_EVENT_BASE1_EV1;
  466. post_event_data[i].loop = loop;
  467. post_event_data[i].num = TEST_CONFIG_ITEMS_TO_REGISTER;
  468. post_event_arg[i].data = &post_event_data[i];
  469. post_event_arg[i].start = xSemaphoreCreateBinary();
  470. post_event_arg[i].done = xSemaphoreCreateBinary();
  471. xTaskCreatePinnedToCore(test_event_post_task, "post", 2048, &post_event_arg[i], s_test_priority, NULL, test_event_get_core());
  472. }
  473. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  474. xSemaphoreGive(post_event_arg[i].start);
  475. }
  476. // Execute some events as they are posted
  477. for (int i = 0; i < (TEST_CONFIG_TASKS_TO_SPAWN * TEST_CONFIG_ITEMS_TO_REGISTER) / 2; i++) {
  478. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  479. }
  480. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  481. xSemaphoreTake(post_event_arg[i].done, portMAX_DELAY);
  482. }
  483. // Execute the rest
  484. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  485. TEST_ASSERT_EQUAL(TEST_CONFIG_TASKS_TO_SPAWN * TEST_CONFIG_ITEMS_TO_REGISTER, count);
  486. // Cleanup
  487. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  488. vSemaphoreDelete(post_event_arg[i].start);
  489. vSemaphoreDelete(post_event_arg[i].done);
  490. }
  491. free(post_event_data);
  492. free(post_event_arg);
  493. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete(loop));
  494. vSemaphoreDelete(arg.mutex);
  495. TEST_TEARDOWN();
  496. }
  497. static void loop_run_task(void* args)
  498. {
  499. esp_event_loop_handle_t event_loop = (esp_event_loop_handle_t) args;
  500. while(1) {
  501. esp_event_loop_run(event_loop, portMAX_DELAY);
  502. }
  503. }
  504. static void performance_test(bool dedicated_task)
  505. {
  506. TEST_SETUP();
  507. const char test_base[] = "qwertyuiopasdfghjklzxvbnmmnbvcxzqwertyuiopasdfghjklzxvbnmmnbvcxz";
  508. #define TEST_CONFIG_BASES (sizeof(test_base) - 1)
  509. #define TEST_CONFIG_IDS (TEST_CONFIG_BASES / 2)
  510. // Create loop
  511. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  512. esp_event_loop_handle_t loop;
  513. if (!dedicated_task) {
  514. loop_args.task_name = NULL;
  515. }
  516. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create(&loop_args, &loop));
  517. performance_data_t data;
  518. // Register the handlers
  519. for (int base = 0; base < TEST_CONFIG_BASES; base++) {
  520. for (int id = 0; id < TEST_CONFIG_IDS; id++) {
  521. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, test_base + base, id, test_event_performance_handler, &data));
  522. }
  523. }
  524. TaskHandle_t mtask = NULL;
  525. if (!dedicated_task) {
  526. xTaskCreate(loop_run_task, "loop_run", loop_args.task_stack_size, (void*) loop, loop_args.task_priority, &mtask);
  527. }
  528. // Perform performance test
  529. float running_sum = 0;
  530. float running_count = 0;
  531. for (int bases = 1; bases <= TEST_CONFIG_BASES; bases *= 2) {
  532. for (int ids = 1; ids <= TEST_CONFIG_IDS; ids *= 2) {
  533. data.performed = 0;
  534. data.expected = bases * ids;
  535. data.done = xSemaphoreCreateBinary();
  536. // Generate randomized list of posts
  537. int post_bases[TEST_CONFIG_BASES];
  538. int post_ids[TEST_CONFIG_IDS];
  539. for (int i = 0; i < bases; i++) {
  540. post_bases[i] = i;
  541. }
  542. for (int i = 0; i < ids; i++) {
  543. post_ids[i] = i;
  544. }
  545. for (int i = 0; i < bases; i++) {
  546. int rand_a = rand() % bases;
  547. int rand_b = rand() % bases;
  548. int temp = post_bases[rand_a];
  549. post_bases[rand_a]= post_bases[rand_b];
  550. post_bases[rand_b] = temp;
  551. }
  552. for (int i = 0; i < ids; i++) {
  553. int rand_a = rand() % ids;
  554. int rand_b = rand() % ids;
  555. int temp = post_ids[rand_a];
  556. post_ids[rand_a]= post_ids[rand_b];
  557. post_ids[rand_b] = temp;
  558. }
  559. // Post the events
  560. int64_t start = esp_timer_get_time();
  561. for (int base = 0; base < bases; base++) {
  562. for (int id = 0; id < ids; id++) {
  563. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, test_base + post_bases[base], post_ids[id], NULL, 0, portMAX_DELAY));
  564. }
  565. }
  566. xSemaphoreTake(data.done, portMAX_DELAY);
  567. int64_t elapsed = esp_timer_get_time() - start;
  568. // Record data
  569. TEST_ASSERT_EQUAL(data.expected, data.performed);
  570. running_count++;
  571. running_sum += data.performed / (elapsed / (1000000.0));
  572. vSemaphoreDelete(data.done);
  573. }
  574. }
  575. int average = (int) (running_sum / (running_count));
  576. #ifdef CONFIG_EVENT_LOOP_PROFILING
  577. ESP_LOGI(TAG, "events dispatched/second with profiling enabled: %d", average);
  578. // Enabling profiling will slow down event dispatch, so the set threshold
  579. // is not valid when it is enabled.
  580. #else
  581. #ifndef CONFIG_SPIRAM_SUPPORT
  582. TEST_PERFORMANCE_GREATER_THAN(EVENT_DISPATCH, "%d", average);
  583. #else
  584. TEST_PERFORMANCE_GREATER_THAN(EVENT_DISPATCH_PSRAM, "%d", average);
  585. #endif // CONFIG_SPIRAM_SUPPORT
  586. #endif // CONFIG_EVENT_LOOP_PROFILING
  587. if (!dedicated_task) {
  588. ((esp_event_loop_instance_t*) loop)->task = mtask;
  589. }
  590. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete(loop));
  591. TEST_TEARDOWN();
  592. }
  593. TEST_CASE("performance test - dedicated task", "[event]")
  594. {
  595. performance_test(true);
  596. }
  597. TEST_CASE("performance test - no dedicated task", "[event]")
  598. {
  599. performance_test(false);
  600. }
  601. TEST_CASE("can post to loop from handler - dedicated task", "[event]")
  602. {
  603. TEST_SETUP();
  604. esp_event_loop_handle_t loop_w_task;
  605. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  606. int count;
  607. simple_arg_t arg = {
  608. .data = &count,
  609. .mutex = xSemaphoreCreateBinary()
  610. };
  611. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create(&loop_args, &loop_w_task));
  612. count = 0;
  613. // Test that a handler can post to a different loop while there is still slots on the queue
  614. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop_w_task, s_test_base1, TEST_EVENT_BASE1_EV1, test_handler_post_w_task, &arg));
  615. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop_w_task, s_test_base1, TEST_EVENT_BASE1_EV2, test_handler_post_w_task, &arg));
  616. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop_w_task, s_test_base1, TEST_EVENT_BASE1_EV1, &loop_w_task, sizeof(&loop_w_task), portMAX_DELAY));
  617. xSemaphoreTake(arg.mutex, portMAX_DELAY);
  618. TEST_ASSERT_EQUAL(2, count);
  619. // Test that other tasks can still post while there is still slots in the queue, while handler is executing
  620. count = 100;
  621. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop_w_task, s_test_base1, TEST_EVENT_BASE1_EV1, &loop_w_task, sizeof(&loop_w_task), portMAX_DELAY));
  622. for (int i = 0; i < loop_args.queue_size; i++) {
  623. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop_w_task, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  624. }
  625. TEST_ASSERT_EQUAL(ESP_ERR_TIMEOUT, esp_event_post_to(loop_w_task, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0,
  626. pdMS_TO_TICKS(CONFIG_INT_WDT_TIMEOUT_MS * TEST_CONFIG_WAIT_MULTIPLIER)));
  627. xSemaphoreGive(arg.mutex);
  628. vTaskDelay(pdMS_TO_TICKS(CONFIG_INT_WDT_TIMEOUT_MS * TEST_CONFIG_WAIT_MULTIPLIER));
  629. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete(loop_w_task));
  630. vSemaphoreDelete(arg.mutex);
  631. TEST_TEARDOWN();
  632. }
  633. TEST_CASE("can post to loop from handler - no dedicated task", "[event]")
  634. {
  635. TEST_SETUP();
  636. esp_event_loop_handle_t loop_wo_task;
  637. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  638. int count;
  639. simple_arg_t arg = {
  640. .data = &count,
  641. .mutex = xSemaphoreCreateBinary()
  642. };
  643. count = 0;
  644. loop_args.queue_size = 1;
  645. loop_args.task_name = NULL;
  646. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create(&loop_args, &loop_wo_task));
  647. TaskHandle_t mtask;
  648. xTaskCreate(test_post_from_handler_loop_task, "task", 2584, (void*) loop_wo_task, s_test_priority, &mtask);
  649. // Test that a handler can post to a different loop while there is still slots on the queue
  650. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop_wo_task, s_test_base1, TEST_EVENT_BASE1_EV1, test_handler_post_wo_task, &arg));
  651. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop_wo_task, s_test_base1, TEST_EVENT_BASE1_EV2, test_handler_post_wo_task, &arg));
  652. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop_wo_task, s_test_base1, TEST_EVENT_BASE1_EV1, &loop_wo_task, sizeof(&loop_wo_task), portMAX_DELAY));
  653. xSemaphoreTake(arg.mutex, portMAX_DELAY);
  654. TEST_ASSERT_EQUAL(2, count);
  655. count = 100;
  656. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop_wo_task, s_test_base1, TEST_EVENT_BASE1_EV1, &loop_wo_task, sizeof(&loop_wo_task), portMAX_DELAY));
  657. vTaskDelay(pdMS_TO_TICKS(CONFIG_INT_WDT_TIMEOUT_MS * TEST_CONFIG_WAIT_MULTIPLIER));
  658. // For loop without tasks, posting is more restrictive. Posting should wait until execution of handler finishes
  659. TEST_ASSERT_EQUAL(ESP_ERR_TIMEOUT, esp_event_post_to(loop_wo_task, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0,
  660. pdMS_TO_TICKS(CONFIG_INT_WDT_TIMEOUT_MS * TEST_CONFIG_WAIT_MULTIPLIER)));
  661. xSemaphoreGive(arg.mutex);
  662. vTaskDelay(pdMS_TO_TICKS(CONFIG_INT_WDT_TIMEOUT_MS * TEST_CONFIG_WAIT_MULTIPLIER));
  663. vTaskDelete(mtask);
  664. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete(loop_wo_task));
  665. vSemaphoreDelete(arg.mutex);
  666. TEST_TEARDOWN();
  667. }
  668. static void test_event_simple_handler_template(void* handler_arg, esp_event_base_t base, int32_t id, void* event_arg)
  669. {
  670. int* count = (int*) handler_arg;
  671. (*count)++;
  672. }
  673. static void test_event_simple_handler_1(void* handler_arg, esp_event_base_t base, int32_t id, void* event_arg)
  674. {
  675. test_event_simple_handler_template(handler_arg, base, id, event_arg);
  676. }
  677. static void test_event_simple_handler_3(void* handler_arg, esp_event_base_t base, int32_t id, void* event_arg)
  678. {
  679. test_event_simple_handler_template(handler_arg, base, id, event_arg);
  680. }
  681. static void test_event_simple_handler_2(void* handler_arg, esp_event_base_t base, int32_t id, void* event_arg)
  682. {
  683. test_event_simple_handler_template(handler_arg, base, id, event_arg);
  684. }
  685. static void test_registration_from_handler_hdlr(void* handler_arg, esp_event_base_t base, int32_t id, void* event_arg)
  686. {
  687. esp_event_loop_handle_t* loop = (esp_event_loop_handle_t*) event_arg;
  688. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(*loop, s_test_base1, TEST_EVENT_BASE1_EV2, test_event_simple_handler_1, handler_arg));
  689. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(*loop, s_test_base1, TEST_EVENT_BASE1_EV2, test_event_simple_handler_2, handler_arg));
  690. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(*loop, s_test_base1, TEST_EVENT_BASE1_EV2, test_event_simple_handler_3, handler_arg));
  691. }
  692. static void test_unregistration_from_handler_hdlr(void* handler_arg, esp_event_base_t base, int32_t id, void* event_arg)
  693. {
  694. esp_event_loop_handle_t* loop = (esp_event_loop_handle_t*) event_arg;
  695. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_unregister_with(*loop, s_test_base1, TEST_EVENT_BASE1_EV2, test_event_simple_handler_1));
  696. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_unregister_with(*loop, s_test_base1, TEST_EVENT_BASE1_EV2, test_event_simple_handler_2));
  697. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_unregister_with(*loop, s_test_base1, TEST_EVENT_BASE1_EV2, test_event_simple_handler_3));
  698. }
  699. TEST_CASE("can register from handler", "[event]")
  700. {
  701. TEST_SETUP();
  702. esp_event_loop_handle_t loop;
  703. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  704. loop_args.task_name = NULL;
  705. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create(&loop_args, &loop));
  706. int count = 0;
  707. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV1, test_registration_from_handler_hdlr, &count));
  708. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base2, TEST_EVENT_BASE2_EV1, test_unregistration_from_handler_hdlr, &count));
  709. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, &loop, sizeof(&loop), portMAX_DELAY));
  710. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  711. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  712. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  713. TEST_ASSERT_EQUAL(3, count);
  714. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE2_EV1, &loop, sizeof(&loop), portMAX_DELAY));
  715. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  716. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  717. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  718. TEST_ASSERT_EQUAL(3, count);
  719. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete(loop));
  720. TEST_TEARDOWN();
  721. }
  722. static void test_create_loop_handler(void* handler_args, esp_event_base_t base, int32_t id, void* event_data)
  723. {
  724. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  725. if (id == TEST_EVENT_BASE1_EV1) {
  726. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create(&loop_args, (esp_event_loop_handle_t*) handler_args));
  727. } else {
  728. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete(*((esp_event_loop_handle_t*) handler_args)));
  729. }
  730. }
  731. TEST_CASE("can create and delete loop from handler", "[event]")
  732. {
  733. TEST_SETUP();
  734. esp_event_loop_handle_t loop;
  735. esp_event_loop_handle_t test_loop;
  736. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  737. loop_args.task_name = NULL;
  738. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create(&loop_args, &loop));
  739. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV1, test_create_loop_handler, &test_loop));
  740. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV2, test_create_loop_handler, &test_loop));
  741. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  742. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  743. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  744. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  745. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete(loop));
  746. TEST_TEARDOWN();
  747. }
  748. #ifdef CONFIG_EVENT_LOOP_PROFILING
  749. TEST_CASE("can dump event loop profile", "[event]")
  750. {
  751. /* this test aims to verify that dumping event loop statistics succeed */
  752. TEST_SETUP();
  753. esp_event_loop_handle_t loop;
  754. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  755. loop_args.task_name = NULL;
  756. loop_args.queue_size = 5;
  757. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create(&loop_args, &loop));
  758. int count = 0;
  759. simple_arg_t arg = {
  760. .data = &count,
  761. .mutex = xSemaphoreCreateMutex()
  762. };
  763. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, ESP_EVENT_ANY_BASE, ESP_EVENT_ANY_ID, test_event_simple_handler, &arg));
  764. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base1, ESP_EVENT_ANY_ID, test_event_simple_handler, &arg));
  765. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV1, test_event_simple_handler, &arg));
  766. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV2, test_event_simple_handler, &arg));
  767. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base2, TEST_EVENT_BASE1_EV1, test_event_simple_handler, &arg));
  768. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base2, TEST_EVENT_BASE1_EV2, test_event_simple_handler, &arg));
  769. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, NULL, 0, 1));
  770. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE1_EV1, NULL, 0, 1));
  771. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0, 1));
  772. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE1_EV2, NULL, 0, 1));
  773. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, NULL, 0, 1));
  774. TEST_ASSERT_EQUAL(ESP_ERR_TIMEOUT, esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE1_EV1, NULL, 0, 1));
  775. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  776. // 5 invocations of loop-levlel handlers + 3 invocation of base-level handlers (s_test_base1) +
  777. // 5 invocations of respective event-level handlers
  778. TEST_ASSERT_EQUAL(13, count);
  779. TEST_ASSERT_EQUAL(ESP_OK, esp_event_dump(stdout));
  780. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete(loop));
  781. vSemaphoreDelete(arg.mutex);
  782. TEST_TEARDOWN();
  783. }
  784. #endif