test_event.c 71 KB

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