Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 39 additions & 25 deletions tests/unit/crawlers/_basic/test_basic_crawler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2453,128 +2453,138 @@ async def test_run_while_running() -> None:
async def test_pause_holds_requests_until_resume() -> None:
"""`pause()` waits for the request in progress, and the paused run starts nothing new until `resume()`."""
crawler = BasicCrawler(concurrency_settings=ConcurrencySettings(desired_concurrency=1, max_concurrency=1))
started = asyncio.Event()
release = asyncio.Event()
visited = list[str]()

@crawler.router.default_handler
async def handler(context: BasicCrawlingContext) -> None:
visited.append(context.request.url)
started.set()
await release.wait()

urls = ['https://a.placeholder.com', 'https://b.placeholder.com']
run_task = asyncio.create_task(crawler.run(urls))
assert await poll_until_condition(lambda: len(visited) == 1)
await asyncio.wait_for(started.wait(), timeout=30)

pause_task = asyncio.create_task(crawler.pause())
await asyncio.sleep(0)
assert not pause_task.done()

release.set()
await asyncio.wait_for(pause_task, timeout=10)
await asyncio.wait_for(pause_task, timeout=30)

# Give a pause that doesn't hold the task loop the chance to start the next request.
await asyncio.sleep(0.1)
assert visited == urls[:1]
assert not run_task.done()

crawler.resume()
await asyncio.wait_for(run_task, timeout=10)
await asyncio.wait_for(run_task, timeout=30)

assert visited == urls


async def test_stop_ends_paused_run() -> None:
"""`stop()` ends a paused run without waiting for `resume()` and starts no new request."""
crawler = BasicCrawler(concurrency_settings=ConcurrencySettings(desired_concurrency=1, max_concurrency=1))
started = asyncio.Event()
release = asyncio.Event()
visited = list[str]()

@crawler.router.default_handler
async def handler(context: BasicCrawlingContext) -> None:
visited.append(context.request.url)
started.set()
await release.wait()

urls = ['https://a.placeholder.com', 'https://b.placeholder.com']
run_task = asyncio.create_task(crawler.run(urls))
assert await poll_until_condition(lambda: len(visited) == 1)
await asyncio.wait_for(started.wait(), timeout=30)

pause_task = asyncio.create_task(crawler.pause())
await asyncio.sleep(0)
release.set()
await asyncio.wait_for(pause_task, timeout=10)
await asyncio.wait_for(pause_task, timeout=30)

crawler.stop()
await asyncio.wait_for(run_task, timeout=10)
await asyncio.wait_for(run_task, timeout=30)

assert visited == urls[:1]


async def test_pause_after_stop_ends_run() -> None:
"""`pause()` after `stop()` waits for the request in progress, and the run still ends."""
crawler = BasicCrawler(concurrency_settings=ConcurrencySettings(desired_concurrency=1, max_concurrency=1))
started = asyncio.Event()
release = asyncio.Event()
visited = list[str]()

@crawler.router.default_handler
async def handler(context: BasicCrawlingContext) -> None:
visited.append(context.request.url)
started.set()
await release.wait()

urls = ['https://a.placeholder.com', 'https://b.placeholder.com']
run_task = asyncio.create_task(crawler.run(urls))
assert await poll_until_condition(lambda: len(visited) == 1)
await asyncio.wait_for(started.wait(), timeout=30)

crawler.stop()
pause_task = asyncio.create_task(crawler.pause())
await asyncio.sleep(0)
assert not pause_task.done()

release.set()
await asyncio.wait_for(pause_task, timeout=10)
await asyncio.wait_for(run_task, timeout=10)
await asyncio.wait_for(pause_task, timeout=30)
await asyncio.wait_for(run_task, timeout=30)

assert visited == urls[:1]


async def test_pause_timeout_after_stop_ends_run() -> None:
"""`pause()` after `stop()` that times out still lets the run end once the request in progress finishes."""
crawler = BasicCrawler(concurrency_settings=ConcurrencySettings(desired_concurrency=1, max_concurrency=1))
started = asyncio.Event()
release = asyncio.Event()
visited = list[str]()

@crawler.router.default_handler
async def handler(context: BasicCrawlingContext) -> None:
visited.append(context.request.url)
started.set()
await release.wait()

urls = ['https://a.placeholder.com', 'https://b.placeholder.com']
run_task = asyncio.create_task(crawler.run(urls))
assert await poll_until_condition(lambda: len(visited) == 1)
await asyncio.wait_for(started.wait(), timeout=30)

crawler.stop()
with pytest.raises(TimeoutError):
await crawler.pause(timeout=timedelta(milliseconds=50))

release.set()
await asyncio.wait_for(run_task, timeout=10)
await asyncio.wait_for(run_task, timeout=30)

assert visited == urls[:1]


async def test_migration_after_stop_ends_run() -> None:
"""A migration after `stop()` still lets the run end once the request in progress finishes."""
crawler = BasicCrawler(concurrency_settings=ConcurrencySettings(desired_concurrency=1, max_concurrency=1))
started = asyncio.Event()
release = asyncio.Event()
visited = list[str]()

