|
| 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 | +#include "esp_netif.h" |
| 11 | + |
| 12 | +namespace mqtt = idf::mqtt; |
| 13 | + |
| 14 | +namespace { |
| 15 | +class TestClient final : public mqtt::Client { |
| 16 | +public: |
| 17 | + using mqtt::Client::Client; |
| 18 | + |
| 19 | + bool constructed{false}; |
| 20 | + bool before_connect{false}; |
| 21 | + bool disconnected{false}; |
| 22 | + |
| 23 | + TestClient(const mqtt::BrokerConfiguration &broker, const mqtt::ClientCredentials &credentials, const mqtt::Configuration &config) : |
| 24 | + mqtt::Client(broker, credentials, config) |
| 25 | + { |
| 26 | + constructed = true; |
| 27 | + } |
| 28 | + |
| 29 | +private: |
| 30 | + void on_connected(esp_mqtt_event_handle_t const event) override |
| 31 | + { |
| 32 | + CHECK(constructed); |
| 33 | + } |
| 34 | + |
| 35 | + void on_data(esp_mqtt_event_handle_t const event) override |
| 36 | + { |
| 37 | + CHECK(constructed); |
| 38 | + } |
| 39 | + |
| 40 | + void on_before_connect(esp_mqtt_event_handle_t const event) override |
| 41 | + { |
| 42 | + CHECK(constructed); |
| 43 | + before_connect = true; |
| 44 | + } |
| 45 | + |
| 46 | + void on_disconnected(const esp_mqtt_event_handle_t event) override |
| 47 | + { |
| 48 | + CHECK(constructed); |
| 49 | + disconnected = true; |
| 50 | + |
| 51 | + } |
| 52 | + |
| 53 | +}; |
| 54 | +} // namespace |
| 55 | + |
| 56 | +TEST_CASE("Client does not auto-start and can dispatch events after construction", "[esp_mqtt_cxx]") |
| 57 | +{ |
| 58 | + mqtt::BrokerConfiguration broker{ |
| 59 | + .address = mqtt::URI{std::string{"mqtt://127.0.0.1:1883"}}, |
| 60 | + .security = mqtt::Insecure{} |
| 61 | + }; |
| 62 | + mqtt::ClientCredentials credentials{}; |
| 63 | + mqtt::Configuration config{}; |
| 64 | + |
| 65 | + TestClient client{broker, credentials, config}; |
| 66 | + |
| 67 | + REQUIRE(client.is_started() == false); |
| 68 | + |
| 69 | + // start the client and expect disconnection (reset by peer) |
| 70 | + // since no server's running on this ESP32 |
| 71 | + client.start(); |
| 72 | + CHECK(client.is_started() == true); |
| 73 | + |
| 74 | + CHECK(client.before_connect); |
| 75 | + usleep(10000); |
| 76 | + CHECK(client.disconnected); |
| 77 | +} |
| 78 | + |
| 79 | +extern "C" void app_main(void) |
| 80 | +{ |
| 81 | + ESP_ERROR_CHECK(esp_netif_init()); |
| 82 | + ESP_ERROR_CHECK(esp_event_loop_create_default()); |
| 83 | + |
| 84 | + Catch::Session session; |
| 85 | + |
| 86 | + int failures = session.run(); |
| 87 | + if (failures > 0) { |
| 88 | + printf("TEST FAILED! number of failures=%d\n", failures); |
| 89 | + return; |
| 90 | + } |
| 91 | + printf("Test passed!\n"); |
| 92 | +} |
0 commit comments