Copy async_lru for Python 3.7 compatibility

This commit is contained in:
Andras Schmelczer 2022-07-29 12:57:16 +02:00
parent 2b7188b1b8
commit 18c6ba62bb
No known key found for this signature in database
GPG key ID: 0EA1BC97D0AB076E
15 changed files with 1321 additions and 0 deletions

48
tests/external/test_exception.py vendored Normal file
View file

@ -0,0 +1,48 @@
import asyncio
import pytest
from great_ai.external.async_lru import alru_cache
pytestmark = pytest.mark.asyncio
async def test_alru_cache_exception(check_lru, loop):
@alru_cache(cache_exceptions=True)
async def coro(val):
1 / 0
inputs = [1, 1, 1]
coros = [coro(v) for v in inputs]
ret = await asyncio.gather(*coros, return_exceptions=True)
check_lru(coro, hits=2, misses=1, cache=1, tasks=0)
for item in ret:
assert isinstance(item, ZeroDivisionError)
with pytest.raises(ZeroDivisionError):
await coro(1)
check_lru(coro, hits=3, misses=1, cache=1, tasks=0)
async def test_alru_not_cache_exception(check_lru, loop):
@alru_cache(cache_exceptions=False)
async def coro(val):
1 / 0
inputs = [1, 1, 1]
coros = [coro(v) for v in inputs]
ret = await asyncio.gather(*coros, return_exceptions=True)
check_lru(coro, hits=2, misses=1, cache=1, tasks=0)
for item in ret:
assert isinstance(item, ZeroDivisionError)
with pytest.raises(ZeroDivisionError):
await coro(1)
check_lru(coro, hits=2, misses=2, cache=1, tasks=0)