1
0
mirror of https://github.com/quay/quay.git synced 2026-01-27 18:42:52 +03:00
Files
quay/data/cache/test/test_cache.py
Joseph Schorr 8ff86175b8 Fix handling of client connections in pymemcache cache (#433)
pymemcache is apparently not thread safe, so our reuse of the client
was causing the occasional hang on read. This change has us open a new
connection per request, and then close it once complete. We also make
sure to close the DB connection before making the memcache connection,
in case the memcache connection takes a bit of time

We should investigate switching to the PooledClient library for reusing
connections (safely) once we verify this change works
2020-06-10 13:19:23 -04:00

67 lines
1.8 KiB
Python

import pytest
from mock import patch
from data.cache import InMemoryDataModelCache, NoopDataModelCache, MemcachedModelCache
from data.cache.cache_key import CacheKey
DATA = {}
class MockClient(object):
def __init__(self, server, **kwargs):
pass
def get(self, key, default=None):
return DATA.get(key, default)
def set(self, key, value, expire=None):
DATA[key] = value
def close(self):
pass
@pytest.mark.parametrize("cache_type", [(NoopDataModelCache), (InMemoryDataModelCache),])
def test_caching(cache_type):
key = CacheKey("foo", "60m")
cache = cache_type()
# Perform two retrievals, and make sure both return.
assert cache.retrieve(key, lambda: {"a": 1234}) == {"a": 1234}
assert cache.retrieve(key, lambda: {"a": 1234}) == {"a": 1234}
def test_memcache():
global DATA
DATA = {}
key = CacheKey("foo", "60m")
with patch("data.cache.impl.Client", MockClient):
cache = MemcachedModelCache(("127.0.0.1", "-1"))
assert cache.retrieve(key, lambda: {"a": 1234}) == {"a": 1234}
assert cache.retrieve(key, lambda: {"a": 1234}) == {"a": 1234}
def test_memcache_should_cache():
global DATA
DATA = {}
key = CacheKey("foo", None)
def sc(value):
return value["a"] != 1234
with patch("data.cache.impl.Client", MockClient):
cache = MemcachedModelCache(("127.0.0.1", "-1"))
assert cache.retrieve(key, lambda: {"a": 1234}, should_cache=sc) == {"a": 1234}
# Ensure not cached since it was `1234`.
assert cache._get_client().get(key.key) is None
# Ensure cached.
assert cache.retrieve(key, lambda: {"a": 2345}, should_cache=sc) == {"a": 2345}
assert cache._get_client().get(key.key) is not None
assert cache.retrieve(key, lambda: {"a": 2345}, should_cache=sc) == {"a": 2345}