fix(mqtt): count connect failures from a flag set before the attempt

The Starting-state gate raced the event task: client_state becomes Starting
only after connect()/reconnect() returns, so a fast failure could deliver
DISCONNECTED first and go uncounted, while a deliberate stop of an attempt in
flight was counted as a failure. A per-slot flag is now raised before the call
and cleared by onConnect, a failed start, or stopSlotClient().

Also stops zero-valued connect_failures/slots_breaker from creating an empty
stats object.
This commit is contained in:
agessaman committed 2026-09-26 11:53:54 -07:00
1 parent 7004b81c0c
commit 73098a44b6
4 files changed
+33 -4

No files matched your search

@@ -158,6 +158,23 @@ TEST(MQTTPayloadBuilder, StatusReportsConnectFailuresAndBreakerOnlyWhenNonZero)
EXPECT_FALSE(healthy_stats["mqtt_slots_breaker"].is<JsonVariant>());
}
// Zero counters with nothing else supplied must not create an empty stats object.
TEST(MQTTPayloadBuilder, StatusOmitsStatsWhenOnlyZeroCountersSupplied) {
JsonDocument scratch;
char buffer[768];
MQTTConnHealth health;
health.connect_failures = 0;
health.slots_breaker = 0;
ASSERT_GT(MQTTPayloadBuilder::buildStatusMessage(
scratch, "node", "id", "model", "firmware", "radio", "client", "online",
kTimestamp, buffer, sizeof(buffer), -1, -1, -1, -1, -999,
-1, -1, -1, -1, -1, -1, nullptr, health), 0);
JsonDocument parsed;
ASSERT_FALSE(deserializeJson(parsed, buffer));
EXPECT_FALSE(parsed["stats"].is<JsonVariant>());
}
// The outage key doubles as the "something is down" flag, so its absence has to be
// meaningful: a fully healthy board must not emit it.
TEST(MQTTPayloadBuilder, StatusOmitsOutageKeyWhenEverySlotIsUp) {