pthread: migrate unit tests to pytest test app

This commit is contained in:
Marius Vikhammer 2022-09-16 15:38:27 +08:00
parent 5aeba87e51
commit 46c092c04a
40 changed files with 167 additions and 41 deletions

View File

@ -1,9 +0,0 @@
set(sources "test_pthread.c"
"test_pthread_cond_var.c"
"test_pthread_local_storage.c"
"test_pthread_cxx.cpp"
"test_pthread_rwlock.c")
idf_component_register(SRCS ${sources}
PRIV_REQUIRES cmock test_utils pthread esp_timer)
target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format")

View File

@ -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(EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/unit-test-app/components")
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(test_pthread)

View File

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

View File

@ -0,0 +1,11 @@
set(sources "test_app_main.c"
"test_pthread.c"
"test_pthread_cond_var.c"
"test_pthread_local_storage.c"
"test_pthread_cxx.cpp"
"test_pthread_rwlock.c")
idf_component_register(SRCS ${sources}
INCLUDE_DIRS "."
REQUIRES pthread esp_timer test_utils
WHOLE_ARCHIVE)

View File

@ -0,0 +1,45 @@
/*
* 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 (e.g. newlib locks), the threshold is left for that case
#define TEST_MEMORY_LEAK_THRESHOLD (-200)
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)
{
printf("Running pthread component unity tests\n");
unity_run_menu();
}

View File

@ -1,3 +1,8 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
#include <iostream>
#include <thread>
#include <condition_variable>

View File

@ -1,3 +1,8 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
#include <iostream>
#include <future>
#include <thread>

View File

@ -1,3 +1,8 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
#include <errno.h>
#include "freertos/FreeRTOS.h"

View File

@ -1,3 +1,8 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
#include <pthread.h>
#include "unity.h"

View File

@ -1,3 +1,8 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
#include <iostream>
#include <sstream>
#include <thread>
@ -135,5 +140,8 @@ TEST_CASE("pthread mix C/C++", "[pthread]")
std::cout << "Join thread " << std::hex << t1.get_id() << std::endl;
t1.join();
}
/* Short delay to allow cleanup, avoid leaks */
vTaskDelay(10);
}
#endif

View File

