fix(websocket): Added unit tests to CI + minor fix to pass it

This commit is contained in:
David Cermak
2023-05-16 18:36:39 +02:00
parent 247baeed22
commit c974c14220
9 changed files with 67 additions and 15 deletions

View File

@ -14,7 +14,7 @@ jobs:
strategy:
matrix:
idf_ver: ["release-v5.0", "release-v5.1", "latest"]
test: [ { app: example, path: "examples" } ]
test: [ { app: example, path: "examples" }, { app: unit_test, path: "test" } ]
runs-on: ubuntu-20.04
container: espressif/idf:${{ matrix.idf_ver }}
env:
@ -53,7 +53,7 @@ jobs:
matrix:
idf_ver: ["release-v5.0", "release-v5.1", "latest"]
idf_target: ["esp32"]
test: [ { app: example, path: "examples" } ]
test: [ { app: example, path: "examples" }, { app: unit_test, path: "test" } ]
runs-on:
- self-hosted
- ESP32-ETHERNET-KIT

View File

@ -424,7 +424,9 @@ static void destroy_and_free_resources(esp_websocket_client_handle_t client)
free(client->if_name);
}
esp_websocket_client_destroy_config(client);
esp_transport_list_destroy(client->transport_list);
if (client->transport_list) {
esp_transport_list_destroy(client->transport_list);
}
vQueueDelete(client->lock);
free(client->tx_buffer);
free(client->rx_buffer);

View File

@ -1,2 +1,8 @@
idf_component_register(SRC_DIRS "."
PRIV_REQUIRES cmock test_utils esp_websocket_client)
# This is the project CMakeLists.txt file for the test subproject
cmake_minimum_required(VERSION 3.16)
set(EXTRA_COMPONENT_DIRS ../../esp_websocket_client
"$ENV{IDF_PATH}/tools/unit-test-app/components")
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(websocket_unit_test)

View File

@ -0,0 +1,4 @@
idf_component_register(SRCS "test_websocket_client.c"
REQUIRES test_utils
INCLUDE_DIRS "."
PRIV_REQUIRES unity esp_websocket_client esp_event)

View File

@ -13,19 +13,36 @@
#include <stdlib.h>
#include <stdbool.h>
#include <esp_websocket_client.h>
#include "esp_event.h"
#include "unity.h"
#include "test_utils.h"
#include "unity_fixture.h"
#include "memory_checks.h"
static void test_leak_setup(const char *file, long line)
TEST_GROUP(websocket);
TEST_SETUP(websocket)
{
printf("%s:%ld\n", file, line);
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 1, 0)
/* IDF v5.0 runs some lazy inits within printf()
* This test sets the leak threshold to 0, so we need to call printf()
* before recording the heap size in test_utils_record_free_mem()
*/
printf("TEST_SETUP: websocket\n");
#endif
test_utils_record_free_mem();
TEST_ESP_OK(test_utils_set_leak_level(0, ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_GENERAL));
}
TEST_CASE("websocket init and deinit", "[websocket][leaks=0]")
TEST_TEAR_DOWN(websocket)
{
test_utils_finish_and_evaluate_leaks(0, 0);
}
TEST(websocket, websocket_init_deinit)
{
test_leak_setup(__FILE__, __LINE__);
const esp_websocket_client_config_t websocket_cfg = {
// no connection takes place, but the uri has to be valid for init() to succeed
.uri = "ws://echo.websocket.org",
@ -35,9 +52,8 @@ TEST_CASE("websocket init and deinit", "[websocket][leaks=0]")
esp_websocket_client_destroy(client);
}
TEST_CASE("websocket init with invalid url", "[websocket][leaks=0]")
TEST(websocket, websocket_init_invalid_url)
{
test_leak_setup(__FILE__, __LINE__);
const esp_websocket_client_config_t websocket_cfg = {
.uri = "INVALID",
};
@ -45,12 +61,23 @@ TEST_CASE("websocket init with invalid url", "[websocket][leaks=0]")
TEST_ASSERT_NULL(client);
}
TEST_CASE("websocket set url with invalid url", "[websocket][leaks=0]")
TEST(websocket, websocket_set_invalid_url)
{
test_leak_setup(__FILE__, __LINE__);
const esp_websocket_client_config_t websocket_cfg = {};
esp_websocket_client_handle_t client = esp_websocket_client_init(&websocket_cfg);
TEST_ASSERT_NOT_EQUAL(NULL, client);
TEST_ASSERT_NOT_EQUAL(ESP_OK, esp_websocket_client_set_uri(client, "INVALID"));
esp_websocket_client_destroy(client);
}
TEST_GROUP_RUNNER(websocket)
{
RUN_TEST_CASE(websocket, websocket_init_deinit)
RUN_TEST_CASE(websocket, websocket_init_invalid_url)
RUN_TEST_CASE(websocket, websocket_set_invalid_url)
}
void app_main(void)
{
UNITY_MAIN(websocket);
}

View File

@ -0,0 +1,8 @@
# SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: Apache-2.0
from pytest_embedded import Dut
def test_websocket(dut: Dut) -> None:
dut.expect_unity_test_output()

View File

@ -0,0 +1,3 @@
CONFIG_IDF_TARGET="esp32"
CONFIG_UNITY_ENABLE_FIXTURE=y
CONFIG_UNITY_ENABLE_IDF_TEST_RUNNER=n

View File

@ -0,0 +1,2 @@
CONFIG_UNITY_ENABLE_FIXTURE=y
CONFIG_UNITY_ENABLE_IDF_TEST_RUNNER=n

View File

@ -4,5 +4,5 @@
from pytest_embedded import Dut
def test_lwip(dut: Dut) -> None:
def test_mdns(dut: Dut) -> None:
dut.expect_unity_test_output()