Skip to content

Commit

Permalink
minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
zmumi committed May 6, 2024
1 parent 7e8bc51 commit f950fa8
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 48 deletions.
2 changes: 1 addition & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[mypy]
no_implicit_optional=False
no_implicit_optional=False
8 changes: 1 addition & 7 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)):
Expand Down
14 changes: 7 additions & 7 deletions tests/end2end/test_showcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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')
Expand Down
28 changes: 14 additions & 14 deletions tests/end2end/test_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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'))
Expand All @@ -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'))
Expand All @@ -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'))
Expand All @@ -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'))

Expand All @@ -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')

Expand All @@ -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')

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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')

Expand Down Expand Up @@ -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')")
Expand Down Expand Up @@ -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
Expand Down
24 changes: 12 additions & 12 deletions tests/end2end/test_wrapper_manually_applied.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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'))
Expand All @@ -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'))
Expand All @@ -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'))
Expand All @@ -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'))

Expand All @@ -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')

Expand All @@ -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')

Expand All @@ -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')

Expand Down Expand Up @@ -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')")
Expand Down Expand Up @@ -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(
Expand Down
5 changes: 3 additions & 2 deletions tests/py310workaround.py
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions tests/unit/test_eviction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit f950fa8

Please sign in to comment.