Skip to content

Commit cdad898

Browse files
committed
fix(mqtt_cxx): Add a simple unit test
1 parent d979e1b commit cdad898

File tree

6 files changed

+92
-1
lines changed

6 files changed

+92
-1
lines changed

.github/workflows/mqtt_cxx__build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
matrix:
1616
idf_ver: ["release-v5.1", "release-v5.2", "release-v5.3", "release-v5.4", "release-v5.5"]
1717
idf_target: ["esp32"]
18-
test: [ { app: mqtt-basic, path: "components/esp_mqtt_cxx/examples" }]
18+
test: [ { app: mqtt-basic, path: "components/esp_mqtt_cxx/examples" }, { app: test, path: "components/esp_mqtt_cxx/test/host" }]
1919
runs-on: ubuntu-22.04
2020
container: espressif/idf:${{ matrix.idf_ver }}
2121
steps:
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
3+
4+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
5+
if(${IDF_TARGET} STREQUAL "linux")
6+
set(EXTRA_COMPONENT_DIRS "../../../../common_components/linux_compat")
7+
set(COMPONENTS main)
8+
endif()
9+
10+
project(esp_mqtt_cxx_host_test)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
idf_component_register(SRCS "test_esp_mqtt_cxx.cpp"
2+
WHOLE_ARCHIVE)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
dependencies:
2+
espressif/catch2: "^3.4.0"
3+
esp_mqtt_cxx:
4+
version: "*"
5+
override_path: '../../../'
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#include "catch2/catch_session.hpp"
7+
#include "catch2/catch_test_macros.hpp"
8+
#include "esp_mqtt.hpp"
9+
#include "esp_mqtt_client_config.hpp"
10+
11+
namespace mqtt = idf::mqtt;
12+
13+
namespace {
14+
class TestClient final : public mqtt::Client {
15+
public:
16+
using mqtt::Client::Client;
17+
18+
bool constructed{false};
19+
bool connected_called{false};
20+
21+
TestClient(const mqtt::BrokerConfiguration &broker, const mqtt::ClientCredentials &credentials, const mqtt::Configuration &config) :
22+
mqtt::Client(broker, credentials, config)
23+
{
24+
constructed = true;
25+
}
26+
27+
private:
28+
void on_connected(esp_mqtt_event_handle_t const event) override
29+
{
30+
CHECK(constructed);
31+
connected_called = true;
32+
}
33+
34+
void on_data(esp_mqtt_event_handle_t const event) override
35+
{
36+
CHECK(constructed);
37+
}
38+
};
39+
} // namespace
40+
41+
TEST_CASE("Client does not auto-start and can dispatch events after construction", "[esp_mqtt_cxx]")
42+
{
43+
mqtt::BrokerConfiguration broker{
44+
.address = mqtt::URI{std::string{"mqtt://example.com"}},
45+
.security = mqtt::Insecure{}
46+
};
47+
mqtt::ClientCredentials credentials{};
48+
mqtt::Configuration config{};
49+
50+
TestClient client{broker, credentials, config};
51+
52+
REQUIRE(client.is_started() == false);
53+
54+
esp_mqtt_event_t event{};
55+
event.event_id = MQTT_EVENT_CONNECTED;
56+
57+
client.dispatch_event_for_test(static_cast<int32_t>(MQTT_EVENT_CONNECTED), &event);
58+
59+
CHECK(client.connected_called);
60+
}
61+
62+
extern "C" void app_main(void)
63+
{
64+
Catch::Session session;
65+
66+
int failures = session.run();
67+
if (failures > 0) {
68+
printf("TEST FAILED! number of failures=%d\n", failures);
69+
exit(1);
70+
}
71+
printf("Test passed!\n");
72+
exit(0);
73+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_COMPILER_CXX_EXCEPTIONS=y

0 commit comments

Comments
 (0)