test: fix flaky test_event_listener_can_be_removed_successfully#1013
Merged
Conversation
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.
Codecov Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…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.
Pijukatel
approved these changes
Jun 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
test_event_listener_can_be_removed_successfullyis flaky on CI (example run). The deployed Actor assertscounter == last_countafterActor.off(), but the count occasionally moved on, so the run reportsFAILEDand 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 aPERSIST_STATEevent 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 thoughoff()behaves correctly.The fix drops the fixed-duration
asyncio.sleepcalls the test relied on. A second reference listener stays subscribed the whole time and acts as a heartbeat, so every step waits until N morePERSIST_STATEevents have actually been observed rather than for a guessed wall-clock duration. Afteroff()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 ifoff()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.