test_event.c 71 KB

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