test_event.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  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 "freertos/portmacro.h"
  9. #include "esp_log.h"
  10. #include "driver/periph_ctrl.h"
  11. #include "driver/timer.h"
  12. #include "esp_event.h"
  13. #include "esp_event_private.h"
  14. #include "esp_event_internal.h"
  15. #include "esp_heap_caps.h"
  16. #include "sdkconfig.h"
  17. #include "unity.h"
  18. #include "test_utils.h"
  19. static const char* TAG = "test_event";
  20. #define TEST_CONFIG_ITEMS_TO_REGISTER 5
  21. #define TEST_CONFIG_TASKS_TO_SPAWN 2
  22. #define TEST_CONFIG_WAIT_MULTIPLIER 5
  23. // The initial logging "initializing test" is to ensure mutex allocation is not counted against memory not being freed
  24. // during teardown.
  25. #define TEST_SETUP() \
  26. ESP_LOGI(TAG, "initializing test"); \
  27. size_t free_mem_before = heap_caps_get_free_size(MALLOC_CAP_DEFAULT); \
  28. test_setup(); \
  29. s_test_core_id = xPortGetCoreID(); \
  30. s_test_priority = uxTaskPriorityGet(NULL);
  31. #define TEST_TEARDOWN() \
  32. test_teardown(); \
  33. vTaskDelay(pdMS_TO_TICKS(CONFIG_ESP_INT_WDT_TIMEOUT_MS * TEST_CONFIG_WAIT_MULTIPLIER)); \
  34. TEST_ASSERT_EQUAL(free_mem_before, heap_caps_get_free_size(MALLOC_CAP_DEFAULT));
  35. typedef struct {
  36. void* data;
  37. SemaphoreHandle_t start;
  38. SemaphoreHandle_t done;
  39. } task_arg_t;
  40. typedef struct {
  41. esp_event_base_t base;
  42. int32_t id;
  43. esp_event_handler_t* handles;
  44. int32_t num;
  45. esp_event_loop_handle_t loop;
  46. bool is_registration;
  47. } handler_registration_data_t;
  48. typedef struct {
  49. esp_event_base_t base;
  50. int32_t id;
  51. esp_event_loop_handle_t loop;
  52. int32_t num;
  53. } post_event_data_t;
  54. typedef struct {
  55. int performed;
  56. int expected;
  57. SemaphoreHandle_t done;
  58. } performance_data_t;
  59. typedef struct {
  60. void* data;
  61. SemaphoreHandle_t mutex;
  62. } simple_arg_t;
  63. typedef struct {
  64. int *arr;
  65. int index;
  66. } ordered_data_t;
  67. static BaseType_t s_test_core_id;
  68. static UBaseType_t s_test_priority;
  69. ESP_EVENT_DECLARE_BASE(s_test_base1);
  70. ESP_EVENT_DECLARE_BASE(s_test_base2);
  71. ESP_EVENT_DEFINE_BASE(s_test_base1);
  72. ESP_EVENT_DEFINE_BASE(s_test_base2);
  73. enum {
  74. TEST_EVENT_BASE1_EV1,
  75. TEST_EVENT_BASE1_EV2,
  76. TEST_EVENT_BASE1_MAX
  77. };
  78. enum {
  79. TEST_EVENT_BASE2_EV1,
  80. TEST_EVENT_BASE2_EV2,
  81. TEST_EVENT_BASE2_MAX
  82. };
  83. static BaseType_t test_event_get_core()
  84. {
  85. static int calls = 0;
  86. if (portNUM_PROCESSORS > 1) {
  87. return (s_test_core_id + calls++) % portNUM_PROCESSORS;
  88. } else {
  89. return s_test_core_id;
  90. }
  91. }
  92. static esp_event_loop_args_t test_event_get_default_loop_args()
  93. {
  94. esp_event_loop_args_t loop_config = {
  95. .queue_size = CONFIG_ESP_SYSTEM_EVENT_QUEUE_SIZE,
  96. .task_name = "loop",
  97. .task_priority = s_test_priority,
  98. .task_stack_size = 2048,
  99. .task_core_id = test_event_get_core()
  100. };
  101. return loop_config;
  102. }
  103. static void test_event_simple_handler(void* event_handler_arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
  104. {
  105. if (!event_handler_arg) {
  106. return;
  107. }
  108. simple_arg_t* arg = (simple_arg_t*) event_handler_arg;
  109. xSemaphoreTake(arg->mutex, portMAX_DELAY);
  110. int* count = (int*) arg->data;
  111. if (event_data == NULL) {
  112. (*count)++;
  113. } else {
  114. (*count) += *((int*) event_data);
  115. }
  116. xSemaphoreGive(arg->mutex);
  117. }
  118. static void test_event_ordered_dispatch(void* event_handler_arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
  119. {
  120. int *arg = (int*) event_handler_arg;
  121. ordered_data_t *data = *((ordered_data_t**) (event_data));
  122. data->arr[data->index++] = *arg;
  123. }
  124. static void test_event_performance_handler(void* event_handler_arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
  125. {
  126. performance_data_t* data = (performance_data_t*) event_handler_arg;
  127. data->performed++;
  128. if (data->performed >= data->expected) {
  129. xSemaphoreGive(data->done);
  130. }
  131. }
  132. static void test_event_post_task(void* args)
  133. {
  134. task_arg_t* arg = (task_arg_t*) args;
  135. post_event_data_t* data = arg->data;
  136. xSemaphoreTake(arg->start, portMAX_DELAY);
  137. for (int i = 0; i < data->num; i++) {
  138. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(data->loop, data->base, data->id, NULL, 0, portMAX_DELAY));
  139. vTaskDelay(1);
  140. }
  141. xSemaphoreGive(arg->done);
  142. vTaskDelete(NULL);
  143. }
  144. static void test_event_simple_handler_registration_task(void* args)
  145. {
  146. task_arg_t* arg = (task_arg_t*) args;
  147. handler_registration_data_t* data = (handler_registration_data_t*) arg->data;
  148. xSemaphoreTake(arg->start, portMAX_DELAY);
  149. for(int i = 0; i < data->num; i++) {
  150. if (data->is_registration) {
  151. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(data->loop, data->base, data->id, data->handles[i], NULL));
  152. } else {
  153. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_unregister_with(data->loop, data->base, data->id, data->handles[i]));
  154. }
  155. vTaskDelay(1);
  156. }
  157. xSemaphoreGive(arg->done);
  158. vTaskDelete(NULL);
  159. }
  160. static void test_handler_post_w_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_ERR_TIMEOUT, esp_event_post_to(*loop, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  178. }
  179. }
  180. }
  181. static void test_handler_post_wo_task(void* event_handler_arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
  182. {
  183. simple_arg_t* arg = (simple_arg_t*) event_handler_arg;
  184. esp_event_loop_handle_t* loop = (esp_event_loop_handle_t*) event_data;
  185. int* count = (int*) arg->data;
  186. (*count)++;
  187. if (*count <= 2) {
  188. if (event_base == s_test_base1 && event_id == TEST_EVENT_BASE1_EV1) {
  189. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(*loop, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  190. } else{
  191. xSemaphoreGive((SemaphoreHandle_t) arg->mutex);
  192. }
  193. } else {
  194. // Test that once the queue is full and the handler attempts to post to the same loop,
  195. // posting does not block indefinitely.
  196. if (event_base == s_test_base1 && event_id == TEST_EVENT_BASE1_EV1) {
  197. xSemaphoreTake((SemaphoreHandle_t) arg->mutex, portMAX_DELAY);
  198. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(*loop, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  199. TEST_ASSERT_EQUAL(ESP_ERR_TIMEOUT, esp_event_post_to(*loop, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  200. }
  201. }
  202. }
  203. static void test_handler_unregister_itself(void* event_handler_arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
  204. {
  205. esp_event_loop_handle_t* loop = (esp_event_loop_handle_t*) event_data;
  206. int* unregistered = (int*) event_handler_arg;
  207. (*unregistered) += (event_base == s_test_base1 ? 0 : 10) + event_id + 1;
  208. // Unregister this handler for this event
  209. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_unregister_with(*loop, event_base, event_id, test_handler_unregister_itself));
  210. }
  211. static void test_post_from_handler_loop_task(void* args)
  212. {
  213. esp_event_loop_handle_t event_loop = (esp_event_loop_handle_t) args;
  214. while(1) {
  215. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_run(event_loop, portMAX_DELAY));
  216. }
  217. }
  218. static void test_setup()
  219. {
  220. TEST_ASSERT_TRUE(TEST_CONFIG_TASKS_TO_SPAWN >= 2);
  221. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create_default());
  222. }
  223. static void test_teardown()
  224. {
  225. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete_default());
  226. }
  227. #define TIMER_DIVIDER 16 // Hardware timer clock divider
  228. #define TIMER_SCALE (TIMER_BASE_CLK / TIMER_DIVIDER) // convert counter value to seconds
  229. #define TIMER_INTERVAL0_SEC (2.0) // sample test interval for the first timer
  230. #if CONFIG_ESP_EVENT_POST_FROM_ISR
  231. static void test_handler_post_from_isr(void* event_handler_arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
  232. {
  233. SemaphoreHandle_t *sem = (SemaphoreHandle_t*) event_handler_arg;
  234. // Event data is just the address value (maybe have been truncated due to casting).
  235. int *data = (int*) event_data;
  236. TEST_ASSERT_EQUAL(*data, (int) (*sem));
  237. xSemaphoreGive(*sem);
  238. }
  239. #endif
  240. #if CONFIG_ESP_EVENT_POST_FROM_ISR
  241. void IRAM_ATTR test_event_on_timer_alarm(void* para)
  242. {
  243. /* Retrieve the interrupt status and the counter value
  244. from the timer that reported the interrupt */
  245. TIMERG0.hw_timer[TIMER_0].update = 1;
  246. uint64_t timer_counter_value =
  247. ((uint64_t) TIMERG0.hw_timer[TIMER_0].cnt_high) << 32
  248. | TIMERG0.hw_timer[TIMER_0].cnt_low;
  249. TIMERG0.int_clr_timers.t0 = 1;
  250. timer_counter_value += (uint64_t) (TIMER_INTERVAL0_SEC * TIMER_SCALE);
  251. TIMERG0.hw_timer[TIMER_0].alarm_high = (uint32_t) (timer_counter_value >> 32);
  252. TIMERG0.hw_timer[TIMER_0].alarm_low = (uint32_t) timer_counter_value;
  253. int data = (int) para;
  254. // Posting events with data more than 4 bytes should fail.
  255. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_event_isr_post(s_test_base1, TEST_EVENT_BASE1_EV1, &data, 5, NULL));
  256. // This should succeedd, as data is int-sized. The handler for the event checks that the passed event data
  257. // is correct.
  258. BaseType_t task_unblocked;
  259. TEST_ASSERT_EQUAL(ESP_OK, esp_event_isr_post(s_test_base1, TEST_EVENT_BASE1_EV1, &data, sizeof(data), &task_unblocked));
  260. if (task_unblocked == pdTRUE) {
  261. portYIELD_FROM_ISR();
  262. }
  263. }
  264. #endif //CONFIG_ESP_EVENT_POST_FROM_ISR
  265. TEST_CASE("create and event loop with any NULL argument fails", "[event]")
  266. {
  267. esp_event_loop_handle_t loop; // with dedicated task
  268. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  269. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_event_loop_create(NULL, &loop));
  270. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_event_loop_create(&loop_args, NULL));
  271. }
  272. TEST_CASE("can create and delete event loops", "[event]")
  273. {
  274. /* this test aims to verify that:
  275. * - creating loops with and without a task succeeds
  276. * - event queue can accomodate the set queue size, and drops the post when exceeded
  277. * - deleting loops with unconsumed posts and unregistered handlers (when unregistration is enabled) does not leak memory */
  278. TEST_SETUP();
  279. esp_event_loop_handle_t loop1; // with dedicated task
  280. esp_event_loop_handle_t loop2; // without dedicated task
  281. esp_event_loop_handle_t loop3; // with leftover post and handlers
  282. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  283. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create(&loop_args, &loop1));
  284. loop_args.task_name = NULL;
  285. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create(&loop_args, &loop2));
  286. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create(&loop_args, &loop3));
  287. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop3, s_test_base1, TEST_EVENT_BASE1_EV1, (void*) 0x00000001, NULL));
  288. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop3, s_test_base1, TEST_EVENT_BASE1_EV2, (void*) 0x00000002, NULL));
  289. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop3, s_test_base2, TEST_EVENT_BASE1_EV1, (void*) 0x00000003, NULL));
  290. for (int i = 0; i < loop_args.queue_size; i++) {
  291. int mod = i % 4;
  292. switch(mod) {
  293. case 0:
  294. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop3, s_test_base1, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  295. break;
  296. case 1:
  297. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop3, s_test_base2, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  298. break;
  299. case 2:
  300. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop3, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  301. break;
  302. case 3:
  303. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop3, s_test_base2, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  304. break;
  305. default:
  306. break;
  307. }
  308. }
  309. TEST_ASSERT_EQUAL(ESP_ERR_TIMEOUT, esp_event_post_to(loop3, s_test_base1, TEST_EVENT_BASE1_EV1, NULL, 0, pdMS_TO_TICKS(10)));
  310. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete(loop1));
  311. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete(loop2));
  312. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete(loop3));
  313. TEST_TEARDOWN();
  314. }
  315. TEST_CASE("can register/unregister handlers for all events/all events for a specific base", "[event]")
  316. {
  317. /* this test aims to verify that handlers can be registered to be called on all events
  318. * or for all events with specific bases */
  319. TEST_SETUP();
  320. esp_event_loop_handle_t loop;
  321. int count = 0;
  322. simple_arg_t arg = {
  323. .data = &count,
  324. .mutex = xSemaphoreCreateMutex()
  325. };
  326. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  327. loop_args.task_name = NULL;
  328. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create(&loop_args, &loop));
  329. /* Register the handler twice to the same base and id but with a different argument (expects to return ESP_OK and log a warning)
  330. * This aims to verify: 1) Handler's argument to be updated
  331. * 2) Registration not to leak memory
  332. */
  333. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, ESP_EVENT_ANY_BASE, ESP_EVENT_ANY_ID, test_event_simple_handler, NULL));
  334. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, ESP_EVENT_ANY_BASE, ESP_EVENT_ANY_ID, test_event_simple_handler, &arg));
  335. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base1, ESP_EVENT_ANY_ID, test_event_simple_handler, &arg));
  336. 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));
  337. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV1, test_event_simple_handler, &arg));
  338. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV2, test_event_simple_handler, &arg));
  339. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base2, TEST_EVENT_BASE1_EV1, test_event_simple_handler, &arg));
  340. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_event_post_to(loop, ESP_EVENT_ANY_BASE, ESP_EVENT_ANY_ID, NULL, 0, portMAX_DELAY));
  341. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_event_post_to(loop, s_test_base1, ESP_EVENT_ANY_ID, NULL, 0, portMAX_DELAY));
  342. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_event_post_to(loop, ESP_EVENT_ANY_BASE, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  343. 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)
  344. 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)
  345. // Post unknown events. Respective loop level and base level handlers should still execute.
  346. 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)
  347. 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)
  348. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  349. TEST_ASSERT_EQUAL(9, count); // 3 + 3 + 2 + 1
  350. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete(loop));
  351. vSemaphoreDelete(arg.mutex);
  352. TEST_TEARDOWN();
  353. }
  354. TEST_CASE("can unregister handler", "[event]")
  355. {
  356. /* this test aims to verify that unregistered handlers no longer execute when events are raised */
  357. TEST_SETUP();
  358. esp_event_loop_handle_t loop;
  359. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  360. loop_args.task_name = NULL;
  361. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create(&loop_args, &loop));
  362. int count = 0;
  363. simple_arg_t arg = {
  364. .data = &count,
  365. .mutex = xSemaphoreCreateMutex()
  366. };
  367. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV1, test_event_simple_handler, &arg));
  368. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base2, TEST_EVENT_BASE1_EV1, test_event_simple_handler, &arg));
  369. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  370. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  371. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  372. TEST_ASSERT_EQUAL(2, count);
  373. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_unregister_with(loop, s_test_base1, TEST_EVENT_BASE1_EV1, test_event_simple_handler));
  374. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  375. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  376. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  377. TEST_ASSERT_EQUAL(3, count);
  378. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete(loop));
  379. vSemaphoreDelete(arg.mutex);
  380. TEST_TEARDOWN();
  381. }
  382. TEST_CASE("handler can unregister itself", "[event]")
  383. {
  384. /* this test aims to verify that handlers can unregister themselves */
  385. TEST_SETUP();
  386. esp_event_loop_handle_t loop;
  387. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  388. loop_args.task_name = NULL;
  389. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create(&loop_args, &loop));
  390. int unregistered = 0;
  391. /*
  392. * s_test_base1, ev1 = 1
  393. * s_test_base1, ev2 = 2
  394. * s_test_base2, ev1 = 11
  395. * s_test_base2, ev2 = 12
  396. */
  397. int expected_unregistered = 0;
  398. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV1, test_handler_unregister_itself, &unregistered));
  399. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV2, test_handler_unregister_itself, &unregistered));
  400. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base2, TEST_EVENT_BASE2_EV1, test_handler_unregister_itself, &unregistered));
  401. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base2, TEST_EVENT_BASE2_EV2, test_handler_unregister_itself, &unregistered));
  402. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV2, &loop, sizeof(loop), portMAX_DELAY));
  403. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  404. expected_unregistered = 2; // base1, ev2
  405. TEST_ASSERT_EQUAL(expected_unregistered, unregistered);
  406. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, &loop, sizeof(loop), portMAX_DELAY));
  407. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE2_EV1, &loop, sizeof(loop), portMAX_DELAY));
  408. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  409. expected_unregistered += 1 + 11; // base1, ev1 + base2, ev1
  410. TEST_ASSERT_EQUAL(expected_unregistered, unregistered);
  411. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE2_EV2, &loop, sizeof(loop), portMAX_DELAY));
  412. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  413. expected_unregistered += 12; // base2, ev2
  414. TEST_ASSERT_EQUAL(expected_unregistered, unregistered);
  415. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, &loop, sizeof(loop), portMAX_DELAY));
  416. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV2, &loop, sizeof(loop), portMAX_DELAY));
  417. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE2_EV1, &loop, sizeof(loop), portMAX_DELAY));
  418. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE2_EV2, &loop, sizeof(loop), portMAX_DELAY));
  419. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  420. TEST_ASSERT_EQUAL(expected_unregistered, unregistered); // all handlers unregistered
  421. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete(loop));
  422. TEST_TEARDOWN();
  423. }
  424. TEST_CASE("can exit running loop at approximately the set amount of time", "[event]")
  425. {
  426. /* this test aims to verify that running loop does not block indefinitely in cases where
  427. * events are posted frequently */
  428. TEST_SETUP();
  429. esp_event_loop_handle_t loop;
  430. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  431. loop_args.task_name = NULL;
  432. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create(&loop_args, &loop));
  433. performance_data_t handler_data = {
  434. .performed = 0,
  435. .expected = INT32_MAX,
  436. .done = xSemaphoreCreateBinary()
  437. };
  438. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV1, test_event_performance_handler, &handler_data));
  439. post_event_data_t post_event_data = {
  440. .base = s_test_base1,
  441. .id = TEST_EVENT_BASE1_EV1,
  442. .loop = loop,
  443. .num = INT32_MAX
  444. };
  445. task_arg_t post_event_arg = {
  446. .data = &post_event_data,
  447. .done = xSemaphoreCreateBinary(),
  448. .start = xSemaphoreCreateBinary()
  449. };
  450. TaskHandle_t post_task;
  451. xTaskCreatePinnedToCore(test_event_post_task, "post", 2048, &post_event_arg, s_test_priority, &post_task, test_event_get_core());
  452. int runtime_ms = 10;
  453. int runtime_us = runtime_ms * 1000;
  454. int64_t start, diff;
  455. start = esp_timer_get_time();
  456. xSemaphoreGive(post_event_arg.start);
  457. // Run the loop for the runtime_ms set amount of time, regardless of whether events
  458. // are still being posted to the loop.
  459. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_run(loop, pdMS_TO_TICKS(runtime_ms)));
  460. diff = (esp_timer_get_time() - start);
  461. // Threshold is 25 percent.
  462. TEST_ASSERT(diff < runtime_us * 1.25f);
  463. // Verify that the post task still continues
  464. TEST_ASSERT_NOT_EQUAL(pdTRUE, xSemaphoreTake(post_event_arg.done, pdMS_TO_TICKS(10)));
  465. vSemaphoreDelete(post_event_arg.done);
  466. vSemaphoreDelete(post_event_arg.start);
  467. vSemaphoreDelete(handler_data.done);
  468. vTaskDelete(post_task);
  469. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete(loop));
  470. TEST_TEARDOWN();
  471. }
  472. TEST_CASE("can register/unregister handlers simultaneously", "[event]")
  473. {
  474. /* this test aims to verify that the event handlers list remains consistent despite
  475. * simultaneous access by differenct tasks */
  476. TEST_SETUP();
  477. const char* base = "base";
  478. int32_t id = 0;
  479. esp_event_loop_handle_t loop;
  480. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  481. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create(&loop_args, &loop));
  482. ESP_LOGI(TAG, "registering handlers");
  483. handler_registration_data_t* registration_data = calloc(TEST_CONFIG_TASKS_TO_SPAWN, sizeof(*registration_data));
  484. task_arg_t* registration_arg = calloc(TEST_CONFIG_TASKS_TO_SPAWN, sizeof(*registration_arg));
  485. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  486. registration_data[i].base = base;
  487. registration_data[i].id = id;
  488. registration_data[i].loop = loop;
  489. registration_data[i].handles = calloc(TEST_CONFIG_ITEMS_TO_REGISTER, sizeof(esp_event_handler_t));
  490. registration_data[i].num = TEST_CONFIG_ITEMS_TO_REGISTER;
  491. registration_data[i].is_registration = true;
  492. for (int j = 0; j < TEST_CONFIG_ITEMS_TO_REGISTER; j++) {
  493. registration_data[i].handles[j] = (void*) (i * TEST_CONFIG_ITEMS_TO_REGISTER) + (j + TEST_CONFIG_ITEMS_TO_REGISTER);
  494. }
  495. registration_arg[i].start = xSemaphoreCreateBinary();
  496. registration_arg[i].done = xSemaphoreCreateBinary();
  497. registration_arg[i].data = &registration_data[i];
  498. xTaskCreatePinnedToCore(test_event_simple_handler_registration_task, "register", 2048, &registration_arg[i], s_test_priority, NULL, test_event_get_core());
  499. }
  500. // Give the semaphores to the spawned registration task
  501. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  502. xSemaphoreGive(registration_arg[i].start);
  503. }
  504. // Take the same semaphores in order to proceed
  505. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  506. xSemaphoreTake(registration_arg[i].done, portMAX_DELAY);
  507. }
  508. ESP_LOGI(TAG, "checking consistency of handlers list");
  509. // Check consistency of events list
  510. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  511. for (int j = 0; j < TEST_CONFIG_ITEMS_TO_REGISTER; j++) {
  512. TEST_ASSERT_TRUE(esp_event_is_handler_registered(loop, base, id, registration_data[i].handles[j]));
  513. }
  514. }
  515. ESP_LOGI(TAG, "unregistering handlers");
  516. /* Test if tasks can unregister simultaneously */
  517. // Unregister registered events
  518. handler_registration_data_t* unregistration_data = calloc(TEST_CONFIG_TASKS_TO_SPAWN, sizeof(*unregistration_data));
  519. task_arg_t* unregistration_arg = calloc(TEST_CONFIG_TASKS_TO_SPAWN, sizeof(*unregistration_arg));
  520. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  521. unregistration_data[i].base = base;
  522. unregistration_data[i].id = id;
  523. unregistration_data[i].loop = loop;
  524. unregistration_data[i].handles = calloc(TEST_CONFIG_ITEMS_TO_REGISTER, sizeof(esp_event_handler_t));
  525. unregistration_data[i].num = TEST_CONFIG_ITEMS_TO_REGISTER;
  526. unregistration_data[i].is_registration = false;
  527. memcpy(unregistration_data[i].handles, registration_data[i].handles, TEST_CONFIG_ITEMS_TO_REGISTER * sizeof(esp_event_handler_t));
  528. unregistration_arg[i].data = &unregistration_data[i];
  529. unregistration_arg[i].start = xSemaphoreCreateBinary();
  530. unregistration_arg[i].done = xSemaphoreCreateBinary();
  531. xTaskCreatePinnedToCore(test_event_simple_handler_registration_task, "unregister", 2048, &unregistration_arg[i], s_test_priority, NULL, test_event_get_core());
  532. }
  533. // Give the semaphores to the spawned unregistration task
  534. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  535. xSemaphoreGive(unregistration_arg[i].start);
  536. }
  537. // Take the same semaphores in order to proceed
  538. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  539. xSemaphoreTake(unregistration_arg[i].done, portMAX_DELAY);
  540. }
  541. ESP_LOGI(TAG, "checking consistency of handlers list");
  542. // Check consistency of events list
  543. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  544. for (int j = 0; j < TEST_CONFIG_ITEMS_TO_REGISTER; j++) {
  545. TEST_ASSERT_FALSE(esp_event_is_handler_registered(loop, base, id, registration_data[i].handles[j]));
  546. }
  547. }
  548. // Do cleanup
  549. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  550. free(registration_data[i].handles);
  551. vSemaphoreDelete(registration_arg[i].start);
  552. vSemaphoreDelete(registration_arg[i].done);
  553. free(unregistration_data[i].handles);
  554. vSemaphoreDelete(unregistration_arg[i].start);
  555. vSemaphoreDelete(unregistration_arg[i].done);
  556. }
  557. free(registration_data);
  558. free(unregistration_data);
  559. free(registration_arg);
  560. free(unregistration_arg);
  561. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete(loop));
  562. TEST_TEARDOWN();
  563. }
  564. TEST_CASE("can post and run events", "[event]")
  565. {
  566. /* this test aims to verify that:
  567. * - multiple tasks can post to the queue simultaneously
  568. * - handlers recieve the appropriate handler arg and associated event data */
  569. TEST_SETUP();
  570. esp_event_loop_handle_t loop;
  571. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  572. loop_args.task_name = NULL;
  573. loop_args.queue_size = TEST_CONFIG_TASKS_TO_SPAWN * TEST_CONFIG_ITEMS_TO_REGISTER;
  574. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create(&loop_args, &loop));
  575. int count = 0;
  576. simple_arg_t arg = {
  577. .data = &count,
  578. .mutex = xSemaphoreCreateMutex()
  579. };
  580. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV1, test_event_simple_handler, &arg));
  581. post_event_data_t* post_event_data = calloc(TEST_CONFIG_TASKS_TO_SPAWN, sizeof(*post_event_data));
  582. task_arg_t* post_event_arg = calloc(TEST_CONFIG_TASKS_TO_SPAWN, sizeof(*post_event_arg));
  583. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++)
  584. {
  585. post_event_data[i].base = s_test_base1;
  586. post_event_data[i].id = TEST_EVENT_BASE1_EV1;
  587. post_event_data[i].loop = loop;
  588. post_event_data[i].num = TEST_CONFIG_ITEMS_TO_REGISTER;
  589. post_event_arg[i].data = &post_event_data[i];
  590. post_event_arg[i].start = xSemaphoreCreateBinary();
  591. post_event_arg[i].done = xSemaphoreCreateBinary();
  592. xTaskCreatePinnedToCore(test_event_post_task, "post", 2048, &post_event_arg[i], s_test_priority, NULL, test_event_get_core());
  593. }
  594. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  595. xSemaphoreGive(post_event_arg[i].start);
  596. }
  597. // Execute some events as they are posted
  598. for (int i = 0; i < (TEST_CONFIG_TASKS_TO_SPAWN * TEST_CONFIG_ITEMS_TO_REGISTER) / 2; i++) {
  599. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  600. }
  601. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  602. xSemaphoreTake(post_event_arg[i].done, portMAX_DELAY);
  603. }
  604. // Execute the rest
  605. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  606. TEST_ASSERT_EQUAL(TEST_CONFIG_TASKS_TO_SPAWN * TEST_CONFIG_ITEMS_TO_REGISTER, count);
  607. // Cleanup
  608. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  609. vSemaphoreDelete(post_event_arg[i].start);
  610. vSemaphoreDelete(post_event_arg[i].done);
  611. }
  612. free(post_event_data);
  613. free(post_event_arg);
  614. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete(loop));
  615. vSemaphoreDelete(arg.mutex);
  616. TEST_TEARDOWN();
  617. }
  618. static void loop_run_task(void* args)
  619. {
  620. esp_event_loop_handle_t event_loop = (esp_event_loop_handle_t) args;
  621. while(1) {
  622. esp_event_loop_run(event_loop, portMAX_DELAY);
  623. }
  624. }
  625. static void performance_test(bool dedicated_task)
  626. {
  627. // rand() seems to do a one-time allocation. Call it here so that the memory it allocates
  628. // is not counted as a leak.
  629. unsigned int _rand __attribute__((unused)) = rand();
  630. TEST_SETUP();
  631. const char test_base[] = "qwertyuiopasdfghjklzxvbnmmnbvcxzqwertyuiopasdfghjklzxvbnmmnbvcxz";
  632. #define TEST_CONFIG_BASES (sizeof(test_base) - 1)
  633. #define TEST_CONFIG_IDS (TEST_CONFIG_BASES / 2)
  634. // Create loop
  635. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  636. esp_event_loop_handle_t loop;
  637. if (!dedicated_task) {
  638. loop_args.task_name = NULL;
  639. }
  640. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create(&loop_args, &loop));
  641. performance_data_t data;
  642. // Register the handlers
  643. for (int base = 0; base < TEST_CONFIG_BASES; base++) {
  644. for (int id = 0; id < TEST_CONFIG_IDS; id++) {
  645. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, test_base + base, id, test_event_performance_handler, &data));
  646. }
  647. }
  648. TaskHandle_t mtask = NULL;
  649. if (!dedicated_task) {
  650. xTaskCreate(loop_run_task, "loop_run", loop_args.task_stack_size, (void*) loop, loop_args.task_priority, &mtask);
  651. }
  652. // Perform performance test
  653. float running_sum = 0;
  654. float running_count = 0;
  655. for (int bases = 1; bases <= TEST_CONFIG_BASES; bases *= 2) {
  656. for (int ids = 1; ids <= TEST_CONFIG_IDS; ids *= 2) {
  657. data.performed = 0;
  658. data.expected = bases * ids;
  659. data.done = xSemaphoreCreateBinary();
  660. // Generate randomized list of posts
  661. int post_bases[TEST_CONFIG_BASES];
  662. int post_ids[TEST_CONFIG_IDS];
  663. for (int i = 0; i < bases; i++) {
  664. post_bases[i] = i;
  665. }
  666. for (int i = 0; i < ids; i++) {
  667. post_ids[i] = i;
  668. }
  669. for (int i = 0; i < bases; i++) {
  670. int rand_a = rand() % bases;
  671. int rand_b = rand() % bases;
  672. int temp = post_bases[rand_a];
  673. post_bases[rand_a]= post_bases[rand_b];
  674. post_bases[rand_b] = temp;
  675. }
  676. for (int i = 0; i < ids; i++) {
  677. int rand_a = rand() % ids;
  678. int rand_b = rand() % ids;
  679. int temp = post_ids[rand_a];
  680. post_ids[rand_a]= post_ids[rand_b];
  681. post_ids[rand_b] = temp;
  682. }
  683. // Post the events
  684. int64_t start = esp_timer_get_time();
  685. for (int base = 0; base < bases; base++) {
  686. for (int id = 0; id < ids; id++) {
  687. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, test_base + post_bases[base], post_ids[id], NULL, 0, portMAX_DELAY));
  688. }
  689. }
  690. xSemaphoreTake(data.done, portMAX_DELAY);
  691. int64_t elapsed = esp_timer_get_time() - start;
  692. // Record data
  693. TEST_ASSERT_EQUAL(data.expected, data.performed);
  694. running_count++;
  695. running_sum += data.performed / (elapsed / (1000000.0));
  696. vSemaphoreDelete(data.done);
  697. }
  698. }
  699. int average = (int) (running_sum / (running_count));
  700. if (!dedicated_task) {
  701. ((esp_event_loop_instance_t*) loop)->task = mtask;
  702. }
  703. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete(loop));
  704. TEST_TEARDOWN();
  705. #ifdef CONFIG_ESP_EVENT_LOOP_PROFILING
  706. ESP_LOGI(TAG, "events dispatched/second with profiling enabled: %d", average);
  707. // Enabling profiling will slow down event dispatch, so the set threshold
  708. // is not valid when it is enabled.
  709. #else
  710. #ifndef CONFIG_ESP32_SPIRAM_SUPPORT
  711. TEST_PERFORMANCE_GREATER_THAN(EVENT_DISPATCH, "%d", average);
  712. #else
  713. TEST_PERFORMANCE_GREATER_THAN(EVENT_DISPATCH_PSRAM, "%d", average);
  714. #endif // CONFIG_ESP32_SPIRAM_SUPPORT
  715. #endif // CONFIG_ESP_EVENT_LOOP_PROFILING
  716. }
  717. TEST_CASE("performance test - dedicated task", "[event]")
  718. {
  719. performance_test(true);
  720. }
  721. TEST_CASE("performance test - no dedicated task", "[event]")
  722. {
  723. performance_test(false);
  724. }
  725. TEST_CASE("can post to loop from handler - dedicated task", "[event]")
  726. {
  727. TEST_SETUP();
  728. esp_event_loop_handle_t loop_w_task;
  729. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  730. int count;
  731. simple_arg_t arg = {
  732. .data = &count,
  733. .mutex = xSemaphoreCreateBinary()
  734. };
  735. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create(&loop_args, &loop_w_task));
  736. count = 0;
  737. // Test that a handler can post to a different loop while there is still slots on the queue
  738. 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));
  739. 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));
  740. 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));
  741. xSemaphoreTake(arg.mutex, portMAX_DELAY);
  742. TEST_ASSERT_EQUAL(2, count);
  743. // Test that other tasks can still post while there is still slots in the queue, while handler is executing
  744. count = 100;
  745. 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));
  746. for (int i = 0; i < loop_args.queue_size; i++) {
  747. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop_w_task, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  748. }
  749. TEST_ASSERT_EQUAL(ESP_ERR_TIMEOUT, esp_event_post_to(loop_w_task, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0,
  750. pdMS_TO_TICKS(CONFIG_ESP_INT_WDT_TIMEOUT_MS * TEST_CONFIG_WAIT_MULTIPLIER)));
  751. xSemaphoreGive(arg.mutex);
  752. vTaskDelay(pdMS_TO_TICKS(CONFIG_ESP_INT_WDT_TIMEOUT_MS * TEST_CONFIG_WAIT_MULTIPLIER));
  753. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete(loop_w_task));
  754. vSemaphoreDelete(arg.mutex);
  755. TEST_TEARDOWN();
  756. }
  757. TEST_CASE("can post to loop from handler - no dedicated task", "[event]")
  758. {
  759. TEST_SETUP();
  760. esp_event_loop_handle_t loop_wo_task;
  761. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  762. int count;
  763. simple_arg_t arg = {
  764. .data = &count,
  765. .mutex = xSemaphoreCreateBinary()
  766. };
  767. count = 0;
  768. loop_args.queue_size = 1;
  769. loop_args.task_name = NULL;
  770. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create(&loop_args, &loop_wo_task));
  771. TaskHandle_t mtask;
  772. xTaskCreate(test_post_from_handler_loop_task, "task", 2584, (void*) loop_wo_task, s_test_priority, &mtask);
  773. // Test that a handler can post to a different loop while there is still slots on the queue
  774. 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));
  775. 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));
  776. 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));
  777. xSemaphoreTake(arg.mutex, portMAX_DELAY);
  778. TEST_ASSERT_EQUAL(2, count);
  779. count = 100;
  780. 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));
  781. vTaskDelay(pdMS_TO_TICKS(CONFIG_ESP_INT_WDT_TIMEOUT_MS * TEST_CONFIG_WAIT_MULTIPLIER));
  782. // For loop without tasks, posting is more restrictive. Posting should wait until execution of handler finishes
  783. TEST_ASSERT_EQUAL(ESP_ERR_TIMEOUT, esp_event_post_to(loop_wo_task, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0,
  784. pdMS_TO_TICKS(CONFIG_ESP_INT_WDT_TIMEOUT_MS * TEST_CONFIG_WAIT_MULTIPLIER)));
  785. xSemaphoreGive(arg.mutex);
  786. vTaskDelay(pdMS_TO_TICKS(CONFIG_ESP_INT_WDT_TIMEOUT_MS * TEST_CONFIG_WAIT_MULTIPLIER));
  787. vTaskDelete(mtask);
  788. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete(loop_wo_task));
  789. vSemaphoreDelete(arg.mutex);
  790. TEST_TEARDOWN();
  791. }
  792. static void test_event_simple_handler_template(void* handler_arg, esp_event_base_t base, int32_t id, void* event_arg)
  793. {
  794. int* count = (int*) handler_arg;
  795. (*count)++;
  796. }
  797. static void test_event_simple_handler_1(void* handler_arg, esp_event_base_t base, int32_t id, void* event_arg)
  798. {
  799. test_event_simple_handler_template(handler_arg, base, id, event_arg);
  800. }
  801. static void test_event_simple_handler_3(void* handler_arg, esp_event_base_t base, int32_t id, void* event_arg)
  802. {
  803. test_event_simple_handler_template(handler_arg, base, id, event_arg);
  804. }
  805. static void test_event_simple_handler_2(void* handler_arg, esp_event_base_t base, int32_t id, void* event_arg)
  806. {
  807. test_event_simple_handler_template(handler_arg, base, id, event_arg);
  808. }
  809. static void test_registration_from_handler_hdlr(void* handler_arg, esp_event_base_t base, int32_t id, void* event_arg)
  810. {
  811. esp_event_loop_handle_t* loop = (esp_event_loop_handle_t*) event_arg;
  812. 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));
  813. 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));
  814. 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));
  815. }
  816. static void test_unregistration_from_handler_hdlr(void* handler_arg, esp_event_base_t base, int32_t id, void* event_arg)
  817. {
  818. esp_event_loop_handle_t* loop = (esp_event_loop_handle_t*) event_arg;
  819. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_unregister_with(*loop, s_test_base1, TEST_EVENT_BASE1_EV2, test_event_simple_handler_1));
  820. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_unregister_with(*loop, s_test_base1, TEST_EVENT_BASE1_EV2, test_event_simple_handler_2));
  821. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_unregister_with(*loop, s_test_base1, TEST_EVENT_BASE1_EV2, test_event_simple_handler_3));
  822. }
  823. TEST_CASE("can register from handler", "[event]")
  824. {
  825. TEST_SETUP();
  826. esp_event_loop_handle_t loop;
  827. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  828. loop_args.task_name = NULL;
  829. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create(&loop_args, &loop));
  830. int count = 0;
  831. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV1, test_registration_from_handler_hdlr, &count));
  832. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base2, TEST_EVENT_BASE2_EV1, test_unregistration_from_handler_hdlr, &count));
  833. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, &loop, sizeof(&loop), portMAX_DELAY));
  834. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  835. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  836. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  837. TEST_ASSERT_EQUAL(3, count);
  838. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE2_EV1, &loop, sizeof(&loop), portMAX_DELAY));
  839. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  840. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  841. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  842. TEST_ASSERT_EQUAL(3, count);
  843. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete(loop));
  844. TEST_TEARDOWN();
  845. }
  846. static void test_create_loop_handler(void* handler_args, esp_event_base_t base, int32_t id, void* event_data)
  847. {
  848. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  849. if (id == TEST_EVENT_BASE1_EV1) {
  850. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create(&loop_args, (esp_event_loop_handle_t*) handler_args));
  851. } else {
  852. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete(*((esp_event_loop_handle_t*) handler_args)));
  853. }
  854. }
  855. TEST_CASE("can create and delete loop from handler", "[event]")
  856. {
  857. TEST_SETUP();
  858. esp_event_loop_handle_t loop;
  859. esp_event_loop_handle_t test_loop;
  860. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  861. loop_args.task_name = NULL;
  862. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create(&loop_args, &loop));
  863. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV1, test_create_loop_handler, &test_loop));
  864. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV2, test_create_loop_handler, &test_loop));
  865. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  866. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  867. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  868. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  869. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete(loop));
  870. TEST_TEARDOWN();
  871. }
  872. TEST_CASE("events are dispatched in the order they are registered", "[event]")
  873. {
  874. TEST_SETUP();
  875. esp_event_loop_handle_t loop;
  876. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  877. loop_args.task_name = NULL;
  878. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create(&loop_args, &loop));
  879. int id_arr[7];
  880. for (int i = 0; i < 7; i++) {
  881. id_arr[i] = i;
  882. }
  883. int data_arr[12] = {0};
  884. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base2, TEST_EVENT_BASE2_EV1, test_event_ordered_dispatch, id_arr + 0));
  885. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, ESP_EVENT_ANY_BASE, ESP_EVENT_ANY_ID, test_event_ordered_dispatch, id_arr + 1));
  886. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base1, ESP_EVENT_ANY_ID, test_event_ordered_dispatch, id_arr + 2));
  887. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base2, TEST_EVENT_BASE2_EV2, test_event_ordered_dispatch, id_arr + 3));
  888. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV1, test_event_ordered_dispatch, id_arr + 4));
  889. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base2, ESP_EVENT_ANY_ID, test_event_ordered_dispatch, id_arr + 5));
  890. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV2, test_event_ordered_dispatch, id_arr + 6));
  891. esp_event_dump(stdout);
  892. ordered_data_t data = {
  893. .arr = data_arr,
  894. .index = 0
  895. };
  896. ordered_data_t* dptr = &data;
  897. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE1_EV2, &dptr, sizeof(dptr), portMAX_DELAY));
  898. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, &dptr, sizeof(dptr), portMAX_DELAY));
  899. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV2, &dptr, sizeof(dptr), portMAX_DELAY));
  900. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE1_EV1, &dptr, sizeof(dptr), portMAX_DELAY));
  901. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  902. // Expected data executing the posts above
  903. int ref_arr[12] = {1, 3, 5, 1, 2, 4, 1, 2, 6, 0, 1, 5};
  904. for (int i = 0; i < 12; i++) {
  905. TEST_ASSERT_EQUAL(ref_arr[i], data_arr[i]);
  906. }
  907. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete(loop));
  908. TEST_TEARDOWN();
  909. }
  910. #if CONFIG_ESP_EVENT_POST_FROM_ISR
  911. TEST_CASE("can properly prepare event data posted to loop", "[event]")
  912. {
  913. TEST_SETUP();
  914. esp_event_loop_handle_t loop;
  915. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  916. loop_args.task_name = NULL;
  917. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create(&loop_args, &loop));
  918. esp_event_post_instance_t post;
  919. esp_event_loop_instance_t* loop_def = (esp_event_loop_instance_t*) loop;
  920. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  921. TEST_ASSERT_EQUAL(pdTRUE, xQueueReceive(loop_def->queue, &post, portMAX_DELAY));
  922. TEST_ASSERT_EQUAL(false, post.data_set);
  923. TEST_ASSERT_EQUAL(false, post.data_allocated);
  924. TEST_ASSERT_EQUAL(NULL, post.data.ptr);
  925. int sample = 0;
  926. TEST_ASSERT_EQUAL(ESP_OK, esp_event_isr_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, &sample, sizeof(sample), NULL));
  927. TEST_ASSERT_EQUAL(pdTRUE, xQueueReceive(loop_def->queue, &post, portMAX_DELAY));
  928. TEST_ASSERT_EQUAL(true, post.data_set);
  929. TEST_ASSERT_EQUAL(false, post.data_allocated);
  930. TEST_ASSERT_EQUAL(false, post.data.val);
  931. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete(loop));
  932. TEST_TEARDOWN();
  933. }
  934. TEST_CASE("can post events from interrupt handler", "[event]")
  935. {
  936. SemaphoreHandle_t sem = xSemaphoreCreateBinary();
  937. /* Select and initialize basic parameters of the timer */
  938. timer_config_t config;
  939. config.divider = TIMER_DIVIDER;
  940. config.counter_dir = TIMER_COUNT_UP;
  941. config.counter_en = TIMER_PAUSE;
  942. config.alarm_en = TIMER_ALARM_EN;
  943. config.intr_type = TIMER_INTR_LEVEL;
  944. config.auto_reload = false;
  945. timer_init(TIMER_GROUP_0, TIMER_0, &config);
  946. /* Timer's counter will initially start from value below.
  947. Also, if auto_reload is set, this value will be automatically reload on alarm */
  948. timer_set_counter_value(TIMER_GROUP_0, TIMER_0, 0x00000000ULL);
  949. /* Configure the alarm value and the interrupt on alarm. */
  950. timer_set_alarm_value(TIMER_GROUP_0, TIMER_0, TIMER_INTERVAL0_SEC * TIMER_SCALE);
  951. timer_enable_intr(TIMER_GROUP_0, TIMER_0);
  952. timer_isr_register(TIMER_GROUP_0, TIMER_0, test_event_on_timer_alarm,
  953. (void *) sem, ESP_INTR_FLAG_IRAM, NULL);
  954. timer_start(TIMER_GROUP_0, TIMER_0);
  955. TEST_SETUP();
  956. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register(s_test_base1, TEST_EVENT_BASE1_EV1,
  957. test_handler_post_from_isr, &sem));
  958. xSemaphoreTake(sem, portMAX_DELAY);
  959. TEST_TEARDOWN();
  960. }
  961. #endif
  962. #ifdef CONFIG_ESP_EVENT_LOOP_PROFILING
  963. TEST_CASE("can dump event loop profile", "[event]")
  964. {
  965. /* this test aims to verify that dumping event loop statistics succeed */
  966. TEST_SETUP();
  967. esp_event_loop_handle_t loop;
  968. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  969. loop_args.task_name = NULL;
  970. loop_args.queue_size = 5;
  971. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create(&loop_args, &loop));
  972. int count = 0;
  973. simple_arg_t arg = {
  974. .data = &count,
  975. .mutex = xSemaphoreCreateMutex()
  976. };
  977. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, ESP_EVENT_ANY_BASE, ESP_EVENT_ANY_ID, test_event_simple_handler, &arg));
  978. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base1, ESP_EVENT_ANY_ID, test_event_simple_handler, &arg));
  979. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV1, test_event_simple_handler, &arg));
  980. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV2, test_event_simple_handler, &arg));
  981. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base2, TEST_EVENT_BASE1_EV1, test_event_simple_handler, &arg));
  982. TEST_ASSERT_EQUAL(ESP_OK, esp_event_handler_register_with(loop, s_test_base2, TEST_EVENT_BASE1_EV2, test_event_simple_handler, &arg));
  983. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, NULL, 0, 1));
  984. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE1_EV1, NULL, 0, 1));
  985. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0, 1));
  986. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE1_EV2, NULL, 0, 1));
  987. TEST_ASSERT_EQUAL(ESP_OK, esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, NULL, 0, 1));
  988. TEST_ASSERT_EQUAL(ESP_ERR_TIMEOUT, esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE1_EV1, NULL, 0, 1));
  989. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  990. // 5 invocations of loop-levlel handlers + 3 invocation of base-level handlers (s_test_base1) +
  991. // 5 invocations of respective event-level handlers
  992. TEST_ASSERT_EQUAL(13, count);
  993. TEST_ASSERT_EQUAL(ESP_OK, esp_event_dump(stdout));
  994. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete(loop));
  995. vSemaphoreDelete(arg.mutex);
  996. TEST_TEARDOWN();
  997. }
  998. #endif