Skip to content

Commit f739c77

Browse files
authored
test: Deflake webhook dispatches iterate test by polling for the dispatch (#1047)
`test_webhook_dispatches_iterate` drained `dispatches().iterate()` once, right after `webhook_client.test()`. The dispatch is recorded asynchronously, so under load the listing can still be empty (`total=0`) and the test fails with `assert 0 >= 1` ([failed run](https://github.com/apify/apify-client-python/actions/runs/33614701601/job/100197733391)). The sibling `test_webhook_dispatches` failed the same way twice before and got a poll in #1004; the iterate variant did not. The test now keeps the dispatch returned by `test()` and drains the iterator via `collect_iterate_until_present` until that ID shows up, then asserts membership, which is stricter than the previous "at least one dispatch" check. *✍️ Drafted by Claude Code*
1 parent 6d2d878 commit f739c77

1 file changed

Lines changed: 11 additions & 19 deletions

File tree

‎tests/integration/test_webhook.py‎

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from __future__ import annotations
44

5-
from collections.abc import AsyncIterator, Iterator
65
from typing import TYPE_CHECKING
76

87
if TYPE_CHECKING:
@@ -259,24 +258,17 @@ async def test_webhook_dispatches_iterate(client: ApifyClient | ApifyClientAsync
259258
webhook_client = client.webhook(created_webhook.id)
260259

261260
try:
262-
# Generate at least one dispatch by hitting the test endpoint
263-
await maybe_await(webhook_client.test())
261+
# Generate a dispatch by hitting the test endpoint
262+
dispatch = await maybe_await(webhook_client.test())
263+
assert isinstance(dispatch, WebhookDispatch)
264264

265-
iterator = webhook_client.dispatches().iterate(limit=10)
266-
collected: list[WebhookDispatch] = []
267-
if is_async:
268-
assert isinstance(iterator, AsyncIterator)
269-
async for d in iterator:
270-
assert isinstance(d, WebhookDispatch)
271-
collected.append(d)
272-
else:
273-
assert isinstance(iterator, Iterator)
274-
for d in iterator:
275-
assert isinstance(d, WebhookDispatch)
276-
collected.append(d)
277-
278-
assert len(collected) >= 1
279-
for dispatch in collected:
280-
assert dispatch.id is not None
265+
# Poll until the dispatch appears in the listing - it is recorded asynchronously (eventual consistency)
266+
collected = await collect_iterate_until_present(
267+
lambda: webhook_client.dispatches().iterate(limit=10),
268+
{dispatch.id},
269+
item_type=WebhookDispatch,
270+
is_async=is_async,
271+
)
272+
assert dispatch.id in {d.id for d in collected}
281273
finally:
282274
await maybe_await(webhook_client.delete())

0 commit comments

Comments
 (0)