test_event.c 46 KB

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