[CHORE] cleanup cache and exception on make test (#1899)

* [CHORE] cleanup cache and exception on `make test`

moved the invalidate_forever task to proper location and use `create_permanent_task`.
and there was a Task never awaited Exception when running tests

* imrpove expiration test

* move cache into utils

* fix issue with pytest_asyncio

s

---------

Co-authored-by: jacksn <jkranawetter05@gmail.com>
This commit is contained in:
dni ⚡ 2023-08-28 11:56:59 +02:00 committed by GitHub
parent 6077edad8f
commit 48f25488df
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 18 deletions

View file

@ -2,23 +2,22 @@ import asyncio
import pytest
from lnbits.cache import Cache
from lnbits.utils.cache import Cache
from tests.conftest import pytest_asyncio
@pytest_asyncio.fixture(scope="session")
async def cache():
cache = Cache()
task = asyncio.create_task(cache.invalidate_forever(interval=0.1))
yield cache
task.cancel()
key = "foo"
value = "bar"
@pytest_asyncio.fixture
async def cache():
cache = Cache(interval=0.1)
task = asyncio.create_task(cache.invalidate_forever())
yield cache
task.cancel()
@pytest.mark.asyncio
async def test_cache_get_set(cache):
cache.set(key, value)
@ -29,8 +28,15 @@ async def test_cache_get_set(cache):
@pytest.mark.asyncio
async def test_cache_expiry(cache):
# gets expired by `get` call
cache.set(key, value, expiry=0.01)
await asyncio.sleep(0.02)
assert not cache.get(key)
# gets expired by invalidation task
cache.set(key, value, expiry=0.1)
await asyncio.sleep(0.2)
assert key not in cache._values
assert not cache.get(key)