test_event.c 47 KB

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