|
32 | 32 | from crawlee.crawlers._adaptive_playwright._adaptive_playwright_crawling_context import AdaptiveContextError |
33 | 33 | from crawlee.errors import RequestThrottledError |
34 | 34 | from crawlee.http_clients import HttpClient, HttpCrawlingResult |
35 | | -from crawlee.request_loaders import ThrottlingRequestManager |
36 | 35 | from crawlee.sessions import SessionPool |
37 | 36 | from crawlee.statistics import Statistics |
38 | 37 | from crawlee.storage_clients import SqlStorageClient |
39 | 38 | from crawlee.storages import KeyValueStore, RequestQueue |
| 39 | +from tests.unit.utils import open_throttler_held_until_reclaim |
40 | 40 |
|
41 | 41 | if TYPE_CHECKING: |
42 | 42 | from collections.abc import AsyncGenerator, Iterator |
@@ -941,16 +941,10 @@ async def request_handler(_context: AdaptivePlaywrightCrawlingContext) -> None: |
941 | 941 | mocked_handler.assert_called() |
942 | 942 |
|
943 | 943 |
|
944 | | -async def test_throttled_429_is_deferred() -> None: |
| 944 | +async def test_throttled_429_is_deferred(monkeypatch: pytest.MonkeyPatch) -> None: |
945 | 945 | """A throttled 429 from the browser sub crawler is retried later without spending a retry.""" |
946 | 946 | url = 'https://throttled.placeholder.com/page' |
947 | | - throttler = ThrottlingRequestManager( |
948 | | - await RequestQueue.open(), |
949 | | - domains=['throttled.placeholder.com'], |
950 | | - request_manager_opener=RequestQueue.open, |
951 | | - base_delay=timedelta(milliseconds=50), |
952 | | - max_delay=timedelta(milliseconds=100), |
953 | | - ) |
| 947 | + throttler = await open_throttler_held_until_reclaim(monkeypatch) |
954 | 948 | failed_request_handler = AsyncMock() |
955 | 949 | crawler = AdaptivePlaywrightCrawler.with_beautifulsoup_static_parser( |
956 | 950 | rendering_type_predictor=_SimpleRenderingTypePredictor( |
@@ -980,15 +974,9 @@ async def handler(context: AdaptivePlaywrightCrawlingContext) -> None: |
980 | 974 | assert handled[0].request.retry_count == 0 |
981 | 975 |
|
982 | 976 |
|
983 | | -async def test_throttled_static_429_is_deferred() -> None: |
| 977 | +async def test_throttled_static_429_is_deferred(monkeypatch: pytest.MonkeyPatch) -> None: |
984 | 978 | """A throttled 429 from the static sub crawler defers the request without falling back to the browser.""" |
985 | | - throttler = ThrottlingRequestManager( |
986 | | - await RequestQueue.open(), |
987 | | - domains=['throttled.placeholder.com'], |
988 | | - request_manager_opener=RequestQueue.open, |
989 | | - base_delay=timedelta(milliseconds=50), |
990 | | - max_delay=timedelta(milliseconds=100), |
991 | | - ) |
| 979 | + throttler = await open_throttler_held_until_reclaim(monkeypatch) |
992 | 980 | http_client = AsyncMock(spec=HttpClient) |
993 | 981 | http_client.crawl.side_effect = [ |
994 | 982 | HttpCrawlingResult(http_response=Mock(status_code=status, headers={}, read=AsyncMock(return_value=b''))) |
@@ -1025,16 +1013,10 @@ async def handler(context: AdaptivePlaywrightCrawlingContext) -> None: |
1025 | 1013 | assert handled[0].request.retry_count == 0 |
1026 | 1014 |
|
1027 | 1015 |
|
1028 | | -async def test_static_handler_raised_throttled_error_is_deferred() -> None: |
| 1016 | +async def test_static_handler_raised_throttled_error_is_deferred(monkeypatch: pytest.MonkeyPatch) -> None: |
1029 | 1017 | """A `RequestThrottledError` from the static handler defers the request without falling back to the browser.""" |
1030 | 1018 | url = 'https://throttled.placeholder.com/page' |
1031 | | - throttler = ThrottlingRequestManager( |
1032 | | - await RequestQueue.open(), |
1033 | | - domains=['throttled.placeholder.com'], |
1034 | | - request_manager_opener=RequestQueue.open, |
1035 | | - base_delay=timedelta(milliseconds=50), |
1036 | | - max_delay=timedelta(milliseconds=100), |
1037 | | - ) |
| 1019 | + throttler = await open_throttler_held_until_reclaim(monkeypatch) |
1038 | 1020 | http_client = AsyncMock(spec=HttpClient) |
1039 | 1021 | http_client.crawl.return_value = HttpCrawlingResult( |
1040 | 1022 | http_response=Mock(status_code=200, headers={}, read=AsyncMock(return_value=b'')) |
|
0 commit comments