Skip to content

Commit

Permalink
Add timeout to avoid hanging if the test fails
Browse files Browse the repository at this point in the history
  • Loading branch information
JWCook committed Oct 6, 2023
1 parent 8ef7b80 commit f479716
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 20 deletions.
2 changes: 1 addition & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
def test(session):
"""Run tests for a specific python version"""
test_paths = session.posargs or [UNIT_TESTS]
session.install('.', 'pytest', 'pytest-xdist', 'requests-mock', 'timeout-decorator')
session.install('.', 'pytest', 'pytest-aiohttp', 'pytest-asyncio', 'pytest-xdist')

cmd = f'pytest -rs {XDIST_ARGS}'
session.run(*cmd.split(' '), *test_paths)
Expand Down
11 changes: 7 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ docs = ["furo", "linkify-it-py", "markdown-it-py", "myst-parser", "python

[tool.poetry.dev-dependencies]
# For unit + integration tests
async-timeout = ">=4.0"
brotli = ">=1.0"
pytest = ">=6.2"
pytest-aiohttp = "^0.3"
Expand Down
15 changes: 9 additions & 6 deletions test/integration/base_backend_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from uuid import uuid4

import pytest
from async_timeout import timeout
from itsdangerous.exc import BadSignature
from itsdangerous.serializer import Serializer

Expand Down Expand Up @@ -108,13 +109,15 @@ async def test_without_contextmanager(self):
"""Test that the cache backend can be safely used without the CachedSession contextmanager.
An "unclosed ClientSession" warning is expected here, however.
"""
session = await self._init_session()
await session.get(httpbin('get'))
del session
# Timeout to avoid hanging if the test fails
async with timeout(5.0):
session = await self._init_session()
await session.get(httpbin('get'))
del session

session = await self._init_session(clear=False)
r = await session.get(httpbin('get'))
assert r.from_cache is True
session = await self._init_session(clear=False)
r = await session.get(httpbin('get'))
assert r.from_cache is True

async def test_request__expire_after(self):
async with self.init_session() as session:
Expand Down
18 changes: 9 additions & 9 deletions test/unit/test_base_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

from aiohttp_client_cache import CachedResponse
from aiohttp_client_cache.backends import CacheBackend, DictCache, get_placeholder_backend
from test.conftest import skip_37

TEST_URL = 'https://test.com'

pytestmark = pytest.mark.asyncio
skip_py37 = pytest.mark.skipif(
skip_37 = pytest.mark.skipif(
version_info < (3, 8), reason='Test requires AsyncMock from python 3.8+'
)

Expand Down Expand Up @@ -71,7 +71,7 @@ async def test_get_response__cache_miss(mock_delete):
mock_delete.assert_not_called()


@skip_py37
@skip_37
@patch.object(CacheBackend, 'delete')
@patch.object(CacheBackend, 'is_cacheable', return_value=False)
async def test_get_response__cache_expired(mock_is_cacheable, mock_delete):
Expand All @@ -84,7 +84,7 @@ async def test_get_response__cache_expired(mock_is_cacheable, mock_delete):
mock_delete.assert_called_with('request-key')


@skip_py37
@skip_37
@pytest.mark.parametrize('error_type', [AttributeError, KeyError, TypeError, pickle.PickleError])
@patch.object(CacheBackend, 'delete')
@patch.object(DictCache, 'read')
Expand All @@ -99,7 +99,7 @@ async def test_get_response__cache_invalid(mock_read, mock_delete, error_type):
mock_delete.assert_not_called()


@skip_py37
@skip_37
@patch.object(DictCache, 'read', return_value=object())
async def test_get_response__quiet_serde_error(mock_read):
"""Test for a quiet deserialization error in which no errors are raised but attributes are
Expand All @@ -113,7 +113,7 @@ async def test_get_response__quiet_serde_error(mock_read):
assert response is None


@skip_py37
@skip_37
async def test_save_response():
cache = CacheBackend()
mock_response = get_mock_response()
Expand All @@ -126,7 +126,7 @@ async def test_save_response():
assert await cache.redirects.read(redirect_key) == 'key'


@skip_py37
@skip_37
async def test_save_response__manual_save():
"""Manually save a response with no cache key provided"""
cache = CacheBackend()
Expand Down Expand Up @@ -193,7 +193,7 @@ async def test_has_url():
assert not await cache.has_url('https://test.com/some_other_path')


@skip_py37
@skip_37
@patch('aiohttp_client_cache.backends.base.create_key')
async def test_create_key(mock_create_key):
"""Actual logic is in cache_keys module; just test to make sure it gets called correctly"""
Expand Down Expand Up @@ -244,7 +244,7 @@ async def test_is_cacheable(method, status, disabled, expired, filter_return, ex
assert await cache.is_cacheable(mock_response) is expected_result


@skip_py37
@skip_37
@pytest.mark.parametrize(
'method, status, disabled, expired, body, expected_result',
[
Expand Down

0 comments on commit f479716

Please sign in to comment.