test_tracks.py: history fixture date-math fails CI shortly after UTC midnight #5

Open
opened 2026-11-21 15:41:48 +00:00 by matt · 0 comments
Owner

CI runs that start in roughly the first 7 hours after UTC midnight deterministically fail test_detail_counts_are_daily_and_scoped_to_the_track in tests/test_tracks.py with assert 0 == 3.

Root cause

NOW = datetime.now(UTC) is captured once at module import time (near the start of the pytest session). The history fixture seeds "today" plays using fractional days_ago offsets:

add(user, "Radiohead", "Reckoner", "In Rainbows", duration=290, mbid="abc", days_ago=0.1)
add(user, "radiohead", "reckoner", "in rainbows", duration=290, mbid="abc", days_ago=0.2)
add(user, "Radiohead", "Reckoner", None, duration=290, mbid=None, days_ago=0.3)

days_ago=0.1..0.3 is 2.4-7.2 hours before NOW. If NOW falls within the first ~7.2 hours after UTC midnight, those timestamps land on the previous UTC calendar day. The app buckets play counts by calendar day, so all 3 seeded plays end up in yesterday's bucket instead of today's. The assertion counts[-1]["count"] == 3 (today's bucket) then sees 0, while the total across all 91 buckets still correctly sums to 3 (that assertion passes).

Evidence

Reproduced on two separate CI runs today (run 1240, attempts 1 and 2), both of which started between 00:30 and 00:41 UTC on 2026-11-21 - inside the failure window. Same assertion, same line (tests/test_tracks.py:105), both times - not flaky/random.

Suggested fix

Seed the "today" plays with a fixed time-of-day anchor (e.g. compute days_ago from NOW.replace(hour=12, minute=0, second=0, microsecond=0) or similar) instead of pure fractional offsets from datetime.now(UTC), so the fixture is independent of wall-clock time at collection.

CI runs that start in roughly the first 7 hours after UTC midnight deterministically fail `test_detail_counts_are_daily_and_scoped_to_the_track` in `tests/test_tracks.py` with `assert 0 == 3`. **Root cause** `NOW = datetime.now(UTC)` is captured once at module import time (near the start of the pytest session). The `history` fixture seeds "today" plays using fractional `days_ago` offsets: ```python add(user, "Radiohead", "Reckoner", "In Rainbows", duration=290, mbid="abc", days_ago=0.1) add(user, "radiohead", "reckoner", "in rainbows", duration=290, mbid="abc", days_ago=0.2) add(user, "Radiohead", "Reckoner", None, duration=290, mbid=None, days_ago=0.3) ``` `days_ago=0.1..0.3` is 2.4-7.2 hours before `NOW`. If `NOW` falls within the first ~7.2 hours after UTC midnight, those timestamps land on the *previous* UTC calendar day. The app buckets play counts by calendar day, so all 3 seeded plays end up in yesterday's bucket instead of today's. The assertion `counts[-1]["count"] == 3` (today's bucket) then sees 0, while the total across all 91 buckets still correctly sums to 3 (that assertion passes). **Evidence** Reproduced on two separate CI runs today (run 1240, attempts 1 and 2), both of which started between 00:30 and 00:41 UTC on 2026-11-21 - inside the failure window. Same assertion, same line (`tests/test_tracks.py:105`), both times - not flaky/random. **Suggested fix** Seed the "today" plays with a fixed time-of-day anchor (e.g. compute `days_ago` from `NOW.replace(hour=12, minute=0, second=0, microsecond=0)` or similar) instead of pure fractional offsets from `datetime.now(UTC)`, so the fixture is independent of wall-clock time at collection.
Sign in to join this conversation.
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: matt/Scrobbler#5