test_event.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041
  1. #include <stdbool.h>
  2. #include <string.h>
  3. #include "esp_event.h"
  4. #include "sdkconfig.h"
  5. #include "freertos/FreeRTOS.h"
  6. #include "freertos/task.h"
  7. #include "esp_log.h"
  8. #include "driver/periph_ctrl.h"
  9. #include "driver/timer.h"
  10. #include "esp_event.h"
  11. #include "esp_event_private.h"
  12. #include "esp_event_internal.h"
  13. #include "esp_heap_caps.h"
  14. #include "sdkconfig.h"
  15. #include "unity.h"
  16. #include "test_utils.h"
  17. static const char* TAG = "test_event";
  18. #define TEST_CONFIG_ITEMS_TO_REGISTER 5
  19. #define TEST_CONFIG_TASKS_TO_SPAWN 2
  20. #define TEST_CONFIG_WAIT_MULTIPLIER 5
  21. // The initial logging "initializing test" is to ensure mutex allocation is not counted against memory not being freed
  22. // during teardown.
  23. #define TEST_SETUP() \
  24. ESP_LOGI(TAG, "initializing test"); \
  25. size_t free_mem_before = heap_caps_get_free_size(MALLOC_CAP_DEFAULT); \
  26. test_setup(); \
  27. s_test_core_id = xPortGetCoreID(); \
  28. s_test_priority = uxTaskPriorityGet(NULL);
  29. #define TEST_TEARDOWN() \
  30. test_teardown(); \
  31. vTaskDelay(pdMS_TO_TICKS(CONFIG_ESP_INT_WDT_TIMEOUT_MS * TEST_CONFIG_WAIT_MULTIPLIER)); \
  32. TEST_ASSERT_EQUAL(free_mem_before, heap_caps_get_free_size(MALLOC_CAP_DEFAULT));
  33. typedef struct {
  34. void* data;
  35. SemaphoreHandle_t start;
  36. SemaphoreHandle_t done;
  37. } task_arg_t;
  38. typedef struct {
  39. esp_event_base_t base;
  40. int32_t id;
  41. esp_event_handler_t* handles;
  42. int32_t num;
  43. esp_event_loop_handle_t loop;
  44. bool is_registration;
  45. } handler_registration_data_t;
  46. typedef struct {
  47. esp_event_base_t base;
  48. int32_t id;
  49. esp_event_loop_handle_t loop;
  50. int32_t num;
  51. } post_event_data_t;
  52. typedef struct {
  53. int performed;
  54. int expected;
  55. SemaphoreHandle_t done;
  56. } performance_data_t;
  57. typedef struct {
  58. void* data;
  59. SemaphoreHandle_t mutex;
  60. } simple_arg_t;
  61. typedef struct {
  62. esp_event_handler_instance_t *context;
  63. void* data;
  64. } instance_unregister_data_t;
  65. typedef struct {
  66. int *arr;
  67. int index;
  68. } ordered_data_t;
  69. static BaseType_t s_test_core_id;
  70. static UBaseType_t s_test_priority;
  71. ESP_EVENT_DECLARE_BASE(s_test_base1);
  72. ESP_EVENT_DECLARE_BASE(s_test_base2);
  73. ESP_EVENT_DEFINE_BASE(s_test_base1);
  74. ESP_EVENT_DEFINE_BASE(s_test_base2);
  75. enum {
  76. TEST_EVENT_BASE1_EV1,
  77. TEST_EVENT_BASE1_EV2,
  78. TEST_EVENT_BASE1_MAX
  79. };
  80. enum {
  81. TEST_EVENT_BASE2_EV1,
  82. TEST_EVENT_BASE2_EV2,
  83. TEST_EVENT_BASE2_MAX
  84. };
  85. static BaseType_t test_event_get_core(void)
  86. {
  87. static int calls = 0;
  88. if (portNUM_PROCESSORS > 1) {
  89. return (s_test_core_id + calls++) % portNUM_PROCESSORS;
  90. } else {
  91. return s_test_core_id;
  92. }
  93. }
  94. static esp_event_loop_args_t test_event_get_default_loop_args(void)
  95. {
  96. esp_event_loop_args_t loop_config = {
  97. .queue_size = CONFIG_ESP_SYSTEM_EVENT_QUEUE_SIZE,
  98. .task_name = "loop",
  99. .task_priority = s_test_priority,
  100. .task_stack_size = 2048,
  101. .task_core_id = test_event_get_core()
  102. };
  103. return loop_config;
  104. }
  105. static void test_event_simple_handler(void* event_handler_arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
  106. {
  107. if (!event_handler_arg) {
  108. return;
  109. }
  110. simple_arg_t* arg = (simple_arg_t*) event_handler_arg;
  111. xSemaphoreTake(arg->mutex, portMAX_DELAY);
  112. int* count = (int*) arg->data;
  113. if (event_data == NULL) {
  114. (*count)++;
  115. } else {
  116. (*count) += *((int*) event_data);
  117. }
  118. xSemaphoreGive(arg->mutex);
  119. }
  120. static void test_event_ordered_dispatch(void* event_handler_arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
  121. {
  122. int *arg = (int*) event_handler_arg;
  123. ordered_data_t *data = *((ordered_data_t**) (event_data));
  124. data->arr[data->index++] = *arg;
  125. }
  126. static void test_event_performance_handler(void* event_handler_arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
  127. {
  128. performance_data_t* data = (performance_data_t*) event_handler_arg;
  129. data->performed++;
  130. if (data->performed >= data->expected) {
  131. xSemaphoreGive(data->done);
  132. }
  133. }
  134. static void test_event_post_task(void* args)
  135. {
  136. task_arg_t* arg = (task_arg_t*) args;
  137. post_event_data_t* data = arg->data;
  138. xSemaphoreTake(arg->start, portMAX_DELAY);
  139. for (int i = 0; i < data->num; i++) {
  140. TEST_ESP_OK(esp_event_post_to(data->loop, data->base, data->id, NULL, 0, portMAX_DELAY));
  141. vTaskDelay(1);
  142. }
  143. xSemaphoreGive(arg->done);
  144. vTaskDelete(NULL);
  145. }
  146. static void test_event_simple_handler_registration_task(void* args)
  147. {
  148. task_arg_t* arg = (task_arg_t*) args;
  149. handler_registration_data_t* data = (handler_registration_data_t*) arg->data;
  150. xSemaphoreTake(arg->start, portMAX_DELAY);
  151. for(int i = 0; i < data->num; i++) {
  152. if (data->is_registration) {
  153. TEST_ESP_OK(esp_event_handler_register_with(data->loop, data->base, data->id, data->handles[i], NULL));
  154. } else {
  155. TEST_ESP_OK(esp_event_handler_unregister_with(data->loop, data->base, data->id, data->handles[i]));
  156. }
  157. vTaskDelay(1);
  158. }
  159. xSemaphoreGive(arg->done);
  160. vTaskDelete(NULL);
  161. }
  162. static void test_handler_post_w_task(void* event_handler_arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
  163. {
  164. simple_arg_t* arg = (simple_arg_t*) event_handler_arg;
  165. esp_event_loop_handle_t* loop = (esp_event_loop_handle_t*) event_data;
  166. int* count = (int*) arg->data;
  167. (*count)++;
  168. if (*count <= 2) {
  169. if (event_base == s_test_base1 && event_id == TEST_EVENT_BASE1_EV1) {
  170. TEST_ESP_OK(esp_event_post_to(*loop, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  171. } else{
  172. xSemaphoreGive((SemaphoreHandle_t) arg->mutex);
  173. }
  174. } else {
  175. // Test that once the queue is full and the handler attempts to post to the same loop,
  176. // posting does not block indefinitely.
  177. if (event_base == s_test_base1 && event_id == TEST_EVENT_BASE1_EV1) {
  178. xSemaphoreTake((SemaphoreHandle_t) arg->mutex, portMAX_DELAY);
  179. TEST_ASSERT_EQUAL(ESP_ERR_TIMEOUT, esp_event_post_to(*loop, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  180. }
  181. }
  182. }
  183. static void test_handler_post_wo_task(void* event_handler_arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
  184. {
  185. simple_arg_t* arg = (simple_arg_t*) event_handler_arg;
  186. esp_event_loop_handle_t* loop = (esp_event_loop_handle_t*) event_data;
  187. int* count = (int*) arg->data;
  188. (*count)++;
  189. if (*count <= 2) {
  190. if (event_base == s_test_base1 && event_id == TEST_EVENT_BASE1_EV1) {
  191. TEST_ESP_OK(esp_event_post_to(*loop, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  192. } else{
  193. xSemaphoreGive((SemaphoreHandle_t) arg->mutex);
  194. }
  195. } else {
  196. // Test that once the queue is full and the handler attempts to post to the same loop,
  197. // posting does not block indefinitely.
  198. if (event_base == s_test_base1 && event_id == TEST_EVENT_BASE1_EV1) {
  199. xSemaphoreTake((SemaphoreHandle_t) arg->mutex, portMAX_DELAY);
  200. TEST_ESP_OK(esp_event_post_to(*loop, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  201. TEST_ASSERT_EQUAL(ESP_ERR_TIMEOUT, esp_event_post_to(*loop, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  202. }
  203. }
  204. }
  205. static void test_handler_unregister_itself(void* event_handler_arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
  206. {
  207. esp_event_loop_handle_t* loop = (esp_event_loop_handle_t*) event_data;
  208. int* unregistered = (int*) event_handler_arg;
  209. (*unregistered) += (event_base == s_test_base1 ? 0 : 10) + event_id + 1;
  210. // Unregister this handler for this event
  211. TEST_ESP_OK(esp_event_handler_unregister_with(*loop, event_base, event_id, test_handler_unregister_itself));
  212. }
  213. static void test_handler_instance_unregister_itself(void* event_handler_arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
  214. {
  215. esp_event_loop_handle_t* loop = (esp_event_loop_handle_t*) event_data;
  216. instance_unregister_data_t *unregister_data = (instance_unregister_data_t*) event_handler_arg;
  217. esp_event_handler_instance_t *context = (esp_event_handler_instance_t*) unregister_data->context;
  218. int *count = (int*) unregister_data->data;
  219. (*count)++;
  220. // Unregister this handler for this event
  221. TEST_ESP_OK(esp_event_handler_instance_unregister_with(*loop, event_base, event_id, *context));
  222. }
  223. static void test_post_from_handler_loop_task(void* args)
  224. {
  225. esp_event_loop_handle_t event_loop = (esp_event_loop_handle_t) args;
  226. while(1) {
  227. TEST_ESP_OK(esp_event_loop_run(event_loop, portMAX_DELAY));
  228. }
  229. }
  230. static void test_setup(void)
  231. {
  232. TEST_ASSERT_TRUE(TEST_CONFIG_TASKS_TO_SPAWN >= 2);
  233. TEST_ESP_OK(esp_event_loop_create_default());
  234. }
  235. static void test_teardown(void)
  236. {
  237. TEST_ESP_OK(esp_event_loop_delete_default());
  238. }
  239. #define TIMER_DIVIDER 16 // Hardware timer clock divider
  240. #define TIMER_SCALE (TIMER_BASE_CLK / TIMER_DIVIDER) // convert counter value to seconds
  241. #define TIMER_INTERVAL0_SEC (2.0) // sample test interval for the first timer
  242. TEST_CASE("create and event loop with any NULL argument fails", "[event]")
  243. {
  244. esp_event_loop_handle_t loop; // with dedicated task
  245. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  246. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_event_loop_create(NULL, &loop));
  247. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_event_loop_create(&loop_args, NULL));
  248. }
  249. TEST_CASE("can create and delete event loops", "[event]")
  250. {
  251. /* this test aims to verify that:
  252. * - creating loops with and without a task succeeds
  253. * - event queue can accomodate the set queue size, and drops the post when exceeded
  254. * - deleting loops with unconsumed posts and unregistered handlers (when unregistration is enabled) does not leak memory */
  255. TEST_SETUP();
  256. esp_event_loop_handle_t loop1; // with dedicated task
  257. esp_event_loop_handle_t loop2; // without dedicated task
  258. esp_event_loop_handle_t loop3; // with leftover post and handlers
  259. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  260. TEST_ESP_OK(esp_event_loop_create(&loop_args, &loop1));
  261. loop_args.task_name = NULL;
  262. TEST_ESP_OK(esp_event_loop_create(&loop_args, &loop2));
  263. TEST_ESP_OK(esp_event_loop_create(&loop_args, &loop3));
  264. TEST_ESP_OK(esp_event_handler_register_with(loop3, s_test_base1, TEST_EVENT_BASE1_EV1, (void*) 0x00000001, NULL));
  265. TEST_ESP_OK(esp_event_handler_register_with(loop3, s_test_base1, TEST_EVENT_BASE1_EV2, (void*) 0x00000002, NULL));
  266. TEST_ESP_OK(esp_event_handler_register_with(loop3, s_test_base2, TEST_EVENT_BASE1_EV1, (void*) 0x00000003, NULL));
  267. for (int i = 0; i < loop_args.queue_size; i++) {
  268. int mod = i % 4;
  269. switch(mod) {
  270. case 0:
  271. TEST_ESP_OK(esp_event_post_to(loop3, s_test_base1, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  272. break;
  273. case 1:
  274. TEST_ESP_OK(esp_event_post_to(loop3, s_test_base2, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  275. break;
  276. case 2:
  277. TEST_ESP_OK(esp_event_post_to(loop3, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  278. break;
  279. case 3:
  280. TEST_ESP_OK(esp_event_post_to(loop3, s_test_base2, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  281. break;
  282. default:
  283. break;
  284. }
  285. }
  286. TEST_ASSERT_EQUAL(ESP_ERR_TIMEOUT, esp_event_post_to(loop3, s_test_base1, TEST_EVENT_BASE1_EV1, NULL, 0, pdMS_TO_TICKS(10)));
  287. TEST_ESP_OK(esp_event_loop_delete(loop1));
  288. TEST_ESP_OK(esp_event_loop_delete(loop2));
  289. TEST_ESP_OK(esp_event_loop_delete(loop3));
  290. TEST_TEARDOWN();
  291. }
  292. TEST_CASE("registering event handler instance without instance context works", "[event]") {
  293. TEST_SETUP();
  294. esp_event_loop_handle_t loop;
  295. int count_1 = 0;
  296. simple_arg_t arg_1 = {
  297. .data = &count_1,
  298. .mutex = xSemaphoreCreateMutex()
  299. };
  300. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  301. loop_args.task_name = NULL;
  302. TEST_ESP_OK(esp_event_loop_create(&loop_args, &loop));
  303. TEST_ESP_OK(esp_event_handler_instance_register_with(loop, ESP_EVENT_ANY_BASE, ESP_EVENT_ANY_ID, test_event_simple_handler, &arg_1, NULL));
  304. TEST_ESP_OK(esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  305. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  306. TEST_ASSERT_EQUAL(1, count_1);
  307. TEST_ESP_OK(esp_event_loop_delete(loop));
  308. vSemaphoreDelete(arg_1.mutex);
  309. TEST_TEARDOWN();
  310. }
  311. TEST_CASE("registering event twice with same handler yields updated handler arg", "[event]") {
  312. TEST_SETUP();
  313. esp_event_loop_handle_t loop;
  314. int count = 0;
  315. simple_arg_t arg = {
  316. .data = &count,
  317. .mutex = xSemaphoreCreateMutex()
  318. };
  319. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  320. loop_args.task_name = NULL;
  321. TEST_ESP_OK(esp_event_loop_create(&loop_args, &loop));
  322. /* Register the handler twice to the same base and id but with a different argument (expects to return ESP_OK and log a warning)
  323. * This aims to verify: 1) Handler's argument to be updated
  324. * 2) Registration not to leak memory
  325. */
  326. TEST_ESP_OK(esp_event_handler_register_with(loop, ESP_EVENT_ANY_BASE, ESP_EVENT_ANY_ID, test_event_simple_handler, NULL));
  327. TEST_ESP_OK(esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  328. // exec loop, no handler data: count stays 0
  329. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  330. TEST_ASSERT_EQUAL(0, count);
  331. // overriding the former registration of the same event
  332. TEST_ESP_OK(esp_event_handler_register_with(loop, ESP_EVENT_ANY_BASE, ESP_EVENT_ANY_ID, test_event_simple_handler, &arg));
  333. TEST_ESP_OK(esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  334. // exec loop, registered handler data exists: count increases
  335. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  336. TEST_ASSERT_EQUAL(1, count);
  337. TEST_ESP_OK(esp_event_loop_delete(loop));
  338. vSemaphoreDelete(arg.mutex);
  339. TEST_TEARDOWN();
  340. }
  341. TEST_CASE("registering event handler instance twice works", "[event]") {
  342. TEST_SETUP();
  343. esp_event_loop_handle_t loop;
  344. int count_1 = 0;
  345. int count_2 = 0;
  346. simple_arg_t arg_1 = {
  347. .data = &count_1,
  348. .mutex = xSemaphoreCreateMutex()
  349. };
  350. simple_arg_t arg_2 = {
  351. .data = &count_2,
  352. .mutex = xSemaphoreCreateMutex()
  353. };
  354. esp_event_handler_instance_t ctx_1;
  355. esp_event_handler_instance_t ctx_2;
  356. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  357. loop_args.task_name = NULL;
  358. TEST_ESP_OK(esp_event_loop_create(&loop_args, &loop));
  359. TEST_ESP_OK(esp_event_handler_instance_register_with(loop, ESP_EVENT_ANY_BASE, ESP_EVENT_ANY_ID, test_event_simple_handler, &arg_1, &ctx_1));
  360. TEST_ESP_OK(esp_event_handler_instance_register_with(loop, ESP_EVENT_ANY_BASE, ESP_EVENT_ANY_ID, test_event_simple_handler, &arg_2, &ctx_2));
  361. TEST_ESP_OK(esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  362. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  363. TEST_ASSERT_EQUAL(1, count_1);
  364. TEST_ASSERT_EQUAL(1, count_2);
  365. TEST_ESP_OK(esp_event_loop_delete(loop));
  366. vSemaphoreDelete(arg_1.mutex);
  367. vSemaphoreDelete(arg_2.mutex);
  368. TEST_TEARDOWN();
  369. }
  370. TEST_CASE("registering with ANY_BASE but specific ID fails", "[event]") {
  371. TEST_SETUP();
  372. esp_event_loop_handle_t loop;
  373. int count = 0;
  374. simple_arg_t arg = {
  375. .data = &count,
  376. .mutex = xSemaphoreCreateMutex()
  377. };
  378. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  379. loop_args.task_name = NULL;
  380. TEST_ESP_OK(esp_event_loop_create(&loop_args, &loop));
  381. 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));
  382. TEST_ESP_OK(esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  383. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  384. TEST_ASSERT_EQUAL(0, count);
  385. TEST_ESP_OK(esp_event_loop_delete(loop));
  386. vSemaphoreDelete(arg.mutex);
  387. TEST_TEARDOWN();
  388. }
  389. TEST_CASE("registering instance with ANY_BASE but specific ID fails", "[event]") {
  390. TEST_SETUP();
  391. esp_event_loop_handle_t loop;
  392. int count = 0;
  393. esp_event_handler_instance_t ctx;
  394. simple_arg_t arg = {
  395. .data = &count,
  396. .mutex = xSemaphoreCreateMutex()
  397. };
  398. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  399. loop_args.task_name = NULL;
  400. TEST_ESP_OK(esp_event_loop_create(&loop_args, &loop));
  401. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_event_handler_instance_register_with(loop, ESP_EVENT_ANY_BASE, TEST_EVENT_BASE1_EV1, test_event_simple_handler, &arg, &ctx));
  402. TEST_ESP_OK(esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  403. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  404. TEST_ASSERT_EQUAL(0, count);
  405. TEST_ESP_OK(esp_event_loop_delete(loop));
  406. vSemaphoreDelete(arg.mutex);
  407. TEST_TEARDOWN();
  408. }
  409. TEST_CASE("Check registering ANY_ID", "[event]") {
  410. TEST_SETUP();
  411. esp_event_loop_handle_t loop;
  412. int count = 0;
  413. simple_arg_t arg = {
  414. .data = &count,
  415. .mutex = xSemaphoreCreateMutex()
  416. };
  417. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  418. loop_args.task_name = NULL;
  419. TEST_ESP_OK(esp_event_loop_create(&loop_args, &loop));
  420. TEST_ESP_OK(esp_event_handler_register_with(loop, s_test_base1, ESP_EVENT_ANY_ID, test_event_simple_handler, &arg));
  421. // handler shouldn't be triggered with different base
  422. TEST_ESP_OK(esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  423. TEST_ESP_OK(esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  424. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  425. TEST_ASSERT_EQUAL(0, count);
  426. // for all events with correct base, it should be triggered
  427. TEST_ESP_OK(esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  428. TEST_ESP_OK(esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  429. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  430. TEST_ASSERT_EQUAL(2, count);
  431. TEST_ESP_OK(esp_event_loop_delete(loop));
  432. vSemaphoreDelete(arg.mutex);
  433. TEST_TEARDOWN();
  434. }
  435. TEST_CASE("Check registering instance with ANY_ID", "[event]") {
  436. TEST_SETUP();
  437. esp_event_loop_handle_t loop;
  438. int count = 0;
  439. simple_arg_t arg = {
  440. .data = &count,
  441. .mutex = xSemaphoreCreateMutex()
  442. };
  443. esp_event_handler_instance_t ctx;
  444. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  445. loop_args.task_name = NULL;
  446. TEST_ESP_OK(esp_event_loop_create(&loop_args, &loop));
  447. TEST_ESP_OK(esp_event_handler_instance_register_with(loop, s_test_base1, ESP_EVENT_ANY_ID, test_event_simple_handler, &arg, &ctx));
  448. // handler shouldn't be triggered with different base
  449. TEST_ESP_OK(esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  450. TEST_ESP_OK(esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  451. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  452. TEST_ASSERT_EQUAL(0, count);
  453. // for all events with correct base, it should be triggered
  454. TEST_ESP_OK(esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  455. TEST_ESP_OK(esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  456. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  457. TEST_ASSERT_EQUAL(2, count);
  458. TEST_ESP_OK(esp_event_loop_delete(loop));
  459. vSemaphoreDelete(arg.mutex);
  460. TEST_TEARDOWN();
  461. }
  462. TEST_CASE("Check registering specific event", "[event]") {
  463. TEST_SETUP();
  464. esp_event_loop_handle_t loop;
  465. int count = 0;
  466. simple_arg_t arg = {
  467. .data = &count,
  468. .mutex = xSemaphoreCreateMutex()
  469. };
  470. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  471. loop_args.task_name = NULL;
  472. TEST_ESP_OK(esp_event_loop_create(&loop_args, &loop));
  473. TEST_ESP_OK(esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV1, test_event_simple_handler, &arg));
  474. // handler should not be triggered with different base
  475. TEST_ESP_OK(esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  476. TEST_ESP_OK(esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  477. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  478. TEST_ASSERT_EQUAL(0, count);
  479. // for incorrect id, it should not be triggered
  480. TEST_ESP_OK(esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  481. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  482. TEST_ASSERT_EQUAL(0, count);
  483. // for correct event and base, it should be triggered
  484. TEST_ESP_OK(esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  485. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  486. TEST_ASSERT_EQUAL(1, count);
  487. TEST_ESP_OK(esp_event_loop_delete(loop));
  488. vSemaphoreDelete(arg.mutex);
  489. TEST_TEARDOWN();
  490. }
  491. TEST_CASE("Check registering instance with specific event", "[event]") {
  492. TEST_SETUP();
  493. esp_event_loop_handle_t loop;
  494. int count = 0;
  495. simple_arg_t arg = {
  496. .data = &count,
  497. .mutex = xSemaphoreCreateMutex()
  498. };
  499. esp_event_handler_instance_t ctx;
  500. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  501. loop_args.task_name = NULL;
  502. TEST_ESP_OK(esp_event_loop_create(&loop_args, &loop));
  503. TEST_ESP_OK(esp_event_handler_instance_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV1, test_event_simple_handler, &arg, &ctx));
  504. // handler should not be triggered with different base
  505. TEST_ESP_OK(esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  506. TEST_ESP_OK(esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  507. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  508. TEST_ASSERT_EQUAL(0, count);
  509. // for incorrect id, it should not be triggered
  510. TEST_ESP_OK(esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  511. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  512. TEST_ASSERT_EQUAL(0, count);
  513. // for correct event and base, it should be triggered
  514. TEST_ESP_OK(esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  515. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  516. TEST_ASSERT_EQUAL(1, count);
  517. TEST_ESP_OK(esp_event_loop_delete(loop));
  518. vSemaphoreDelete(arg.mutex);
  519. TEST_TEARDOWN();
  520. }
  521. TEST_CASE("Specific event is not called when no correct events are posted", "[event]") {
  522. TEST_SETUP();
  523. esp_event_loop_handle_t loop;
  524. int count = 0;
  525. simple_arg_t arg = {
  526. .data = &count,
  527. .mutex = xSemaphoreCreateMutex()
  528. };
  529. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  530. loop_args.task_name = NULL;
  531. TEST_ESP_OK(esp_event_loop_create(&loop_args, &loop));
  532. TEST_ESP_OK(esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV1, test_event_simple_handler, &arg));
  533. // handler should not be triggered by any of these postings
  534. TEST_ESP_OK(esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  535. TEST_ESP_OK(esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  536. TEST_ESP_OK(esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  537. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  538. TEST_ASSERT_EQUAL(0, count);
  539. // Post unknown events.
  540. TEST_ESP_OK(esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_MAX, NULL, 0, portMAX_DELAY));
  541. TEST_ESP_OK(esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE2_MAX, NULL, 0, portMAX_DELAY));
  542. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  543. TEST_ASSERT_EQUAL(0, count);
  544. TEST_ESP_OK(esp_event_loop_delete(loop));
  545. vSemaphoreDelete(arg.mutex);
  546. TEST_TEARDOWN();
  547. }
  548. TEST_CASE("Specific event instance is not called when no correct events are posted", "[event]") {
  549. TEST_SETUP();
  550. esp_event_loop_handle_t loop;
  551. int count = 0;
  552. simple_arg_t arg = {
  553. .data = &count,
  554. .mutex = xSemaphoreCreateMutex()
  555. };
  556. esp_event_handler_instance_t ctx;
  557. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  558. loop_args.task_name = NULL;
  559. TEST_ESP_OK(esp_event_loop_create(&loop_args, &loop));
  560. TEST_ESP_OK(esp_event_handler_instance_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV1, test_event_simple_handler, &arg, &ctx));
  561. // handler should not be triggered by any of these postings
  562. TEST_ESP_OK(esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  563. TEST_ESP_OK(esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  564. TEST_ESP_OK(esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  565. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  566. TEST_ASSERT_EQUAL(0, count);
  567. // Post unknown events.
  568. TEST_ESP_OK(esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_MAX, NULL, 0, portMAX_DELAY));
  569. TEST_ESP_OK(esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE2_MAX, NULL, 0, portMAX_DELAY));
  570. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  571. TEST_ASSERT_EQUAL(0, count);
  572. TEST_ESP_OK(esp_event_loop_delete(loop));
  573. vSemaphoreDelete(arg.mutex);
  574. TEST_TEARDOWN();
  575. }
  576. TEST_CASE("can register/unregister handlers for all events/all events for a specific base", "[event]")
  577. {
  578. /* this test aims to verify that handlers can be registered to be called on all events
  579. * or for all events with specific bases */
  580. TEST_SETUP();
  581. esp_event_loop_handle_t loop;
  582. int count = 0;
  583. simple_arg_t arg = {
  584. .data = &count,
  585. .mutex = xSemaphoreCreateMutex()
  586. };
  587. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  588. loop_args.task_name = NULL;
  589. TEST_ESP_OK(esp_event_loop_create(&loop_args, &loop));
  590. /* Register the handler twice to the same base and id but with a different argument (expects to return ESP_OK and log a warning)
  591. * This aims to verify: 1) Handler's argument to be updated
  592. * 2) Registration not to leak memory
  593. */
  594. TEST_ESP_OK(esp_event_handler_register_with(loop, ESP_EVENT_ANY_BASE, ESP_EVENT_ANY_ID, test_event_simple_handler, NULL));
  595. TEST_ESP_OK(esp_event_handler_register_with(loop, ESP_EVENT_ANY_BASE, ESP_EVENT_ANY_ID, test_event_simple_handler, &arg));
  596. TEST_ESP_OK(esp_event_handler_register_with(loop, s_test_base1, ESP_EVENT_ANY_ID, test_event_simple_handler, &arg));
  597. 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));
  598. TEST_ESP_OK(esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV1, test_event_simple_handler, &arg));
  599. TEST_ESP_OK(esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV2, test_event_simple_handler, &arg));
  600. TEST_ESP_OK(esp_event_handler_register_with(loop, s_test_base2, TEST_EVENT_BASE1_EV1, test_event_simple_handler, &arg));
  601. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_event_post_to(loop, ESP_EVENT_ANY_BASE, ESP_EVENT_ANY_ID, NULL, 0, portMAX_DELAY));
  602. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_event_post_to(loop, s_test_base1, ESP_EVENT_ANY_ID, NULL, 0, portMAX_DELAY));
  603. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_event_post_to(loop, ESP_EVENT_ANY_BASE, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  604. TEST_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)
  605. TEST_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)
  606. // Post unknown events. Respective loop level and base level handlers should still execute.
  607. TEST_ESP_OK(esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_MAX, NULL, 0, portMAX_DELAY)); // exec loop and base level (+2)
  608. TEST_ESP_OK(esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE2_MAX, NULL, 0, portMAX_DELAY)); // exec loop level (+1)
  609. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  610. TEST_ASSERT_EQUAL(9, count); // 3 + 3 + 2 + 1
  611. TEST_ESP_OK(esp_event_loop_delete(loop));
  612. vSemaphoreDelete(arg.mutex);
  613. TEST_TEARDOWN();
  614. }
  615. TEST_CASE("can unregister handler", "[event]")
  616. {
  617. /* this test aims to verify that unregistered handlers no longer execute when events are raised */
  618. TEST_SETUP();
  619. esp_event_loop_handle_t loop;
  620. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  621. loop_args.task_name = NULL;
  622. TEST_ESP_OK(esp_event_loop_create(&loop_args, &loop));
  623. int count = 0;
  624. simple_arg_t arg = {
  625. .data = &count,
  626. .mutex = xSemaphoreCreateMutex()
  627. };
  628. TEST_ESP_OK(esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV1, test_event_simple_handler, &arg));
  629. TEST_ESP_OK(esp_event_handler_register_with(loop, s_test_base2, TEST_EVENT_BASE1_EV1, test_event_simple_handler, &arg));
  630. TEST_ESP_OK(esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  631. TEST_ESP_OK(esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  632. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  633. TEST_ASSERT_EQUAL(2, count);
  634. TEST_ESP_OK(esp_event_handler_unregister_with(loop, s_test_base1, TEST_EVENT_BASE1_EV1, test_event_simple_handler));
  635. TEST_ESP_OK(esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  636. TEST_ESP_OK(esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  637. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  638. TEST_ASSERT_EQUAL(3, count);
  639. TEST_ESP_OK(esp_event_loop_delete(loop));
  640. vSemaphoreDelete(arg.mutex);
  641. TEST_TEARDOWN();
  642. }
  643. TEST_CASE("can unregister handler instance", "[event]")
  644. {
  645. /* this test aims to verify that unregistered handlers no longer execute when events are raised */
  646. TEST_SETUP();
  647. esp_event_loop_handle_t loop;
  648. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  649. loop_args.task_name = NULL;
  650. TEST_ESP_OK(esp_event_loop_create(&loop_args, &loop));
  651. int count = 0;
  652. simple_arg_t arg = {
  653. .data = &count,
  654. .mutex = xSemaphoreCreateMutex()
  655. };
  656. esp_event_handler_instance_t ctx;
  657. TEST_ESP_OK(esp_event_handler_instance_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV1, test_event_simple_handler, &arg, &ctx));
  658. TEST_ESP_OK(esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  659. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  660. TEST_ASSERT_EQUAL(1, count);
  661. TEST_ESP_OK(esp_event_handler_instance_unregister_with(loop, s_test_base1, TEST_EVENT_BASE1_EV1, ctx));
  662. TEST_ESP_OK(esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  663. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  664. TEST_ASSERT_EQUAL(1, count);
  665. TEST_ESP_OK(esp_event_loop_delete(loop));
  666. vSemaphoreDelete(arg.mutex);
  667. TEST_TEARDOWN();
  668. }
  669. TEST_CASE("handler can unregister itself", "[event]")
  670. {
  671. /* this test aims to verify that handlers can unregister themselves */
  672. TEST_SETUP();
  673. esp_event_loop_handle_t loop;
  674. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  675. loop_args.task_name = NULL;
  676. TEST_ESP_OK(esp_event_loop_create(&loop_args, &loop));
  677. int unregistered = 0;
  678. /*
  679. * s_test_base1, ev1 = 1
  680. * s_test_base1, ev2 = 2
  681. * s_test_base2, ev1 = 11
  682. * s_test_base2, ev2 = 12
  683. */
  684. int expected_unregistered = 0;
  685. TEST_ESP_OK(esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV1, test_handler_unregister_itself, &unregistered));
  686. TEST_ESP_OK(esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV2, test_handler_unregister_itself, &unregistered));
  687. TEST_ESP_OK(esp_event_handler_register_with(loop, s_test_base2, TEST_EVENT_BASE2_EV1, test_handler_unregister_itself, &unregistered));
  688. TEST_ESP_OK(esp_event_handler_register_with(loop, s_test_base2, TEST_EVENT_BASE2_EV2, test_handler_unregister_itself, &unregistered));
  689. TEST_ESP_OK(esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV2, &loop, sizeof(loop), portMAX_DELAY));
  690. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  691. expected_unregistered = 2; // base1, ev2
  692. TEST_ASSERT_EQUAL(expected_unregistered, unregistered);
  693. TEST_ESP_OK(esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, &loop, sizeof(loop), portMAX_DELAY));
  694. TEST_ESP_OK(esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE2_EV1, &loop, sizeof(loop), portMAX_DELAY));
  695. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  696. expected_unregistered += 1 + 11; // base1, ev1 + base2, ev1
  697. TEST_ASSERT_EQUAL(expected_unregistered, unregistered);
  698. TEST_ESP_OK(esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE2_EV2, &loop, sizeof(loop), portMAX_DELAY));
  699. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  700. expected_unregistered += 12; // base2, ev2
  701. TEST_ASSERT_EQUAL(expected_unregistered, unregistered);
  702. TEST_ESP_OK(esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, &loop, sizeof(loop), portMAX_DELAY));
  703. TEST_ESP_OK(esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV2, &loop, sizeof(loop), portMAX_DELAY));
  704. TEST_ESP_OK(esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE2_EV1, &loop, sizeof(loop), portMAX_DELAY));
  705. TEST_ESP_OK(esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE2_EV2, &loop, sizeof(loop), portMAX_DELAY));
  706. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  707. TEST_ASSERT_EQUAL(expected_unregistered, unregistered); // all handlers unregistered
  708. TEST_ESP_OK(esp_event_loop_delete(loop));
  709. TEST_TEARDOWN();
  710. }
  711. TEST_CASE("handler instance can unregister itself", "[event]")
  712. {
  713. /* this test aims to verify that handlers can unregister themselves */
  714. TEST_SETUP();
  715. esp_event_loop_handle_t loop;
  716. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  717. loop_args.task_name = NULL;
  718. TEST_ESP_OK(esp_event_loop_create(&loop_args, &loop));
  719. esp_event_handler_instance_t ctx;
  720. int count = 0;
  721. instance_unregister_data_t instance_data = {
  722. .context = &ctx,
  723. .data = &count
  724. };
  725. TEST_ESP_OK(esp_event_handler_instance_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV1, test_handler_instance_unregister_itself, &instance_data, &ctx));
  726. TEST_ESP_OK(esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, &loop, sizeof(loop), portMAX_DELAY));
  727. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  728. TEST_ASSERT_EQUAL(1, count);
  729. vTaskDelay(1000);
  730. TEST_ESP_OK(esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, &loop, sizeof(loop), portMAX_DELAY));
  731. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  732. TEST_ASSERT_EQUAL(1, count);
  733. TEST_ESP_OK(esp_event_loop_delete(loop));
  734. TEST_TEARDOWN();
  735. }
  736. TEST_CASE("can exit running loop at approximately the set amount of time", "[event]")
  737. {
  738. /* this test aims to verify that running loop does not block indefinitely in cases where
  739. * events are posted frequently */
  740. TEST_SETUP();
  741. esp_event_loop_handle_t loop;
  742. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  743. loop_args.task_name = NULL;
  744. TEST_ESP_OK(esp_event_loop_create(&loop_args, &loop));
  745. performance_data_t handler_data = {
  746. .performed = 0,
  747. .expected = INT32_MAX,
  748. .done = xSemaphoreCreateBinary()
  749. };
  750. TEST_ESP_OK(esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV1, test_event_performance_handler, &handler_data));
  751. post_event_data_t post_event_data = {
  752. .base = s_test_base1,
  753. .id = TEST_EVENT_BASE1_EV1,
  754. .loop = loop,
  755. .num = INT32_MAX
  756. };
  757. task_arg_t post_event_arg = {
  758. .data = &post_event_data,
  759. .done = xSemaphoreCreateBinary(),
  760. .start = xSemaphoreCreateBinary()
  761. };
  762. TaskHandle_t post_task;
  763. xTaskCreatePinnedToCore(test_event_post_task, "post", 2048, &post_event_arg, s_test_priority, &post_task, test_event_get_core());
  764. int runtime_ms = 10;
  765. int runtime_us = runtime_ms * 1000;
  766. int64_t start, diff;
  767. start = esp_timer_get_time();
  768. xSemaphoreGive(post_event_arg.start);
  769. // Run the loop for the runtime_ms set amount of time, regardless of whether events
  770. // are still being posted to the loop.
  771. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(runtime_ms)));
  772. diff = (esp_timer_get_time() - start);
  773. // Threshold is 25 percent.
  774. TEST_ASSERT(diff < runtime_us * 1.25f);
  775. // Verify that the post task still continues
  776. TEST_ASSERT_NOT_EQUAL(pdTRUE, xSemaphoreTake(post_event_arg.done, pdMS_TO_TICKS(10)));
  777. vSemaphoreDelete(post_event_arg.done);
  778. vSemaphoreDelete(post_event_arg.start);
  779. vSemaphoreDelete(handler_data.done);
  780. vTaskDelete(post_task);
  781. TEST_ESP_OK(esp_event_loop_delete(loop));
  782. TEST_TEARDOWN();
  783. }
  784. TEST_CASE("can register/unregister handlers simultaneously", "[event]")
  785. {
  786. /* this test aims to verify that the event handlers list remains consistent despite
  787. * simultaneous access by differenct tasks */
  788. TEST_SETUP();
  789. const char* base = "base";
  790. int32_t id = 0;
  791. esp_event_loop_handle_t loop;
  792. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  793. TEST_ESP_OK(esp_event_loop_create(&loop_args, &loop));
  794. ESP_LOGI(TAG, "registering handlers");
  795. handler_registration_data_t* registration_data = calloc(TEST_CONFIG_TASKS_TO_SPAWN, sizeof(*registration_data));
  796. task_arg_t* registration_arg = calloc(TEST_CONFIG_TASKS_TO_SPAWN, sizeof(*registration_arg));
  797. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  798. registration_data[i].base = base;
  799. registration_data[i].id = id;
  800. registration_data[i].loop = loop;
  801. registration_data[i].handles = calloc(TEST_CONFIG_ITEMS_TO_REGISTER, sizeof(esp_event_handler_t));
  802. registration_data[i].num = TEST_CONFIG_ITEMS_TO_REGISTER;
  803. registration_data[i].is_registration = true;
  804. for (int j = 0; j < TEST_CONFIG_ITEMS_TO_REGISTER; j++) {
  805. registration_data[i].handles[j] = (void*) (i * TEST_CONFIG_ITEMS_TO_REGISTER) + (j + TEST_CONFIG_ITEMS_TO_REGISTER);
  806. }
  807. registration_arg[i].start = xSemaphoreCreateBinary();
  808. registration_arg[i].done = xSemaphoreCreateBinary();
  809. registration_arg[i].data = &registration_data[i];
  810. xTaskCreatePinnedToCore(test_event_simple_handler_registration_task, "register", 2048, &registration_arg[i], s_test_priority, NULL, test_event_get_core());
  811. }
  812. // Give the semaphores to the spawned registration task
  813. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  814. xSemaphoreGive(registration_arg[i].start);
  815. }
  816. // Take the same semaphores in order to proceed
  817. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  818. xSemaphoreTake(registration_arg[i].done, portMAX_DELAY);
  819. }
  820. ESP_LOGI(TAG, "checking consistency of handlers list");
  821. // Check consistency of events list
  822. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  823. for (int j = 0; j < TEST_CONFIG_ITEMS_TO_REGISTER; j++) {
  824. TEST_ASSERT_TRUE(esp_event_is_handler_registered(loop, base, id, registration_data[i].handles[j]));
  825. }
  826. }
  827. ESP_LOGI(TAG, "unregistering handlers");
  828. /* Test if tasks can unregister simultaneously */
  829. // Unregister registered events
  830. handler_registration_data_t* unregistration_data = calloc(TEST_CONFIG_TASKS_TO_SPAWN, sizeof(*unregistration_data));
  831. task_arg_t* unregistration_arg = calloc(TEST_CONFIG_TASKS_TO_SPAWN, sizeof(*unregistration_arg));
  832. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  833. unregistration_data[i].base = base;
  834. unregistration_data[i].id = id;
  835. unregistration_data[i].loop = loop;
  836. unregistration_data[i].handles = calloc(TEST_CONFIG_ITEMS_TO_REGISTER, sizeof(esp_event_handler_t));
  837. unregistration_data[i].num = TEST_CONFIG_ITEMS_TO_REGISTER;
  838. unregistration_data[i].is_registration = false;
  839. memcpy(unregistration_data[i].handles, registration_data[i].handles, TEST_CONFIG_ITEMS_TO_REGISTER * sizeof(esp_event_handler_t));
  840. unregistration_arg[i].data = &unregistration_data[i];
  841. unregistration_arg[i].start = xSemaphoreCreateBinary();
  842. unregistration_arg[i].done = xSemaphoreCreateBinary();
  843. xTaskCreatePinnedToCore(test_event_simple_handler_registration_task, "unregister", 2048, &unregistration_arg[i], s_test_priority, NULL, test_event_get_core());
  844. }
  845. // Give the semaphores to the spawned unregistration task
  846. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  847. xSemaphoreGive(unregistration_arg[i].start);
  848. }
  849. // Take the same semaphores in order to proceed
  850. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  851. xSemaphoreTake(unregistration_arg[i].done, portMAX_DELAY);
  852. }
  853. ESP_LOGI(TAG, "checking consistency of handlers list");
  854. // Check consistency of events list
  855. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  856. for (int j = 0; j < TEST_CONFIG_ITEMS_TO_REGISTER; j++) {
  857. TEST_ASSERT_FALSE(esp_event_is_handler_registered(loop, base, id, registration_data[i].handles[j]));
  858. }
  859. }
  860. // Do cleanup
  861. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  862. free(registration_data[i].handles);
  863. vSemaphoreDelete(registration_arg[i].start);
  864. vSemaphoreDelete(registration_arg[i].done);
  865. free(unregistration_data[i].handles);
  866. vSemaphoreDelete(unregistration_arg[i].start);
  867. vSemaphoreDelete(unregistration_arg[i].done);
  868. }
  869. free(registration_data);
  870. free(unregistration_data);
  871. free(registration_arg);
  872. free(unregistration_arg);
  873. TEST_ESP_OK(esp_event_loop_delete(loop));
  874. TEST_TEARDOWN();
  875. }
  876. TEST_CASE("posting ANY_EVENT or ANY_ID fails", "[event]") {
  877. TEST_SETUP();
  878. esp_event_loop_handle_t loop;
  879. int count = 0;
  880. simple_arg_t arg = {
  881. .data = &count,
  882. .mutex = xSemaphoreCreateMutex()
  883. };
  884. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  885. loop_args.task_name = NULL;
  886. TEST_ESP_OK(esp_event_loop_create(&loop_args, &loop));
  887. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_event_post_to(loop, ESP_EVENT_ANY_BASE, ESP_EVENT_ANY_ID, NULL, 0, portMAX_DELAY));
  888. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_event_post_to(loop, s_test_base1, ESP_EVENT_ANY_ID, NULL, 0, portMAX_DELAY));
  889. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_event_post_to(loop, ESP_EVENT_ANY_BASE, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  890. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  891. TEST_ASSERT_EQUAL(0, count);
  892. TEST_ESP_OK(esp_event_loop_delete(loop));
  893. vSemaphoreDelete(arg.mutex);
  894. TEST_TEARDOWN();
  895. }
  896. TEST_CASE("can post and run events", "[event]")
  897. {
  898. /* this test aims to verify that:
  899. * - multiple tasks can post to the queue simultaneously
  900. * - handlers recieve the appropriate handler arg and associated event data */
  901. TEST_SETUP();
  902. esp_event_loop_handle_t loop;
  903. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  904. loop_args.task_name = NULL;
  905. loop_args.queue_size = TEST_CONFIG_TASKS_TO_SPAWN * TEST_CONFIG_ITEMS_TO_REGISTER;
  906. TEST_ESP_OK(esp_event_loop_create(&loop_args, &loop));
  907. int count = 0;
  908. simple_arg_t arg = {
  909. .data = &count,
  910. .mutex = xSemaphoreCreateMutex()
  911. };
  912. TEST_ESP_OK(esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV1, test_event_simple_handler, &arg));
  913. post_event_data_t* post_event_data = calloc(TEST_CONFIG_TASKS_TO_SPAWN, sizeof(*post_event_data));
  914. task_arg_t* post_event_arg = calloc(TEST_CONFIG_TASKS_TO_SPAWN, sizeof(*post_event_arg));
  915. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++)
  916. {
  917. post_event_data[i].base = s_test_base1;
  918. post_event_data[i].id = TEST_EVENT_BASE1_EV1;
  919. post_event_data[i].loop = loop;
  920. post_event_data[i].num = TEST_CONFIG_ITEMS_TO_REGISTER;
  921. post_event_arg[i].data = &post_event_data[i];
  922. post_event_arg[i].start = xSemaphoreCreateBinary();
  923. post_event_arg[i].done = xSemaphoreCreateBinary();
  924. xTaskCreatePinnedToCore(test_event_post_task, "post", 2048, &post_event_arg[i], s_test_priority, NULL, test_event_get_core());
  925. }
  926. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  927. xSemaphoreGive(post_event_arg[i].start);
  928. }
  929. // Execute some events as they are posted
  930. for (int i = 0; i < (TEST_CONFIG_TASKS_TO_SPAWN * TEST_CONFIG_ITEMS_TO_REGISTER) / 2; i++) {
  931. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  932. }
  933. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  934. xSemaphoreTake(post_event_arg[i].done, portMAX_DELAY);
  935. }
  936. // Execute the rest
  937. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  938. TEST_ASSERT_EQUAL(TEST_CONFIG_TASKS_TO_SPAWN * TEST_CONFIG_ITEMS_TO_REGISTER, count);
  939. // Cleanup
  940. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  941. vSemaphoreDelete(post_event_arg[i].start);
  942. vSemaphoreDelete(post_event_arg[i].done);
  943. }
  944. free(post_event_data);
  945. free(post_event_arg);
  946. TEST_ESP_OK(esp_event_loop_delete(loop));
  947. vSemaphoreDelete(arg.mutex);
  948. TEST_TEARDOWN();
  949. }
  950. TEST_CASE("can post and run events with instances", "[event]")
  951. {
  952. /* this test aims to verify that:
  953. * - multiple tasks can post to the queue simultaneously
  954. * - handlers recieve the appropriate handler arg and associated event data */
  955. TEST_SETUP();
  956. esp_event_loop_handle_t loop;
  957. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  958. loop_args.task_name = NULL;
  959. loop_args.queue_size = TEST_CONFIG_TASKS_TO_SPAWN * TEST_CONFIG_ITEMS_TO_REGISTER;
  960. TEST_ESP_OK(esp_event_loop_create(&loop_args, &loop));
  961. int count = 0;
  962. simple_arg_t arg = {
  963. .data = &count,
  964. .mutex = xSemaphoreCreateMutex()
  965. };
  966. esp_event_handler_instance_t ctx;
  967. TEST_ESP_OK(esp_event_handler_instance_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV1, test_event_simple_handler, &arg, &ctx));
  968. post_event_data_t* post_event_data = calloc(TEST_CONFIG_TASKS_TO_SPAWN, sizeof(*post_event_data));
  969. task_arg_t* post_event_arg = calloc(TEST_CONFIG_TASKS_TO_SPAWN, sizeof(*post_event_arg));
  970. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++)
  971. {
  972. post_event_data[i].base = s_test_base1;
  973. post_event_data[i].id = TEST_EVENT_BASE1_EV1;
  974. post_event_data[i].loop = loop;
  975. post_event_data[i].num = TEST_CONFIG_ITEMS_TO_REGISTER;
  976. post_event_arg[i].data = &post_event_data[i];
  977. post_event_arg[i].start = xSemaphoreCreateBinary();
  978. post_event_arg[i].done = xSemaphoreCreateBinary();
  979. xTaskCreatePinnedToCore(test_event_post_task, "post", 2048, &post_event_arg[i], s_test_priority, NULL, test_event_get_core());
  980. }
  981. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  982. xSemaphoreGive(post_event_arg[i].start);
  983. }
  984. // Execute some events as they are posted
  985. for (int i = 0; i < (TEST_CONFIG_TASKS_TO_SPAWN * TEST_CONFIG_ITEMS_TO_REGISTER) / 2; i++) {
  986. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  987. }
  988. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  989. xSemaphoreTake(post_event_arg[i].done, portMAX_DELAY);
  990. }
  991. // Execute the rest
  992. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  993. TEST_ASSERT_EQUAL(TEST_CONFIG_TASKS_TO_SPAWN * TEST_CONFIG_ITEMS_TO_REGISTER, count);
  994. // Cleanup
  995. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  996. vSemaphoreDelete(post_event_arg[i].start);
  997. vSemaphoreDelete(post_event_arg[i].done);
  998. }
  999. free(post_event_data);
  1000. free(post_event_arg);
  1001. TEST_ESP_OK(esp_event_loop_delete(loop));
  1002. vSemaphoreDelete(arg.mutex);
  1003. TEST_TEARDOWN();
  1004. }
  1005. static void loop_run_task(void* args)
  1006. {
  1007. esp_event_loop_handle_t event_loop = (esp_event_loop_handle_t) args;
  1008. while(1) {
  1009. esp_event_loop_run(event_loop, portMAX_DELAY);
  1010. }
  1011. }
  1012. static void performance_test(bool dedicated_task)
  1013. {
  1014. // rand() seems to do a one-time allocation. Call it here so that the memory it allocates
  1015. // is not counted as a leak.
  1016. unsigned int _rand __attribute__((unused)) = rand();
  1017. TEST_SETUP();
  1018. const char test_base[] = "qwertyuiopasdfghjklzxvbnmmnbvcxz";
  1019. #define TEST_CONFIG_BASES (sizeof(test_base) - 1)
  1020. #define TEST_CONFIG_IDS (TEST_CONFIG_BASES / 2)
  1021. // Create loop
  1022. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  1023. esp_event_loop_handle_t loop;
  1024. if (!dedicated_task) {
  1025. loop_args.task_name = NULL;
  1026. }
  1027. TEST_ESP_OK(esp_event_loop_create(&loop_args, &loop));
  1028. performance_data_t data;
  1029. // Register the handlers
  1030. for (int base = 0; base < TEST_CONFIG_BASES; base++) {
  1031. for (int id = 0; id < TEST_CONFIG_IDS; id++) {
  1032. TEST_ESP_OK(esp_event_handler_register_with(loop, test_base + base, id, test_event_performance_handler, &data));
  1033. }
  1034. }
  1035. TaskHandle_t mtask = NULL;
  1036. if (!dedicated_task) {
  1037. xTaskCreate(loop_run_task, "loop_run", loop_args.task_stack_size, (void*) loop, loop_args.task_priority, &mtask);
  1038. }
  1039. // Perform performance test
  1040. float running_sum = 0;
  1041. float running_count = 0;
  1042. for (int bases = 1; bases <= TEST_CONFIG_BASES; bases *= 2) {
  1043. for (int ids = 1; ids <= TEST_CONFIG_IDS; ids *= 2) {
  1044. data.performed = 0;
  1045. data.expected = bases * ids;
  1046. data.done = xSemaphoreCreateBinary();
  1047. // Generate randomized list of posts
  1048. int post_bases[TEST_CONFIG_BASES];
  1049. int post_ids[TEST_CONFIG_IDS];
  1050. for (int i = 0; i < bases; i++) {
  1051. post_bases[i] = i;
  1052. }
  1053. for (int i = 0; i < ids; i++) {
  1054. post_ids[i] = i;
  1055. }
  1056. for (int i = 0; i < bases; i++) {
  1057. int rand_a = rand() % bases;
  1058. int rand_b = rand() % bases;
  1059. int temp = post_bases[rand_a];
  1060. post_bases[rand_a]= post_bases[rand_b];
  1061. post_bases[rand_b] = temp;
  1062. }
  1063. for (int i = 0; i < ids; i++) {
  1064. int rand_a = rand() % ids;
  1065. int rand_b = rand() % ids;
  1066. int temp = post_ids[rand_a];
  1067. post_ids[rand_a]= post_ids[rand_b];
  1068. post_ids[rand_b] = temp;
  1069. }
  1070. // Post the events
  1071. int64_t start = esp_timer_get_time();
  1072. for (int base = 0; base < bases; base++) {
  1073. for (int id = 0; id < ids; id++) {
  1074. TEST_ESP_OK(esp_event_post_to(loop, test_base + post_bases[base], post_ids[id], NULL, 0, portMAX_DELAY));
  1075. }
  1076. }
  1077. xSemaphoreTake(data.done, portMAX_DELAY);
  1078. int64_t elapsed = esp_timer_get_time() - start;
  1079. // Record data
  1080. TEST_ASSERT_EQUAL(data.expected, data.performed);
  1081. running_count++;
  1082. running_sum += data.performed / (elapsed / (1000000.0));
  1083. vSemaphoreDelete(data.done);
  1084. }
  1085. }
  1086. int average = (int) (running_sum / (running_count));
  1087. if (!dedicated_task) {
  1088. ((esp_event_loop_instance_t*) loop)->task = mtask;
  1089. }
  1090. TEST_ESP_OK(esp_event_loop_delete(loop));
  1091. TEST_TEARDOWN();
  1092. #ifdef CONFIG_ESP_EVENT_LOOP_PROFILING
  1093. ESP_LOGI(TAG, "events dispatched/second with profiling enabled: %d", average);
  1094. // Enabling profiling will slow down event dispatch, so the set threshold
  1095. // is not valid when it is enabled.
  1096. #else
  1097. #ifndef CONFIG_SPIRAM
  1098. TEST_PERFORMANCE_GREATER_THAN(EVENT_DISPATCH, "%d", average);
  1099. #else
  1100. TEST_PERFORMANCE_GREATER_THAN(EVENT_DISPATCH_PSRAM, "%d", average);
  1101. #endif // CONFIG_SPIRAM
  1102. #endif // CONFIG_ESP_EVENT_LOOP_PROFILING
  1103. }
  1104. TEST_CASE("performance test - dedicated task", "[event]")
  1105. {
  1106. performance_test(true);
  1107. }
  1108. TEST_CASE("performance test - no dedicated task", "[event]")
  1109. {
  1110. performance_test(false);
  1111. }
  1112. TEST_CASE("can post to loop from handler - dedicated task", "[event]")
  1113. {
  1114. TEST_SETUP();
  1115. esp_event_loop_handle_t loop_w_task;
  1116. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  1117. int count;
  1118. simple_arg_t arg = {
  1119. .data = &count,
  1120. .mutex = xSemaphoreCreateBinary()
  1121. };
  1122. TEST_ESP_OK(esp_event_loop_create(&loop_args, &loop_w_task));
  1123. count = 0;
  1124. // Test that a handler can post to a different loop while there is still slots on the queue
  1125. TEST_ESP_OK(esp_event_handler_register_with(loop_w_task, s_test_base1, TEST_EVENT_BASE1_EV1, test_handler_post_w_task, &arg));
  1126. TEST_ESP_OK(esp_event_handler_register_with(loop_w_task, s_test_base1, TEST_EVENT_BASE1_EV2, test_handler_post_w_task, &arg));
  1127. TEST_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));
  1128. xSemaphoreTake(arg.mutex, portMAX_DELAY);
  1129. TEST_ASSERT_EQUAL(2, count);
  1130. // Test that other tasks can still post while there is still slots in the queue, while handler is executing
  1131. count = 100;
  1132. TEST_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));
  1133. for (int i = 0; i < loop_args.queue_size; i++) {
  1134. TEST_ESP_OK(esp_event_post_to(loop_w_task, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  1135. }
  1136. TEST_ASSERT_EQUAL(ESP_ERR_TIMEOUT, esp_event_post_to(loop_w_task, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0,
  1137. pdMS_TO_TICKS(CONFIG_ESP_INT_WDT_TIMEOUT_MS * TEST_CONFIG_WAIT_MULTIPLIER)));
  1138. xSemaphoreGive(arg.mutex);
  1139. vTaskDelay(pdMS_TO_TICKS(CONFIG_ESP_INT_WDT_TIMEOUT_MS * TEST_CONFIG_WAIT_MULTIPLIER));
  1140. TEST_ESP_OK(esp_event_loop_delete(loop_w_task));
  1141. vSemaphoreDelete(arg.mutex);
  1142. TEST_TEARDOWN();
  1143. }
  1144. TEST_CASE("can post to loop from handler instance - dedicated task", "[event]")
  1145. {
  1146. TEST_SETUP();
  1147. esp_event_loop_handle_t loop_w_task;
  1148. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  1149. int count;
  1150. simple_arg_t arg = {
  1151. .data = &count,
  1152. .mutex = xSemaphoreCreateBinary()
  1153. };
  1154. TEST_ESP_OK(esp_event_loop_create(&loop_args, &loop_w_task));
  1155. count = 0;
  1156. esp_event_handler_instance_t ctx_1;
  1157. esp_event_handler_instance_t ctx_2;
  1158. // Test that a handler can post to a different loop while there is still slots on the queue
  1159. TEST_ESP_OK(esp_event_handler_instance_register_with(loop_w_task, s_test_base1, TEST_EVENT_BASE1_EV1, test_handler_post_w_task, &arg, &ctx_1));
  1160. TEST_ESP_OK(esp_event_handler_instance_register_with(loop_w_task, s_test_base1, TEST_EVENT_BASE1_EV2, test_handler_post_w_task, &arg, &ctx_2));
  1161. TEST_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));
  1162. xSemaphoreTake(arg.mutex, portMAX_DELAY);
  1163. TEST_ASSERT_EQUAL(2, count);
  1164. // Test that other tasks can still post while there is still slots in the queue, while handler is executing
  1165. count = 100;
  1166. TEST_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));
  1167. for (int i = 0; i < loop_args.queue_size; i++) {
  1168. TEST_ESP_OK(esp_event_post_to(loop_w_task, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  1169. }
  1170. TEST_ASSERT_EQUAL(ESP_ERR_TIMEOUT, esp_event_post_to(loop_w_task, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0,
  1171. pdMS_TO_TICKS(CONFIG_ESP_INT_WDT_TIMEOUT_MS * TEST_CONFIG_WAIT_MULTIPLIER)));
  1172. xSemaphoreGive(arg.mutex);
  1173. vTaskDelay(pdMS_TO_TICKS(CONFIG_ESP_INT_WDT_TIMEOUT_MS * TEST_CONFIG_WAIT_MULTIPLIER));
  1174. TEST_ESP_OK(esp_event_loop_delete(loop_w_task));
  1175. vSemaphoreDelete(arg.mutex);
  1176. TEST_TEARDOWN();
  1177. }
  1178. TEST_CASE("can post to loop from handler - no dedicated task", "[event]")
  1179. {
  1180. TEST_SETUP();
  1181. esp_event_loop_handle_t loop_wo_task;
  1182. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  1183. int count;
  1184. simple_arg_t arg = {
  1185. .data = &count,
  1186. .mutex = xSemaphoreCreateBinary()
  1187. };
  1188. count = 0;
  1189. loop_args.queue_size = 1;
  1190. loop_args.task_name = NULL;
  1191. TEST_ESP_OK(esp_event_loop_create(&loop_args, &loop_wo_task));
  1192. TaskHandle_t mtask;
  1193. xTaskCreate(test_post_from_handler_loop_task, "task", 2584, (void*) loop_wo_task, s_test_priority, &mtask);
  1194. // Test that a handler can post to a different loop while there is still slots on the queue
  1195. TEST_ESP_OK(esp_event_handler_register_with(loop_wo_task, s_test_base1, TEST_EVENT_BASE1_EV1, test_handler_post_wo_task, &arg));
  1196. TEST_ESP_OK(esp_event_handler_register_with(loop_wo_task, s_test_base1, TEST_EVENT_BASE1_EV2, test_handler_post_wo_task, &arg));
  1197. TEST_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));
  1198. xSemaphoreTake(arg.mutex, portMAX_DELAY);
  1199. TEST_ASSERT_EQUAL(2, count);
  1200. count = 100;
  1201. TEST_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));
  1202. vTaskDelay(pdMS_TO_TICKS(CONFIG_ESP_INT_WDT_TIMEOUT_MS * TEST_CONFIG_WAIT_MULTIPLIER));
  1203. // For loop without tasks, posting is more restrictive. Posting should wait until execution of handler finishes
  1204. TEST_ASSERT_EQUAL(ESP_ERR_TIMEOUT, esp_event_post_to(loop_wo_task, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0,
  1205. pdMS_TO_TICKS(CONFIG_ESP_INT_WDT_TIMEOUT_MS * TEST_CONFIG_WAIT_MULTIPLIER)));
  1206. xSemaphoreGive(arg.mutex);
  1207. vTaskDelay(pdMS_TO_TICKS(CONFIG_ESP_INT_WDT_TIMEOUT_MS * TEST_CONFIG_WAIT_MULTIPLIER));
  1208. vTaskDelete(mtask);
  1209. TEST_ESP_OK(esp_event_loop_delete(loop_wo_task));
  1210. vSemaphoreDelete(arg.mutex);
  1211. TEST_TEARDOWN();
  1212. }
  1213. TEST_CASE("can post to loop from handler instance - no dedicated task", "[event]")
  1214. {
  1215. TEST_SETUP();
  1216. esp_event_loop_handle_t loop_wo_task;
  1217. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  1218. int count;
  1219. simple_arg_t arg = {
  1220. .data = &count,
  1221. .mutex = xSemaphoreCreateBinary()
  1222. };
  1223. count = 0;
  1224. esp_event_handler_instance_t ctx_1;
  1225. esp_event_handler_instance_t ctx_2;
  1226. loop_args.queue_size = 1;
  1227. loop_args.task_name = NULL;
  1228. TEST_ESP_OK(esp_event_loop_create(&loop_args, &loop_wo_task));
  1229. TaskHandle_t mtask;
  1230. xTaskCreate(test_post_from_handler_loop_task, "task", 2584, (void*) loop_wo_task, s_test_priority, &mtask);
  1231. // Test that a handler can post to a different loop while there is still slots on the queue
  1232. TEST_ESP_OK(esp_event_handler_instance_register_with(loop_wo_task, s_test_base1, TEST_EVENT_BASE1_EV1, test_handler_post_wo_task, &arg, &ctx_1));
  1233. TEST_ESP_OK(esp_event_handler_instance_register_with(loop_wo_task, s_test_base1, TEST_EVENT_BASE1_EV2, test_handler_post_wo_task, &arg, &ctx_2));
  1234. TEST_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));
  1235. xSemaphoreTake(arg.mutex, portMAX_DELAY);
  1236. TEST_ASSERT_EQUAL(2, count);
  1237. count = 100;
  1238. TEST_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));
  1239. vTaskDelay(pdMS_TO_TICKS(CONFIG_ESP_INT_WDT_TIMEOUT_MS * TEST_CONFIG_WAIT_MULTIPLIER));
  1240. // For loop without tasks, posting is more restrictive. Posting should wait until execution of handler finishes
  1241. TEST_ASSERT_EQUAL(ESP_ERR_TIMEOUT, esp_event_post_to(loop_wo_task, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0,
  1242. pdMS_TO_TICKS(CONFIG_ESP_INT_WDT_TIMEOUT_MS * TEST_CONFIG_WAIT_MULTIPLIER)));
  1243. xSemaphoreGive(arg.mutex);
  1244. vTaskDelay(pdMS_TO_TICKS(CONFIG_ESP_INT_WDT_TIMEOUT_MS * TEST_CONFIG_WAIT_MULTIPLIER));
  1245. vTaskDelete(mtask);
  1246. TEST_ESP_OK(esp_event_loop_delete(loop_wo_task));
  1247. vSemaphoreDelete(arg.mutex);
  1248. TEST_TEARDOWN();
  1249. }
  1250. static void test_event_simple_handler_template(void* handler_arg, esp_event_base_t base, int32_t id, void* event_arg)
  1251. {
  1252. int* count = (int*) handler_arg;
  1253. (*count)++;
  1254. }
  1255. static void test_event_simple_handler_1(void* handler_arg, esp_event_base_t base, int32_t id, void* event_arg)
  1256. {
  1257. test_event_simple_handler_template(handler_arg, base, id, event_arg);
  1258. }
  1259. static void test_event_simple_handler_3(void* handler_arg, esp_event_base_t base, int32_t id, void* event_arg)
  1260. {
  1261. test_event_simple_handler_template(handler_arg, base, id, event_arg);
  1262. }
  1263. static void test_event_simple_handler_2(void* handler_arg, esp_event_base_t base, int32_t id, void* event_arg)
  1264. {
  1265. test_event_simple_handler_template(handler_arg, base, id, event_arg);
  1266. }
  1267. static void test_registration_from_handler_hdlr(void* handler_arg, esp_event_base_t base, int32_t id, void* event_arg)
  1268. {
  1269. esp_event_loop_handle_t* loop = (esp_event_loop_handle_t*) event_arg;
  1270. TEST_ESP_OK(esp_event_handler_register_with(*loop, s_test_base1, TEST_EVENT_BASE1_EV2, test_event_simple_handler_1, handler_arg));
  1271. TEST_ESP_OK(esp_event_handler_register_with(*loop, s_test_base1, TEST_EVENT_BASE1_EV2, test_event_simple_handler_2, handler_arg));
  1272. TEST_ESP_OK(esp_event_handler_register_with(*loop, s_test_base1, TEST_EVENT_BASE1_EV2, test_event_simple_handler_3, handler_arg));
  1273. }
  1274. static void test_unregistration_from_handler_hdlr(void* handler_arg, esp_event_base_t base, int32_t id, void* event_arg)
  1275. {
  1276. esp_event_loop_handle_t* loop = (esp_event_loop_handle_t*) event_arg;
  1277. TEST_ESP_OK(esp_event_handler_unregister_with(*loop, s_test_base1, TEST_EVENT_BASE1_EV2, test_event_simple_handler_1));
  1278. TEST_ESP_OK(esp_event_handler_unregister_with(*loop, s_test_base1, TEST_EVENT_BASE1_EV2, test_event_simple_handler_2));
  1279. TEST_ESP_OK(esp_event_handler_unregister_with(*loop, s_test_base1, TEST_EVENT_BASE1_EV2, test_event_simple_handler_3));
  1280. }
  1281. TEST_CASE("can register from handler", "[event]")
  1282. {
  1283. TEST_SETUP();
  1284. esp_event_loop_handle_t loop;
  1285. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  1286. loop_args.task_name = NULL;
  1287. TEST_ESP_OK(esp_event_loop_create(&loop_args, &loop));
  1288. int count = 0;
  1289. TEST_ESP_OK(esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV1, test_registration_from_handler_hdlr, &count));
  1290. TEST_ESP_OK(esp_event_handler_register_with(loop, s_test_base2, TEST_EVENT_BASE2_EV1, test_unregistration_from_handler_hdlr, &count));
  1291. TEST_ESP_OK(esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, &loop, sizeof(&loop), portMAX_DELAY));
  1292. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  1293. TEST_ESP_OK(esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  1294. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  1295. TEST_ASSERT_EQUAL(3, count);
  1296. TEST_ESP_OK(esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE2_EV1, &loop, sizeof(&loop), portMAX_DELAY));
  1297. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  1298. TEST_ESP_OK(esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  1299. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  1300. TEST_ASSERT_EQUAL(3, count);
  1301. TEST_ESP_OK(esp_event_loop_delete(loop));
  1302. TEST_TEARDOWN();
  1303. }
  1304. static void test_create_loop_handler(void* handler_args, esp_event_base_t base, int32_t id, void* event_data)
  1305. {
  1306. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  1307. if (id == TEST_EVENT_BASE1_EV1) {
  1308. TEST_ESP_OK(esp_event_loop_create(&loop_args, (esp_event_loop_handle_t*) handler_args));
  1309. } else {
  1310. TEST_ESP_OK(esp_event_loop_delete(*((esp_event_loop_handle_t*) handler_args)));
  1311. }
  1312. }
  1313. TEST_CASE("can create and delete loop from handler", "[event]")
  1314. {
  1315. TEST_SETUP();
  1316. esp_event_loop_handle_t loop;
  1317. esp_event_loop_handle_t test_loop;
  1318. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  1319. loop_args.task_name = NULL;
  1320. TEST_ESP_OK(esp_event_loop_create(&loop_args, &loop));
  1321. TEST_ESP_OK(esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV1, test_create_loop_handler, &test_loop));
  1322. TEST_ESP_OK(esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV2, test_create_loop_handler, &test_loop));
  1323. TEST_ESP_OK(esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  1324. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  1325. TEST_ESP_OK(esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV2, NULL, 0, portMAX_DELAY));
  1326. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  1327. TEST_ESP_OK(esp_event_loop_delete(loop));
  1328. TEST_TEARDOWN();
  1329. }
  1330. TEST_CASE("events are dispatched in the order they are registered", "[event]")
  1331. {
  1332. TEST_SETUP();
  1333. esp_event_loop_handle_t loop;
  1334. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  1335. loop_args.task_name = NULL;
  1336. TEST_ESP_OK(esp_event_loop_create(&loop_args, &loop));
  1337. int id_arr[7];
  1338. for (int i = 0; i < 7; i++) {
  1339. id_arr[i] = i;
  1340. }
  1341. int data_arr[12] = {0};
  1342. TEST_ESP_OK(esp_event_handler_register_with(loop, s_test_base2, TEST_EVENT_BASE2_EV1, test_event_ordered_dispatch, id_arr + 0));
  1343. TEST_ESP_OK(esp_event_handler_register_with(loop, ESP_EVENT_ANY_BASE, ESP_EVENT_ANY_ID, test_event_ordered_dispatch, id_arr + 1));
  1344. TEST_ESP_OK(esp_event_handler_register_with(loop, s_test_base1, ESP_EVENT_ANY_ID, test_event_ordered_dispatch, id_arr + 2));
  1345. TEST_ESP_OK(esp_event_handler_register_with(loop, s_test_base2, TEST_EVENT_BASE2_EV2, test_event_ordered_dispatch, id_arr + 3));
  1346. TEST_ESP_OK(esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV1, test_event_ordered_dispatch, id_arr + 4));
  1347. TEST_ESP_OK(esp_event_handler_register_with(loop, s_test_base2, ESP_EVENT_ANY_ID, test_event_ordered_dispatch, id_arr + 5));
  1348. TEST_ESP_OK(esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV2, test_event_ordered_dispatch, id_arr + 6));
  1349. esp_event_dump(stdout);
  1350. ordered_data_t data = {
  1351. .arr = data_arr,
  1352. .index = 0
  1353. };
  1354. ordered_data_t* dptr = &data;
  1355. TEST_ESP_OK(esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE1_EV2, &dptr, sizeof(dptr), portMAX_DELAY));
  1356. TEST_ESP_OK(esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, &dptr, sizeof(dptr), portMAX_DELAY));
  1357. TEST_ESP_OK(esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV2, &dptr, sizeof(dptr), portMAX_DELAY));
  1358. TEST_ESP_OK(esp_event_post_to(loop, s_test_base2, TEST_EVENT_BASE1_EV1, &dptr, sizeof(dptr), portMAX_DELAY));
  1359. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  1360. // Expected data executing the posts above
  1361. int ref_arr[12] = {1, 3, 5, 1, 2, 4, 1, 2, 6, 0, 1, 5};
  1362. for (int i = 0; i < 12; i++) {
  1363. TEST_ASSERT_EQUAL(ref_arr[i], data_arr[i]);
  1364. }
  1365. TEST_ESP_OK(esp_event_loop_delete(loop));
  1366. TEST_TEARDOWN();
  1367. }
  1368. #if CONFIG_ESP_EVENT_POST_FROM_ISR
  1369. TEST_CASE("can properly prepare event data posted to loop", "[event]")
  1370. {
  1371. TEST_SETUP();
  1372. esp_event_loop_handle_t loop;
  1373. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  1374. loop_args.task_name = NULL;
  1375. TEST_ESP_OK(esp_event_loop_create(&loop_args, &loop));
  1376. esp_event_post_instance_t post;
  1377. esp_event_loop_instance_t* loop_def = (esp_event_loop_instance_t*) loop;
  1378. TEST_ESP_OK(esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  1379. TEST_ASSERT_EQUAL(pdTRUE, xQueueReceive(loop_def->queue, &post, portMAX_DELAY));
  1380. TEST_ASSERT_EQUAL(false, post.data_set);
  1381. TEST_ASSERT_EQUAL(false, post.data_allocated);
  1382. TEST_ASSERT_EQUAL(NULL, post.data.ptr);
  1383. int sample = 0;
  1384. TEST_ESP_OK(esp_event_isr_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, &sample, sizeof(sample), NULL));
  1385. TEST_ASSERT_EQUAL(pdTRUE, xQueueReceive(loop_def->queue, &post, portMAX_DELAY));
  1386. TEST_ASSERT_EQUAL(true, post.data_set);
  1387. TEST_ASSERT_EQUAL(false, post.data_allocated);
  1388. TEST_ASSERT_EQUAL(false, post.data.val);
  1389. TEST_ESP_OK(esp_event_loop_delete(loop));
  1390. TEST_TEARDOWN();
  1391. }
  1392. static void test_handler_post_from_isr(void* event_handler_arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
  1393. {
  1394. SemaphoreHandle_t *sem = (SemaphoreHandle_t*) event_handler_arg;
  1395. // Event data is just the address value (maybe have been truncated due to casting).
  1396. int *data = (int*) event_data;
  1397. TEST_ASSERT_EQUAL(*data, (int) (*sem));
  1398. xSemaphoreGive(*sem);
  1399. }
  1400. void IRAM_ATTR test_event_on_timer_alarm(void* para)
  1401. {
  1402. /* Retrieve the interrupt status and the counter value
  1403. from the timer that reported the interrupt */
  1404. uint64_t timer_counter_value =
  1405. timer_group_get_counter_value_in_isr(TIMER_GROUP_0, TIMER_0);
  1406. timer_group_clr_intr_status_in_isr(TIMER_GROUP_0, TIMER_0);
  1407. timer_counter_value += (uint64_t) (TIMER_INTERVAL0_SEC * TIMER_SCALE);
  1408. timer_group_set_alarm_value_in_isr(TIMER_GROUP_0, TIMER_0, timer_counter_value);
  1409. int data = (int) para;
  1410. // Posting events with data more than 4 bytes should fail.
  1411. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_event_isr_post(s_test_base1, TEST_EVENT_BASE1_EV1, &data, 5, NULL));
  1412. // This should succeedd, as data is int-sized. The handler for the event checks that the passed event data
  1413. // is correct.
  1414. BaseType_t task_unblocked;
  1415. TEST_ESP_OK(esp_event_isr_post(s_test_base1, TEST_EVENT_BASE1_EV1, &data, sizeof(data), &task_unblocked));
  1416. if (task_unblocked == pdTRUE) {
  1417. portYIELD_FROM_ISR();
  1418. }
  1419. }
  1420. TEST_CASE("can post events from interrupt handler", "[event]")
  1421. {
  1422. SemaphoreHandle_t sem = xSemaphoreCreateBinary();
  1423. /* Select and initialize basic parameters of the timer */
  1424. timer_config_t config;
  1425. config.divider = TIMER_DIVIDER;
  1426. config.counter_dir = TIMER_COUNT_UP;
  1427. config.counter_en = TIMER_PAUSE;
  1428. config.alarm_en = TIMER_ALARM_EN;
  1429. config.intr_type = TIMER_INTR_LEVEL;
  1430. config.auto_reload = false;
  1431. timer_init(TIMER_GROUP_0, TIMER_0, &config);
  1432. /* Timer's counter will initially start from value below.
  1433. Also, if auto_reload is set, this value will be automatically reload on alarm */
  1434. timer_set_counter_value(TIMER_GROUP_0, TIMER_0, 0x00000000ULL);
  1435. /* Configure the alarm value and the interrupt on alarm. */
  1436. timer_set_alarm_value(TIMER_GROUP_0, TIMER_0, TIMER_INTERVAL0_SEC * TIMER_SCALE);
  1437. timer_enable_intr(TIMER_GROUP_0, TIMER_0);
  1438. timer_isr_register(TIMER_GROUP_0, TIMER_0, test_event_on_timer_alarm,
  1439. (void *) sem, ESP_INTR_FLAG_IRAM, NULL);
  1440. timer_start(TIMER_GROUP_0, TIMER_0);
  1441. TEST_SETUP();
  1442. TEST_ESP_OK(esp_event_handler_register(s_test_base1, TEST_EVENT_BASE1_EV1,
  1443. test_handler_post_from_isr, &sem));
  1444. xSemaphoreTake(sem, portMAX_DELAY);
  1445. TEST_TEARDOWN();
  1446. }
  1447. #endif // CONFIG_ESP_EVENT_POST_FROM_ISR