test_event.c 71 KB

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