test_event_target.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. /*
  2. * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. /* This file contains tests only runnable on the chip targets */
  7. #include <stdbool.h>
  8. #include <string.h>
  9. #include "esp_event.h"
  10. #include "sdkconfig.h"
  11. #include "freertos/FreeRTOS.h"
  12. #include "freertos/task.h"
  13. #include "esp_log.h"
  14. #include "driver/gptimer.h"
  15. #include "esp_event.h"
  16. #include "esp_event_private.h"
  17. #include "esp_event_internal.h"
  18. #include "esp_heap_caps.h"
  19. #include "esp_timer.h"
  20. #include "sdkconfig.h"
  21. #include "unity.h"
  22. #include "unity_test_utils_memory.h"
  23. #include "test_utils.h"
  24. static const char* TAG = "test_event";
  25. static const TickType_t ZERO_DELAY = 0;
  26. #define TEST_CONFIG_ITEMS_TO_REGISTER 5
  27. #define TEST_CONFIG_TASKS_TO_SPAWN 2
  28. _Static_assert(TEST_CONFIG_TASKS_TO_SPAWN >= 2); // some tests test simultaneous posting of events, etc.
  29. /* Time used in tearDown function to wait for cleaning up memory in background tasks */
  30. #define TEST_CONFIG_TEARDOWN_WAIT 30
  31. typedef struct {
  32. void* data;
  33. SemaphoreHandle_t start;
  34. SemaphoreHandle_t done;
  35. } task_arg_t;
  36. typedef struct {
  37. esp_event_base_t base;
  38. int32_t id;
  39. esp_event_handler_t* handles;
  40. int32_t num;
  41. esp_event_loop_handle_t loop;
  42. bool is_registration;
  43. } handler_registration_data_t;
  44. typedef struct {
  45. esp_event_base_t base;
  46. int32_t id;
  47. esp_event_loop_handle_t loop;
  48. int32_t num;
  49. } post_event_data_t;
  50. typedef struct {
  51. int performed;
  52. int expected;
  53. SemaphoreHandle_t done;
  54. } performance_data_t;
  55. typedef struct {
  56. void* data;
  57. SemaphoreHandle_t mutex;
  58. } simple_arg_t;
  59. ESP_EVENT_DECLARE_BASE(s_test_base1);
  60. enum {
  61. TEST_EVENT_BASE1_EV1,
  62. TEST_EVENT_BASE1_MAX
  63. };
  64. static BaseType_t get_other_core(void)
  65. {
  66. return (xPortGetCoreID() + 1) % portNUM_PROCESSORS;
  67. }
  68. static esp_event_loop_args_t test_event_get_default_loop_args(void)
  69. {
  70. esp_event_loop_args_t loop_config = {
  71. .queue_size = CONFIG_ESP_SYSTEM_EVENT_QUEUE_SIZE,
  72. .task_name = "loop",
  73. .task_priority = uxTaskPriorityGet(NULL),
  74. .task_stack_size = 2048,
  75. .task_core_id = get_other_core()
  76. };
  77. return loop_config;
  78. }
  79. static void test_event_simple_handler(void* event_handler_arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
  80. {
  81. if (!event_handler_arg) {
  82. return;
  83. }
  84. simple_arg_t* arg = (simple_arg_t*) event_handler_arg;
  85. xSemaphoreTake(arg->mutex, portMAX_DELAY);
  86. int* count = (int*) arg->data;
  87. if (event_data == NULL) {
  88. (*count)++;
  89. } else {
  90. (*count) += *((int*) event_data);
  91. }
  92. xSemaphoreGive(arg->mutex);
  93. }
  94. static void test_event_performance_handler(void* event_handler_arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
  95. {
  96. performance_data_t* data = (performance_data_t*) event_handler_arg;
  97. data->performed++;
  98. if (data->performed >= data->expected) {
  99. xSemaphoreGive(data->done);
  100. }
  101. }
  102. static void test_event_post_task(void* args)
  103. {
  104. task_arg_t* arg = (task_arg_t*) args;
  105. post_event_data_t* data = arg->data;
  106. xSemaphoreTake(arg->start, portMAX_DELAY);
  107. for (int i = 0; i < data->num; i++) {
  108. TEST_ESP_OK(esp_event_post_to(data->loop, data->base, data->id, NULL, 0, portMAX_DELAY));
  109. vTaskDelay(1);
  110. }
  111. xSemaphoreGive(arg->done);
  112. vTaskDelete(NULL);
  113. }
  114. static void test_event_simple_handler_registration_task(void* args)
  115. {
  116. task_arg_t* arg = (task_arg_t*) args;
  117. handler_registration_data_t* data = (handler_registration_data_t*) arg->data;
  118. xSemaphoreTake(arg->start, portMAX_DELAY);
  119. for(int i = 0; i < data->num; i++) {
  120. if (data->is_registration) {
  121. TEST_ESP_OK(esp_event_handler_register_with(data->loop, data->base, data->id, data->handles[i], NULL));
  122. } else {
  123. TEST_ESP_OK(esp_event_handler_unregister_with(data->loop, data->base, data->id, data->handles[i]));
  124. }
  125. vTaskDelay(1);
  126. }
  127. xSemaphoreGive(arg->done);
  128. vTaskDelete(NULL);
  129. }
  130. // Ignore this test on QEMU for now since it relies on esp_timer which is based on the host run time on ESP32-QEMU
  131. TEST_CASE("can exit running loop at approximately the set amount of time", "[event][qemu-ignore]")
  132. {
  133. /* this test aims to verify that running loop does not block indefinitely in cases where
  134. * events are posted frequently */
  135. esp_event_loop_handle_t loop;
  136. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  137. loop_args.task_name = NULL;
  138. TEST_ESP_OK(esp_event_loop_create(&loop_args, &loop));
  139. performance_data_t handler_data = {
  140. .performed = 0,
  141. .expected = INT32_MAX,
  142. .done = xSemaphoreCreateBinary()
  143. };
  144. TEST_ESP_OK(esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV1, test_event_performance_handler, &handler_data));
  145. post_event_data_t post_event_data = {
  146. .base = s_test_base1,
  147. .id = TEST_EVENT_BASE1_EV1,
  148. .loop = loop,
  149. .num = INT32_MAX
  150. };
  151. task_arg_t post_event_arg = {
  152. .data = &post_event_data,
  153. .done = xSemaphoreCreateBinary(),
  154. .start = xSemaphoreCreateBinary()
  155. };
  156. TaskHandle_t post_task;
  157. xTaskCreatePinnedToCore(test_event_post_task, "post", 2048, &post_event_arg, uxTaskPriorityGet(NULL), &post_task, get_other_core());
  158. int runtime_ms = 10;
  159. int runtime_us = runtime_ms * 1000;
  160. int64_t start, diff;
  161. start = esp_timer_get_time();
  162. xSemaphoreGive(post_event_arg.start);
  163. // Run the loop for the runtime_ms set amount of time, regardless of whether events
  164. // are still being posted to the loop.
  165. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(runtime_ms)));
  166. diff = (esp_timer_get_time() - start);
  167. // Threshold is 25 percent.
  168. TEST_ASSERT_LESS_THAN_INT(runtime_us * 1.25f, diff);
  169. // Verify that the post task still continues
  170. TEST_ASSERT_NOT_EQUAL(pdTRUE, xSemaphoreTake(post_event_arg.done, pdMS_TO_TICKS(10)));
  171. vSemaphoreDelete(post_event_arg.done);
  172. vSemaphoreDelete(post_event_arg.start);
  173. vSemaphoreDelete(handler_data.done);
  174. vTaskDelete(post_task);
  175. TEST_ESP_OK(esp_event_loop_delete(loop));
  176. vTaskDelay(pdMS_TO_TICKS(TEST_CONFIG_TEARDOWN_WAIT));
  177. }
  178. TEST_CASE("can register/unregister handlers simultaneously", "[event]")
  179. {
  180. /* this test aims to verify that the event handlers list remains consistent despite
  181. * simultaneous access by differenct tasks */
  182. const char* base = "base";
  183. int32_t id = 0;
  184. esp_event_loop_handle_t loop;
  185. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  186. TEST_ESP_OK(esp_event_loop_create(&loop_args, &loop));
  187. ESP_LOGI(TAG, "registering handlers");
  188. handler_registration_data_t* registration_data = calloc(TEST_CONFIG_TASKS_TO_SPAWN, sizeof(*registration_data));
  189. task_arg_t* registration_arg = calloc(TEST_CONFIG_TASKS_TO_SPAWN, sizeof(*registration_arg));
  190. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  191. registration_data[i].base = base;
  192. registration_data[i].id = id;
  193. registration_data[i].loop = loop;
  194. registration_data[i].handles = calloc(TEST_CONFIG_ITEMS_TO_REGISTER, sizeof(esp_event_handler_t));
  195. registration_data[i].num = TEST_CONFIG_ITEMS_TO_REGISTER;
  196. registration_data[i].is_registration = true;
  197. for (int j = 0; j < TEST_CONFIG_ITEMS_TO_REGISTER; j++) {
  198. registration_data[i].handles[j] = (void*) (i * TEST_CONFIG_ITEMS_TO_REGISTER) + (j + TEST_CONFIG_ITEMS_TO_REGISTER);
  199. }
  200. registration_arg[i].start = xSemaphoreCreateBinary();
  201. registration_arg[i].done = xSemaphoreCreateBinary();
  202. registration_arg[i].data = &registration_data[i];
  203. xTaskCreatePinnedToCore(test_event_simple_handler_registration_task, "register", 2048, &registration_arg[i], uxTaskPriorityGet(NULL), NULL, i % portNUM_PROCESSORS);
  204. }
  205. // Give the semaphores to the spawned registration task
  206. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  207. xSemaphoreGive(registration_arg[i].start);
  208. }
  209. // Take the same semaphores in order to proceed
  210. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  211. xSemaphoreTake(registration_arg[i].done, portMAX_DELAY);
  212. }
  213. ESP_LOGI(TAG, "checking consistency of handlers list");
  214. // Check consistency of events list
  215. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  216. for (int j = 0; j < TEST_CONFIG_ITEMS_TO_REGISTER; j++) {
  217. TEST_ASSERT_TRUE(esp_event_is_handler_registered(loop, base, id, registration_data[i].handles[j]));
  218. }
  219. }
  220. ESP_LOGI(TAG, "unregistering handlers");
  221. /* Test if tasks can unregister simultaneously */
  222. // Unregister registered events
  223. handler_registration_data_t* unregistration_data = calloc(TEST_CONFIG_TASKS_TO_SPAWN, sizeof(*unregistration_data));
  224. task_arg_t* unregistration_arg = calloc(TEST_CONFIG_TASKS_TO_SPAWN, sizeof(*unregistration_arg));
  225. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  226. unregistration_data[i].base = base;
  227. unregistration_data[i].id = id;
  228. unregistration_data[i].loop = loop;
  229. unregistration_data[i].handles = calloc(TEST_CONFIG_ITEMS_TO_REGISTER, sizeof(esp_event_handler_t));
  230. unregistration_data[i].num = TEST_CONFIG_ITEMS_TO_REGISTER;
  231. unregistration_data[i].is_registration = false;
  232. memcpy(unregistration_data[i].handles, registration_data[i].handles, TEST_CONFIG_ITEMS_TO_REGISTER * sizeof(esp_event_handler_t));
  233. unregistration_arg[i].data = &unregistration_data[i];
  234. unregistration_arg[i].start = xSemaphoreCreateBinary();
  235. unregistration_arg[i].done = xSemaphoreCreateBinary();
  236. xTaskCreatePinnedToCore(test_event_simple_handler_registration_task, "unregister", 2048, &unregistration_arg[i], uxTaskPriorityGet(NULL), NULL, i % portNUM_PROCESSORS);
  237. }
  238. // Give the semaphores to the spawned unregistration task
  239. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  240. xSemaphoreGive(unregistration_arg[i].start);
  241. }
  242. // Take the same semaphores in order to proceed
  243. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  244. xSemaphoreTake(unregistration_arg[i].done, portMAX_DELAY);
  245. }
  246. ESP_LOGI(TAG, "checking consistency of handlers list");
  247. // Check consistency of events list
  248. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  249. for (int j = 0; j < TEST_CONFIG_ITEMS_TO_REGISTER; j++) {
  250. TEST_ASSERT_FALSE(esp_event_is_handler_registered(loop, base, id, registration_data[i].handles[j]));
  251. }
  252. }
  253. // Do cleanup
  254. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  255. free(registration_data[i].handles);
  256. vSemaphoreDelete(registration_arg[i].start);
  257. vSemaphoreDelete(registration_arg[i].done);
  258. free(unregistration_data[i].handles);
  259. vSemaphoreDelete(unregistration_arg[i].start);
  260. vSemaphoreDelete(unregistration_arg[i].done);
  261. }
  262. free(registration_data);
  263. free(unregistration_data);
  264. free(registration_arg);
  265. free(unregistration_arg);
  266. TEST_ESP_OK(esp_event_loop_delete(loop));
  267. vTaskDelay(pdMS_TO_TICKS(TEST_CONFIG_TEARDOWN_WAIT));
  268. }
  269. TEST_CASE("can post and run events simultaneously", "[event]")
  270. {
  271. /* this test aims to verify that:
  272. * - multiple tasks can post to the queue simultaneously
  273. * - handlers recieve the appropriate handler arg and associated event data */
  274. esp_event_loop_handle_t loop;
  275. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  276. loop_args.task_name = NULL;
  277. loop_args.queue_size = TEST_CONFIG_TASKS_TO_SPAWN * TEST_CONFIG_ITEMS_TO_REGISTER;
  278. TEST_ESP_OK(esp_event_loop_create(&loop_args, &loop));
  279. int count = 0;
  280. simple_arg_t arg = {
  281. .data = &count,
  282. .mutex = xSemaphoreCreateMutex()
  283. };
  284. TEST_ESP_OK(esp_event_handler_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV1, test_event_simple_handler, &arg));
  285. post_event_data_t* post_event_data = calloc(TEST_CONFIG_TASKS_TO_SPAWN, sizeof(*post_event_data));
  286. task_arg_t* post_event_arg = calloc(TEST_CONFIG_TASKS_TO_SPAWN, sizeof(*post_event_arg));
  287. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++)
  288. {
  289. post_event_data[i].base = s_test_base1;
  290. post_event_data[i].id = TEST_EVENT_BASE1_EV1;
  291. post_event_data[i].loop = loop;
  292. post_event_data[i].num = TEST_CONFIG_ITEMS_TO_REGISTER;
  293. post_event_arg[i].data = &post_event_data[i];
  294. post_event_arg[i].start = xSemaphoreCreateBinary();
  295. post_event_arg[i].done = xSemaphoreCreateBinary();
  296. xTaskCreatePinnedToCore(test_event_post_task, "post", 2048, &post_event_arg[i], uxTaskPriorityGet(NULL), NULL, i % portNUM_PROCESSORS);
  297. }
  298. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  299. xSemaphoreGive(post_event_arg[i].start);
  300. }
  301. // Execute some events as they are posted
  302. for (int i = 0; i < (TEST_CONFIG_TASKS_TO_SPAWN * TEST_CONFIG_ITEMS_TO_REGISTER) / 2; i++) {
  303. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  304. }
  305. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  306. xSemaphoreTake(post_event_arg[i].done, portMAX_DELAY);
  307. }
  308. // Execute the rest
  309. for (size_t i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN * TEST_CONFIG_ITEMS_TO_REGISTER; i++) {
  310. TEST_ESP_OK(esp_event_loop_run(loop, ZERO_DELAY));
  311. }
  312. TEST_ASSERT_EQUAL(TEST_CONFIG_TASKS_TO_SPAWN * TEST_CONFIG_ITEMS_TO_REGISTER, count);
  313. // Cleanup
  314. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  315. vSemaphoreDelete(post_event_arg[i].start);
  316. vSemaphoreDelete(post_event_arg[i].done);
  317. }
  318. free(post_event_data);
  319. free(post_event_arg);
  320. TEST_ESP_OK(esp_event_loop_delete(loop));
  321. vSemaphoreDelete(arg.mutex);
  322. vTaskDelay(pdMS_TO_TICKS(TEST_CONFIG_TEARDOWN_WAIT));
  323. }
  324. TEST_CASE("can post and run events simultaneously with instances", "[event]")
  325. {
  326. /* this test aims to verify that:
  327. * - multiple tasks can post to the queue simultaneously
  328. * - handlers recieve the appropriate handler arg and associated event data */
  329. esp_event_loop_handle_t loop;
  330. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  331. loop_args.task_name = NULL;
  332. loop_args.queue_size = TEST_CONFIG_TASKS_TO_SPAWN * TEST_CONFIG_ITEMS_TO_REGISTER;
  333. TEST_ESP_OK(esp_event_loop_create(&loop_args, &loop));
  334. int count = 0;
  335. simple_arg_t arg = {
  336. .data = &count,
  337. .mutex = xSemaphoreCreateMutex()
  338. };
  339. esp_event_handler_instance_t ctx;
  340. TEST_ESP_OK(esp_event_handler_instance_register_with(loop, s_test_base1, TEST_EVENT_BASE1_EV1, test_event_simple_handler, &arg, &ctx));
  341. post_event_data_t* post_event_data = calloc(TEST_CONFIG_TASKS_TO_SPAWN, sizeof(*post_event_data));
  342. task_arg_t* post_event_arg = calloc(TEST_CONFIG_TASKS_TO_SPAWN, sizeof(*post_event_arg));
  343. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++)
  344. {
  345. post_event_data[i].base = s_test_base1;
  346. post_event_data[i].id = TEST_EVENT_BASE1_EV1;
  347. post_event_data[i].loop = loop;
  348. post_event_data[i].num = TEST_CONFIG_ITEMS_TO_REGISTER;
  349. post_event_arg[i].data = &post_event_data[i];
  350. post_event_arg[i].start = xSemaphoreCreateBinary();
  351. post_event_arg[i].done = xSemaphoreCreateBinary();
  352. xTaskCreatePinnedToCore(test_event_post_task, "post", 2048, &post_event_arg[i], uxTaskPriorityGet(NULL), NULL, i % portNUM_PROCESSORS);
  353. }
  354. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  355. xSemaphoreGive(post_event_arg[i].start);
  356. }
  357. // Execute some events as they are posted
  358. for (int i = 0; i < (TEST_CONFIG_TASKS_TO_SPAWN * TEST_CONFIG_ITEMS_TO_REGISTER) / 2; i++) {
  359. TEST_ESP_OK(esp_event_loop_run(loop, pdMS_TO_TICKS(10)));
  360. }
  361. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  362. xSemaphoreTake(post_event_arg[i].done, portMAX_DELAY);
  363. }
  364. // Execute the rest, we use the maximum number of events because we don't know
  365. // if any events have been dispatched before
  366. for (size_t i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN * TEST_CONFIG_ITEMS_TO_REGISTER; i++) {
  367. TEST_ESP_OK(esp_event_loop_run(loop, ZERO_DELAY));
  368. }
  369. TEST_ASSERT_EQUAL(TEST_CONFIG_TASKS_TO_SPAWN * TEST_CONFIG_ITEMS_TO_REGISTER, count);
  370. // Cleanup
  371. for (int i = 0; i < TEST_CONFIG_TASKS_TO_SPAWN; i++) {
  372. vSemaphoreDelete(post_event_arg[i].start);
  373. vSemaphoreDelete(post_event_arg[i].done);
  374. }
  375. free(post_event_data);
  376. free(post_event_arg);
  377. TEST_ESP_OK(esp_event_loop_delete(loop));
  378. vSemaphoreDelete(arg.mutex);
  379. vTaskDelay(pdMS_TO_TICKS(TEST_CONFIG_TEARDOWN_WAIT));
  380. }
  381. static void loop_run_task(void* args)
  382. {
  383. esp_event_loop_handle_t event_loop = (esp_event_loop_handle_t) args;
  384. while(1) {
  385. esp_event_loop_run(event_loop, portMAX_DELAY);
  386. }
  387. }
  388. static void performance_test(bool dedicated_task)
  389. {
  390. // rand() seems to do a one-time allocation. Call it here so that the memory it allocates
  391. // is not counted as a leak.
  392. unsigned int _rand __attribute__((unused)) = rand();
  393. const char test_base[] = "qwertyuiopasdfghjklzxvbnmmnbvcxz";
  394. #define TEST_CONFIG_BASES (sizeof(test_base) - 1)
  395. #define TEST_CONFIG_IDS (TEST_CONFIG_BASES / 2)
  396. // Create loop
  397. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  398. esp_event_loop_handle_t loop;
  399. if (!dedicated_task) {
  400. loop_args.task_name = NULL;
  401. }
  402. TEST_ESP_OK(esp_event_loop_create(&loop_args, &loop));
  403. performance_data_t data;
  404. // Register the handlers
  405. for (int base = 0; base < TEST_CONFIG_BASES; base++) {
  406. for (int id = 0; id < TEST_CONFIG_IDS; id++) {
  407. TEST_ESP_OK(esp_event_handler_register_with(loop, test_base + base, id, test_event_performance_handler, &data));
  408. }
  409. }
  410. TaskHandle_t mtask = NULL;
  411. if (!dedicated_task) {
  412. xTaskCreate(loop_run_task, "loop_run", loop_args.task_stack_size, (void*) loop, loop_args.task_priority, &mtask);
  413. }
  414. // Perform performance test
  415. float running_sum = 0;
  416. float running_count = 0;
  417. for (int bases = 1; bases <= TEST_CONFIG_BASES; bases *= 2) {
  418. for (int ids = 1; ids <= TEST_CONFIG_IDS; ids *= 2) {
  419. data.performed = 0;
  420. data.expected = bases * ids;
  421. data.done = xSemaphoreCreateBinary();
  422. // Generate randomized list of posts
  423. int post_bases[TEST_CONFIG_BASES];
  424. int post_ids[TEST_CONFIG_IDS];
  425. for (int i = 0; i < bases; i++) {
  426. post_bases[i] = i;
  427. }
  428. for (int i = 0; i < ids; i++) {
  429. post_ids[i] = i;
  430. }
  431. for (int i = 0; i < bases; i++) {
  432. int rand_a = rand() % bases;
  433. int rand_b = rand() % bases;
  434. int temp = post_bases[rand_a];
  435. post_bases[rand_a]= post_bases[rand_b];
  436. post_bases[rand_b] = temp;
  437. }
  438. for (int i = 0; i < ids; i++) {
  439. int rand_a = rand() % ids;
  440. int rand_b = rand() % ids;
  441. int temp = post_ids[rand_a];
  442. post_ids[rand_a]= post_ids[rand_b];
  443. post_ids[rand_b] = temp;
  444. }
  445. // Post the events
  446. int64_t start = esp_timer_get_time();
  447. for (int base = 0; base < bases; base++) {
  448. for (int id = 0; id < ids; id++) {
  449. TEST_ESP_OK(esp_event_post_to(loop, test_base + post_bases[base], post_ids[id], NULL, 0, portMAX_DELAY));
  450. }
  451. }
  452. xSemaphoreTake(data.done, portMAX_DELAY);
  453. int64_t elapsed = esp_timer_get_time() - start;
  454. // Record data
  455. TEST_ASSERT_EQUAL(data.expected, data.performed);
  456. running_count++;
  457. running_sum += data.performed / (elapsed / (1000000.0));
  458. vSemaphoreDelete(data.done);
  459. }
  460. }
  461. int average = (int) (running_sum / (running_count));
  462. if (!dedicated_task) {
  463. ((esp_event_loop_instance_t*) loop)->task = mtask;
  464. }
  465. TEST_ESP_OK(esp_event_loop_delete(loop));
  466. vTaskDelay(pdMS_TO_TICKS(TEST_CONFIG_TEARDOWN_WAIT));
  467. #ifdef CONFIG_ESP_EVENT_LOOP_PROFILING
  468. ESP_LOGI(TAG, "events dispatched/second with profiling enabled: %d", average);
  469. // Enabling profiling will slow down event dispatch, so the set threshold
  470. // is not valid when it is enabled.
  471. #else
  472. #ifndef CONFIG_SPIRAM
  473. TEST_PERFORMANCE_GREATER_THAN(EVENT_DISPATCH, "%d", average);
  474. #else
  475. TEST_PERFORMANCE_GREATER_THAN(EVENT_DISPATCH_PSRAM, "%d", average);
  476. #endif // CONFIG_SPIRAM
  477. #endif // CONFIG_ESP_EVENT_LOOP_PROFILING
  478. }
  479. TEST_CASE("performance test - dedicated task", "[event][qemu-ignore]")
  480. {
  481. performance_test(true);
  482. }
  483. TEST_CASE("performance test - no dedicated task", "[event][qemu-ignore]")
  484. {
  485. performance_test(false);
  486. }
  487. #if CONFIG_ESP_EVENT_POST_FROM_ISR
  488. TEST_CASE("data posted normally is correctly set internally", "[event][intr]")
  489. {
  490. esp_event_loop_handle_t loop;
  491. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  492. loop_args.task_name = NULL;
  493. TEST_ESP_OK(esp_event_loop_create(&loop_args, &loop));
  494. esp_event_post_instance_t post;
  495. esp_event_loop_instance_t* loop_def = (esp_event_loop_instance_t*) loop;
  496. TEST_ESP_OK(esp_event_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  497. TEST_ASSERT_EQUAL(pdTRUE, xQueueReceive(loop_def->queue, &post, portMAX_DELAY));
  498. TEST_ASSERT_EQUAL(false, post.data_set);
  499. TEST_ASSERT_EQUAL(false, post.data_allocated);
  500. TEST_ASSERT_EQUAL(NULL, post.data.ptr);
  501. TEST_ESP_OK(esp_event_loop_delete(loop));
  502. vTaskDelay(pdMS_TO_TICKS(TEST_CONFIG_TEARDOWN_WAIT));
  503. }
  504. TEST_CASE("data posted from ISR is correctly set internally", "[event][intr]")
  505. {
  506. esp_event_loop_handle_t loop;
  507. esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
  508. loop_args.task_name = NULL;
  509. TEST_ESP_OK(esp_event_loop_create(&loop_args, &loop));
  510. esp_event_post_instance_t post;
  511. esp_event_loop_instance_t* loop_def = (esp_event_loop_instance_t*) loop;
  512. int sample = 0;
  513. TEST_ESP_OK(esp_event_isr_post_to(loop, s_test_base1, TEST_EVENT_BASE1_EV1, &sample, sizeof(sample), NULL));
  514. TEST_ASSERT_EQUAL(pdTRUE, xQueueReceive(loop_def->queue, &post, portMAX_DELAY));
  515. TEST_ASSERT_EQUAL(true, post.data_set);
  516. TEST_ASSERT_EQUAL(false, post.data_allocated);
  517. TEST_ASSERT_EQUAL(false, post.data.val);
  518. TEST_ESP_OK(esp_event_loop_delete(loop));
  519. vTaskDelay(pdMS_TO_TICKS(TEST_CONFIG_TEARDOWN_WAIT));
  520. }
  521. static void test_handler_post_from_isr(void* event_handler_arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
  522. {
  523. SemaphoreHandle_t *sem = (SemaphoreHandle_t*) event_handler_arg;
  524. // Event data is just the address value (maybe have been truncated due to casting).
  525. int *data = (int*) event_data;
  526. TEST_ASSERT_EQUAL(*data, (int) (*sem));
  527. xSemaphoreGive(*sem);
  528. }
  529. bool test_event_on_timer_alarm(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *user_ctx)
  530. {
  531. int data = (int)user_ctx;
  532. gptimer_stop(timer);
  533. // Posting events with data more than 4 bytes should fail.
  534. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_event_isr_post(s_test_base1, TEST_EVENT_BASE1_EV1, &data, 5, NULL));
  535. // This should succeedd, as data is int-sized. The handler for the event checks that the passed event data
  536. // is correct.
  537. BaseType_t task_unblocked;
  538. TEST_ESP_OK(esp_event_isr_post(s_test_base1, TEST_EVENT_BASE1_EV1, &data, sizeof(data), &task_unblocked));
  539. return task_unblocked == pdTRUE;
  540. }
  541. TEST_CASE("can post events from interrupt handler", "[event][intr]")
  542. {
  543. /* Lazy allocated resources in gptimer/intr_alloc */
  544. unity_utils_set_leak_level(160);
  545. TEST_ESP_OK(esp_event_loop_create_default());
  546. SemaphoreHandle_t sem = xSemaphoreCreateBinary();
  547. gptimer_handle_t gptimer = NULL;
  548. /* Select and initialize basic parameters of the timer */
  549. gptimer_config_t config = {
  550. .clk_src = GPTIMER_CLK_SRC_DEFAULT,
  551. .direction = GPTIMER_COUNT_UP,
  552. .resolution_hz = 1000000, // 1MHz, 1 tick = 1us
  553. };
  554. TEST_ESP_OK(gptimer_new_timer(&config, &gptimer));
  555. gptimer_alarm_config_t alarm_config = {
  556. .reload_count = 0,
  557. .alarm_count = 500000,
  558. };
  559. gptimer_event_callbacks_t cbs = {
  560. .on_alarm = test_event_on_timer_alarm
  561. };
  562. TEST_ESP_OK(gptimer_register_event_callbacks(gptimer, &cbs, sem));
  563. TEST_ESP_OK(gptimer_set_alarm_action(gptimer, &alarm_config));
  564. TEST_ESP_OK(gptimer_enable(gptimer));
  565. TEST_ESP_OK(gptimer_start(gptimer));
  566. TEST_ESP_OK(esp_event_handler_register(s_test_base1, TEST_EVENT_BASE1_EV1,
  567. test_handler_post_from_isr, &sem));
  568. xSemaphoreTake(sem, portMAX_DELAY);
  569. vTaskDelay(pdMS_TO_TICKS(TEST_CONFIG_TEARDOWN_WAIT));
  570. vSemaphoreDelete(sem);
  571. TEST_ESP_OK(gptimer_disable(gptimer));
  572. TEST_ESP_OK(gptimer_del_timer(gptimer));
  573. TEST_ESP_OK(esp_event_loop_delete_default());
  574. vTaskDelay(2);
  575. }
  576. #endif // CONFIG_ESP_EVENT_POST_FROM_ISR