Skip to content

Commit 153e7fd

Browse files
fix(test): resolve flaky test_midnight_heartbeats boundary condition (#157)
* fix(test): resolve flaky test_midnight_heartbeats boundary condition 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) * style: fix Black formatting in test_midnight_heartbeats
1 parent 4089343 commit 153e7fd

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

tests/test_client.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,11 @@ def test_midnight(aw_client, bucket):
253253

254254
def test_midnight_heartbeats(aw_client, bucket):
255255
now = datetime.now(tz=timezone.utc) - timedelta(days=1)
256-
midnight = now.replace(hour=23, minute=50)
256+
# Zero out seconds/microseconds for deterministic midnight boundary.
257+
# Without this, residual microseconds cause non-deterministic boundary
258+
# matching when querying events around midnight (the endtime >= start_query
259+
# condition can include/exclude boundary events depending on microseconds).
260+
midnight = now.replace(hour=23, minute=50, second=0, microsecond=0)
257261
events = _create_periodic_events(20, start=midnight, delta=timedelta(minutes=1))
258262

259263
label_ring = ["1", "1", "2", "3", "4"]
@@ -264,9 +268,11 @@ def test_midnight_heartbeats(aw_client, bucket):
264268
recv_events_merged = aw_client.get_events(bucket, limit=-1)
265269
assert len(recv_events_merged) == 4 / 5 * len(events)
266270

267-
recv_events_after_midnight = aw_client.get_events(
268-
bucket, start=midnight + timedelta(minutes=10)
269-
)
271+
# Query from midnight (00:00), which is exactly midnight + 10 minutes from
272+
# the 23:50 start. Use a small offset to avoid boundary ambiguity where
273+
# events ending exactly at midnight could be included by the >= condition.
274+
query_start = midnight + timedelta(minutes=10, milliseconds=1)
275+
recv_events_after_midnight = aw_client.get_events(bucket, start=query_start)
270276
pprint(recv_events_after_midnight)
271277
assert len(recv_events_after_midnight) == int(len(recv_events_merged) / 2)
272278

0 commit comments

Comments
 (0)