Skip to content

Commit

Permalink
Improve cache testing
Browse files Browse the repository at this point in the history
  • Loading branch information
banesullivan committed Jun 17, 2022
1 parent 3666b66 commit 188ce5f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
9 changes: 5 additions & 4 deletions django_large_image/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
class DjangoCache(BaseCache):
"""Use Django cache as the backing cache for large-image."""

def __init__(self, cache, getsizeof=None):
def __init__(self, cache, alias, getsizeof=None):
super().__init__(0, getsizeof=getsizeof)
self._django_cache = cache
self._alias = alias

def __repr__(self): # pragma: no cover
return f'DjangoCache<{repr(self._django_cache._alias)}>'
return f'DjangoCache<{repr(self._alias)}>'

def __iter__(self): # pragma: no cover
# return invalid iter
Expand Down Expand Up @@ -65,8 +66,8 @@ def getCache(): # noqa: N802
try:
name = getattr(settings, 'LARGE_IMAGE_CACHE_NAME', 'default')
dajngo_cache = caches[name]
except ImproperlyConfigured:
except ImproperlyConfigured: # pragma: no cover
raise TileCacheConfigurationError
cache_lock = threading.Lock()
cache = DjangoCache(dajngo_cache)
cache = DjangoCache(dajngo_cache, alias=name)
return cache, cache_lock
45 changes: 33 additions & 12 deletions project/example/core/tests/test_cache.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,57 @@
from contextlib import contextmanager

from large_image.cache_util.base import BaseCache
import pytest

from django_large_image import tilesource
from django_large_image.cache import DjangoCache


@pytest.fixture
def cache_miss_counter():
@contextmanager
def cache_tracker():
cache, _ = DjangoCache.getCache()
cache.clear()

class Counter:
def __init__(self):
self.count = 0
self.reset()
self.cache = cache

def reset(self):
self.count = 0
self.keys = set()

counter = Counter()

def missing(*args, **kwargs):
def missing(self, key, *args, **kwargs):
counter.count += 1
BaseCache.__missing__(*args, **kwargs)
counter.keys.add(key)
BaseCache.__missing__(self, key, *args, **kwargs)

original = DjangoCache.__missing__
DjangoCache.__missing__ = missing
yield counter
DjangoCache.__missing__ = original


def test_tile(geotiff_path, cache_miss_counter):
def test_cache_tile(geotiff_path):
source = tilesource.get_tilesource_from_path(geotiff_path)
with cache_tracker() as tracker:
_ = source.getTile(0, 0, 0, encoding='PNG')
assert tracker.count == 1
_ = source.getTile(0, 0, 0, encoding='PNG')
assert tracker.count == 1


def test_cache_access(geotiff_path):
source = tilesource.get_tilesource_from_path(geotiff_path)
cache_miss_counter.reset()
# Check size of cache
_ = source.getTile(0, 0, 0, encoding='PNG')
assert cache_miss_counter.count == 1
_ = source.getTile(0, 0, 0, encoding='PNG')
assert cache_miss_counter.count == 1
with cache_tracker() as tracker:
_ = source.getTile(0, 0, 0, encoding='PNG')
assert tracker.count == 1
assert all([k in tracker.cache for k in tracker.keys])
for k in tracker.keys:
del tracker.cache[k]
assert all([k not in tracker.cache for k in tracker.keys])
_ = source.getTile(0, 0, 0, encoding='PNG')
assert tracker.count == 2
assert len(tracker.keys) == 1

0 comments on commit 188ce5f

Please sign in to comment.