test_event.c 48 KB

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