Skip to content

Commit 79e205b

Browse files
authored
test: Rebalance parallel test scheduling and trim the slowest tests (#1037)
The integration test job grew from ~3.5 min to ~6.3 min when the suite started running across both built-in HTTP transports. Almost none of that was extra work: with 806 tests the suite is only 1432 worker-seconds, which is ~90s on 16 workers. The job took 353s because one worker held the critical path while the other 15 sat idle. xdist's default `load` scheduler seeds every worker with a chunk of `items_per_node // 4` consecutive tests and never reassigns them. With 806 tests that chunk is 12, and one worker drew the `test_build.py` block holding all four variants of `test_build_delete_and_abort` (~87s each), which it then ran back to back: 88s, 170s, 261s, 350s. Every other worker was finished by 170s. The same thing happened before the transport matrix, with 2 variants instead of 4 - so the old 3.5 min was already this one test. Three changes: 1. `--dist worksteal` in `addopts`, so an idle worker takes over tests still queued on a busy one. Measured on the unit suite at identical concurrency: `1558 passed in 40.65s` -> `1558 passed in 15.58s`. `--dist` is inert without `-n`, so single-file runs are unaffected. 2. `test_build_delete_and_abort` starts both Actor builds before awaiting either. `build()` returns as soon as the build is queued, so the platform can run them concurrently instead of the test serializing two ~40s builds. 3. `test_request_queue_unlock_requests` polled `list_head` until the locked IDs disappeared from it, which `list_head` does not reflect - so every variant burned the full 30s deadline and then discarded the poll result. It now polls the outcome it cares about, accumulating `unlocked_count` across `unlock_requests()` calls until all three locks are accounted for, and asserts that total. Simulated against the per-test durations reconstructed from the CI logs: 353s -> 100s with change 1 alone, -> 72s with all three. Change 2 only pays off if the platform builds two versions of the same Actor concurrently. If it serializes them per Actor the test still passes, it just stays at ~90s and the job lands nearer 2 min than 1.5. *✍️ Drafted by Claude Code*
1 parent 4aa6eb9 commit 79e205b

3 files changed

Lines changed: 20 additions & 22 deletions

File tree

‎pyproject.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ known-local-folder = ["apify_client"]
187187
max-branches = 18
188188

189189
[tool.pytest.ini_options]
190-
addopts = "-r a --verbose"
190+
addopts = "-r a --verbose --dist worksteal"
191191
asyncio_default_fixture_loop_scope = "function"
192192
asyncio_mode = "auto"
193193
pythonpath = ["."]

‎tests/integration/test_build.py‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,17 +185,18 @@ async def test_build_delete_and_abort(client: ApifyClient | ApifyClientAsync) ->
185185
actor_client = client.actor(created_actor.id)
186186

187187
try:
188-
# Build both versions - we need 2 builds because we can't delete the default build
188+
# Both versions are built because the default build cannot be deleted. `build` returns as soon as the
189+
# build is queued, so starting both before awaiting either lets the platform run them concurrently.
189190
first_build = await maybe_await(actor_client.build(version_number='0.1'))
190191
assert isinstance(first_build, Build)
191-
first_build_client = client.build(first_build.id)
192-
await maybe_await(first_build_client.wait_for_finish())
193-
194192
second_build = await maybe_await(actor_client.build(version_number='0.2'))
195193
assert isinstance(second_build, Build)
194+
195+
first_build_client = client.build(first_build.id)
196196
second_build_client = client.build(second_build.id)
197197

198-
# Wait for the second build to finish
198+
await maybe_await(first_build_client.wait_for_finish())
199+
199200
finished_build = await maybe_await(second_build_client.wait_for_finish())
200201
assert isinstance(finished_build, Build)
201202
assert finished_build.status in ('SUCCEEDED', 'FAILED')

‎tests/integration/test_request_queue.py‎

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -573,26 +573,23 @@ async def test_request_queue_unlock_requests(client: ApifyClient | ApifyClientAs
573573

574574
await ensure_queue_is_populated(rq_client, expected_count=5)
575575

576-
result = await maybe_await(rq_client.list_and_lock_head(limit=3, lock_duration=timedelta(seconds=60)))
577-
assert isinstance(result, LockedRequestQueueHead)
578-
lock_response = result
576+
lock_response = await maybe_await(rq_client.list_and_lock_head(limit=3, lock_duration=timedelta(seconds=60)))
577+
assert isinstance(lock_response, LockedRequestQueueHead)
579578
assert len(lock_response.items) == 3
580-
locked_ids = {item.id for item in lock_response.items}
581579

582-
# Locks are acknowledged before they are visible to subsequent reads, so unlocking immediately can
583-
# see fewer locks than were just acquired. Since locked requests are excluded from the queue head,
584-
# poll `list_head` until the locked IDs disappear from it (best-effort mitigation of the race).
585-
async def all_locks_visible() -> bool:
586-
head = await maybe_await(rq_client.list_head(limit=5))
587-
assert isinstance(head, RequestQueueHead)
588-
return locked_ids.isdisjoint(item.id for item in head.items)
580+
# Locks are acknowledged before they are all visible to `unlock_requests`, so a single call can report
581+
# fewer locks than were just acquired. Each call unlocks whatever is visible to it, so the counts are
582+
# accumulated until every lock is accounted for.
583+
unlocked_counts: list[int] = []
589584

590-
await poll_until_condition(all_locks_visible)
585+
async def unlocked_so_far() -> int:
586+
unlock_response = await maybe_await(rq_client.unlock_requests())
587+
assert isinstance(unlock_response, UnlockRequestsResult)
588+
unlocked_counts.append(unlock_response.unlocked_count)
589+
return sum(unlocked_counts)
591590

592-
# Unlock all requests
593-
unlock_response = await maybe_await(rq_client.unlock_requests())
594-
assert isinstance(unlock_response, UnlockRequestsResult)
595-
assert unlock_response.unlocked_count == 3
591+
unlocked_total = await poll_until_condition(unlocked_so_far, lambda total: total == 3)
592+
assert unlocked_total == 3
596593
finally:
597594
await maybe_await(rq_client.delete())
598595

0 commit comments

Comments
 (0)