Skip to content

Commit e23e54d

Browse files
committed
test: fix flaky test_request_queue_unlock_requests
Lock writes propagate asynchronously after `list_and_lock_head` returns, so unlocking immediately could see fewer locks than acquired; poll `list_head` until the locked IDs disappear from the queue head before unlocking.
1 parent cbf3de4 commit e23e54d

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

‎tests/integration/test_request_queue.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
get_random_string,
1313
maybe_await,
1414
maybe_sleep,
15+
poll_until_condition,
1516
)
1617
from apify_client._models import (
1718
BatchAddResult,
@@ -560,6 +561,17 @@ async def test_request_queue_unlock_requests(client: ApifyClient | ApifyClientAs
560561
assert isinstance(result, LockedRequestQueueHead)
561562
lock_response = result
562563
assert len(lock_response.items) == 3
564+
locked_ids = {item.id for item in lock_response.items}
565+
566+
# Locks are acknowledged before they are visible to subsequent reads, so unlocking immediately can
567+
# see fewer locks than were just acquired. Since locked requests are excluded from the queue head,
568+
# poll `list_head` until the locked IDs disappear from it (best-effort mitigation of the race).
569+
async def all_locks_visible() -> bool:
570+
head = await maybe_await(rq_client.list_head(limit=5))
571+
assert isinstance(head, RequestQueueHead)
572+
return locked_ids.isdisjoint(item.id for item in head.items)
573+
574+
await poll_until_condition(all_locks_visible, timeout=30, poll_interval=1)
563575

564576
# Unlock all requests
565577
unlock_response = await maybe_await(rq_client.unlock_requests())

0 commit comments

Comments
 (0)