Skip to content

test(streams): harden take() ack tests against event-loop timing (fix PyPy flake)#709

Closed
wbarnha wants to merge 3 commits into
masterfrom
claude/harden-take-tests
Closed

test(streams): harden take() ack tests against event-loop timing (fix PyPy flake)#709
wbarnha wants to merge 3 commits into
masterfrom
claude/harden-take-tests

Conversation

@wbarnha

@wbarnha wbarnha commented Jul 19, 2026

Copy link
Copy Markdown
Member

Description

Fixes a flaky-test failure seen on the PyPy 3.11 CI leg (e.g. run
29672563646,
surfaced on #690). Four stream.take() tests failed with:

if not event.ack.called:
>   assert event.message.acked
E   assert False

Root cause

stream.take() acks the events it consumes from a background task. The
tests asserted the ack had happened after exactly two await asyncio.sleep(0)
yields:

await asyncio.sleep(0)
await asyncio.sleep(0)
if not event.ack.called:
    assert event.message.acked

On slower interpreters (notably PyPy) that background task hasn't run within
two event-loop ticks, so event.message.acked is still False and the
assertion flakes. The tests' own comments already hinted at this fragility
("need one sleep on 3.6.0–3.6.6; need two on 3.6.7 :-/"). It is not a product
regression — every CPython leg (3.10–3.14) passed, and the run went green on
re-run.

Fix

Introduce a small wait_for_stream_ack() helper that polls for the ack
(up to a 1s timeout) instead of assuming a fixed number of ticks, and use it in
the four affected tests:

  • test_take
  • test_take_wit_timestamp
  • test_take_wit_timestamp_wit_simple_value
  • test_take_wit_timestamp_without_timestamp_field

The existing assertions are left unchanged, so a genuinely missing ack still
fails the test with the same signal once the timeout elapses — this only
removes the timing race, not the coverage.

Verified locally: all four tests pass.

🤖 Generated with Claude Code


Generated by Claude Code

The `test_take` / `test_take_wit_timestamp*` tests asserted that an event
consumed via `stream.take()` had been acked after exactly two
`await asyncio.sleep(0)` yields.  `take()` acks consumed events from a
background task, and on slower interpreters (notably PyPy) that task hasn't
run within two event-loop ticks, so `event.message.acked` was still False and
the assertion flaked -- as seen on the PyPy CI leg.

Replace the fixed sleeps with `wait_for_stream_ack()`, which polls for the ack
(up to a 1s timeout) instead of assuming a fixed number of ticks.  The existing
assertions are unchanged, so a genuinely missing ack still fails the test with
the same signal after the timeout.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HHPL4VFWQRQPpjR1gXSKyL
@codecov

codecov Bot commented Jul 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.15%. Comparing base (ade8a09) to head (03e5fde).
⚠️ Report is 6 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #709   +/-   ##
=======================================
  Coverage   94.15%   94.15%           
=======================================
  Files         104      104           
  Lines       11136    11136           
  Branches     1201     1201           
=======================================
  Hits        10485    10485           
  Misses        550      550           
  Partials      101      101           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

wbarnha and others added 2 commits July 19, 2026 13:00
… PyPy

The polling helper alone still left the take() tests failing on the PyPy CI
leg.  Root cause: breaking out of `async for value in s.take(...)` leaves the
take() async generator suspended, and the `finally:` block that acks the
buffered events only runs when the generator is finalized.  CPython's
refcounting finalizes it immediately, so polling a few event-loop ticks was
enough -- but PyPy has no refcounting, so without an explicit gc.collect()
the finalizer (and therefore the ack) may never run at all, no matter how
long the helper waited.

Nudge the collector on each poll round (the same trick faust's own
Consumer.wait_empty() uses) and yield with a real sleep so the scheduled
aclose() gets to run.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HHPL4VFWQRQPpjR1gXSKyL
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.

1 participant