|
12 | 12 | get_random_string, |
13 | 13 | maybe_await, |
14 | 14 | maybe_sleep, |
| 15 | + poll_until_condition, |
15 | 16 | ) |
16 | 17 | from apify_client._models import ( |
17 | 18 | BatchAddResult, |
@@ -560,11 +561,18 @@ async def test_request_queue_unlock_requests(client: ApifyClient | ApifyClientAs |
560 | 561 | assert isinstance(result, LockedRequestQueueHead) |
561 | 562 | lock_response = result |
562 | 563 | assert len(lock_response.items) == 3 |
563 | | - |
564 | | - # Wait briefly for eventual consistency: the lock-head response is acknowledged before all |
565 | | - # three lock writes have propagated to the replica that `unlock_requests` reads, so unlocking |
566 | | - # immediately can see fewer locks than were just acquired. |
567 | | - await maybe_sleep(3, is_async=is_async) |
| 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) |
568 | 576 |
|
569 | 577 | # Unlock all requests |
570 | 578 | unlock_response = await maybe_await(rq_client.unlock_requests()) |
|
0 commit comments