22
33import pytest
44
5- from crawlee import service_locator
5+ from crawlee import Request , service_locator
6+ from crawlee ._types import BasicCrawlingContext
67from crawlee .configuration import Configuration as CrawleeConfiguration
78from crawlee .crawlers import BasicCrawler
89from crawlee .errors import ServiceConflictError
@@ -77,14 +78,14 @@ async def test_existing_apify_config_throws_error_when_set_in_actor() -> None:
7778
7879
7980async def test_setting_config_after_actor_raises_exception () -> None :
80- """Test that passing setting configuration in service locator after actor wa created raises an exception."""
81+ """Test that setting configuration in service locator after actor was created raises an exception."""
8182 async with Actor ():
8283 with pytest .raises (ServiceConflictError ):
8384 service_locator .set_configuration (ApifyConfiguration ())
8485
8586
8687async def test_actor_using_input_configuration () -> None :
87- """Test that passing setting configuration in service locator after actor wa created raises an exception."""
88+ """Test that setting configuration in service locator after actor was created raises an exception."""
8889 apify_config = ApifyConfiguration ()
8990 async with Actor (configuration = apify_config ):
9091 pass
@@ -111,19 +112,37 @@ async def test_crawler_implicit_configuration() -> None:
111112 assert Actor .config is service_locator .get_configuration () is crawler ._service_locator .get_configuration ()
112113
113114
114- async def test_crawlers_own_configuration () -> None :
115+ async def test_crawlers_own_configuration (tmp_path : Path ) -> None :
115116 """Test that crawlers can use own configurations without crashing."""
116117 config_actor = ApifyConfiguration ()
117- apify_crawler_1 = ApifyConfiguration ()
118- apify_crawler_2 = ApifyConfiguration ()
118+ dir_1 = tmp_path / 'dir_1'
119+ dir_2 = tmp_path / 'dir_2'
120+ config_crawler_1 = ApifyConfiguration ()
121+ config_actor .storage_dir = str (dir_1 )
122+ config_crawler_2 = ApifyConfiguration ()
123+ config_crawler_2 .storage_dir = str (dir_2 )
119124
120125 async with Actor (configuration = config_actor ):
121- crawler_1 = BasicCrawler (configuration = apify_crawler_1 )
122- crawler_2 = BasicCrawler (configuration = apify_crawler_2 )
126+
127+ async def request_handler (context : BasicCrawlingContext ) -> None :
128+ Actor .log .info (f'Processing: { context .request .url } ' )
129+
130+ crawler_1 = BasicCrawler (configuration = config_crawler_1 , request_handler = request_handler )
131+ crawler_2 = BasicCrawler (configuration = config_crawler_2 , request_handler = request_handler )
132+ await crawler_1 .add_requests ([Request .from_url (url = 'http://example.com/1' )])
133+ await crawler_2 .add_requests (
134+ [Request .from_url (url = 'http://example.com/2' ), Request .from_url (url = 'http://example.com/3' )]
135+ )
136+
137+ await crawler_1 .run ()
138+ await crawler_2 .run ()
123139
124140 assert service_locator .get_configuration () is config_actor
125- assert crawler_1 ._service_locator .get_configuration () is apify_crawler_1
126- assert crawler_2 ._service_locator .get_configuration () is apify_crawler_2
141+ assert crawler_1 ._service_locator .get_configuration () is config_crawler_1
142+ assert crawler_2 ._service_locator .get_configuration () is config_crawler_2
143+
144+ assert crawler_1 .statistics .state .requests_total == 1
145+ assert crawler_2 .statistics .state .requests_total == 2
127146
128147
129148async def test_crawler_global_configuration () -> None :
@@ -163,8 +182,8 @@ async def test_storage_retrieved_is_different_with_different_config(tmp_path: Pa
163182
164183
165184async def test_storage_retrieved_is_same_with_equivalent_config () -> None :
166- """Test that retrieving storage depends on used configuration. If two same configuration(even if they are different
167- instances) are used it returns same storage."""
185+ """Test that retrieving storage depends on used configuration. If two equivalent configuration(even if they are
186+ different instances) are used it returns same storage."""
168187 config_actor = ApifyConfiguration ()
169188 apify_crawler = ApifyConfiguration ()
170189
0 commit comments