Skip to content

test: fix flaky test_event_listener_can_be_removed_successfully#1013

Merged
vdusek merged 2 commits into
masterfrom
test/fix-flaky-off-event-race
Jun 26, 2026
Merged

test: fix flaky test_event_listener_can_be_removed_successfully#1013
vdusek merged 2 commits into
masterfrom
test/fix-flaky-off-event-race

Conversation

@vdusek

@vdusek vdusek commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

test_event_listener_can_be_removed_successfully is flaky on CI (example run). The deployed Actor asserts counter == last_count after Actor.off(), but the count occasionally moved on, so the run reports FAILED and the test fails.

The cause is a timing race, not a bug in Actor.off(). EventManager.emit() dispatches each listener through an asyncio task, so there's latency between a PERSIST_STATE event being emitted and the counter being incremented. off() only stops future dispatch. It can't cancel an invocation already in flight from an event emitted just before it. Under load that increment lands after the snapshot, so the equality check fails even though off() behaves correctly.

The fix drops the fixed-duration asyncio.sleep calls the test relied on. A second reference listener stays subscribed the whole time and acts as a heartbeat, so every step waits until N more PERSIST_STATE events have actually been observed rather than for a guessed wall-clock duration. After off() the test waits one event cycle (which flushes any invocation dispatched just before it), snapshots the removed listener's count, then waits several more event cycles and asserts the count did not move. That still fails if off() ever genuinely breaks. Both listeners are async so they run on the event loop and can drive the heartbeat safely.

Verified locally against the real EventManager (no platform needed): the previous fixed-sleep version fails 40/40 under load, while this event-driven version holds 0 failures across every condition tried, including the exact stress that broke the old version, listeners slower than the emit interval, and 150-run batches.

Snapshot the event counter after Actor.off() and a drain, so an in-flight PERSIST_STATE listener invocation emitted just before off() can no longer land after the snapshot and trip the equality assertion.
@vdusek vdusek added adhoc Ad-hoc unplanned task added during the sprint. t-tooling Issues with this label are in the ownership of the tooling team. labels Jun 25, 2026
@vdusek vdusek self-assigned this Jun 25, 2026
@codecov

codecov Bot commented Jun 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.72%. Comparing base (078ab87) to head (640e594).
⚠️ Report is 4 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1013      +/-   ##
==========================================
+ Coverage   91.69%   91.72%   +0.03%     
==========================================
  Files          50       50              
  Lines        3203     3203              
==========================================
+ Hits         2937     2938       +1     
+ Misses        266      265       -1     
Flag Coverage Δ
e2e 35.56% <ø> (ø)
integration 57.25% <ø> (+0.03%) ⬆️
unit 83.14% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ 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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

…ed sleeps

Wait until a reference listener has observed N more PERSIST_STATE events rather than sleeping fixed durations, so the test adapts to a slow/loaded runner and no longer races with in-flight listener dispatch.
@vdusek vdusek requested a review from Pijukatel June 26, 2026 08:02
@vdusek vdusek marked this pull request as ready for review June 26, 2026 08:02
@vdusek vdusek merged commit da89431 into master Jun 26, 2026
31 checks passed
@vdusek vdusek deleted the test/fix-flaky-off-event-race branch June 26, 2026 09:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

adhoc Ad-hoc unplanned task added during the sprint. t-tooling Issues with this label are in the ownership of the tooling team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants