diff --git a/MQTT_IMPLEMENTATION.md b/MQTT_IMPLEMENTATION.md index bfc20c99..e880814e 100644 --- a/MQTT_IMPLEMENTATION.md +++ b/MQTT_IMPLEMENTATION.md @@ -575,9 +575,13 @@ route. Each medium keeps its own DHCP DNS servers. **Self-heal.** With `mqtt.selfheal on`, the node alerts and reboots 1 min later when a bridge stop never completes (10 min), an MQTT client cannot be stopped (30 min), or every slot is down for 2 h while an NTP reply in the last hour shows the internet is reachable. +The all-slots-down hold adds up to 30 min of per-node random delay, chosen once at boot. Slots refused by their broker do not count. It waits for 30 min of uptime, doubles the -hold per consecutive self-heal reboot (up to 8x), and only alerts after 5 in a row. Until -a slot stays connected for 10 min, the status payload carries `selfheal` +hold per consecutive self-heal reboot (up to 8x), and only alerts after 5 in a row. +All-slots-down gets one reboot per outage, and only if no self-heal reboot of any cause +has occurred since the last healthy period; after that it only alerts. Ten healthy +minutes resets the count and re-arms all-slots-down, either with a connected slot or +with the bridge deliberately off and clean. Until then, the status payload carries `selfheal` (`stop_unproven`, `client_quarantined` or `all_slots_down`) and `selfheal_n` (count) next to `reset_reason`. While no slot is connected, the bridge probes NTP about every 20 min without changing the clock. diff --git a/docs/cli_commands.md b/docs/cli_commands.md index 14c04da9..820193b4 100644 --- a/docs/cli_commands.md +++ b/docs/cli_commands.md @@ -1412,7 +1412,7 @@ Covers serial NMEA receivers (`MicroNMEALocationProvider`). **Default:** `on` -> **Note:** When the observer is stuck in a state only a reboot clears, the node sends an alert and reboots 1 min later. The states are a bridge stop that never completed (10 min), an MQTT client that could not be stopped (30 min), and every slot down for 2 h while NTP shows the internet is reachable. Slots refused by their broker (wrong credentials) do not count. A node must be up 30 min first, each consecutive self-heal reboot doubles the wait (up to 8x), and after 5 in a row it only alerts. 10 min with a connected slot resets the count. `get` also shows the last self-heal cause and count, or `none`. The repeater keeps working in all of these states. +> **Note:** When the observer is stuck in a state only a reboot clears, the node sends an alert and reboots 1 min later. The states are a bridge stop that never completed (10 min), an MQTT client that could not be stopped (30 min), and every slot down for 2 h while NTP shows the internet is reachable. The all-slots-down hold adds up to 30 min of per-node random delay, chosen once at boot. Slots refused by their broker (wrong credentials) do not count. A node must be up 30 min first, each consecutive self-heal reboot doubles the wait (up to 8x), and after 5 in a row it only alerts. All-slots-down gets one reboot per outage, and only if no self-heal reboot of any cause has occurred since the last healthy period; after that it only alerts. Ten healthy minutes resets the count and re-arms all-slots-down, either with a connected slot or with the bridge deliberately off and clean. `get` also shows the last self-heal cause and count, or `none`. The repeater keeps working in all of these states. --- diff --git a/src/helpers/MQTTSelfHealPolicy.h b/src/helpers/MQTTSelfHealPolicy.h index a78411a7..ea174c69 100644 --- a/src/helpers/MQTTSelfHealPolicy.h +++ b/src/helpers/MQTTSelfHealPolicy.h @@ -50,6 +50,7 @@ struct Config { uint32_t evidence_max_age_ms; // oldest NTP reply that still proves the internet is reachable uint8_t max_backoff_shift; // hold time doubles per consecutive reboot, up to this uint8_t max_consecutive; // stop rebooting after this many without recovery + uint8_t all_down_max_reboots; // all-slots-down reboot allowance since last healthy }; // A late stop ack and the breaker probe both resolve well inside these windows, @@ -65,6 +66,7 @@ static inline Config defaultConfig() { c.evidence_max_age_ms = 60UL * 60UL * 1000UL; c.max_backoff_shift = 3; c.max_consecutive = 5; + c.all_down_max_reboots = 1; return c; } @@ -158,6 +160,11 @@ static inline bool causePresent(const Inputs& in, const Config& cfg, Cause c) { } } +static inline bool rebootSuppressed(const Config& cfg, Cause c, uint8_t consecutive_reboots) { + return consecutive_reboots >= cfg.max_consecutive || + (c == Cause::AllSlotsDown && consecutive_reboots >= cfg.all_down_max_reboots); +} + static inline Result tick(State& st, const Inputs& in, const Config& cfg, uint8_t consecutive_reboots) { Result r{}; @@ -206,10 +213,10 @@ static inline Result tick(State& st, const Inputs& in, const Config& cfg, } if (!st.uptime_ok) return r; - bool suppressed = consecutive_reboots >= cfg.max_consecutive; - // A pending reboot outranks a new alert so the grace timer cannot be starved. - for (uint8_t i = 0; i < kCauseCount && !suppressed; i++) { + for (uint8_t i = 0; i < kCauseCount; i++) { + Cause c = (Cause)(i + 1); + if (rebootSuppressed(cfg, c, consecutive_reboots)) continue; if (!st.active[i] || !st.alerted[i]) continue; if ((uint32_t)(in.now_ms - st.alerted_ms[i]) >= cfg.alert_grace_ms) { r.action = Action::Reboot; @@ -229,7 +236,7 @@ static inline Result tick(State& st, const Inputs& in, const Config& cfg, r.action = Action::Alert; r.cause = c; r.held_ms = held; - r.reboot_suppressed = suppressed; + r.reboot_suppressed = rebootSuppressed(cfg, c, consecutive_reboots); return r; } } diff --git a/src/helpers/bridges/ObserverSelfHeal.h b/src/helpers/bridges/ObserverSelfHeal.h index 134ab851..407c6676 100644 --- a/src/helpers/bridges/ObserverSelfHeal.h +++ b/src/helpers/bridges/ObserverSelfHeal.h @@ -5,11 +5,12 @@ #include "helpers/MQTTSelfHealPolicy.h" // Drives MQTTSelfHealPolicy from the role's main loop: samples the bridge about -// every 10 s and performs the returned action. Holds only the policy state. +// every 10 s and performs the returned action. // // Platform provides (all static): // bool kSupported; false compiles the driver to nothing // uint32_t nowMs(); +// uint32_t randomBelow(uint32_t bound); uniform in [0, bound) // MQTTSelfHealPolicy::RtcRecord rtcLoad(); // void rtcStore(const MQTTSelfHealPolicy::RtcRecord&); // Host provides: getObserverPrefs() (mqtt_selfheal_enabled, alert_enabled), getNodePrefs() (bridge_enabled), @@ -19,10 +20,21 @@ #if defined(ESP_PLATFORM) && defined(WITH_MQTT_BRIDGE) #include +#include #include "helpers/ObserverSelfHealRtc.h" struct ObserverSelfHealPlatform { static const bool kSupported = true; static uint32_t nowMs() { return (uint32_t)millis(); } + static uint32_t randomBelow(uint32_t bound) { + if (bound == 0) return 0; + // Reject the partial bucket so modulo reduction stays uniform. + const uint32_t threshold = (uint32_t)(-bound) % bound; + uint32_t value; + do { + value = esp_random(); + } while (value < threshold); + return value % bound; + } static MQTTSelfHealPolicy::RtcRecord rtcLoad() { return ObserverSelfHealRtc::load(); } static void rtcStore(const MQTTSelfHealPolicy::RtcRecord& rec) { ObserverSelfHealRtc::store(rec); } }; @@ -30,18 +42,24 @@ struct ObserverSelfHealPlatform { struct ObserverSelfHealPlatform { static const bool kSupported = false; static uint32_t nowMs() { return 0; } + static uint32_t randomBelow(uint32_t) { return 0; } static MQTTSelfHealPolicy::RtcRecord rtcLoad() { return MQTTSelfHealPolicy::rtcCleared(); } static void rtcStore(const MQTTSelfHealPolicy::RtcRecord&) {} }; #endif +#ifndef MQTT_SELFHEAL_ALL_DOWN_JITTER_MS +#define MQTT_SELFHEAL_ALL_DOWN_JITTER_MS (30UL * 60UL * 1000UL) +#endif +static_assert(MQTT_SELFHEAL_ALL_DOWN_JITTER_MS < UINT32_MAX, "All-down jitter must fit below UINT32_MAX"); + // Log provides line(msg, detail = nullptr) and flush(). template class ObserverSelfHeal { public: static const uint32_t kTickMs = 10000; - ObserverSelfHeal() : _st(), _last_tick_ms(0) {} + ObserverSelfHeal() : _st(), _last_tick_ms(0), _all_down_jitter_ms(UINT32_MAX) {} // The bridge was started, stopped or restarted by the sequencer. void onBridgeRestart() { MQTTSelfHealPolicy::onBridgeRestart(_st); } @@ -51,13 +69,18 @@ public: void poll(Host& host, Bridge* bridge, bool ota_active = false) { if (!Platform::kSupported) return; namespace Heal = MQTTSelfHealPolicy; + if (_all_down_jitter_ms == UINT32_MAX) { + _all_down_jitter_ms = Platform::randomBelow(MQTT_SELFHEAL_ALL_DOWN_JITTER_MS + 1); + } const uint32_t now = Platform::nowMs(); if ((uint32_t)(now - _last_tick_ms) < kTickMs) return; _last_tick_ms = now; const auto* obs = host.getObserverPrefs(); const Heal::Inputs in = sample(host, bridge, now, ota_active); - const Heal::Config cfg = Heal::defaultConfig(); + Heal::Config cfg = Heal::defaultConfig(); + const uint64_t hold = (uint64_t)cfg.all_slots_down_ms + _all_down_jitter_ms; + cfg.all_slots_down_ms = hold > UINT32_MAX ? UINT32_MAX : (uint32_t)hold; const Heal::RtcRecord rec = Platform::rtcLoad(); const Heal::Result r = Heal::tick(_st, in, cfg, Heal::rtcConsecutive(rec)); switch (r.action) { @@ -71,7 +94,8 @@ public: case Heal::Action::Reboot: { // Confirm on a fresh sample so one torn read cannot reboot the node. const Heal::Inputs again = sample(host, bridge, Platform::nowMs(), ota_active); - if (!again.enabled || again.ota_in_progress || !Heal::causePresent(again, cfg, r.cause)) break; + if (!again.enabled || again.ota_in_progress || !Heal::causePresent(again, cfg, r.cause) || + Heal::rebootSuppressed(cfg, r.cause, Heal::rtcConsecutive(rec))) break; // Count the reboot before it happens: the boot-loop limit depends on it. Platform::rtcStore(Heal::rtcBeforeReboot(rec, r.cause)); Log::line("MQTT self-heal: rebooting for ", Heal::causeLabel(r.cause)); @@ -113,4 +137,5 @@ private: MQTTSelfHealPolicy::State _st; uint32_t _last_tick_ms; + uint32_t _all_down_jitter_ms; }; diff --git a/test/test_mqtt_self_heal_policy/test_mqtt_self_heal_policy.cpp b/test/test_mqtt_self_heal_policy/test_mqtt_self_heal_policy.cpp index f38eeb8b..5ac7afa0 100644 --- a/test/test_mqtt_self_heal_policy/test_mqtt_self_heal_policy.cpp +++ b/test/test_mqtt_self_heal_policy/test_mqtt_self_heal_policy.cpp @@ -56,6 +56,7 @@ TEST(MQTTSelfHealPolicy, DefaultsMatchTheAgreedThresholds) { EXPECT_EQ(60 * kMin, cfg.evidence_max_age_ms); EXPECT_EQ(3, cfg.max_backoff_shift); EXPECT_EQ(5, cfg.max_consecutive); + EXPECT_EQ(1, cfg.all_down_max_reboots); } TEST(MQTTSelfHealPolicy, HealthyNodeNeverActs) { @@ -369,6 +370,145 @@ TEST(MQTTSelfHealPolicy, RebootLimitAlertsOnceAndNeverReboots) { } } +TEST(MQTTSelfHealPolicy, AllSlotsDownRebootsAtCountZero) { + Harness h; + const uint32_t start = 40 * kMin; + h.tick(AllDown(start)); + Heal::Result r = h.tick(AllDown(start + 120 * kMin)); + ASSERT_EQ(Heal::Action::Alert, r.action); + EXPECT_EQ(Heal::Cause::AllSlotsDown, r.cause); + EXPECT_FALSE(r.reboot_suppressed); + EXPECT_EQ(Heal::Action::None, h.tick(AllDown(start + 121 * kMin - 1)).action); + r = h.tick(AllDown(start + 121 * kMin)); + EXPECT_EQ(Heal::Action::Reboot, r.action); + EXPECT_EQ(Heal::Cause::AllSlotsDown, r.cause); +} + +TEST(MQTTSelfHealPolicy, AllSlotsDownAlertsOnceWithoutRebootAfterAnySelfHealReboot) { + for (uint8_t count = 1; count <= 5; count++) { + SCOPED_TRACE(count); + Harness h; + h.consecutive = count; + const uint32_t start = 40 * kMin; + const uint32_t hold = Heal::requiredHoldMs(h.cfg, Heal::Cause::AllSlotsDown, count); + h.tick(AllDown(start)); + EXPECT_EQ(Heal::Action::None, h.tick(AllDown(start + hold - 1)).action); + Heal::Result r = h.tick(AllDown(start + hold)); + ASSERT_EQ(Heal::Action::Alert, r.action); + EXPECT_EQ(Heal::Cause::AllSlotsDown, r.cause); + EXPECT_TRUE(r.reboot_suppressed); + for (uint32_t t = hold + kMin; t <= 7UL * 24 * 60 * kMin; t += kMin) { + EXPECT_EQ(Heal::Action::None, h.tick(AllDown(start + t)).action); + } + } +} + +TEST(MQTTSelfHealPolicy, AllSlotsDownRebootAllowanceIsConfigurable) { + Harness h; + h.consecutive = 1; + h.cfg.all_down_max_reboots = 2; + const uint32_t start = 40 * kMin; + h.tick(AllDown(start)); + Heal::Result r = h.tick(AllDown(start + 240 * kMin)); + EXPECT_EQ(Heal::Action::Alert, r.action); + EXPECT_FALSE(r.reboot_suppressed); + EXPECT_EQ(Heal::Action::Reboot, h.tick(AllDown(start + 241 * kMin)).action); + + Harness disabled; + disabled.cfg.all_down_max_reboots = 0; + disabled.tick(AllDown(start)); + r = disabled.tick(AllDown(start + 120 * kMin)); + EXPECT_EQ(Heal::Action::Alert, r.action); + EXPECT_TRUE(r.reboot_suppressed); + EXPECT_EQ(Heal::Action::None, disabled.tick(AllDown(start + 121 * kMin)).action); +} + +TEST(MQTTSelfHealPolicy, StuckClientCausesKeepTheirFiveRebootLimit) { + for (Heal::Cause cause : {Heal::Cause::StopUnproven, Heal::Cause::ClientQuarantined}) { + for (uint8_t count = 1; count <= 5; count++) { + SCOPED_TRACE(Heal::causeLabel(cause)); + SCOPED_TRACE(count); + Harness h; + h.consecutive = count; + const uint32_t start = 40 * kMin; + Heal::Inputs in = cause == Heal::Cause::StopUnproven ? Unproven(start) : Healthy(start); + in.client_quarantined = cause == Heal::Cause::ClientQuarantined; + h.tick(in); + const uint32_t hold = Heal::requiredHoldMs(h.cfg, cause, count); + in.now_ms = start + hold - 1; + EXPECT_EQ(Heal::Action::None, h.tick(in).action); + in.now_ms++; + Heal::Result r = h.tick(in); + EXPECT_EQ(Heal::Action::Alert, r.action); + EXPECT_EQ(cause, r.cause); + EXPECT_EQ(count == 5, r.reboot_suppressed); + in.now_ms += h.cfg.alert_grace_ms - 1; + EXPECT_EQ(Heal::Action::None, h.tick(in).action); + in.now_ms++; + r = h.tick(in); + EXPECT_EQ(count == 5 ? Heal::Action::None : Heal::Action::Reboot, r.action); + if (count < 5) EXPECT_EQ(cause, r.cause); + } + } +} + +TEST(MQTTSelfHealPolicy, SuppressedAllSlotsDownCannotBlockAStuckClientReboot) { + for (Heal::Cause cause : {Heal::Cause::StopUnproven, Heal::Cause::ClientQuarantined}) { + Harness h; + h.consecutive = 1; + const uint32_t start = 40 * kMin; + h.tick(AllDown(start)); + Heal::Inputs in = AllDown(start + 240 * kMin); + Heal::Result r = h.tick(in); + ASSERT_EQ(Heal::Action::Alert, r.action); + ASSERT_TRUE(r.reboot_suppressed); + in.stop_unproven = cause == Heal::Cause::StopUnproven; + in.client_quarantined = cause == Heal::Cause::ClientQuarantined; + h.tick(in); + in.now_ms += Heal::requiredHoldMs(h.cfg, cause, h.consecutive); + r = h.tick(in); + EXPECT_EQ(Heal::Action::Alert, r.action); + EXPECT_EQ(cause, r.cause); + EXPECT_FALSE(r.reboot_suppressed); + in.now_ms += h.cfg.alert_grace_ms; + r = h.tick(in); + EXPECT_EQ(Heal::Action::Reboot, r.action); + EXPECT_EQ(cause, r.cause); + } +} + +TEST(MQTTSelfHealPolicy, PendingRebootOutranksASuppressedAllSlotsDownAlert) { + Harness h; + h.consecutive = 1; + h.cfg.all_slots_down_ms = h.cfg.stop_unproven_ms; + Heal::Inputs in = AllDown(40 * kMin); + in.stop_unproven = true; + h.tick(in); + in.now_ms += 20 * kMin; + EXPECT_EQ(Heal::Cause::StopUnproven, h.tick(in).cause); + in.now_ms += h.cfg.alert_grace_ms; + Heal::Result r = h.tick(in); + EXPECT_EQ(Heal::Action::Reboot, r.action); + EXPECT_EQ(Heal::Cause::StopUnproven, r.cause); +} + +TEST(MQTTSelfHealPolicy, HealthyCounterResetRearmsAllSlotsDown) { + Harness h; + h.consecutive = 1; + const uint32_t start = 40 * kMin; + h.tick(AllDown(start)); + EXPECT_TRUE(h.tick(AllDown(start + 240 * kMin)).reboot_suppressed); + h.tick(Healthy(start + 241 * kMin)); + EXPECT_EQ(Heal::Action::ClearBootCounter, h.tick(Healthy(start + 251 * kMin)).action); + h.consecutive = 0; + const uint32_t again = start + 252 * kMin; + h.tick(AllDown(again)); + Heal::Result r = h.tick(AllDown(again + 120 * kMin)); + EXPECT_EQ(Heal::Action::Alert, r.action); + EXPECT_FALSE(r.reboot_suppressed); + EXPECT_EQ(Heal::Action::Reboot, h.tick(AllDown(again + 121 * kMin)).action); +} + TEST(MQTTSelfHealPolicy, HealthyPeriodClearsBootCounterOnce) { Harness h; h.consecutive = 2; @@ -470,6 +610,12 @@ TEST(MQTTSelfHealPolicy, AlertTextNamesCauseAndOutcome) { r.reboot_suppressed = true; Heal::formatAlert(r, cfg, buf, sizeof(buf)); EXPECT_STREQ("MQTT self-heal: stop unproven for 10m, reboot limit reached", buf); + + r.cause = Heal::Cause::AllSlotsDown; + r.held_ms = UINT32_MAX; + char suppressed[80]; + EXPECT_LT(Heal::formatAlert(r, cfg, suppressed, sizeof(suppressed)), (int)sizeof(suppressed)); + EXPECT_STREQ("MQTT self-heal: all slots down for 71582m, reboot limit reached", suppressed); } } // namespace diff --git a/test/test_mqtt_self_heal_policy/test_observer_self_heal.cpp b/test/test_mqtt_self_heal_policy/test_observer_self_heal.cpp new file mode 100644 index 00000000..955e0f58 --- /dev/null +++ b/test/test_mqtt_self_heal_policy/test_observer_self_heal.cpp @@ -0,0 +1,147 @@ +#include + +#include +#include + +#include "helpers/bridges/ObserverSelfHeal.h" + +namespace { + +namespace Heal = MQTTSelfHealPolicy; +const uint32_t kMin = 60000UL; + +struct FakePlatform { + static const bool kSupported = true; + static uint32_t now; + static uint32_t jitter; + static uint32_t random_bound; + static unsigned random_calls; + static Heal::RtcRecord rtc; + static uint32_t nowMs() { return now; } + static uint32_t randomBelow(uint32_t bound) { + random_calls++; + random_bound = bound; + return jitter; + } + static Heal::RtcRecord rtcLoad() { return rtc; } + static void rtcStore(const Heal::RtcRecord& rec) { rtc = rec; } +}; + +uint32_t FakePlatform::now = 0; +uint32_t FakePlatform::jitter = 0; +uint32_t FakePlatform::random_bound = 0; +unsigned FakePlatform::random_calls = 0; +Heal::RtcRecord FakePlatform::rtc{}; + +struct FakeLog { + static void line(const char*, const char* = nullptr) {} + static void flush() {} +}; + +struct FakeHost { + struct ObserverPrefs { + bool mqtt_selfheal_enabled = true; + bool alert_enabled = true; + } obs; + struct NodePrefs { bool bridge_enabled = true; } node; + std::vector alerts; + unsigned reboots = 0; + ObserverPrefs* getObserverPrefs() { return &obs; } + NodePrefs* getNodePrefs() { return &node; } + bool sendAlertText(const char* msg) { alerts.emplace_back(msg); return true; } + void rebootForSelfHeal() { reboots++; } + bool isOtaInProgress() { return false; } +}; + +struct FakeBridge { + bool connected = false; + bool isRunning() const { return true; } + bool isStopUnproven() const { return false; } + bool anyClientQuarantined() const { return false; } + bool isNetworkUp() const { return true; } + uint32_t ntpEvidenceAgeMs() const { return 0; } + uint8_t candidateSlotCount() const { return 1; } + uint8_t connectedSlotCount() const { return connected ? 1 : 0; } +}; + +class ObserverSelfHealJitterTest : public ::testing::Test { +protected: + void SetUp() override { + FakePlatform::now = 0; + FakePlatform::jitter = 0; + FakePlatform::random_bound = 0; + FakePlatform::random_calls = 0; + FakePlatform::rtc = Heal::rtcCleared(); + } + void pollAt(uint32_t now) { + FakePlatform::now = now; + driver.poll(host, &bridge); + } + ObserverSelfHeal driver; + FakeHost host; + FakeBridge bridge; +}; + +TEST_F(ObserverSelfHealJitterTest, JitterIsChosenAtFirstPollAndAddedToTheHold) { + EXPECT_EQ(0u, FakePlatform::random_calls); + FakePlatform::jitter = MQTT_SELFHEAL_ALL_DOWN_JITTER_MS; + pollAt(0); + EXPECT_EQ(1u, FakePlatform::random_calls); + EXPECT_EQ(MQTT_SELFHEAL_ALL_DOWN_JITTER_MS + 1, FakePlatform::random_bound); + const uint32_t start = 40 * kMin; + pollAt(start); + pollAt(start + 120 * kMin); + EXPECT_TRUE(host.alerts.empty()); + const uint32_t hold = 120 * kMin + MQTT_SELFHEAL_ALL_DOWN_JITTER_MS; + pollAt(start + hold - 10000); + EXPECT_TRUE(host.alerts.empty()); + pollAt(start + hold); + ASSERT_EQ(1u, host.alerts.size()); + EXPECT_EQ("MQTT self-heal: all slots down for 150m, rebooting in 1m", host.alerts[0]); + pollAt(start + hold + kMin); + EXPECT_EQ(1u, host.reboots); + EXPECT_EQ(1, Heal::rtcConsecutive(FakePlatform::rtc)); + EXPECT_EQ(1u, FakePlatform::random_calls); +} + +TEST_F(ObserverSelfHealJitterTest, ZeroJitterKeepsTheBaseHold) { + const uint32_t start = 40 * kMin; + pollAt(start); + pollAt(start + 120 * kMin - 10000); + EXPECT_TRUE(host.alerts.empty()); + pollAt(start + 120 * kMin); + ASSERT_EQ(1u, host.alerts.size()); + EXPECT_EQ("MQTT self-heal: all slots down for 120m, rebooting in 1m", host.alerts[0]); +} + +TEST_F(ObserverSelfHealJitterTest, SuppressedOutageUsesJitterAndNeverReboots) { + FakePlatform::jitter = 15 * kMin; + FakePlatform::rtc = Heal::rtcMake(1, Heal::Cause::StopUnproven); + const uint32_t start = 40 * kMin; + pollAt(start); + pollAt(start + 240 * kMin); + EXPECT_TRUE(host.alerts.empty()); + pollAt(start + 270 * kMin); + ASSERT_EQ(1u, host.alerts.size()); + EXPECT_EQ("MQTT self-heal: all slots down for 270m, reboot limit reached", host.alerts[0]); + for (uint32_t t = 271 * kMin; t < 7UL * 24 * 60 * kMin; t += kMin) pollAt(start + t); + EXPECT_EQ(1u, host.alerts.size()); + EXPECT_EQ(0u, host.reboots); + EXPECT_EQ(1, Heal::rtcConsecutive(FakePlatform::rtc)); +} + +TEST_F(ObserverSelfHealJitterTest, BridgeRestartKeepsTheChosenJitter) { + FakePlatform::jitter = 15 * kMin; + pollAt(40 * kMin); + driver.onBridgeRestart(); + FakePlatform::jitter = 0; + const uint32_t start = 50 * kMin; + pollAt(start); + pollAt(start + 120 * kMin); + EXPECT_TRUE(host.alerts.empty()); + pollAt(start + 135 * kMin); + ASSERT_EQ(1u, host.alerts.size()); + EXPECT_EQ(1u, FakePlatform::random_calls); +} + +} // namespace diff --git a/test/test_observer_bridge_control/test_observer_bridge_control.cpp b/test/test_observer_bridge_control/test_observer_bridge_control.cpp index 48841636..f2db64b8 100644 --- a/test/test_observer_bridge_control/test_observer_bridge_control.cpp +++ b/test/test_observer_bridge_control/test_observer_bridge_control.cpp @@ -26,6 +26,7 @@ struct FakeHealPlatform { static uint32_t now; static MQTTSelfHealPolicy::RtcRecord rtc; static uint32_t nowMs() { return now; } + static uint32_t randomBelow(uint32_t) { return 0; } static MQTTSelfHealPolicy::RtcRecord rtcLoad() { return rtc; } static void rtcStore(const MQTTSelfHealPolicy::RtcRecord& rec) { rtc = rec;