Copy async_lru for Python 3.7 compatibility
This commit is contained in:
parent
2b7188b1b8
commit
18c6ba62bb
15 changed files with 1321 additions and 0 deletions
50
tests/external/test_partialmethod.py
vendored
Normal file
50
tests/external/test_partialmethod.py
vendored
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
import asyncio
|
||||
from functools import partial, partialmethod
|
||||
|
||||
import pytest
|
||||
from great_ai.external.async_lru import alru_cache
|
||||
|
||||
pytestmark = pytest.mark.asyncio
|
||||
|
||||
|
||||
async def test_partialmethod_basic(check_lru, loop):
|
||||
class Obj:
|
||||
async def _coro(self, val):
|
||||
return val
|
||||
|
||||
coro = alru_cache(partialmethod(_coro, 2))
|
||||
|
||||
obj = Obj()
|
||||
|
||||
coros = [obj.coro() for _ in range(5)]
|
||||
|
||||
check_lru(obj.coro, hits=0, misses=0, cache=0, tasks=0)
|
||||
|
||||
ret = await asyncio.gather(*coros)
|
||||
|
||||
check_lru(obj.coro, hits=4, misses=1, cache=1, tasks=0)
|
||||
|
||||
assert ret == [2, 2, 2, 2, 2]
|
||||
|
||||
|
||||
async def test_partialmethod_partial(check_lru, loop):
|
||||
class Obj:
|
||||
def __init__(self):
|
||||
self.coro = alru_cache(partial(self._coro, 2))
|
||||
|
||||
async def __coro(self, val1, val2):
|
||||
return val1 + val2
|
||||
|
||||
_coro = partialmethod(__coro, 1)
|
||||
|
||||
obj = Obj()
|
||||
|
||||
coros = [obj.coro() for _ in range(5)]
|
||||
|
||||
check_lru(obj.coro, hits=0, misses=0, cache=0, tasks=0)
|
||||
|
||||
ret = await asyncio.gather(*coros)
|
||||
|
||||
check_lru(obj.coro, hits=4, misses=1, cache=1, tasks=0)
|
||||
|
||||
assert ret == [3, 3, 3, 3, 3]
|
||||
Loading…
Add table
Add a link
Reference in a new issue