Skip to content

Commit ad43eec

Browse files
committed
test: fix flaky test_request_queue_unlock_requests
Wait for the locks to propagate before unlocking, polling list_head via poll_until_condition until the locked requests drop out of the queue head, rather than unlocking immediately after list_and_lock_head returns.
1 parent 903c070 commit ad43eec

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

tests/integration/test_request_queue.py

Lines changed: 13 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,18 @@ 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+
# Wait for the locks to propagate before unlocking (eventual consistency): the lock-head response
567+
# is acknowledged before all three lock writes are visible to subsequent reads, so unlocking
568+
# immediately can see fewer locks than were just acquired. Locked requests are excluded from the
569+
# queue head, so poll `list_head` until none of the locked requests reappear there, rather than
570+
# guessing a fixed sleep.
571+
async def all_locks_visible() -> bool:
572+
head = await maybe_await(rq_client.list_head(limit=5))
573+
return isinstance(head, RequestQueueHead) and locked_ids.isdisjoint(item.id for item in head.items)
574+
575+
await poll_until_condition(all_locks_visible, timeout=30, poll_interval=1)
563576

564577
# Unlock all requests
565578
unlock_response = await maybe_await(rq_client.unlock_requests())

0 commit comments

Comments
 (0)