diff --git a/mypy.ini b/mypy.ini index db14311..d3d503f 100644 --- a/mypy.ini +++ b/mypy.ini @@ -1,2 +1,2 @@ [mypy] -no_implicit_optional=False \ No newline at end of file +no_implicit_optional=False diff --git a/tests/__init__.py b/tests/__init__.py index 05075a9..6d06d9e 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -16,12 +16,6 @@ async def _ensure_background_tasks_finished(): await asyncio.sleep(0) -async def _ensure_asyncio_background_tasks_finished(): - # let other tasks to acquire IO loop - for i in range(10): - await asyncio.sleep(0) - - class AnyObject(object): def __eq__(self, o: object) -> bool: return True @@ -30,7 +24,7 @@ def __eq__(self, o: object) -> bool: def _assert_called_once_with(mock, args, kwargs): """Mock assertions do not support "any" placeholders. This method allows to use AnyObject as any argument.""" calls_list = mock.call_args_list - assert 1 == len(calls_list), 'Called {} times but expected only one call'.format(len(calls_list)) + assert len(calls_list) == 1, 'Called {} times but expected only one call'.format(len(calls_list)) call_args, call_kwargs = calls_list[0] for arg_num in range(len(args)): diff --git a/tests/end2end/test_showcase.py b/tests/end2end/test_showcase.py index 79fef73..ba331ef 100644 --- a/tests/end2end/test_showcase.py +++ b/tests/end2end/test_showcase.py @@ -6,7 +6,7 @@ from memoize.configuration import DefaultInMemoryCacheConfiguration from memoize.exceptions import CachedMethodFailedException from memoize.wrapper import memoize -from tests import _ensure_asyncio_background_tasks_finished +from tests import _ensure_background_tasks_finished @pytest.mark.asyncio(scope="class") @@ -40,37 +40,37 @@ async def get_value_or_throw(arg, kwarg=None): # when #2: background refresh - returns stale time.sleep(UPDATE_S) - await _ensure_asyncio_background_tasks_finished() + await _ensure_background_tasks_finished() value, should_throw = 'ok #2', False res2 = await get_value_or_throw('test', kwarg='args') # when #3: no refresh (cache up-to-date) time.sleep(.10) - await _ensure_asyncio_background_tasks_finished() + await _ensure_background_tasks_finished() value, should_throw = 'ok #3', False res3 = await get_value_or_throw('test', kwarg='args') # when #4: no refresh (cache up-to-date) but starts throwing time.sleep(.10) - await _ensure_asyncio_background_tasks_finished() + await _ensure_background_tasks_finished() value, should_throw = 'throws #1', True res4 = await get_value_or_throw('test', kwarg='args') # when #5: background refresh while throwing - returns stale time.sleep(UPDATE_S) - await _ensure_asyncio_background_tasks_finished() + await _ensure_background_tasks_finished() value, should_throw = 'throws #2', True res5 = await get_value_or_throw('test', kwarg='args') # when #6: stale value from cache as method raised during background refresh at #5 time.sleep(.10) - await _ensure_asyncio_background_tasks_finished() + await _ensure_background_tasks_finished() value, should_throw = 'throws #3', True res6 = await get_value_or_throw('test', kwarg='args') # when #7: stale expired - cache throws synchronously time.sleep(EXPIRE_S) - await _ensure_asyncio_background_tasks_finished() + await _ensure_background_tasks_finished() value, should_throw = 'throws #4', True with pytest.raises(Exception) as context: await get_value_or_throw('test', kwarg='args') diff --git a/tests/end2end/test_wrapper.py b/tests/end2end/test_wrapper.py index 9454545..2441ed9 100644 --- a/tests/end2end/test_wrapper.py +++ b/tests/end2end/test_wrapper.py @@ -11,7 +11,7 @@ from memoize.exceptions import CachedMethodFailedException from memoize.storage import LocalInMemoryCacheStorage from memoize.wrapper import memoize -from tests import _ensure_asyncio_background_tasks_finished +from tests import _ensure_background_tasks_finished @pytest.mark.asyncio(scope="class") @@ -33,7 +33,7 @@ async def get_value(arg, kwarg=None): # when res1 = await get_value('test', kwarg='args') time.sleep(.200) - await _ensure_asyncio_background_tasks_finished() + await _ensure_background_tasks_finished() value = 1 # calling thrice be more confident about behaviour of parallel execution res2 = await self._call_thrice(lambda: get_value('test', kwarg='args')) @@ -54,7 +54,7 @@ async def get_value(arg, kwarg=None): # when res1 = await get_value('test', kwarg='args') time.sleep(.200) - await _ensure_asyncio_background_tasks_finished() + await _ensure_background_tasks_finished() value = 1 # calling thrice be more confident about behaviour of parallel execution res2 = await self._call_thrice(lambda: get_value('test', kwarg='args')) @@ -75,7 +75,7 @@ async def get_value(arg, kwarg=None): # when res1 = await get_value('test', kwarg='args') time.sleep(.200) - await _ensure_asyncio_background_tasks_finished() + await _ensure_background_tasks_finished() value = 1 # calling thrice be more confident about behaviour of parallel execution res2 = await self._call_thrice(lambda: get_value('test', kwarg='args')) @@ -96,10 +96,10 @@ async def get_value(arg, kwarg=None): # when res1 = await get_value('test', kwarg='args') time.sleep(.200) - await _ensure_asyncio_background_tasks_finished() + await _ensure_background_tasks_finished() value = 1 await get_value('test', kwarg='args') - await _ensure_asyncio_background_tasks_finished() + await _ensure_background_tasks_finished() # calling thrice be more confident about behaviour of parallel execution res2 = await self._call_thrice(lambda: get_value('test', kwarg='args')) @@ -118,7 +118,7 @@ async def get_value(arg, kwarg=None): # when res1 = await get_value('test1', kwarg='args') time.sleep(.200) - await _ensure_asyncio_background_tasks_finished() + await _ensure_background_tasks_finished() value = 1 res2 = await get_value('test2', kwarg='args') @@ -137,7 +137,7 @@ async def get_value(arg, kwarg=None): # when res1 = await get_value('test', kwarg='args1') time.sleep(.200) - await _ensure_asyncio_background_tasks_finished() + await _ensure_background_tasks_finished() value = 1 res2 = await get_value('test', kwarg='args2') @@ -180,9 +180,9 @@ async def test_should_return_timeout_for_all_concurrent_callers(self): @memoize(configuration=DefaultInMemoryCacheConfiguration(method_timeout=timedelta(milliseconds=1))) async def get_value(arg, kwarg=None): - await _ensure_asyncio_background_tasks_finished() + await _ensure_background_tasks_finished() time.sleep(.200) - await _ensure_asyncio_background_tasks_finished() + await _ensure_background_tasks_finished() return value # when @@ -223,7 +223,7 @@ async def get_value(arg, kwarg=None): # when res1 = await get_value('test1', kwarg='args1') time.sleep(.200) - await _ensure_asyncio_background_tasks_finished() + await _ensure_background_tasks_finished() value = 1 res2 = await get_value('test2', kwarg='args2') @@ -253,7 +253,7 @@ async def get_value(arg, kwarg=None): await get_value('test2', kwarg='args2') await get_value('test3', kwarg='args3') await get_value('test4', kwarg='args4') - await _ensure_asyncio_background_tasks_finished() + await _ensure_background_tasks_finished() # then s1 = await storage.get("('test1', 'args1')") @@ -303,9 +303,9 @@ async def test_should_throw_exception_on_refresh_timeout(self): @memoize(configuration=DefaultInMemoryCacheConfiguration(method_timeout=timedelta(milliseconds=100))) async def get_value(arg, kwarg=None): - await _ensure_asyncio_background_tasks_finished() + await _ensure_background_tasks_finished() time.sleep(.200) - await _ensure_asyncio_background_tasks_finished() + await _ensure_background_tasks_finished() return 0 # when diff --git a/tests/end2end/test_wrapper_manually_applied.py b/tests/end2end/test_wrapper_manually_applied.py index 10f140f..f1a7339 100644 --- a/tests/end2end/test_wrapper_manually_applied.py +++ b/tests/end2end/test_wrapper_manually_applied.py @@ -11,7 +11,7 @@ from memoize.exceptions import CachedMethodFailedException from memoize.storage import LocalInMemoryCacheStorage from memoize.wrapper import memoize -from tests import _ensure_asyncio_background_tasks_finished +from tests import _ensure_background_tasks_finished @pytest.mark.asyncio(scope="class") @@ -32,7 +32,7 @@ async def get_value(arg, kwarg=None): # when res1 = await get_value_cached('test', kwarg='args') await asyncio.sleep(0.200) - await _ensure_asyncio_background_tasks_finished() + await _ensure_background_tasks_finished() value = 1 # calling thrice be more confident about behaviour of parallel execution res2 = await self._call_thrice(lambda: get_value_cached('test', kwarg='args')) @@ -56,7 +56,7 @@ async def get_value(arg, kwarg=None): # when res1 = await get_value_cached('test', kwarg='args') await asyncio.sleep(0.200) - await _ensure_asyncio_background_tasks_finished() + await _ensure_background_tasks_finished() value = 1 # calling thrice be more confident about behaviour of parallel execution res2 = await self._call_thrice(lambda: get_value_cached('test', kwarg='args')) @@ -80,7 +80,7 @@ async def get_value(arg, kwarg=None): # when res1 = await get_value_cached('test', kwarg='args') await asyncio.sleep(0.200) - await _ensure_asyncio_background_tasks_finished() + await _ensure_background_tasks_finished() value = 1 # calling thrice be more confident about behaviour of parallel execution res2 = await self._call_thrice(lambda: get_value_cached('test', kwarg='args')) @@ -104,10 +104,10 @@ async def get_value(arg, kwarg=None): # when res1 = await get_value_cached('test', kwarg='args') await asyncio.sleep(0.200) - await _ensure_asyncio_background_tasks_finished() + await _ensure_background_tasks_finished() value = 1 await get_value_cached('test', kwarg='args') - await _ensure_asyncio_background_tasks_finished() + await _ensure_background_tasks_finished() # calling thrice be more confident about behaviour of parallel execution res2 = await self._call_thrice(lambda: get_value_cached('test', kwarg='args')) @@ -127,7 +127,7 @@ async def get_value(arg, kwarg=None): # when res1 = await get_value_cached('test1', kwarg='args') await asyncio.sleep(0.200) - await _ensure_asyncio_background_tasks_finished() + await _ensure_background_tasks_finished() value = 1 res2 = await get_value_cached('test2', kwarg='args') @@ -147,7 +147,7 @@ async def get_value(arg, kwarg=None): # when res1 = await get_value_cached('test', kwarg='args1') await asyncio.sleep(0.200) - await _ensure_asyncio_background_tasks_finished() + await _ensure_background_tasks_finished() value = 1 res2 = await get_value_cached('test', kwarg='args2') @@ -174,7 +174,7 @@ async def get_value(arg, kwarg=None): # when res1 = await get_value_cached('test1', kwarg='args1') await asyncio.sleep(0.200) - await _ensure_asyncio_background_tasks_finished() + await _ensure_background_tasks_finished() value = 1 res2 = await get_value_cached('test2', kwarg='args2') @@ -206,7 +206,7 @@ async def get_value(arg, kwarg=None): await get_value_cached('test2', kwarg='args2') await get_value_cached('test3', kwarg='args3') await get_value_cached('test4', kwarg='args4') - await _ensure_asyncio_background_tasks_finished() + await _ensure_background_tasks_finished() # then s1 = await storage.get("('test1', 'args1')") @@ -257,9 +257,9 @@ async def get_value(arg, kwarg=None): async def test_should_throw_exception_on_refresh_timeout(self): # given async def get_value(arg, kwarg=None): - await _ensure_asyncio_background_tasks_finished() + await _ensure_background_tasks_finished() time.sleep(.200) - await _ensure_asyncio_background_tasks_finished() + await _ensure_background_tasks_finished() return 0 get_value_cached = memoize( diff --git a/tests/py310workaround.py b/tests/py310workaround.py index c5cf52e..b14ec15 100644 --- a/tests/py310workaround.py +++ b/tests/py310workaround.py @@ -1,3 +1,4 @@ def fix_python_3_10_compatibility(): - import collections - collections.MutableMapping = collections.abc.MutableMapping # workaround for python 3.10 + pass + # import collections + # collections.MutableMapping = collections.abc.MutableMapping # workaround for python 3.10 diff --git a/tests/unit/test_eviction.py b/tests/unit/test_eviction.py index 1453f61..f44e7ef 100644 --- a/tests/unit/test_eviction.py +++ b/tests/unit/test_eviction.py @@ -8,7 +8,7 @@ from memoize.configuration import MutableCacheConfiguration, DefaultInMemoryCacheConfiguration from memoize.entrybuilder import ProvidedLifeSpanCacheEntryBuilder from memoize.wrapper import memoize -from tests import _assert_called_once_with, AnyObject, _as_future, _ensure_asyncio_background_tasks_finished, \ +from tests import _assert_called_once_with, AnyObject, _as_future, _ensure_background_tasks_finished, \ _ensure_background_tasks_finished @@ -167,14 +167,14 @@ async def sample_method(arg, kwarg=None): return arg, kwarg await sample_method('test', kwarg='args') - await _ensure_asyncio_background_tasks_finished() + await _ensure_background_tasks_finished() time.sleep(.200) eviction_strategy.next_to_release.reset_mock() storage.release.reset_mock() # when await sample_method('test', kwarg='args') - await _ensure_asyncio_background_tasks_finished() + await _ensure_background_tasks_finished() # then eviction_strategy.next_to_release.assert_called_once_with() diff --git a/tox.ini b/tox.ini index 3cc3977..e30c144 100644 --- a/tox.ini +++ b/tox.ini @@ -26,10 +26,10 @@ commands = [gh-actions] -python = +python = # mypy and coverage have separate workflow in GitHub Actions 3.7: py37 3.8: py38 3.9: py39 - 3.10: py310,mypy,coverage + 3.10: py310 3.11: py311 3.12: py312 \ No newline at end of file