|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING |
| 4 | + |
| 5 | +import pytest |
| 6 | +import respx |
| 7 | +from httpx import Request, Response |
| 8 | + |
| 9 | +from apify_client import ApifyClient, ApifyClientAsync |
| 10 | + |
| 11 | +if TYPE_CHECKING: |
| 12 | + from apify_client._dynamic_timeout import DynamicTimeoutFunction, RequestContent |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture |
| 16 | +def get_dynamic_timeout_function() -> DynamicTimeoutFunction: |
| 17 | + """Example of a dynamic timeout function.""" |
| 18 | + |
| 19 | + def get_dynamic_timeout(method: str, url: str, content: RequestContent) -> int | None: |
| 20 | + """Return suitable timeout. |
| 21 | +
|
| 22 | + For POST on endpoint v2/datasets/whatever/items timeout is proportional to the size of the content. |
| 23 | + For everything else return fixed 30.""" |
| 24 | + if isinstance(content, bytes) and method == 'POST' and url.endswith('v2/datasets/whatever/items'): |
| 25 | + dynamic_timeout_based_on_size = int(len(content) / 10) |
| 26 | + return min(360, max(5, dynamic_timeout_based_on_size)) # Saturate in range 5-360 seconds |
| 27 | + return 30 |
| 28 | + |
| 29 | + return get_dynamic_timeout |
| 30 | + |
| 31 | + |
| 32 | +@respx.mock |
| 33 | +@pytest.mark.parametrize( |
| 34 | + ('content', 'expected_timeout'), |
| 35 | + [ |
| 36 | + pytest.param('abcd', 5, id='Small payload'), |
| 37 | + pytest.param('abcd' * 10000, 9, id='Payload in the dynamic timeout interval interval'), |
| 38 | + pytest.param('abcd' * 1000000, 360, id='Large payload'), |
| 39 | + ], |
| 40 | +) |
| 41 | +async def test_dynamic_timeout_async_client( |
| 42 | + get_dynamic_timeout_function: DynamicTimeoutFunction, content: str, expected_timeout: int |
| 43 | +) -> None: |
| 44 | + def check_timeout(request: Request) -> Response: |
| 45 | + assert request.extensions['timeout'] == { |
| 46 | + 'connect': expected_timeout, |
| 47 | + 'pool': expected_timeout, |
| 48 | + 'read': expected_timeout, |
| 49 | + 'write': expected_timeout, |
| 50 | + } |
| 51 | + return Response(201) |
| 52 | + |
| 53 | + respx.post('https://api.apify.com/v2/datasets/whatever/items').mock(side_effect=check_timeout) |
| 54 | + client = ApifyClientAsync(get_dynamic_timeout=get_dynamic_timeout_function) |
| 55 | + await client.dataset(dataset_id='whatever').push_items({'some_key': content}) |
| 56 | + |
| 57 | + |
| 58 | +@respx.mock |
| 59 | +async def test_dynamic_timeout_async_client_default() -> None: |
| 60 | + expected_timeout = 360 |
| 61 | + |
| 62 | + def check_timeout(request: Request) -> Response: |
| 63 | + assert request.extensions['timeout'] == { |
| 64 | + 'connect': expected_timeout, |
| 65 | + 'pool': expected_timeout, |
| 66 | + 'read': expected_timeout, |
| 67 | + 'write': expected_timeout, |
| 68 | + } |
| 69 | + return Response(201) |
| 70 | + |
| 71 | + respx.post('https://api.apify.com/v2/datasets/whatever/items').mock(side_effect=check_timeout) |
| 72 | + client = ApifyClientAsync() |
| 73 | + await client.dataset(dataset_id='whatever').push_items({'some_key': 'abcd'}) |
| 74 | + |
| 75 | + |
| 76 | +@respx.mock |
| 77 | +@pytest.mark.parametrize( |
| 78 | + ('content', 'expected_timeout'), |
| 79 | + [ |
| 80 | + pytest.param('abcd', 5, id='Small payload'), |
| 81 | + pytest.param('abcd' * 10000, 9, id='Payload in the dynamic timeout interval interval'), |
| 82 | + pytest.param('abcd' * 1000000, 360, id='Large payload'), |
| 83 | + ], |
| 84 | +) |
| 85 | +def test_dynamic_timeout_sync_client( |
| 86 | + get_dynamic_timeout_function: DynamicTimeoutFunction, content: str, expected_timeout: int |
| 87 | +) -> None: |
| 88 | + def check_timeout(request: Request) -> Response: |
| 89 | + assert request.extensions['timeout'] == { |
| 90 | + 'connect': expected_timeout, |
| 91 | + 'pool': expected_timeout, |
| 92 | + 'read': expected_timeout, |
| 93 | + 'write': expected_timeout, |
| 94 | + } |
| 95 | + return Response(201) |
| 96 | + |
| 97 | + respx.post('https://api.apify.com/v2/datasets/whatever/items').mock(side_effect=check_timeout) |
| 98 | + client = ApifyClient(get_dynamic_timeout=get_dynamic_timeout_function) |
| 99 | + client.dataset(dataset_id='whatever').push_items({'some_key': content}) |
| 100 | + |
| 101 | + |
| 102 | +@respx.mock |
| 103 | +def test_dynamic_timeout_sync_client_default() -> None: |
| 104 | + expected_timeout = 360 |
| 105 | + |
| 106 | + def check_timeout(request: Request) -> Response: |
| 107 | + assert request.extensions['timeout'] == { |
| 108 | + 'connect': expected_timeout, |
| 109 | + 'pool': expected_timeout, |
| 110 | + 'read': expected_timeout, |
| 111 | + 'write': expected_timeout, |
| 112 | + } |
| 113 | + return Response(201) |
| 114 | + |
| 115 | + respx.post('https://api.apify.com/v2/datasets/whatever/items').mock(side_effect=check_timeout) |
| 116 | + client = ApifyClient() |
| 117 | + client.dataset(dataset_id='whatever').push_items({'some_key': 'abcd'}) |
0 commit comments