|
| 1 | +from tests.integration.conftest import MakeActorFunction, RunActorFunction |
| 2 | + |
| 3 | + |
| 4 | +async def test_actor_on_platform_max_crawl_depth( |
| 5 | + make_actor: MakeActorFunction, |
| 6 | + run_actor: RunActorFunction, |
| 7 | +) -> None: |
| 8 | + """Test that the actor respects max_crawl_depth.""" |
| 9 | + |
| 10 | + async def main() -> None: |
| 11 | + """The crawler entry point.""" |
| 12 | + import re |
| 13 | + |
| 14 | + from crawlee.crawlers import ParselCrawler, ParselCrawlingContext |
| 15 | + |
| 16 | + from apify import Actor |
| 17 | + |
| 18 | + async with Actor: |
| 19 | + crawler = ParselCrawler(max_crawl_depth=2) |
| 20 | + finished = [] |
| 21 | + enqueue_pattern = re.compile(r'http://localhost:8080/2+$') |
| 22 | + |
| 23 | + @crawler.router.default_handler |
| 24 | + async def default_handler(context: ParselCrawlingContext) -> None: |
| 25 | + """Default request handler.""" |
| 26 | + context.log.info(f'Processing {context.request.url} ...') |
| 27 | + await context.enqueue_links(include=[enqueue_pattern]) |
| 28 | + await context.push_data({'Url': context.request.url}) |
| 29 | + finished.append(context.request.url) |
| 30 | + |
| 31 | + await crawler.run(['http://localhost:8080/']) |
| 32 | + assert finished == ['http://localhost:8080/', 'http://localhost:8080/2', 'http://localhost:8080/22'] |
| 33 | + # assert some dataset |
| 34 | + |
| 35 | + actor = await make_actor(label='parsel-crawler', main_func=main) |
| 36 | + run_result = await run_actor(actor) |
| 37 | + |
| 38 | + assert run_result.status == 'SUCCEEDED' |
| 39 | + |
| 40 | + |
| 41 | +async def test_actor_on_platform_max_requests_per_crawl( |
| 42 | + make_actor: MakeActorFunction, |
| 43 | + run_actor: RunActorFunction, |
| 44 | +) -> None: |
| 45 | + """Test that the actor respects max_requests_per_crawl.""" |
| 46 | + |
| 47 | + async def main() -> None: |
| 48 | + """The crawler entry point.""" |
| 49 | + from crawlee import ConcurrencySettings |
| 50 | + from crawlee.crawlers import ParselCrawler, ParselCrawlingContext |
| 51 | + |
| 52 | + from apify import Actor |
| 53 | + |
| 54 | + async with Actor: |
| 55 | + crawler = ParselCrawler( |
| 56 | + max_requests_per_crawl=3, concurrency_settings=ConcurrencySettings(max_concurrency=1) |
| 57 | + ) |
| 58 | + finished = [] |
| 59 | + |
| 60 | + @crawler.router.default_handler |
| 61 | + async def default_handler(context: ParselCrawlingContext) -> None: |
| 62 | + """Default request handler.""" |
| 63 | + context.log.info(f'Processing {context.request.url} ...') |
| 64 | + await context.enqueue_links() |
| 65 | + await context.push_data({'Url': context.request.url}) |
| 66 | + finished.append(context.request.url) |
| 67 | + |
| 68 | + await crawler.run(['http://localhost:8080/']) |
| 69 | + assert len(finished) == 3 |
| 70 | + # assert some dataset |
| 71 | + |
| 72 | + actor = await make_actor(label='parsel-crawler', main_func=main) |
| 73 | + run_result = await run_actor(actor) |
| 74 | + |
| 75 | + assert run_result.status == 'SUCCEEDED' |
0 commit comments