test_default_loop.c 3.7 KB

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