|
26 | 26 | from crawlee.configuration import Configuration |
27 | 27 | from crawlee.crawlers import BasicCrawler |
28 | 28 | from crawlee.errors import RequestCollisionError, SessionError, UserDefinedErrorHandlerError |
29 | | -from crawlee.events import Event, EventCrawlerStatusData |
30 | | -from crawlee.events._local_event_manager import LocalEventManager |
| 29 | +from crawlee.events import Event, EventCrawlerStatusData, LocalEventManager |
31 | 30 | from crawlee.request_loaders import RequestList, RequestManagerTandem |
32 | 31 | from crawlee.sessions import Session, SessionPool |
33 | 32 | from crawlee.statistics import FinalStatistics |
@@ -2118,3 +2117,36 @@ async def handler_2(context: BasicCrawlingContext) -> None: |
2118 | 2117 |
|
2119 | 2118 | await rq1.drop() |
2120 | 2119 | await rq2.drop() |
| 2120 | + |
| 2121 | + |
| 2122 | +async def test_globa_and_local_event_manager_in_crawler_run() -> None: |
| 2123 | + """Test that both global and local event managers are used in crawler run""" |
| 2124 | + |
| 2125 | + config = service_locator.get_configuration() |
| 2126 | + |
| 2127 | + local_event_manager = LocalEventManager.from_config(config) |
| 2128 | + |
| 2129 | + crawler = BasicCrawler(event_manager=local_event_manager) |
| 2130 | + |
| 2131 | + handler_call = AsyncMock() |
| 2132 | + |
| 2133 | + @crawler.router.default_handler |
| 2134 | + async def handler(context: BasicCrawlingContext) -> None: |
| 2135 | + global_event_manager = service_locator.get_event_manager() |
| 2136 | + handler_call(local_event_manager.active, global_event_manager.active) |
| 2137 | + |
| 2138 | + await crawler.run(['https://a.placeholder.com']) |
| 2139 | + |
| 2140 | + assert handler_call.call_count == 1 |
| 2141 | + |
| 2142 | + local_em_state, global_em_state = handler_call.call_args_list[0][0] |
| 2143 | + |
| 2144 | + # Both event managers should be active. |
| 2145 | + assert local_em_state is True |
| 2146 | + assert global_em_state is True |
| 2147 | + |
| 2148 | + global_event_manager = service_locator.get_event_manager() |
| 2149 | + |
| 2150 | + # After crawler is finished, both event managers should be inactive. |
| 2151 | + assert local_event_manager.active is False |
| 2152 | + assert global_event_manager.active is False |
0 commit comments