MQTT unit test can inject WiFiClient (#5716)

pull/5722/head
Eric Severance 2025-01-02 03:32:39 -08:00 zatwierdzone przez GitHub
rodzic 93e2bc7058
commit 9f7cbf1b4f
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: B5690EEEBB952194
2 zmienionych plików z 30 dodań i 22 usunięć

Wyświetl plik

@ -282,7 +282,9 @@ void mqttInit()
}
#if HAS_NETWORKING
MQTT::MQTT() : concurrency::OSThread("mqtt"), pubSub(mqttClient), mqttQueue(MAX_MQTT_QUEUE)
MQTT::MQTT() : MQTT(std::unique_ptr<MQTTClient>(new MQTTClient())) {}
MQTT::MQTT(std::unique_ptr<MQTTClient> _mqttClient)
: concurrency::OSThread("mqtt"), mqttQueue(MAX_MQTT_QUEUE), mqttClient(std::move(_mqttClient)), pubSub(*mqttClient)
#else
MQTT::MQTT() : concurrency::OSThread("mqtt"), mqttQueue(MAX_MQTT_QUEUE)
#endif
@ -420,13 +422,13 @@ void MQTT::reconnect()
}
} else {
LOG_INFO("Use non-TLS-encrypted session");
pubSub.setClient(mqttClient);
pubSub.setClient(*mqttClient);
}
#else
pubSub.setClient(mqttClient);
pubSub.setClient(*mqttClient);
#endif
#elif HAS_NETWORKING
pubSub.setClient(mqttClient);
pubSub.setClient(*mqttClient);
#endif
std::pair<String, uint16_t> hostAndPort = parseHostAndPort(serverAddr, serverPort);
@ -444,7 +446,7 @@ void MQTT::reconnect()
enabled = true; // Start running background process again
runASAP = true;
reconnectCount = 0;
isMqttServerAddressPrivate = isPrivateIpAddress(mqttClient.remoteIP());
isMqttServerAddressPrivate = isPrivateIpAddress(mqttClient->remoteIP());
publishNodeInfo();
sendSubscriptions();

Wyświetl plik

@ -22,6 +22,7 @@
#if HAS_NETWORKING
#include <PubSubClient.h>
#include <memory>
#endif
#define MAX_MQTT_QUEUE 16
@ -32,24 +33,7 @@
*/
class MQTT : private concurrency::OSThread
{
// supposedly the current version is busted:
// http://www.iotsharing.com/2017/08/how-to-use-esp32-mqtts-with-mqtts-mosquitto-broker-tls-ssl.html
#if HAS_WIFI
WiFiClient mqttClient;
#if !defined(ARCH_PORTDUINO)
#if (defined(ESP_ARDUINO_VERSION_MAJOR) && ESP_ARDUINO_VERSION_MAJOR < 3) || defined(RPI_PICO)
WiFiClientSecure wifiSecureClient;
#endif
#endif
#endif
#if HAS_ETHERNET
EthernetClient mqttClient;
#endif
public:
#if HAS_NETWORKING
PubSubClient pubSub;
#endif
MQTT();
/**
@ -93,7 +77,29 @@ class MQTT : private concurrency::OSThread
virtual int32_t runOnce() override;
#ifndef PIO_UNIT_TESTING
private:
#endif
// supposedly the current version is busted:
// http://www.iotsharing.com/2017/08/how-to-use-esp32-mqtts-with-mqtts-mosquitto-broker-tls-ssl.html
#if HAS_WIFI
using MQTTClient = WiFiClient;
#if !defined(ARCH_PORTDUINO)
#if (defined(ESP_ARDUINO_VERSION_MAJOR) && ESP_ARDUINO_VERSION_MAJOR < 3) || defined(RPI_PICO)
WiFiClientSecure wifiSecureClient;
#endif
#endif
#endif
#if HAS_ETHERNET
using MQTTClient = EthernetClient;
#endif
#if HAS_NETWORKING
std::unique_ptr<MQTTClient> mqttClient;
PubSubClient pubSub;
explicit MQTT(std::unique_ptr<MQTTClient> mqttClient);
#endif
std::string cryptTopic = "/2/e/"; // msh/2/e/CHANNELID/NODEID
std::string jsonTopic = "/2/json/"; // msh/2/json/CHANNELID/NODEID
std::string mapTopic = "/2/map/"; // For protobuf-encoded MapReport messages