From 6feab513e4afea7f6132fc6fde7c640fc46b7754 Mon Sep 17 00:00:00 2001 From: Sudeep Mohanty Date: Tue, 4 Oct 2022 13:19:07 +0200 Subject: [PATCH] esp_ringbuf: migrated esp_ringbuf component tests to pytest framework This commit migrates the esp_ringbuf tests to the pytest framework. --- components/esp_ringbuf/.build-test-rules.yml | 6 +++ components/esp_ringbuf/test/CMakeLists.txt | 4 -- .../esp_ringbuf/test_apps/CMakeLists.txt | 8 ++++ components/esp_ringbuf/test_apps/README.md | 3 ++ .../esp_ringbuf/test_apps/main/CMakeLists.txt | 6 +++ .../{test => test_apps/main}/test_ringbuf.c | 6 ++- .../test_apps/main/test_ringbuf_main.c | 41 +++++++++++++++++++ .../test_apps/pytest_esp_ringbuf.py | 22 ++++++++++ .../test_apps/sdkconfig.ci.default | 1 + .../test_apps/sdkconfig.ci.ringbuf_flash | 2 + tools/unit-test-app/configs/default_2_c2 | 2 +- tools/unit-test-app/configs/freertos_flash | 3 +- 12 files changed, 95 insertions(+), 9 deletions(-) create mode 100644 components/esp_ringbuf/.build-test-rules.yml delete mode 100644 components/esp_ringbuf/test/CMakeLists.txt create mode 100644 components/esp_ringbuf/test_apps/CMakeLists.txt create mode 100644 components/esp_ringbuf/test_apps/README.md create mode 100644 components/esp_ringbuf/test_apps/main/CMakeLists.txt rename components/esp_ringbuf/{test => test_apps/main}/test_ringbuf.c (99%) create mode 100644 components/esp_ringbuf/test_apps/main/test_ringbuf_main.c create mode 100644 components/esp_ringbuf/test_apps/pytest_esp_ringbuf.py create mode 100644 components/esp_ringbuf/test_apps/sdkconfig.ci.default create mode 100644 components/esp_ringbuf/test_apps/sdkconfig.ci.ringbuf_flash diff --git a/components/esp_ringbuf/.build-test-rules.yml b/components/esp_ringbuf/.build-test-rules.yml new file mode 100644 index 0000000000..5243ad2622 --- /dev/null +++ b/components/esp_ringbuf/.build-test-rules.yml @@ -0,0 +1,6 @@ +# Documentation: .gitlab/ci/README.md#manifest-file-to-control-the-buildtest-apps + +components/esp_ringbuf/test_apps: + enable: + - if: IDF_TARGET in ["esp32", "esp32c3", "esp32s2"] + reason: covers all target types diff --git a/components/esp_ringbuf/test/CMakeLists.txt b/components/esp_ringbuf/test/CMakeLists.txt deleted file mode 100644 index b5fc2058f7..0000000000 --- a/components/esp_ringbuf/test/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ -idf_component_register(SRC_DIRS "." - PRIV_INCLUDE_DIRS "." - PRIV_REQUIRES cmock test_utils esp_ringbuf driver) -target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format") diff --git a/components/esp_ringbuf/test_apps/CMakeLists.txt b/components/esp_ringbuf/test_apps/CMakeLists.txt new file mode 100644 index 0000000000..9fbc23324e --- /dev/null +++ b/components/esp_ringbuf/test_apps/CMakeLists.txt @@ -0,0 +1,8 @@ +# 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) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(test_esp_ringbuf) diff --git a/components/esp_ringbuf/test_apps/README.md b/components/esp_ringbuf/test_apps/README.md new file mode 100644 index 0000000000..26722d6ebb --- /dev/null +++ b/components/esp_ringbuf/test_apps/README.md @@ -0,0 +1,3 @@ +| Supported Targets | ESP32 | ESP32-C3 | ESP32-S2 | +| ----------------- | ----- | -------- | -------- | + diff --git a/components/esp_ringbuf/test_apps/main/CMakeLists.txt b/components/esp_ringbuf/test_apps/main/CMakeLists.txt new file mode 100644 index 0000000000..6e0d2e13cb --- /dev/null +++ b/components/esp_ringbuf/test_apps/main/CMakeLists.txt @@ -0,0 +1,6 @@ +set(srcs "test_ringbuf_main.c" + "test_ringbuf.c") + +idf_component_register(SRCS ${srcs} + PRIV_REQUIRES esp_ringbuf driver spi_flash unity + WHOLE_ARCHIVE) diff --git a/components/esp_ringbuf/test/test_ringbuf.c b/components/esp_ringbuf/test_apps/main/test_ringbuf.c similarity index 99% rename from components/esp_ringbuf/test/test_ringbuf.c rename to components/esp_ringbuf/test_apps/main/test_ringbuf.c index cd98cfbad7..8264b6fb06 100644 --- a/components/esp_ringbuf/test/test_ringbuf.c +++ b/components/esp_ringbuf/test_apps/main/test_ringbuf.c @@ -16,7 +16,6 @@ #include "esp_heap_caps.h" #include "spi_flash_mmap.h" #include "unity.h" -#include "test_utils.h" #include "esp_rom_sys.h" //Definitions used in multiple test cases @@ -678,7 +677,7 @@ TEST_CASE("Test ring buffer with queue sets", "[esp_ringbuf]") } //Create a task to send items to each ring buffer int no_of_items = BUFFER_SIZE / SMALL_ITEM_SIZE; - xTaskCreatePinnedToCore(queue_set_receiving_task, "rec tsk", 2048, (void *)queue_set, UNITY_FREERTOS_PRIORITY + 1, NULL, 0); + xTaskCreatePinnedToCore(queue_set_receiving_task, "rec tsk", 2048, (void *)queue_set, 10, NULL, 0); //Send multiple items to each type of ring buffer for (int i = 0; i < no_of_items; i++) { @@ -695,6 +694,7 @@ TEST_CASE("Test ring buffer with queue sets", "[esp_ringbuf]") vRingbufferDelete(buffer_handles[i]); } vQueueDelete(queue_set); + vTaskDelay(1); } /* -------------------------- Test ring buffer ISR ----------------------------- @@ -1019,6 +1019,7 @@ TEST_CASE("Test static ring buffer SMP", "[esp_ringbuf]") } #endif +#if !CONFIG_RINGBUF_PLACE_FUNCTIONS_INTO_FLASH && !CONFIG_RINGBUF_PLACE_ISR_FUNCTIONS_INTO_FLASH /* -------------------------- Test ring buffer IRAM ------------------------- */ static IRAM_ATTR __attribute__((noinline)) bool iram_ringbuf_test(void) @@ -1044,3 +1045,4 @@ TEST_CASE("Test ringbuffer functions work with flash cache disabled", "[esp_ring { TEST_ASSERT( iram_ringbuf_test() ); } +#endif /* !CONFIG_RINGBUF_PLACE_FUNCTIONS_INTO_FLASH && !CONFIG_RINGBUF_PLACE_ISR_FUNCTIONS_INTO_FLASH */ diff --git a/components/esp_ringbuf/test_apps/main/test_ringbuf_main.c b/components/esp_ringbuf/test_apps/main/test_ringbuf_main.c new file mode 100644 index 0000000000..b131f2b318 --- /dev/null +++ b/components/esp_ringbuf/test_apps/main/test_ringbuf_main.c @@ -0,0 +1,41 @@ +/* + * 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" + +// Some resources are lazy allocated in the sleep code, the threshold is left for that case +#define TEST_MEMORY_LEAK_THRESHOLD (-500) + +static size_t before_free_8bit; +static size_t before_free_32bit; + +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 >= TEST_MEMORY_LEAK_THRESHOLD, "memory leak"); +} + +void setUp(void) +{ + 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) +{ + 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) +{ + unity_run_menu(); +} diff --git a/components/esp_ringbuf/test_apps/pytest_esp_ringbuf.py b/components/esp_ringbuf/test_apps/pytest_esp_ringbuf.py new file mode 100644 index 0000000000..db867d764c --- /dev/null +++ b/components/esp_ringbuf/test_apps/pytest_esp_ringbuf.py @@ -0,0 +1,22 @@ +# 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 +@pytest.mark.parametrize( + 'config', + [ + 'default', + 'ringbuf_flash' + ] +) +def test_esp_ringbuf(dut: Dut) -> None: + dut.expect('Press ENTER to see the list of tests') + dut.write('*') + dut.expect_unity_test_output() diff --git a/components/esp_ringbuf/test_apps/sdkconfig.ci.default b/components/esp_ringbuf/test_apps/sdkconfig.ci.default new file mode 100644 index 0000000000..d306fa0e42 --- /dev/null +++ b/components/esp_ringbuf/test_apps/sdkconfig.ci.default @@ -0,0 +1 @@ +# Default configuration diff --git a/components/esp_ringbuf/test_apps/sdkconfig.ci.ringbuf_flash b/components/esp_ringbuf/test_apps/sdkconfig.ci.ringbuf_flash new file mode 100644 index 0000000000..5b8198abb7 --- /dev/null +++ b/components/esp_ringbuf/test_apps/sdkconfig.ci.ringbuf_flash @@ -0,0 +1,2 @@ +CONFIG_RINGBUF_PLACE_FUNCTIONS_INTO_FLASH=y +CONFIG_RINGBUF_PLACE_ISR_FUNCTIONS_INTO_FLASH=y diff --git a/tools/unit-test-app/configs/default_2_c2 b/tools/unit-test-app/configs/default_2_c2 index f7639af24c..2b2cc8c20e 100644 --- a/tools/unit-test-app/configs/default_2_c2 +++ b/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_ringbuf esp_wifi espcoredump hal lwip mdns mqtt newlib nvs_flash partition_table sdmmc spiffs +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 spiffs diff --git a/tools/unit-test-app/configs/freertos_flash b/tools/unit-test-app/configs/freertos_flash index 3f20ec3c5e..99c6b71961 100644 --- a/tools/unit-test-app/configs/freertos_flash +++ b/tools/unit-test-app/configs/freertos_flash @@ -1,3 +1,2 @@ -TEST_COMPONENTS=freertos driver spi_flash esp_ringbuf +TEST_COMPONENTS=freertos driver spi_flash CONFIG_FREERTOS_PLACE_FUNCTIONS_INTO_FLASH=y -CONFIG_RINGBUF_PLACE_FUNCTIONS_INTO_FLASH=y