123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- from pytest import fixture
- from time import time
- from app.rest.PageCache import PageCache
- from app.rest.CachedLoadingPage import (
- CachedLoadingPage,
- )
- @fixture
- def cache():
- return PageCache(0)
- def test_add(cache: PageCache):
- val = 'test-cached-value'
- key = 'test-key'
- assert cache.add(key, CachedLoadingPage(val, lambda _: None, incremental=False)).value == val
- def test_get(cache: PageCache):
- val = 'test-cached-value'
- key = 'test-key'
- assert cache.get(key) is None
- assert cache.add(key, CachedLoadingPage(val, lambda q: q.put('next-val'), incremental=False)).value == val
- assert cache.get(key).value == 'next-val'
- def test_remove(cache: PageCache):
- val = 'test-cached-value'
- key = 'test-key'
- assert cache.get(key) is None
- assert cache.add(key, CachedLoadingPage(val, lambda q: q.put('next-val'), incremental=False)).value == val
- cache.remove(key)
- assert cache.get(key) is None
- def test_enforce_limit(cache: PageCache):
- val = 'test-cached-value'
- key = 'test-key'
- assert cache.add(key, CachedLoadingPage(val, lambda _: None, incremental=False)).value == val
-
- assert cache.add('other', CachedLoadingPage(val, lambda _: None, incremental=False)).value == val
- assert cache.get(key) is None
- assert cache.get('other').value == val
- def test_clean_stale(cache: PageCache):
- val = 'test-cached-value'
- key = 'test-key'
- page = CachedLoadingPage(val, lambda _: None, incremental=False)
- page._created = time() - 10*60
-
- assert cache.add(key, page).value == val
- assert cache.get(key) is None
-
- page = CachedLoadingPage(val, lambda _: None, incremental=False)
- assert cache.add(key, page).value == val
-
- page._created = time() - 10*60
- assert cache.get(key) is None
- page = CachedLoadingPage(val, lambda _: None, incremental=False)
- assert cache.add(key, page).value == val
-
- page._created = time() - 10*60
-
- assert cache.add('other', CachedLoadingPage(val, lambda _: None, incremental=False)).value == val
- assert cache.get(key) is None
- assert cache.get('other').value == val
|