@crawler.router.default_handler
async def handler(context: BasicCrawlingContext) -> None:
visited.append(context.request.url)
started.set()
await release.wait()

urls = ['https://a.placeholder.com', 'https://b.placeholder.com']
run_task = asyncio.create_task(crawler.run(urls))
assert await poll_until_condition(lambda: len(visited) == 1)
await asyncio.wait_for(started.wait(), timeout=30)

crawler.stop()
event_manager = service_locator.get_event_manager()
Expand All @@ -2583,7 +2593,7 @@ async def handler(context: BasicCrawlingContext) -> None:

release.set()
await event_manager.wait_for_all_listeners_to_complete()
await asyncio.wait_for(run_task, timeout=10)
await asyncio.wait_for(run_task, timeout=30)

assert visited == urls[:1]

Expand All @@ -2594,25 +2604,27 @@ async def test_abort_on_error_ends_paused_run() -> None:
concurrency_settings=ConcurrencySettings(desired_concurrency=1, max_concurrency=1),
abort_on_error=True,
)
started = asyncio.Event()
release = asyncio.Event()
visited = list[str]()

@crawler.router.default_handler
async def handler(context: BasicCrawlingContext) -> None:
visited.append(context.request.url)
started.set()
await release.wait()
raise RuntimeError('Request failed')

urls = ['https://a.placeholder.com', 'https://b.placeholder.com']
run_task = asyncio.create_task(crawler.run(urls))
assert await poll_until_condition(lambda: len(visited) == 1)
await asyncio.wait_for(started.wait(), timeout=30)

# The pause gives up on the request in progress, so the request fails while the crawler is already paused.
with pytest.raises(TimeoutError):
await crawler.pause(timeout=timedelta(milliseconds=50))

release.set()
await asyncio.wait_for(run_task, timeout=10)
await asyncio.wait_for(run_task, timeout=30)

assert visited == urls[:1]

Expand Down Expand Up @@ -2645,15 +2657,15 @@ async def handler(context: BasicCrawlingContext) -> None:

urls = ['https://a.placeholder.com', 'https://b.placeholder.com', 'https://c.placeholder.com']
run_task = asyncio.create_task(crawler.run(urls))
assert await poll_until_condition(lambda: failed_request_handler.await_count == 1)
assert await poll_until_condition(lambda: failed_request_handler.await_count == 1, timeout=30)

pause_task = asyncio.create_task(crawler.pause())
await asyncio.sleep(0)
assert not pause_task.done()

release.set()
await asyncio.wait_for(pause_task, timeout=10)
await asyncio.wait_for(run_task, timeout=10)
await asyncio.wait_for(pause_task, timeout=30)
await asyncio.wait_for(run_task, timeout=30)

assert len(visited) == 2

Expand Down Expand Up @@ -2695,17 +2707,19 @@ async def test_migration_pauses_crawl_and_persists_statistics(
) -> None:
"""A migration or an abort pauses the crawl and persists the statistics once the request in progress finishes."""
crawler = BasicCrawler(concurrency_settings=ConcurrencySettings(desired_concurrency=1, max_concurrency=1))
started = asyncio.Event()
release = asyncio.Event()
visited = list[str]()

@crawler.router.default_handler
async def handler(context: BasicCrawlingContext) -> None:
visited.append(context.request.url)
started.set()
await release.wait()

urls = ['https://a.placeholder.com', 'https://b.placeholder.com']
run_task = asyncio.create_task(crawler.run(urls))
assert await poll_until_condition(lambda: len(visited) == 1)
await asyncio.wait_for(started.wait(), timeout=30)

succeeded_when_persisted = list[int]()
persist_state = AsyncMock(
Expand All @@ -2725,23 +2739,23 @@ async def handler(context: BasicCrawlingContext) -> None:
assert visited == urls[:1]

crawler.resume()
await asyncio.wait_for(run_task, timeout=10)
await asyncio.wait_for(run_task, timeout=30)


async def test_migration_timeout_logs_error(monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture) -> None:
"""A migration that the request in progress outlasts logs an error."""
monkeypatch.setattr(BasicCrawler, '_SAFE_MIGRATION_WAIT', timedelta(milliseconds=50))
crawler = BasicCrawler(concurrency_settings=ConcurrencySettings(desired_concurrency=1, max_concurrency=1))
started = asyncio.Event()
release = asyncio.Event()
visited = list[str]()

@crawler.router.default_handler
async def handler(context: BasicCrawlingContext) -> None:
visited.append(context.request.url)
async def handler(_: BasicCrawlingContext) -> None:
started.set()
await release.wait()

run_task = asyncio.create_task(crawler.run(['https://a.placeholder.com']))
assert await poll_until_condition(lambda: len(visited) == 1)
await asyncio.wait_for(started.wait(), timeout=30)

event_manager = service_locator.get_event_manager()
with caplog.at_level(logging.ERROR, logger='crawlee'):
Expand All @@ -2752,7 +2766,7 @@ async def handler(context: BasicCrawlingContext) -> None:

release.set()
crawler.resume()
await asyncio.wait_for(run_task, timeout=10)
await asyncio.wait_for(run_task, timeout=30)


@pytest.mark.parametrize(
Expand Down
Loading