Skip to content

Commit c974c14

Browse files
committed
fix(websocket): Added unit tests to CI + minor fix to pass it
1 parent 247baee commit c974c14

File tree

9 files changed

+67
-15
lines changed

9 files changed

+67
-15
lines changed

.github/workflows/websocket__build-target-test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
strategy:
1515
matrix:
1616
idf_ver: ["release-v5.0", "release-v5.1", "latest"]
17-
test: [ { app: example, path: "examples" } ]
17+
test: [ { app: example, path: "examples" }, { app: unit_test, path: "test" } ]
1818
runs-on: ubuntu-20.04
1919
container: espressif/idf:${{ matrix.idf_ver }}
2020
env:
@@ -53,7 +53,7 @@ jobs:
5353
matrix:
5454
idf_ver: ["release-v5.0", "release-v5.1", "latest"]
5555
idf_target: ["esp32"]
56-
test: [ { app: example, path: "examples" } ]
56+
test: [ { app: example, path: "examples" }, { app: unit_test, path: "test" } ]
5757
runs-on:
5858
- self-hosted
5959
- ESP32-ETHERNET-KIT

components/esp_websocket_client/esp_websocket_client.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,9 @@ static void destroy_and_free_resources(esp_websocket_client_handle_t client)
424424
free(client->if_name);
425425
}
426426
esp_websocket_client_destroy_config(client);
427-
esp_transport_list_destroy(client->transport_list);
427+
if (client->transport_list) {
428+
esp_transport_list_destroy(client->transport_list);
429+
}
428430
vQueueDelete(client->lock);
429431
free(client->tx_buffer);
430432
free(client->rx_buffer);
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
1-
idf_component_register(SRC_DIRS "."
2-
PRIV_REQUIRES cmock test_utils esp_websocket_client)
1+
# This is the project CMakeLists.txt file for the test subproject
2+
cmake_minimum_required(VERSION 3.16)
3+
4+
set(EXTRA_COMPONENT_DIRS ../../esp_websocket_client
5+
"$ENV{IDF_PATH}/tools/unit-test-app/components")
6+
7+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
8+
project(websocket_unit_test)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
idf_component_register(SRCS "test_websocket_client.c"
2+
REQUIRES test_utils
3+
INCLUDE_DIRS "."
4+
PRIV_REQUIRES unity esp_websocket_client esp_event)

components/esp_websocket_client/test/test_websocket_client.c renamed to components/esp_websocket_client/test/main/test_websocket_client.c

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,36 @@
1313
#include <stdlib.h>
1414
#include <stdbool.h>
1515
#include <esp_websocket_client.h>
16-
16+
#include "esp_event.h"
1717
#include "unity.h"
18+
#include "test_utils.h"
19+
20+
#include "unity_fixture.h"
1821
#include "memory_checks.h"
1922

20-
static void test_leak_setup(const char *file, long line)
23+
TEST_GROUP(websocket);
24+
25+
TEST_SETUP(websocket)
2126
{
22-
printf("%s:%ld\n", file, line);
27+
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 1, 0)
28+
/* IDF v5.0 runs some lazy inits within printf()
29+
* This test sets the leak threshold to 0, so we need to call printf()
30+
* before recording the heap size in test_utils_record_free_mem()
31+
*/
32+
printf("TEST_SETUP: websocket\n");
33+
#endif
2334
test_utils_record_free_mem();
35+
TEST_ESP_OK(test_utils_set_leak_level(0, ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_GENERAL));
2436
}
2537

26-
TEST_CASE("websocket init and deinit", "[websocket][leaks=0]")
38+
TEST_TEAR_DOWN(websocket)
39+
{
40+
test_utils_finish_and_evaluate_leaks(0, 0);
41+
}
42+
43+
44+
TEST(websocket, websocket_init_deinit)
2745
{
28-
test_leak_setup(__FILE__, __LINE__);
2946
const esp_websocket_client_config_t websocket_cfg = {
3047
// no connection takes place, but the uri has to be valid for init() to succeed
3148
.uri = "ws://echo.websocket.org",
@@ -35,22 +52,32 @@ TEST_CASE("websocket init and deinit", "[websocket][leaks=0]")
3552
esp_websocket_client_destroy(client);
3653
}
3754

38-
TEST_CASE("websocket init with invalid url", "[websocket][leaks=0]")
55+
TEST(websocket, websocket_init_invalid_url)
3956
{
40-
test_leak_setup(__FILE__, __LINE__);
4157
const esp_websocket_client_config_t websocket_cfg = {
4258
.uri = "INVALID",
4359
};
4460
esp_websocket_client_handle_t client = esp_websocket_client_init(&websocket_cfg);
4561
TEST_ASSERT_NULL(client);
4662
}
4763

48-
TEST_CASE("websocket set url with invalid url", "[websocket][leaks=0]")
64+
TEST(websocket, websocket_set_invalid_url)
4965
{
50-
test_leak_setup(__FILE__, __LINE__);
5166
const esp_websocket_client_config_t websocket_cfg = {};
5267
esp_websocket_client_handle_t client = esp_websocket_client_init(&websocket_cfg);
5368
TEST_ASSERT_NOT_EQUAL(NULL, client);
5469
TEST_ASSERT_NOT_EQUAL(ESP_OK, esp_websocket_client_set_uri(client, "INVALID"));
5570
esp_websocket_client_destroy(client);
5671
}
72+
73+
TEST_GROUP_RUNNER(websocket)
74+
{
75+
RUN_TEST_CASE(websocket, websocket_init_deinit)
76+
RUN_TEST_CASE(websocket, websocket_init_invalid_url)
77+
RUN_TEST_CASE(websocket, websocket_set_invalid_url)
78+
}
79+
80+
void app_main(void)
81+
{
82+
UNITY_MAIN(websocket);
83+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
from pytest_embedded import Dut
5+
6+
7+
def test_websocket(dut: Dut) -> None:
8+
dut.expect_unity_test_output()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CONFIG_IDF_TARGET="esp32"
2+
CONFIG_UNITY_ENABLE_FIXTURE=y
3+
CONFIG_UNITY_ENABLE_IDF_TEST_RUNNER=n
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CONFIG_UNITY_ENABLE_FIXTURE=y
2+
CONFIG_UNITY_ENABLE_IDF_TEST_RUNNER=n

components/mdns/tests/unit_test/pytest_mdns.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
from pytest_embedded import Dut
55

66

7-
def test_lwip(dut: Dut) -> None:
7+
def test_mdns(dut: Dut) -> None:
88
dut.expect_unity_test_output()

0 commit comments

Comments
 (0)