Skip to content

Fix the test that fails for one hour a day, every night the journal flush runs - #5013

Merged
springfall2008 merged 1 commit into
mainfrom
fix/test-machine-clock-dates
Sep 9, 2026
Merged

springfall2008 merged 1 commit into
mainfrom
fix/test-machine-clock-dates

Conversation

@springfall2008

Copy link
Copy Markdown
Owner

test_energydataservice fails for one hour a day, and the nightly journal flush is scheduled inside that hour.

The bug

today    = datetime.now().strftime("%Y-%m-%d")                    # ← the machine's clock
tomorrow = (datetime.now() + timedelta(days=1)).strftime(...)     # ← the machine's clock
tz_offset = int(my_predbat.midnight_utc.tzinfo.utcoffset(...))    # ← Predbat's clock

The fixture dates come from the host, the offset from Predbat (Europe/London in the harness). When those disagree on the date, the published rates are a day out from the day the code is modelling and rates[1440] doesn't exist — KeyError: 1440.

On a UTC runner during BST they disagree between 23:00 and 00:00 UTC.

Why it matters now

This isn't intermittent flakiness. The nightly /journal-update flush fires at local midnight, which during BST is 23:0x UTC — so it lands in the failing hour every night. #5011 is a docs-only change (one markdown file) that failed CI at 23:12 UTC for exactly this reason. Until BST ends in late October, every nightly journal PR fails the same way.

The fix

Anchor the fixture to my_predbat.midnight_utc — the same clock the next line already reads. Identical trap and identical fix to #4998 for the multi-car IOG tests last week, and the trap already has a bullet in the debug journal ("Build test dates from Predbat's clock, not the machine's").

Verification

Reproduced with TZ=Pacific/Pitcairn, which puts the host a calendar day behind Europe/London:

before after
--test energydataservice, shifted TZ KeyError: 1440 passes
--test energydataservice, normal TZ passes passes
--quick, shifted TZ aborts all passed, 82.98s

The suite aborts on the first uncaught exception, so this was masking anything later in the run. With it fixed, nothing else in the quick suite fails in that window.

Worth considering separately

Nineteen other test files call datetime.now(). None is currently affected — I checked by running the whole suite in the window — but the pattern is latent, and this is the second instance in a week. A scheduled CI run in a shifted timezone would catch the next one before a maintainer does. I haven't added one here since it's a workflow change with its own cost.

🤖 Generated with Claude Code

…t the host's

test_energydataservice built its rate fixture from datetime.now() - the machine's clock -
while reading the timezone offset two lines later from my_predbat.midnight_utc, which is
local midnight in Predbat's configured timezone. Between 23:00 and 00:00 UTC under BST
those land on different dates, so the published rates are a day out from the day the code
is modelling and rates[1440] does not exist: KeyError: 1440.

This is not intermittent. The nightly journal flush runs at local midnight, which during
BST is 23:0x UTC, so it lands in the failing hour every night - PR #5011 is a docs-only
change that failed CI for exactly this reason, at 23:12 UTC.

Same trap and same fix as PR #4998 for the multi-car IOG tests, and the trap already has
a bullet in the debug journal ("Build test dates from Predbat's clock, not the machine's").

Verified by running the suite with TZ=Pacific/Pitcairn, which puts the host a calendar day
behind Europe/London: energydataservice reproduces the KeyError before the change and
passes after, as does the full quick suite (82.98s, 4 slow tests skipped). It also still
passes on an unshifted clock.

The suite aborts on the first uncaught exception, so this was masking anything later in
the run; with it fixed, nothing else in the quick suite fails in that window. Nineteen
other test files call datetime.now(), none of them currently affected, but the pattern is
latent - a scheduled CI run in a shifted timezone would catch the next one before a
maintainer does.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Copilot AI lite review requested due to automatic review settings September 9, 2026 07:14

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

The change is narrowly scoped to a test fixture, aligns all date/offset inputs to the same clock, and I found no functional or maintainability issues in the updated code.

Pull request overview

This PR fixes a deterministic timezone/date mismatch in the test_energydataservice fixture that caused CI failures for one hour per day during BST, by aligning the fixture’s date source with Predbat’s own configured clock (my_predbat.midnight_utc).

Changes:

  • Build today/tomorrow fixture dates from my_predbat.midnight_utc instead of datetime.now() to avoid host-vs-harness day disagreement.
  • Add an in-test comment explaining the failure window and why anchoring to Predbat’s clock resolves it.
File summaries
File Description
apps/predbat/tests/test_energydataservice.py Anchors test fixture dates to Predbat’s configured local-midnight clock to eliminate the 23:00–00:00 UTC BST failure window.
Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 0
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@springfall2008
springfall2008 merged commit d48cb22 into main Sep 9, 2026
3 checks passed
@springfall2008
springfall2008 deleted the fix/test-machine-clock-dates branch September 9, 2026 07:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants