mirror of
https://github.com/alexhopeoconnor/arduino-home-assistant.git
synced 2026-10-04 02:48:13 +10:00
test: add MQTT callback publish regressions and mock callback tracking
PubSubClientMock flags inbound callback context and counts publish attempts from callbacks. Entity and core tests assert unsafe callback publishes are rejected by the mock (baseline before HAMqtt deferred publish).
This commit is contained in:
@@ -7,6 +7,7 @@ static TestCase tests[] = {
|
||||
TEST_ENTRY(test_MqttTest_reconnect_interval_default_value),
|
||||
TEST_ENTRY(test_MqttTest_reconnect_interval_setter),
|
||||
TEST_ENTRY(test_MqttTest_reconnect_interval_throttles_attempts),
|
||||
TEST_ENTRY(test_MqttTest_publish_attempt_from_message_callback_is_rejected),
|
||||
|
||||
TEST_ENTRY(test_DeviceTest_default_unique_id),
|
||||
TEST_ENTRY(test_DeviceTest_unique_id_constructor_char),
|
||||
|
||||
@@ -17,6 +17,7 @@ extern void test_MqttTest_maximum_number_of_device_types(void);
|
||||
extern void test_MqttTest_reconnect_interval_default_value(void);
|
||||
extern void test_MqttTest_reconnect_interval_setter(void);
|
||||
extern void test_MqttTest_reconnect_interval_throttles_attempts(void);
|
||||
extern void test_MqttTest_publish_attempt_from_message_callback_is_rejected(void);
|
||||
|
||||
extern void test_DeviceTest_default_unique_id(void);
|
||||
extern void test_DeviceTest_unique_id_constructor_char(void);
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
|
||||
static const char* testDeviceId = "testDevice";
|
||||
static const char* testUniqueId = "uniqueId";
|
||||
static HAMqtt* activeMqtt = nullptr;
|
||||
static bool messageCallbackCalled = false;
|
||||
|
||||
const char ComponentNameStr[] PROGMEM = {"componentName"};
|
||||
|
||||
@@ -20,6 +22,17 @@ protected:
|
||||
}
|
||||
};
|
||||
|
||||
void onMessagePublishAttempt(const char* topic, const uint8_t* payload, uint16_t length)
|
||||
{
|
||||
(void)topic;
|
||||
(void)payload;
|
||||
(void)length;
|
||||
|
||||
messageCallbackCalled = true;
|
||||
TEST_ASSERT_NOT_NULL(activeMqtt);
|
||||
TEST_ASSERT_FALSE(activeMqtt->publish("testData/testDevice/echo", "1", false));
|
||||
}
|
||||
|
||||
void test_MqttTest_maximum_number_of_device_types(void)
|
||||
{
|
||||
HADevice device(testDeviceId);
|
||||
@@ -70,3 +83,26 @@ void test_MqttTest_reconnect_interval_throttles_attempts(void)
|
||||
mqtt.loop();
|
||||
TEST_ASSERT_EQUAL_UINT16(2, mock->getConnectCallsNb());
|
||||
}
|
||||
|
||||
void test_MqttTest_publish_attempt_from_message_callback_is_rejected(void)
|
||||
{
|
||||
PubSubClientMock* mock = new PubSubClientMock();
|
||||
HADevice device(testDeviceId);
|
||||
HAMqtt mqtt(mock, device);
|
||||
mqtt.setDataPrefix("testData");
|
||||
mqtt.begin("testHost", "testUser", "testPass");
|
||||
mock->connectDummy();
|
||||
|
||||
activeMqtt = &mqtt;
|
||||
messageCallbackCalled = false;
|
||||
mock->resetPublishCallsFromCallbackNb();
|
||||
mqtt.onMessage(onMessagePublishAttempt);
|
||||
|
||||
mock->fakeMessage("testData/testDevice/input", "1");
|
||||
|
||||
TEST_ASSERT_TRUE(messageCallbackCalled);
|
||||
TEST_ASSERT_EQUAL_UINT16(1, mock->getPublishCallsFromCallbackNb());
|
||||
TEST_ASSERT_EQUAL_UINT8(0, mock->getFlushedMessagesNb());
|
||||
|
||||
activeMqtt = nullptr;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user