Commit da89431
authored
test: fix flaky test_event_listener_can_be_removed_successfully (#1013)
`test_event_listener_can_be_removed_successfully` is flaky on CI
([example
run](https://github.com/apify/apify-sdk-python/actions/runs/28158933735/job/83394293634)).
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.1 parent 18009a1 commit da89431
1 file changed
Lines changed: 41 additions & 14 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
75 | 75 | | |
76 | 76 | | |
77 | 77 | | |
78 | | - | |
79 | 78 | | |
80 | 79 | | |
81 | 80 | | |
82 | 81 | | |
83 | 82 | | |
84 | 83 | | |
85 | 84 | | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
92 | 110 | | |
93 | 111 | | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
101 | 128 | | |
102 | 129 | | |
103 | 130 | | |
| |||
0 commit comments