|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from datetime import timedelta |
| 4 | +from unittest.mock import AsyncMock, MagicMock, Mock |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from apify_client import ApifyClient, ApifyClientAsync |
| 9 | + |
| 10 | + |
| 11 | +@pytest.mark.parametrize('resource', [pytest.param('actor', id='actor'), pytest.param('task', id='task')]) |
| 12 | +def test_call_passes_timeout_to_poll_sync(monkeypatch: pytest.MonkeyPatch, resource: str) -> None: |
| 13 | + """Apply the per-request timeout to both the start and poll requests.""" |
| 14 | + client = ApifyClient(token='test_token') |
| 15 | + sub_client = getattr(client, resource)('resource-id') |
| 16 | + start = Mock(return_value=Mock(id='run-id')) |
| 17 | + poll = Mock(return_value=Mock(id='run-id')) |
| 18 | + monkeypatch.setattr(sub_client, 'start', start) |
| 19 | + monkeypatch.setattr(sub_client._client_registry, 'run_client', Mock(return_value=Mock(wait_for_finish=poll))) |
| 20 | + |
| 21 | + timeout = timedelta(seconds=90) |
| 22 | + kwargs = {'logger': None} if resource == 'actor' else {} |
| 23 | + sub_client.call(timeout=timeout, wait_duration=timedelta(seconds=120), **kwargs) |
| 24 | + |
| 25 | + assert start.call_args.kwargs['timeout'] == timeout |
| 26 | + poll.assert_called_once_with(wait_duration=timedelta(seconds=120), timeout=timeout) |
| 27 | + |
| 28 | + |
| 29 | +@pytest.mark.parametrize('resource', [pytest.param('actor', id='actor'), pytest.param('task', id='task')]) |
| 30 | +async def test_call_passes_timeout_to_poll_async(monkeypatch: pytest.MonkeyPatch, resource: str) -> None: |
| 31 | + """Apply the per-request timeout to both async start and poll requests.""" |
| 32 | + client = ApifyClientAsync(token='test_token') |
| 33 | + sub_client = getattr(client, resource)('resource-id') |
| 34 | + start = AsyncMock(return_value=Mock(id='run-id')) |
| 35 | + poll = AsyncMock(return_value=Mock(id='run-id')) |
| 36 | + monkeypatch.setattr(sub_client, 'start', start) |
| 37 | + monkeypatch.setattr(sub_client._client_registry, 'run_client', Mock(return_value=Mock(wait_for_finish=poll))) |
| 38 | + |
| 39 | + timeout = timedelta(seconds=90) |
| 40 | + kwargs = {'logger': None} if resource == 'actor' else {} |
| 41 | + await sub_client.call(timeout=timeout, wait_duration=timedelta(seconds=120), **kwargs) |
| 42 | + |
| 43 | + assert start.call_args.kwargs['timeout'] == timeout |
| 44 | + poll.assert_awaited_once_with(wait_duration=timedelta(seconds=120), timeout=timeout) |
| 45 | + |
| 46 | + |
| 47 | +def test_actor_call_passes_timeout_to_poll_with_log_redirect(monkeypatch: pytest.MonkeyPatch) -> None: |
| 48 | + """Keep the poll timeout when the default log redirect is active.""" |
| 49 | + actor = ApifyClient(token='test_token').actor('actor-id') |
| 50 | + monkeypatch.setattr(actor, 'start', Mock(return_value=Mock(id='run-id'))) |
| 51 | + poll = Mock() |
| 52 | + run_client = Mock(wait_for_finish=poll) |
| 53 | + run_client.get_status_message_watcher.return_value = MagicMock() |
| 54 | + run_client.get_streamed_log.return_value = MagicMock() |
| 55 | + monkeypatch.setattr(actor._client_registry, 'run_client', Mock(return_value=run_client)) |
| 56 | + |
| 57 | + timeout = timedelta(seconds=90) |
| 58 | + actor.call(timeout=timeout) |
| 59 | + |
| 60 | + poll.assert_called_once_with(wait_duration=None, timeout=timeout) |
| 61 | + |
| 62 | + |
| 63 | +async def test_actor_call_passes_timeout_to_poll_with_log_redirect_async(monkeypatch: pytest.MonkeyPatch) -> None: |
| 64 | + """Keep the async poll timeout when the default log redirect is active.""" |
| 65 | + actor = ApifyClientAsync(token='test_token').actor('actor-id') |
| 66 | + monkeypatch.setattr(actor, 'start', AsyncMock(return_value=Mock(id='run-id'))) |
| 67 | + poll = AsyncMock() |
| 68 | + run_client = Mock(wait_for_finish=poll) |
| 69 | + run_client.get_status_message_watcher = AsyncMock(return_value=MagicMock()) |
| 70 | + run_client.get_streamed_log = AsyncMock(return_value=MagicMock()) |
| 71 | + monkeypatch.setattr(actor._client_registry, 'run_client', Mock(return_value=run_client)) |
| 72 | + |
| 73 | + timeout = timedelta(seconds=90) |
| 74 | + await actor.call(timeout=timeout) |
| 75 | + |
| 76 | + poll.assert_awaited_once_with(wait_duration=None, timeout=timeout) |
0 commit comments