Procházet zdrojové kódy

esp_event: move tests to pytest framework

Marius Vikhammer před 3 roky
rodič
revize
d73e1d9754

+ 6 - 0
components/esp_event/.build-test-rules.yml

@@ -0,0 +1,6 @@
+# Documentation: .gitlab/ci/README.md#manifest-file-to-control-the-buildtest-apps
+
+components/esp_event/test_apps:
+  enable:
+    - if: IDF_TARGET in ["esp32", "esp32s2", "esp32c3"]
+      reason: covers all major arch types, xtensa vs riscv, single vs dual-core

+ 0 - 4
components/esp_event/test/CMakeLists.txt

@@ -1,4 +0,0 @@
-idf_component_register(SRC_DIRS "."
-                       PRIV_INCLUDE_DIRS . ../private_include
-                       PRIV_REQUIRES cmock test_utils esp_event driver esp_timer)
- target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format")

+ 10 - 0
components/esp_event/test_apps/CMakeLists.txt

@@ -0,0 +1,10 @@
+
+# The following lines of boilerplate have to be in your project's
+# CMakeLists in this exact order for cmake to work correctly
+cmake_minimum_required(VERSION 3.16)
+
+set(COMPONENTS main)
+set(EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/unit-test-app/components")
+
+include($ENV{IDF_PATH}/tools/cmake/project.cmake)
+project(test_esp_event)

+ 2 - 0
components/esp_event/test_apps/README.md

@@ -0,0 +1,2 @@
+| Supported Targets | ESP32 | ESP32-C3 | ESP32-S2 |
+| ----------------- | ----- | -------- | -------- |

+ 8 - 0
components/esp_event/test_apps/main/CMakeLists.txt

@@ -0,0 +1,8 @@
+set(srcs "test_event_main.c"
+         "test_default_loop.c"
+         "test_event.c")
+
+idf_component_register(SRCS ${srcs}
+                       PRIV_INCLUDE_DIRS . ../../private_include
+                       PRIV_REQUIRES esp_event driver esp_timer unity test_utils
+                       WHOLE_ARCHIVE)

+ 5 - 2
components/esp_event/test/test_default_loop.c → components/esp_event/test_apps/main/test_default_loop.c

@@ -1,3 +1,8 @@
+/*
+ * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
+ *
+ * SPDX-License-Identifier: Unlicense OR CC0-1.0
+ */
 
 #include <stdbool.h>
 #include <string.h>
@@ -18,8 +23,6 @@
 #include "sdkconfig.h"
 #include "unity.h"
 
-#include "test_utils.h"
-
 typedef struct {
     void* data;
     SemaphoreHandle_t mutex;

+ 11 - 0
components/esp_event/test/test_event.c → components/esp_event/test_apps/main/test_event.c

@@ -1,3 +1,8 @@
+/*
+ * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
+ *
+ * SPDX-License-Identifier: Unlicense OR CC0-1.0
+ */
 #include <stdbool.h>
 #include <string.h>
 
@@ -107,6 +112,8 @@ enum {
     TEST_EVENT_BASE2_MAX
 };
 
+extern void set_leak_threshold(int threshold);
+
 static BaseType_t test_event_get_core(void)
 {
     static int calls = 0;
@@ -1984,6 +1991,9 @@ bool test_event_on_timer_alarm(gptimer_handle_t timer, const gptimer_alarm_event
 
 TEST_CASE("can post events from interrupt handler", "[event]")
 {
+    /* Lazy allocated resources in gptimer/intr_alloc */
+    set_leak_threshold(-120);
+
     SemaphoreHandle_t sem = xSemaphoreCreateBinary();
     gptimer_handle_t gptimer = NULL;
     /* Select and initialize basic parameters of the timer */
@@ -2014,6 +2024,7 @@ TEST_CASE("can post events from interrupt handler", "[event]")
     xSemaphoreTake(sem, portMAX_DELAY);
 
     TEST_TEARDOWN();
+    vSemaphoreDelete(sem);
     TEST_ESP_OK(gptimer_disable(gptimer));
     TEST_ESP_OK(gptimer_del_timer(gptimer));
 }

+ 71 - 0
components/esp_event/test_apps/main/test_event_main.c

@@ -0,0 +1,71 @@
+/*
+ * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#include "unity.h"
+#include "unity_test_runner.h"
+#include "esp_heap_caps.h"
+#include "esp_log.h"
+#ifdef CONFIG_HEAP_TRACING
+#include "memory_checks.h"
+#include "esp_heap_trace.h"
+#endif
+
+#define TEST_MEMORY_LEAK_THRESHOLD_DEFAULT 0
+static int leak_threshold = TEST_MEMORY_LEAK_THRESHOLD_DEFAULT;
+
+void set_leak_threshold(int threshold)
+{
+    leak_threshold = threshold;
+}
+
+static size_t before_free_8bit;
+static size_t before_free_32bit;
+static const char* TAG = "event_test_app";
+
+static void check_leak(size_t before_free, size_t after_free, const char *type)
+{
+    ssize_t delta = after_free - before_free;
+    printf("MALLOC_CAP_%s: Before %u bytes free, After %u bytes free (delta %d)\n", type, before_free, after_free, delta);
+    TEST_ASSERT_MESSAGE(delta >= leak_threshold, "memory leak");
+}
+
+void setUp(void)
+{
+    // If heap tracing is enabled in kconfig, leak trace the test
+#ifdef CONFIG_HEAP_TRACING
+    setup_heap_record();
+    heap_trace_start(HEAP_TRACE_LEAKS);
+#endif
+
+    leak_threshold = TEST_MEMORY_LEAK_THRESHOLD_DEFAULT;
+
+    before_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT);
+    before_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT);
+}
+
+void tearDown(void)
+{
+#ifdef CONFIG_HEAP_TRACING
+    heap_trace_stop();
+    heap_trace_dump();
+#endif
+
+    size_t after_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT);
+    size_t after_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT);
+    check_leak(before_free_8bit, after_free_8bit, "8BIT");
+    check_leak(before_free_32bit, after_free_32bit, "32BIT");
+
+}
+
+void app_main(void)
+{
+    ESP_LOGI(TAG, "Running esp-event test app");
+    // rand() seems to do a one-time allocation. Call it here so that the memory it allocates
+    // is not counted as a leak.
+    unsigned int _rand __attribute__((unused)) = rand();
+
+    unity_run_menu();
+}

+ 15 - 0
components/esp_event/test_apps/pytest_esp_event.py

@@ -0,0 +1,15 @@
+# SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
+# SPDX-License-Identifier: CC0-1.0
+
+import pytest
+from pytest_embedded import Dut
+
+
+@pytest.mark.esp32
+@pytest.mark.esp32s2
+@pytest.mark.esp32c3
+@pytest.mark.generic
+def test_esp_event(dut: Dut) -> None:
+    dut.expect('Press ENTER to see the list of tests')
+    dut.write('*')
+    dut.expect_unity_test_output(timeout=120)

+ 1 - 0
components/esp_event/test_apps/sdkconfig.defaults

@@ -0,0 +1 @@
+CONFIG_ESP_TASK_WDT_EN=n

+ 2 - 0
components/esp_event/test_apps/sdkconfig.defaults.esp32

@@ -0,0 +1,2 @@
+# Set CPU frequency to max for performance tests
+CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ_240=y

+ 2 - 0
components/esp_event/test_apps/sdkconfig.defaults.esp32s2

@@ -0,0 +1,2 @@
+# Set CPU frequency to max for performance tests
+CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ_240=y

+ 1 - 1
tools/unit-test-app/configs/default_2_c2

@@ -1,3 +1,3 @@
 # This config is split between targets since different component needs to be included
 CONFIG_IDF_TARGET="esp32c2"
-TEST_COMPONENTS=app_trace efuse esp_common esp_eth esp_event esp_hid esp_netif esp_phy esp_wifi espcoredump hal lwip mdns mqtt newlib nvs_flash partition_table sdmmc
+TEST_COMPONENTS=app_trace efuse esp_common esp_eth esp_hid esp_netif esp_phy esp_wifi espcoredump hal lwip mdns mqtt newlib nvs_flash partition_table sdmmc

+ 1 - 1
tools/unit-test-app/configs/default_3_c2

@@ -1,3 +1,3 @@
 # This config is split between targets since different component needs to be included
 CONFIG_IDF_TARGET="esp32c2"
-TEST_EXCLUDE_COMPONENTS=app_trace efuse esp_common esp_eth esp_event esp_hid esp_netif esp_phy esp_ringbuf esp_wifi espcoredump hal lwip mdns mqtt newlib nvs_flash partition_table sdmmc freertos esp_hw_support esp_ipc esp_system esp_timer driver soc spi_flash vfs
+TEST_EXCLUDE_COMPONENTS=app_trace efuse esp_common esp_eth esp_hid esp_netif esp_phy esp_ringbuf esp_wifi espcoredump hal lwip mdns mqtt newlib nvs_flash partition_table sdmmc freertos esp_hw_support esp_ipc esp_system esp_timer driver soc spi_flash vfs