Skip to content

Commit

Permalink
general: Honour timeout from user configuration file in aiohttp requests
Browse files Browse the repository at this point in the history
We only honour the request timeout from user configuration file with
synchronous downloads but not with asyncio/aiohttp, fix that.

Resolves: #571

Signed-off-by: Erik Skultety <[email protected]>
  • Loading branch information
eskultety committed Jul 15, 2024
1 parent 8d77085 commit 34b72cc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
7 changes: 6 additions & 1 deletion cachi2/core/package_managers/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ async def _async_download_binary_file(
:raise FetchError: If download failed
"""
try:
async with session.get(url, auth=auth, raise_for_status=True) as resp:
timeout = aiohttp.ClientTimeout(total=get_config().requests_timeout)

log.debug(
f"aiohttp.ClientSession.get(url: {url}, timeout: {timeout}, raise_for_status: True)"
)
async with session.get(url, timeout=timeout, auth=auth, raise_for_status=True) as resp:
with open(download_path, "wb") as f:
while True:
chunk = await resp.content.read(chunk_size)
Expand Down
10 changes: 8 additions & 2 deletions tests/unit/package_managers/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from unittest import mock
from unittest.mock import MagicMock

import aiohttp
import aiohttp_retry
import pytest
import requests
Expand Down Expand Up @@ -145,7 +146,10 @@ def test_extract_git_info(url: str, nonstandard_info: Any) -> None:


@pytest.mark.asyncio
async def test_async_download_binary_file(tmp_path: Path, caplog: pytest.LogCaptureFixture) -> None:
async def test_async_download_binary_file(
tmp_path: Path,
caplog: pytest.LogCaptureFixture,
) -> None:
url = "http://example.com/file.tar"
download_path = tmp_path / "file.tar"

Expand Down Expand Up @@ -174,7 +178,9 @@ async def mock_aenter() -> MagicMock:
assert f.read() == b"first_chunk-second_chunk-"

assert session.get.called
assert session.get.call_args == mock.call(url, auth=None, raise_for_status=True)
assert session.get.call_args == mock.call(
url, timeout=aiohttp.ClientTimeout(total=300), auth=None, raise_for_status=True
)


@pytest.mark.asyncio
Expand Down

0 comments on commit 34b72cc

Please sign in to comment.