Skip to content

Commit bf84763

Browse files
committed
chore(test): poll for alert store expiry instead of fixed sleeps
test_monitorclient_remove_old_alerts read the alert store once at a fixed offset after starting mender-monitor and asserted an exact key count. mender-monitor purges expired records on a periodic loop with variable restart latency, so the single timed read raced the purge cycle. The faster mender_device.run() from the SSH backoff change (no longer sleeping 1s before every healthy command) shifted the read ~2s earlier and collapsed the timing margin, surfacing the latent flakiness (seen as '4' == '2' and '1' == '0'). Poll until the store drains to the expected count instead. It drains monotonically (4 -> 2 -> 0), so 1s polling reliably observes each plateau regardless of purge-loop latency. Signed-off-by: pasinskim <marcin.pasinski@northern.tech>
1 parent 11cde1b commit bf84763

1 file changed

Lines changed: 33 additions & 22 deletions

File tree

tests/tests/test_monitor_client.py

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1593,30 +1593,41 @@ def test_monitorclient_remove_old_alerts(self, monitor_commercial_setup_no_clien
15931593
)
15941594
mender_device.run("systemctl start mender-monitor")
15951595

1596-
# T8: mender-monitor started
1597-
time.sleep(alert_resend_interval_s)
1598-
time.sleep(alert_resend_interval_s)
1599-
1600-
# Shift by 1s to avoid race condition when checking
1601-
time.sleep(1)
1596+
def count_alert_keys():
1597+
output = mender_device.run(
1598+
"bash -c 'cd /usr/share/mender-monitor && . lib/fixlenstore-lib.sh;"
1599+
+ "keys_nolock | wc -l;'"
1600+
)
1601+
return int(output.strip())
1602+
1603+
def wait_for_alert_count(expected, max_wait_s):
1604+
# mender-monitor purges expired records on a periodic loop
1605+
# (DEFAULT_ALERT_STORE_RESEND_INTERVAL_S) whose restart latency
1606+
# varies, so poll for the expected count instead of reading once at a
1607+
# fixed offset, which races the purge cycle. The store drains
1608+
# monotonically (4 -> 2 -> 0), so 1s polling reliably observes each
1609+
# plateau. (QA-1527)
1610+
deadline = time.monotonic() + max_wait_s
1611+
count = None
1612+
while time.monotonic() < deadline:
1613+
count = count_alert_keys()
1614+
logger.info(
1615+
"test_monitorclient_remove_old_alerts: %d keys in store" % count
1616+
)
1617+
if count == expected:
1618+
return
1619+
time.sleep(1)
1620+
assert count == expected, "expected %d alert keys in store, got %s" % (
1621+
expected,
1622+
count,
1623+
)
16021624

1603-
# T16+1: key1, key2 expired
1604-
output = mender_device.run(
1605-
"bash -c 'cd /usr/share/mender-monitor && . lib/fixlenstore-lib.sh;"
1606-
+ "keys_nolock | wc -l;'"
1607-
)
1608-
logger.info("test_monitorclient_remove_old_alerts got %s keys" % output)
1609-
assert output == "2\n"
1625+
# key1, key2 expire at alert_max_age, ~8s before key3, key4 (inserted 8s
1626+
# later), leaving a wide window where exactly 2 keys remain.
1627+
wait_for_alert_count(2, max_wait_s=2 * alert_max_age)
16101628

1611-
time.sleep(alert_resend_interval_s)
1612-
time.sleep(alert_resend_interval_s)
1613-
# T24+1: key3, key4 expired
1614-
output = mender_device.run(
1615-
"bash -c 'cd /usr/share/mender-monitor && . lib/fixlenstore-lib.sh;"
1616-
+ "keys_nolock | wc -l;'"
1617-
)
1618-
logger.info("test_monitorclient_remove_old_alerts got %s keys" % output)
1619-
assert output == "0\n"
1629+
# key3, key4 expire ~8s after key1, key2, draining the store completely.
1630+
wait_for_alert_count(0, max_wait_s=2 * alert_max_age)
16201631

16211632
mender_device.run(
16221633
"mv /usr/share/mender-monitor/config/config.sh.backup /usr/share/mender-monitor/config/config.sh"

0 commit comments

Comments
 (0)