@ -1,5 +1,11 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
// Test pthread_create_key, pthread_delete_key, pthread_setspecific, pthread_getspecific
#include <pthread.h>
#include <inttypes.h>
#include "unity.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
@ -36,7 +42,7 @@ TEST_CASE("pthread local storage unique keys", "[pthread]")
for (int i = 0; i < NUM_KEYS; i++) {
TEST_ASSERT_EQUAL(0, pthread_key_create(&keys[i], NULL));
printf("New key %d = %d\n", i, keys[i]);
printf("New key %d = %"PRIu32"\n", i, keys[i]);
}
for (int i = 0; i < NUM_KEYS; i++) {

View File

@ -1,13 +1,7 @@
/*
* SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: CC0
*
* This example code is in the Public Domain (or CC0 licensed, at your option.)
*
* Unless required by applicable law or agreed to in writing, this
* software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied.
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
#include "sdkconfig.h"

View File

@ -0,0 +1,35 @@
# SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: CC0-1.0
import pytest
from pytest_embedded import Dut
@pytest.mark.generic
@pytest.mark.supported_targets
@pytest.mark.parametrize(
'config',
[
'default',
],
indirect=True,
)
def test_pthread(dut: Dut) -> None:
dut.expect_exact('Press ENTER to see the list of tests')
dut.write('*')
dut.expect_unity_test_output(timeout=300)
@pytest.mark.generic
@pytest.mark.parametrize(
'config',
[
pytest.param('single_core_esp32', marks=[pytest.mark.esp32]),
pytest.param('single_core_esp32s3', marks=[pytest.mark.esp32s3]),
],
indirect=True,
)
def test_pthread_single_core(dut: Dut) -> None:
dut.expect_exact('Press ENTER to see the list of tests')
dut.write('*')
dut.expect_unity_test_output(timeout=300)

View File

@ -0,0 +1,2 @@
CONFIG_IDF_TARGET="esp32"
CONFIG_FREERTOS_UNICORE=y

View File

@ -0,0 +1,2 @@
CONFIG_IDF_TARGET="esp32s3"
CONFIG_FREERTOS_UNICORE=y

View File

@ -0,0 +1,2 @@
# Some of the tests will starve the watchdog
CONFIG_ESP_TASK_WDT=n

View File

@ -1,3 +1,3 @@
# This config is split between targets since different component needs to be excluded (esp32, esp32s2)
CONFIG_IDF_TARGET="esp32"
TEST_EXCLUDE_COMPONENTS=bt app_update freertos esp_hw_support esp_ipc esp_pm esp_system esp_timer driver heap pthread soc spi_flash vfs test_utils experimental_cpp_component
TEST_EXCLUDE_COMPONENTS=bt app_update freertos esp_hw_support esp_ipc esp_pm esp_system esp_timer driver heap soc spi_flash vfs test_utils experimental_cpp_component

View File

@ -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 bootloader_support console efuse esp_common esp_eth esp_event esp_hid esp_netif esp_phy esp_ringbuf esp_rom esp_wifi espcoredump hal lwip mdns mqtt newlib nvs_flash partition_table sdmmc spiffs
TEST_COMPONENTS=app_trace console efuse esp_common esp_eth esp_event esp_hid esp_netif esp_phy esp_ringbuf esp_rom esp_wifi espcoredump hal lwip mdns mqtt newlib nvs_flash partition_table sdmmc spiffs

View File

@ -1,3 +1,3 @@
# This config is split between targets since different component needs to be excluded
CONFIG_IDF_TARGET="esp32c3"
TEST_EXCLUDE_COMPONENTS=bt app_update esp_pm freertos esp_hw_support esp_ipc esp_system esp_timer driver heap pthread soc spi_flash vfs lwip spiffs experimental_cpp_component perfmon test_utils
TEST_EXCLUDE_COMPONENTS=bt app_update esp_pm freertos esp_hw_support esp_ipc esp_system esp_timer driver heap soc spi_flash vfs lwip spiffs experimental_cpp_component perfmon test_utils

View File

@ -1,3 +1,3 @@
# This config is split between targets since different component needs to be excluded (esp32, esp32s2)
CONFIG_IDF_TARGET="esp32s2"
TEST_EXCLUDE_COMPONENTS=bt app_update freertos esp_hw_support esp_ipc esp_pm esp_system esp_timer driver heap pthread soc spi_flash vfs experimental_cpp_component
TEST_EXCLUDE_COMPONENTS=bt app_update freertos esp_hw_support esp_ipc esp_pm esp_system esp_timer driver heap soc spi_flash vfs experimental_cpp_component

View File

@ -1,3 +1,3 @@
# This config is split between targets since different component needs to be excluded (esp32, esp32s2)
CONFIG_IDF_TARGET="esp32s3"
TEST_EXCLUDE_COMPONENTS=bt app_update freertos esp32s3 esp_ipc esp_pm esp_system esp_timer driver heap pthread soc spi_flash vfs experimental_cpp_component test_utils
TEST_EXCLUDE_COMPONENTS=bt app_update freertos esp32s3 esp_ipc esp_pm esp_system esp_timer driver heap soc spi_flash vfs experimental_cpp_component test_utils

View File

@ -1,4 +1,4 @@
# continue from default
CONFIG_IDF_TARGET="esp32"
TEST_COMPONENTS=pthread soc spi_flash vfs
TEST_COMPONENTS=soc spi_flash vfs
CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD=y

View File

@ -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 bootloader_support console efuse esp_common esp_eth esp_event esp_hid esp_netif esp_phy esp_ringbuf esp_rom esp_wifi espcoredump hal lwip mdns mqtt newlib nvs_flash partition_table sdmmc spiffs freertos esp_hw_support esp_ipc esp_system esp_timer driver heap pthread soc spi_flash vfs
TEST_EXCLUDE_COMPONENTS=app_trace console efuse esp_common esp_eth esp_event esp_hid esp_netif esp_phy esp_ringbuf esp_rom esp_wifi espcoredump hal lwip mdns mqtt newlib nvs_flash partition_table sdmmc spiffs freertos esp_hw_support esp_ipc esp_system esp_timer driver heap soc spi_flash vfs

View File

@ -1,3 +1,3 @@
# This config is split between targets since different component needs to be included
CONFIG_IDF_TARGET="esp32c2"
TEST_COMPONENTS=freertos esp_hw_support esp_ipc esp_system esp_timer driver heap pthread soc spi_flash vfs
TEST_COMPONENTS=freertos esp_hw_support esp_ipc esp_system esp_timer driver heap soc spi_flash vfs

View File

@ -1,3 +1,3 @@
# This config is split between targets since different component needs to be included
CONFIG_IDF_TARGET="esp32c3"
TEST_COMPONENTS=freertos esp_hw_support esp_ipc esp_system esp_timer driver heap pthread
TEST_COMPONENTS=freertos esp_hw_support esp_ipc esp_system esp_timer driver heap

View File

@ -1,3 +1,3 @@
# This config is split between targets since different component needs to be included (esp32, esp32s2)
CONFIG_IDF_TARGET="esp32s2"
TEST_COMPONENTS=pthread soc spi_flash vfs
TEST_COMPONENTS=soc spi_flash vfs

View File

@ -1,3 +1,3 @@
# This config is split between targets since different component needs to be included
CONFIG_IDF_TARGET="esp32s3"
TEST_COMPONENTS=freertos esp_hw_support esp_ipc esp_system esp_timer driver heap pthread soc spi_flash vfs
TEST_COMPONENTS=freertos esp_hw_support esp_ipc esp_system esp_timer driver heap soc spi_flash vfs

View File

@ -1,5 +1,5 @@
CONFIG_IDF_TARGET="esp32"
TEST_EXCLUDE_COMPONENTS=bt app_update driver esp_hw_support esp_ipc esp_pm esp_system esp_timer spi_flash test_utils heap pthread soc experimental_cpp_component esp-tls freertos sdmmc
TEST_EXCLUDE_COMPONENTS=bt app_update driver esp_hw_support esp_ipc esp_pm esp_system esp_timer spi_flash test_utils heap soc experimental_cpp_component esp-tls freertos sdmmc
CONFIG_SPIRAM=y
CONFIG_ESP_INT_WDT_TIMEOUT_MS=800
CONFIG_SPIRAM_OCCUPY_NO_HOST=y

View File

@ -1,5 +1,5 @@
CONFIG_IDF_TARGET="esp32"
TEST_COMPONENTS=esp_hw_support esp_ipc esp_system esp_timer spi_flash heap pthread soc
TEST_COMPONENTS=esp_hw_support esp_ipc esp_system esp_timer spi_flash heap soc
CONFIG_SPIRAM=y
CONFIG_ESP_INT_WDT_TIMEOUT_MS=800
CONFIG_SPIRAM_OCCUPY_NO_HOST=y

View File

@ -1,5 +1,5 @@
CONFIG_IDF_TARGET="esp32"
TEST_COMPONENTS=freertos esp_hw_support esp_system esp_ipc esp_timer driver heap pthread soc spi_flash vfs
TEST_COMPONENTS=freertos esp_hw_support esp_system esp_ipc esp_timer driver heap soc spi_flash vfs
CONFIG_COMPILER_OPTIMIZATION_SIZE=y
CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_SIZE=y
CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT=y

View File

@ -1,6 +1,6 @@
# This config is split between targets since different component needs to be included (esp32, esp32s2)
CONFIG_IDF_TARGET="esp32"
TEST_EXCLUDE_COMPONENTS=bt app_update freertos esp_hw_support esp_ipc esp_pm esp_system esp_timer driver heap pthread soc spi_flash vfs test_utils experimental_cpp_component
TEST_EXCLUDE_COMPONENTS=bt app_update freertos esp_hw_support esp_ipc esp_pm esp_system esp_timer driver heap soc spi_flash vfs test_utils experimental_cpp_component
CONFIG_COMPILER_OPTIMIZATION_SIZE=y
CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_SIZE=y
CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT=y

View File

@ -1,6 +1,6 @@
# This config is split between targets since different component needs to be excluded (esp32, esp32s2)
CONFIG_IDF_TARGET="esp32s2"
TEST_EXCLUDE_COMPONENTS=bt app_update freertos esp_hw_support esp_ipc esp_pm esp_system esp_timer driver heap pthread soc spi_flash vfs test_utils experimental_cpp_component
TEST_EXCLUDE_COMPONENTS=bt app_update freertos esp_hw_support esp_ipc esp_pm esp_system esp_timer driver heap soc spi_flash vfs test_utils experimental_cpp_component
CONFIG_COMPILER_OPTIMIZATION_SIZE=y
CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_SIZE=y
CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT=y

View File

@ -1,5 +1,5 @@
CONFIG_IDF_TARGET="esp32c2"
TEST_COMPONENTS=freertos esp_hw_support esp_system esp_ipc esp_timer driver heap pthread soc spi_flash vfs sdmmc
TEST_COMPONENTS=freertos esp_hw_support esp_system esp_ipc esp_timer driver heap soc spi_flash vfs sdmmc
CONFIG_COMPILER_OPTIMIZATION_SIZE=y
CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT=y
CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_SIZE=y

View File

@ -1,5 +1,5 @@
CONFIG_IDF_TARGET="esp32c3"
TEST_COMPONENTS=freertos esp_hw_support esp_ipc esp_system esp_timer driver heap pthread
TEST_COMPONENTS=freertos esp_hw_support esp_ipc esp_system esp_timer driver heap
CONFIG_COMPILER_OPTIMIZATION_SIZE=y
CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT=y
CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_SIZE=y

View File

@ -1,6 +1,6 @@
# This config is split between targets since different component needs to be included (esp32, esp32s2)
CONFIG_IDF_TARGET="esp32s2"
TEST_COMPONENTS=freertos esp_hw_support esp_system esp_timer driver heap pthread soc spi_flash vfs
TEST_COMPONENTS=freertos esp_hw_support esp_system esp_timer driver heap soc spi_flash vfs
CONFIG_COMPILER_OPTIMIZATION_SIZE=y
CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_SIZE=y
CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT=y

View File

@ -1,5 +1,5 @@
CONFIG_IDF_TARGET="esp32s3"
TEST_COMPONENTS=freertos esp_hw_support esp_system esp_ipc esp_timer driver heap pthread soc spi_flash vfs
TEST_COMPONENTS=freertos esp_hw_support esp_system esp_ipc esp_timer driver heap soc spi_flash vfs
CONFIG_COMPILER_OPTIMIZATION_SIZE=y
CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_SIZE=y
CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT=y

View File

@ -1,6 +1,6 @@
# This config is split between targets since different component needs to be included (esp32, esp32s2)
CONFIG_IDF_TARGET="esp32"
TEST_COMPONENTS=freertos esp_hw_support esp_system esp_timer driver heap pthread soc spi_flash vfs
TEST_COMPONENTS=freertos esp_hw_support esp_system esp_timer driver heap soc spi_flash vfs
CONFIG_MEMMAP_SMP=n
CONFIG_FREERTOS_UNICORE=y
CONFIG_ESP32_IRAM_AS_8BIT_ACCESSIBLE_MEMORY=y

View File

@ -1,6 +1,6 @@
# This config is split between targets since different component needs to be excluded (esp32, esp32s2)
CONFIG_IDF_TARGET="esp32"
TEST_EXCLUDE_COMPONENTS=bt app_update freertos esp_hw_support esp_system esp_pm esp_ipc esp_timer driver heap pthread soc spi_flash vfs test_utils experimental_cpp_component
TEST_EXCLUDE_COMPONENTS=bt app_update freertos esp_hw_support esp_system esp_pm esp_ipc esp_timer driver heap soc spi_flash vfs test_utils experimental_cpp_component
CONFIG_MEMMAP_SMP=n
CONFIG_FREERTOS_UNICORE=y
CONFIG_ESP32_RTCDATA_IN_FAST_MEM=y

View File

@ -1,6 +1,6 @@
# This config is split between targets since different component needs to be excluded (esp32, esp32s2)
CONFIG_IDF_TARGET="esp32s2"
TEST_EXCLUDE_COMPONENTS=bt app_update freertos esp_hw_support esp_ipc esp_system esp_pm esp_timer driver heap pthread soc spi_flash vfs experimental_cpp_component
TEST_EXCLUDE_COMPONENTS=bt app_update freertos esp_hw_support esp_ipc esp_system esp_pm esp_timer driver heap soc spi_flash vfs experimental_cpp_component
CONFIG_MEMMAP_SMP=n
CONFIG_FREERTOS_UNICORE=y
CONFIG_ESP32S2_RTCDATA_IN_FAST_MEM=y