Skip to content

Comments

fix(test): resolve flaky test_midnight_heartbeats boundary condition#157

Merged
ErikBjare merged 2 commits intoActivityWatch:masterfrom
TimeToBuildBob:fix/flaky-midnight-heartbeat-test
Feb 23, 2026
Merged

fix(test): resolve flaky test_midnight_heartbeats boundary condition#157
ErikBjare merged 2 commits intoActivityWatch:masterfrom
TimeToBuildBob:fix/flaky-midnight-heartbeat-test

Conversation

@TimeToBuildBob
Copy link
Contributor

@TimeToBuildBob TimeToBuildBob commented Feb 23, 2026

Summary

Fixes the intermittently failing test_midnight_heartbeats test (seen in ActivityWatch/activitywatch#1211 CI as assert 9 == 8).

Root cause: The test creates events spanning midnight, then queries events "after midnight." Two issues caused non-deterministic results:

  1. midnight retained residual microseconds from datetime.now(), making the exact boundary non-deterministic across runs
  2. Event 9 (at 23:59 with 1-min duration) has endtime exactly at midnight, and the server's endtime >= start_query SQL condition sometimes includes it

Fix:

  • Zero out seconds/microseconds on the start timestamp for deterministic boundaries
  • Add a 1ms offset to the query start to avoid the ambiguous boundary where events ending exactly at midnight could be included by the >= condition

Test plan

  • Existing test_midnight_heartbeats passes consistently (no more flaky failures)
  • Other midnight tests unaffected

Important

Fixes flaky test_midnight_heartbeats by ensuring deterministic timestamp boundaries and adding a 1ms offset to avoid boundary ambiguity.

  • Behavior:
    • Fixes flaky test_midnight_heartbeats in test_client.py by ensuring deterministic timestamp boundaries.
    • Zeroes out seconds/microseconds in midnight to prevent non-deterministic boundary matching.
    • Adds 1ms offset to query_start to avoid including events ending exactly at midnight.
  • Test Plan:
    • Ensures test_midnight_heartbeats passes consistently.
    • Confirms other midnight tests remain unaffected.

This description was created by Ellipsis for 7eeec25. You can customize this summary. It will automatically update as commits are pushed.

The test was intermittently failing with `assert 9 == 8` because:

1. `midnight` retained residual microseconds from `datetime.now()`, making
   the exact midnight boundary non-deterministic across runs
2. Event 9 (starting at 23:59 with 1-min duration) has endtime exactly at
   midnight, and the server's `endtime >= start_query` condition sometimes
   includes it in the "after midnight" query results

Fix: zero out seconds/microseconds on the start timestamp for deterministic
boundaries, and add a 1ms offset to the query start to avoid the ambiguous
boundary where events ending exactly at midnight could be included.

Closes ActivityWatch/activitywatch#1211 (follow-up)
Copy link

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

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

Important

Looks good to me! 👍

Reviewed everything up to 7eeec25 in 11 seconds. Click for details.
  • Reviewed 31 lines of code in 1 files
  • Skipped 0 files when reviewing.
  • Skipped posting 0 draft comments. View those below.
  • Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.

Workflow ID: wflow_23UrQ42ILyXTdwOr

You can customize Ellipsis by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.

@greptile-apps
Copy link

greptile-apps bot commented Feb 23, 2026

Greptile Summary

Fixes intermittent test failures in test_midnight_heartbeats by addressing timing precision issues around midnight boundaries.

The fix applies two changes:

  • Zeros out seconds and microseconds when creating the midnight boundary timestamp to ensure deterministic test behavior across runs
  • Adds a 1ms offset to the query start time to avoid the ambiguous boundary condition where events ending exactly at midnight could be included or excluded based on the >= SQL condition

The root cause was non-deterministic microsecond values from datetime.now() being retained in the midnight calculation, causing the test to sometimes see 8 events and sometimes 9 events after midnight depending on whether event 9 (ending exactly at midnight) was included in the query results.

Confidence Score: 5/5

  • This PR is safe to merge with no risk
  • The changes are minimal, well-documented, and directly address a specific flaky test issue. The fix adds deterministic timestamp handling by zeroing out seconds/microseconds and includes a 1ms offset to avoid boundary ambiguity. This is a standard approach for fixing timing-related test flakiness and does not affect production code.
  • No files require special attention

Important Files Changed

Filename Overview
tests/test_client.py Fixes flaky test by zeroing seconds/microseconds and adding 1ms offset to avoid boundary ambiguity

Last reviewed commit: 7eeec25

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

1 file reviewed, no comments

Edit Code Review Agent Settings | Greptile

@TimeToBuildBob
Copy link
Contributor Author

Fixed Black formatting failure — collapsed the multi-line get_events() call to a single line in 61f046d. CI should be green now.

@ErikBjare ErikBjare merged commit 153e7fd into ActivityWatch:master Feb 23, 2026
6 checks passed
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