Skip to content

Commit f44557c

Browse files
authored
test: poll for task runs to fix flaky integration tests (#1123)
`test_task_runs` failed on master ([run 38285189656](https://github.com/apify/apify-client-python/actions/runs/38285189656/job/114931753660)) with `assert 0 >= 1`: the task's run list was empty right after `call()` returned. The run listing is eventually consistent, so a read straight after the run finishes can miss it. The JS client hit the same flake in apify/apify-client-js#1136. Both `test_task_runs` and `test_task_last_run` now poll with `poll_until_condition` until the API returns the run that `call()` started. `test_task_runs` also checks for that run's id instead of just a non-empty page. *✍️ Drafted by Claude Code*
1 parent 20ace93 commit f44557c

1 file changed

Lines changed: 13 additions & 8 deletions

File tree

‎tests/integration/test_task.py‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -284,12 +284,14 @@ async def test_task_runs(client: ApifyClient | ApifyClientAsync) -> None:
284284
run = await maybe_await(task_client.call())
285285
assert isinstance(run, Run)
286286

287-
# List runs for this task
288-
runs_client = task_client.runs()
289-
runs_page = await maybe_await(runs_client.list(limit=10))
290-
assert isinstance(runs_page, ListOfRuns)
291-
assert runs_page.items is not None
292-
assert len(runs_page.items) >= 1
287+
# Poll until the run appears in the task's run listing (eventual consistency)
288+
async def list_runs() -> ListOfRuns:
289+
page = await maybe_await(task_client.runs().list(limit=10))
290+
assert isinstance(page, ListOfRuns)
291+
return page
292+
293+
runs_page = await poll_until_condition(list_runs, lambda page: run.id in {r.id for r in page.items})
294+
assert run.id in {r.id for r in runs_page.items}
293295

294296
# Cleanup run
295297
await maybe_await(client.run(run.id).delete())
@@ -322,9 +324,12 @@ async def test_task_last_run(client: ApifyClient | ApifyClientAsync) -> None:
322324
run = await maybe_await(task_client.call())
323325
assert isinstance(run, Run)
324326

325-
# Get last run client
327+
# Poll until the last run resolves to this run (eventual consistency)
326328
last_run_client = task_client.last_run()
327-
last_run = await maybe_await(last_run_client.get())
329+
last_run = await poll_until_condition(
330+
lambda: maybe_await(last_run_client.get()),
331+
lambda r: isinstance(r, Run) and r.id == run.id,
332+
)
328333
assert isinstance(last_run, Run)
329334
assert last_run.id == run.id
330335

0 commit comments

Comments
 (0)