Skip to content

Freeze the mDNS clock in the cache TTL tests - #3014

Merged
J. Nick Koston (bdraco) merged 2 commits into
mainfrom
fix-flaky-mdns-ttl-test
Nov 5, 2026
Merged

J. Nick Koston (bdraco) merged 2 commits into
mainfrom
fix-flaky-mdns-ttl-test

Conversation

@bdraco

Copy link
Copy Markdown
Member

What does this implement/fix?

Make the mDNS cache TTL tests deterministic by freezing the clock the helper reads, so a slow runner can no longer push the remaining TTL outside the old 0.5 s tolerance (it failed esphome/esphome#22288's downstream job with 39.437 == 40.0 ± 0.5).

# before: records built from one clock read, the helper reads the clock again later
created=current_time_millis() - 80_000
assert ttl_remaining == pytest.approx(40.0, abs=0.5)

# after: one captured `now` for both sides, exact assertion
with patch(_MDNS_CLOCK, return_value=now):
    ttl_remaining = monitor.mdns.get_mdns_a_record_ttl_remaining("kitchen")
assert ttl_remaining == 40.0
  • Effect: the three wall-clock assertions in tests/test_state_monitor_reachability.py (get_mdns_a_record_ttl_remaining min-across-A/AAAA, and the two get_mdns_cache_info age/TTL tests) become exact.
  • Scope: tests only; no production change.

Related issue or feature (if applicable):

Types of changes

  • Bugfix (non-breaking change which fixes an issue) — bugfix
  • New feature (non-breaking change which adds functionality) — new-feature
  • Enhancement to an existing feature — enhancement
  • Breaking change (fix or feature that would cause existing functionality to not work as expected) — breaking-change
  • Refactor (no behaviour change) — refactor
  • Documentation only — docs
  • Maintenance / chore — maintenance
  • CI / workflow change — ci
  • Dependencies bump — dependencies

Frontend coordination

  • No frontend change needed
  • Companion frontend PR: esphome/device-builder-frontend#

Checklist

  • The code change is tested and works locally.
  • Pre-commit hooks pass (ruff, codespell, yaml/json/python checks).
  • Tests have been added or updated under tests/ where applicable.
  • components.index.json / definitions/components/*.json have not been hand-edited (regenerate via script/sync_components.py if a sync is needed).
  • Architecture-level changes are reflected in docs/ARCHITECTURE.md and/or docs/API.md.

@bdraco

Copy link
Copy Markdown
Member Author

ESPHome Bot (@esphbot) review

@github-actions github-actions Bot added the maintenance Maintenance / chores label Nov 5, 2026
@bdraco
J. Nick Koston (bdraco) marked this pull request as ready for review November 5, 2026 00:51
Copilot AI balanced review requested due to automatic review settings November 5, 2026 00:51

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are recorded for the test-only changes making cache age and remaining-TTL assertions deterministic.

No open issues 🎉

What changed in this PR

This test-only change makes mDNS cache age and remaining-TTL assertions deterministic, preventing elapsed time on slow runners from causing failures. Production behavior is unchanged.

Changes:

  • Capture one timestamp per test and freeze the helper’s clock at that value.
  • Replace tolerance-based assertions with exact checks in three cache tests.
File Description
tests/​test_state_monitor_reachability.py Adds a shared clock patch target and deterministic age/TTL assertions.

Review effort: Balanced

@esphbot

ESPHome Bot (esphbot) commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Previous review — superseded by a newer review below.

@esphbot ESPHome Bot (esphbot) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tip

No blocking issues found — ready to merge.

@codspeed

codspeed Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 28 untouched benchmarks
⏩ 1 skipped benchmark1


Comparing fix-flaky-mdns-ttl-test (60d6a80) with main (e1e2450)

Open in CodSpeed

Footnotes

  1. 1 benchmark was skipped, so the baseline result was used instead. If it was deleted from the codebase, click here and archive it to remove it from the performance reports. ↩

@codecov

codecov Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.76%. Comparing base (d2eac18) to head (60d6a80).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #3014   +/-   ##
=======================================
  Coverage   99.76%   99.76%           
=======================================
  Files         300      300           
  Lines       26388    26388           
=======================================
  Hits        26326    26326           
  Misses         62       62           
Flag Coverage Δ
py3.12 99.73% <ø> (ø)
py3.14 99.61% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@bdraco

Copy link
Copy Markdown
Member Author

Addressed in 60d6a80.

  • Suggestion 1 (fractional now): this was not rare in practice; the first CI run on 94cc6cc hit it on two jobs (39.999999999999986 == 40.0, 89.99999999999999 == 90.0). Every frozen anchor is now float(int(current_time_millis())), so the millisecond adds and subtracts are exact.
  • The two sibling tests you flagged (test_get_mdns_cache_info_picks_latest_record and test_get_mdns_cache_info_countdown_prefers_the_esphomelib_ptr) now freeze the same clock and assert exact values; no 0.5 s tolerance remains in the file.

ESPHome Bot (@esphbot) review

@esphbot

Copy link
Copy Markdown
Contributor

J. Nick Koston (@bdraco): Addressed in 60d6a80.

Checked at 60d6a80. All five anchors use float(int(...)). No pytest.approx left. Both sibling tests now freeze the clock. Both points resolved.

@esphbot

Copy link
Copy Markdown
Contributor

PR Review — Freeze the mDNS clock in the cache TTL tests

Merge-ready. Both points from the last review are fixed in 60d6a80, and I found no new issues.

What works well:

  • Every frozen anchor is now float(int(current_time_millis())) (lines 683, 722, 776, 1225, 1372). All the millisecond adds and subtracts are on whole numbers, so the exact == checks can no longer fail from float rounding. This matches the CI failures J. Nick Koston (@bdraco) reported (39.999999999999986 == 40.0, 89.99999999999999 == 90.0).
  • The two sibling tests flagged last time, test_get_mdns_cache_info_picks_latest_record and test_get_mdns_cache_info_countdown_prefers_the_esphomelib_ptr, now patch the same clock. No pytest.approx or 0.5 s tolerance is left in the file.
  • The patch target is correct. mdns.py imports current_time_millis from zeroconf, and the two helpers read it at lines 219 and 247. Each patch wraps only the helper call, so zeroconf's own cache lookups still use the real clock, which stays close to now.
  • The tests that still call the unpatched clock (lines 845–1346) only check record selection, TXT decoding and freshness, with gaps of 1 s or more. Their outcome doesn't depend on runner speed.

No outstanding issues.


✅ Resolved since last review (1)

Previously-flagged issues verified fixed
  • tests/test_state_monitor_reachability.py:722 Exact float equality depends on now having no fractional bits


Checklist

  • Patch target matches the name the production code looks up
  • Test determinism (integer anchors, exact float arithmetic)
  • No remaining wall-clock tolerances in the TTL and age assertions
  • Resource cleanup (Zeroconf closed in finally)
  • Scope matches the PR description (tests only)

Automated review by Kōan (Claude) HEAD=60d6a80 24s

@esphbot ESPHome Bot (esphbot) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tip

No blocking issues found — ready to merge.

@bdraco
J. Nick Koston (bdraco) merged commit 2b897dc into main Nov 5, 2026
26 checks passed
@bdraco
J. Nick Koston (bdraco) deleted the fix-flaky-mdns-ttl-test branch November 5, 2026 01:03
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 7, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

maintenance Maintenance / chores

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants