test_default_loop.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include <stdbool.h>
  2. #include <string.h>
  3. #include "esp_event.h"
  4. #include "sdkconfig.h"
  5. #include "freertos/FreeRTOS.h"
  6. #include "freertos/task.h"
  7. #include "esp_log.h"
  8. #include "esp_event.h"
  9. #include "esp_event_private.h"
  10. #include "esp_event_internal.h"
  11. #include "esp_heap_caps.h"
  12. #include "sdkconfig.h"
  13. #include "unity.h"
  14. #include "test_utils.h"
  15. typedef struct {
  16. void* data;
  17. SemaphoreHandle_t mutex;
  18. } simple_arg_t;
  19. static const char* TAG = "test_event";
  20. ESP_EVENT_DECLARE_BASE(s_default_test_base1);
  21. ESP_EVENT_DECLARE_BASE(s_default_test_base2);
  22. ESP_EVENT_DEFINE_BASE(s_default_test_base1);
  23. ESP_EVENT_DEFINE_BASE(s_default_test_base2);
  24. enum {
  25. TEST_EVENT_BASE1_EV1,
  26. TEST_EVENT_BASE1_EV2,
  27. TEST_EVENT_BASE1_MAX
  28. };
  29. enum {
  30. TEST_EVENT_BASE2_EV1,
  31. TEST_EVENT_BASE2_EV2,
  32. TEST_EVENT_BASE2_MAX
  33. };
  34. // The initial logging "initializing test" is to ensure mutex allocation is not counted against memory not being freed
  35. // during teardown.
  36. #define TEST_SETUP() \
  37. ESP_LOGI(TAG, "initializing test");
  38. static void test_event_simple_handler(void* event_handler_arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
  39. {
  40. if (!event_handler_arg) {
  41. return;
  42. }
  43. simple_arg_t* arg = (simple_arg_t*) event_handler_arg;
  44. xSemaphoreTake(arg->mutex, portMAX_DELAY);
  45. int* count = (int*) arg->data;
  46. if (event_data == NULL) {
  47. (*count)++;
  48. } else {
  49. (*count) += *((int*) event_data);
  50. }
  51. xSemaphoreGive(arg->mutex);
  52. }
  53. TEST_CASE("default loop: can create and delete loop", "[event]")
  54. {
  55. TEST_SETUP();
  56. TEST_ESP_OK(esp_event_loop_create_default());
  57. TEST_ESP_OK(esp_event_loop_delete_default());
  58. }
  59. TEST_CASE("default loop: registering fails on uninitialized default loop", "[event]")
  60. {
  61. TEST_SETUP();
  62. esp_event_handler_instance_t instance;
  63. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_STATE, esp_event_handler_instance_register(s_default_test_base1,
  64. TEST_EVENT_BASE1_EV1,
  65. test_event_simple_handler,
  66. NULL,
  67. &instance));
  68. }
  69. TEST_CASE("default loop: registering/unregistering event works", "[event]")
  70. {
  71. TEST_SETUP();
  72. int count = 0;
  73. simple_arg_t arg = {
  74. .data = &count,
  75. .mutex = xSemaphoreCreateMutex()
  76. };
  77. TEST_ESP_OK(esp_event_loop_create_default());
  78. esp_event_handler_instance_t instance;
  79. TEST_ESP_OK(esp_event_handler_instance_register(s_default_test_base1,
  80. TEST_EVENT_BASE1_EV1,
  81. test_event_simple_handler,
  82. &arg,
  83. &instance));
  84. TEST_ASSERT(instance);
  85. TEST_ESP_OK(esp_event_post(s_default_test_base1, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  86. vTaskDelay(10);
  87. TEST_ASSERT_EQUAL(1, count);
  88. TEST_ESP_OK(esp_event_handler_instance_unregister(s_default_test_base1,
  89. TEST_EVENT_BASE1_EV1,
  90. &instance));
  91. vTaskDelay(10);
  92. TEST_ASSERT_EQUAL(1, count);
  93. TEST_ESP_OK(esp_event_loop_delete_default());
  94. vSemaphoreDelete(arg.mutex);
  95. }
  96. TEST_CASE("default event loop: registering event handler instance without instance context works", "[event]") {
  97. TEST_SETUP();
  98. int count_1 = 0;
  99. simple_arg_t arg_1 = {
  100. .data = &count_1,
  101. .mutex = xSemaphoreCreateMutex()
  102. };
  103. TEST_ESP_OK(esp_event_loop_create_default());
  104. TEST_ESP_OK(esp_event_handler_instance_register(ESP_EVENT_ANY_BASE, ESP_EVENT_ANY_ID, test_event_simple_handler, &arg_1, NULL));
  105. TEST_ESP_OK(esp_event_post(s_default_test_base1, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
  106. vTaskDelay(10);
  107. TEST_ASSERT_EQUAL(1, count_1);
  108. TEST_ESP_OK(esp_event_loop_delete_default());
  109. vSemaphoreDelete(arg_1.mutex);
  110. }