@@ -651,6 +651,44 @@ async def test_empty_rq_behavior(request_queue_apify: RequestQueue, rq_poll_time
651651 assert metadata .pending_request_count == 0 , f'metadata.pending_request_count={ metadata .pending_request_count } '
652652
653653
654+ async def test_is_empty_and_is_finished (request_queue_apify : RequestQueue , rq_poll_timeout : int ) -> None :
655+ """Test `is_empty` and `is_finished` across the queue lifecycle."""
656+
657+ rq = request_queue_apify
658+ Actor .log .info ('Request queue opened' )
659+
660+ # Initially the queue is empty and finished.
661+ is_empty = await poll_until_condition (rq .is_empty , timeout = rq_poll_timeout , backoff_factor = 2 )
662+ is_finished = await poll_until_condition (rq .is_finished , timeout = rq_poll_timeout , backoff_factor = 2 )
663+ assert is_empty is True , f'is_empty={ is_empty } '
664+ assert is_finished is True , f'is_finished={ is_finished } '
665+
666+ # After adding a request it is neither empty nor finished.
667+ await rq .add_request ('https://example.com' )
668+ is_empty = await poll_until_condition (rq .is_empty , condition = lambda e : e is False , timeout = rq_poll_timeout )
669+ is_finished = await poll_until_condition (rq .is_finished , condition = lambda f : f is False , timeout = rq_poll_timeout )
670+ assert is_empty is False , f'is_empty={ is_empty } '
671+ assert is_finished is False , f'is_finished={ is_finished } '
672+
673+ # Fetch the request without handling it.
674+ request = await poll_until_condition (rq .fetch_next_request , timeout = rq_poll_timeout , backoff_factor = 2 )
675+ assert request is not None , f'request={ request } '
676+
677+ # The queue is empty, because there is no request available for fetching.
678+ is_empty = await poll_until_condition (rq .is_empty , timeout = rq_poll_timeout , backoff_factor = 2 )
679+ assert is_empty is True , f'is_empty={ is_empty } '
680+ # The queue is not finished, because there is a request being processed.
681+ is_finished = await poll_until_condition (rq .is_finished , condition = lambda f : f is False , timeout = rq_poll_timeout )
682+ assert is_finished is False , f'is_finished={ is_finished } '
683+
684+ # After marking the request as handled the queue is empty and finished again.
685+ await rq .mark_request_as_handled (request )
686+ is_empty = await poll_until_condition (rq .is_empty , timeout = rq_poll_timeout , backoff_factor = 2 )
687+ is_finished = await poll_until_condition (rq .is_finished , timeout = rq_poll_timeout , backoff_factor = 2 )
688+ assert is_empty is True , f'is_empty={ is_empty } '
689+ assert is_finished is True , f'is_finished={ is_finished } '
690+
691+
654692async def test_large_batch_operations (
655693 request_queue_apify : RequestQueue ,
656694 rq_poll_timeout : int ,
0 commit comments