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