| 1 |
""" |
| 2 |
tests.cache |
| 3 |
~~~~~~~~~~~ |
| 4 |
|
| 5 |
Tests the cache system |
| 6 |
|
| 7 |
:copyright: (c) 2014 by Armin Ronacher. |
| 8 |
:license: BSD, see LICENSE for more details. |
| 9 |
""" |
| 10 |
|
| 11 |
import pickle |
| 12 |
import time |
| 13 |
|
| 14 |
import pytest |
| 15 |
|
| 16 |
from flask_caching import backends |
| 17 |
from flask_caching.backends import RedisSentinelCache |
| 18 |
|
| 19 |
try: |
| 20 |
import redis |
| 21 |
except ImportError: |
| 22 |
redis = None |
| 23 |
|
| 24 |
try: |
| 25 |
import pylibmc as memcache |
| 26 |
except ImportError: |
| 27 |
try: |
| 28 |
from google.appengine.api import memcache |
| 29 |
except ImportError: |
| 30 |
try: |
| 31 |
import memcache |
| 32 |
except ImportError: |
| 33 |
memcache = None |
| 34 |
|
| 35 |
|
| 36 |
class CacheTestsBase: |
| 37 |
_can_use_fast_sleep = True |
| 38 |
_guaranteed_deletes = True |
| 39 |
|
| 40 |
@pytest.fixture |
| 41 |
def make_cache(self): |
| 42 |
"""Return a cache class or factory.""" |
| 43 |
raise NotImplementedError() |
| 44 |
|
| 45 |
@pytest.fixture |
| 46 |
def c(self, make_cache): |
| 47 |
"""Return a cache instance.""" |
| 48 |
return make_cache() |
| 49 |
|
| 50 |
@pytest.fixture |
| 51 |
def sleep(self, clock): |
| 52 |
"""Stay a while and listen...""" |
| 53 |
return clock.advance if self._can_use_fast_sleep else time.sleep |
| 54 |
|
| 55 |
|
| 56 |
class GenericCacheTests(CacheTestsBase): |
| 57 |
def test_generic_get_dict(self, c): |
| 58 |
assert c.set("a", "a") |
| 59 |
assert c.set("b", "b") |
| 60 |
d = c.get_dict("a", "b") |
| 61 |
assert "a" in d |
| 62 |
assert "a" == d["a"] |
| 63 |
assert "b" in d |
| 64 |
assert "b" == d["b"] |
| 65 |
|
| 66 |
def test_generic_set_get(self, c): |
| 67 |
for i in range(3): |
| 68 |
assert c.set(str(i), i * i) |
| 69 |
|
| 70 |
for i in range(3): |
| 71 |
result = c.get(str(i)) |
| 72 |
assert result == i * i, result |
| 73 |
|
| 74 |
def test_generic_get_set(self, c): |
| 75 |
assert c.set("foo", ["bar"]) |
| 76 |
assert c.get("foo") == ["bar"] |
| 77 |
|
| 78 |
def test_generic_get_many(self, c): |
| 79 |
assert c.set("foo", ["bar"]) |
| 80 |
assert c.set("spam", "eggs") |
| 81 |
assert c.get_many("foo", "spam") == [["bar"], "eggs"] |
| 82 |
|
| 83 |
def test_generic_set_many(self, c): |
| 84 |
assert c.set_many({"foo": "bar", "spam": ["eggs"]}) |
| 85 |
assert c.get("foo") == "bar" |
| 86 |
assert c.get("spam") == ["eggs"] |
| 87 |
|
| 88 |
def test_generic_add(self, c): |
| 89 |
# sanity check that add() works like set() |
| 90 |
assert c.add("foo", "bar") |
| 91 |
assert c.get("foo") == "bar" |
| 92 |
assert not c.add("foo", "qux") |
| 93 |
assert c.get("foo") == "bar" |
| 94 |
|
| 95 |
def test_generic_delete(self, c): |
| 96 |
assert c.add("foo", "bar") |
| 97 |
assert c.get("foo") == "bar" |
| 98 |
assert c.delete("foo") |
| 99 |
assert c.get("foo") is None |
| 100 |
|
| 101 |
def test_generic_delete_many(self, c): |
| 102 |
assert c.add("foo", "bar") |
| 103 |
assert c.add("spam", "eggs") |
| 104 |
assert c.delete_many("foo", "spam") |
| 105 |
assert c.get("foo") is None |
| 106 |
assert c.get("spam") is None |
| 107 |
|
| 108 |
def test_generic_inc_dec(self, c): |
| 109 |
assert c.set("foo", 1) |
| 110 |
assert c.inc("foo") == c.get("foo") == 2 |
| 111 |
assert c.dec("foo") == c.get("foo") == 1 |
| 112 |
assert c.delete("foo") |
| 113 |
|
| 114 |
def test_generic_true_false(self, c): |
| 115 |
assert c.set("foo", True) |
| 116 |
assert c.get("foo") in (True, 1) |
| 117 |
assert c.set("bar", False) |
| 118 |
assert c.get("bar") in (False, 0) |
| 119 |
|
| 120 |
def test_generic_timeout(self, c, sleep): |
| 121 |
c.set("foo", "bar", 0) |
| 122 |
assert c.get("foo") == "bar" |
| 123 |
c.set("baz", "qux", 1) |
| 124 |
assert c.get("baz") == "qux" |
| 125 |
sleep(3) |
| 126 |
# timeout of zero means no timeout |
| 127 |
assert c.get("foo") == "bar" |
| 128 |
if self._guaranteed_deletes: |
| 129 |
assert c.get("baz") is None |
| 130 |
|
| 131 |
def test_generic_has(self, c): |
| 132 |
assert c.has("foo") in (False, 0) |
| 133 |
assert c.has("spam") in (False, 0) |
| 134 |
assert c.set("foo", "bar") |
| 135 |
assert c.has("foo") in (True, 1) |
| 136 |
assert c.has("spam") in (False, 0) |
| 137 |
c.delete("foo") |
| 138 |
assert c.has("foo") in (False, 0) |
| 139 |
assert c.has("spam") in (False, 0) |
| 140 |
|
| 141 |
def test_generic_get_bytes(self, c): |
| 142 |
assert c.set("foo", b"bar") |
| 143 |
assert c.get("foo") == b"bar" |
| 144 |
|
| 145 |
|
| 146 |
class TestSimpleCache(GenericCacheTests): |
| 147 |
@pytest.fixture |
| 148 |
def make_cache(self): |
| 149 |
return backends.SimpleCache |
| 150 |
|
| 151 |
def test_purge(self): |
| 152 |
c = backends.SimpleCache(threshold=2) |
| 153 |
c.set("a", "a") |
| 154 |
c.set("b", "b") |
| 155 |
c.set("c", "c") |
| 156 |
c.set("d", "d") |
| 157 |
# Cache purges old items *before* it sets new ones. |
| 158 |
assert len(c._cache) == 3 |
| 159 |
|
| 160 |
def test_deprecated_ignore_errors(self): |
| 161 |
with pytest.warns(DeprecationWarning, match="ignore_errors"): |
| 162 |
c = backends.SimpleCache(ignore_errors=True) |
| 163 |
assert c.ignore_delete_many_errors is True |
| 164 |
|
| 165 |
def test_ignore_delete_many_errors(self): |
| 166 |
c = backends.SimpleCache(ignore_delete_many_errors=True) |
| 167 |
assert c.ignore_delete_many_errors is True |
| 168 |
|
| 169 |
|
| 170 |
class TestFileSystemCache(GenericCacheTests): |
| 171 |
@pytest.fixture |
| 172 |
def make_cache(self, tmpdir): |
| 173 |
return lambda **kw: backends.FileSystemCache(cache_dir=str(tmpdir), **kw) |
| 174 |
|
| 175 |
def test_deprecated_ignore_errors(self, tmpdir): |
| 176 |
with pytest.warns(DeprecationWarning, match="ignore_errors"): |
| 177 |
c = backends.FileSystemCache(cache_dir=str(tmpdir), ignore_errors=True) |
| 178 |
assert c.ignore_delete_many_errors is True |
| 179 |
|
| 180 |
def test_ignore_delete_many_errors(self, tmpdir): |
| 181 |
c = backends.FileSystemCache( |
| 182 |
cache_dir=str(tmpdir), ignore_delete_many_errors=True |
| 183 |
) |
| 184 |
assert c.ignore_delete_many_errors is True |
| 185 |
|
| 186 |
|
| 187 |
# don't use pytest.mark.skipif on subclasses |
| 188 |
# https://bitbucket.org/hpk42/pytest/issue/568 |
| 189 |
# skip happens in requirements fixture instead |
| 190 |
class TestRedisCache(GenericCacheTests): |
| 191 |
_can_use_fast_sleep = False |
| 192 |
|
| 193 |
def gen_key_prefix(self): |
| 194 |
return "werkzeug-test-case:" |
| 195 |
|
| 196 |
@pytest.fixture(scope="class", autouse=True) |
| 197 |
@classmethod |
| 198 |
def requirements(cls, redis_server): |
| 199 |
pass |
| 200 |
|
| 201 |
@pytest.fixture(params=(None, False, True, gen_key_prefix)) |
| 202 |
def make_cache(self, request): |
| 203 |
key_prefix = "werkzeug-test-case:" |
| 204 |
if request.param is None: |
| 205 |
host = "localhost" |
| 206 |
elif request.param: |
| 207 |
host = redis.StrictRedis(port=6360) |
| 208 |
elif callable(request.param): |
| 209 |
key_prefix = gen_key_prefix # noqa (flake8 error: undefined) |
| 210 |
host = redis.Redis(port=6360) |
| 211 |
else: |
| 212 |
host = redis.Redis(port=6360) |
| 213 |
|
| 214 |
c = backends.RedisCache(host=host, key_prefix=key_prefix, port=6360) |
| 215 |
yield lambda: c |
| 216 |
c.clear() |
| 217 |
|
| 218 |
def test_compat(self, c): |
| 219 |
assert c._write_client.set(c.key_prefix + "foo", "Awesome") |
| 220 |
assert c.get("foo") == b"Awesome" |
| 221 |
assert c._write_client.set(c.key_prefix + "foo", "42") |
| 222 |
assert c.get("foo") == 42 |
| 223 |
|
| 224 |
def test_empty_host(self): |
| 225 |
with pytest.raises(ValueError) as exc_info: |
| 226 |
backends.RedisCache(host=None) |
| 227 |
assert str(exc_info.value) == "RedisCache host parameter may not be None" |
| 228 |
|
| 229 |
|
| 230 |
class TestRedisCacheClientsOverride(CacheTestsBase): |
| 231 |
_can_use_fast_sleep = False |
| 232 |
|
| 233 |
@pytest.fixture() |
| 234 |
def make_cache(self, request): |
| 235 |
c = RedisSentinelCache() |
| 236 |
yield lambda: c |
| 237 |
|
| 238 |
def test_client_override_reflected_on_cachelib_methods(self, c): |
| 239 |
EXPECTED_GET_MANY_VALUES = ["bacon", "spam", "eggs"] |
| 240 |
|
| 241 |
class DummyWriteClient: |
| 242 |
# cachelib < 0.15 uses setex(), >= 0.15 uses set(ex=...) |
| 243 |
def set(self, *args, **kwargs): |
| 244 |
return "spam" |
| 245 |
|
| 246 |
def setex(self, *args, **kwargs): |
| 247 |
return "spam" |
| 248 |
|
| 249 |
class DummyReadClient: |
| 250 |
def mget(self, *args, **kwargs): |
| 251 |
values = [ |
| 252 |
b"!" + pickle.dumps(v, pickle.HIGHEST_PROTOCOL) |
| 253 |
for v in EXPECTED_GET_MANY_VALUES |
| 254 |
] |
| 255 |
return values |
| 256 |
|
| 257 |
c._write_client = DummyWriteClient() |
| 258 |
c._read_client = DummyReadClient() |
| 259 |
actual_values = c.get_many("foo") |
| 260 |
assert c.set("bacon", "eggs") == "spam" |
| 261 |
for actual, expected in zip( |
| 262 |
actual_values, EXPECTED_GET_MANY_VALUES, strict=False |
| 263 |
): |
| 264 |
assert actual == expected |
| 265 |
|
| 266 |
|
| 267 |
class TestMemcachedCache(GenericCacheTests): |
| 268 |
_can_use_fast_sleep = False |
| 269 |
_guaranteed_deletes = False |
| 270 |
|
| 271 |
@pytest.fixture(scope="class", autouse=True) |
| 272 |
@classmethod |
| 273 |
def requirements(cls, memcache_server): |
| 274 |
pass |
| 275 |
|
| 276 |
@pytest.fixture |
| 277 |
def make_cache(self): |
| 278 |
c = backends.MemcachedCache( |
| 279 |
servers=["127.0.0.1:11212"], key_prefix="werkzeug-test-case:" |
| 280 |
) |
| 281 |
yield lambda: c |
| 282 |
c.clear() |
| 283 |
|
| 284 |
def test_compat(self, c): |
| 285 |
assert c.set("foo", "bar") |
| 286 |
assert c.get("foo") == "bar" |
| 287 |
|
| 288 |
def test_huge_timeouts(self, c): |
| 289 |
# Timeouts greater than epoch are interpreted as POSIX timestamps |
| 290 |
# (i.e. not relative to now, but relative to epoch) |
| 291 |
epoch = 2592000 |
| 292 |
c.set("foo", "bar", epoch + 100) |
| 293 |
assert c.get("foo") == "bar" |
| 294 |
|
| 295 |
def test_timeouts(self, c): |
| 296 |
c.set("foo", "bar", 1) |
| 297 |
assert c.get("foo") == "bar" |
| 298 |
time.sleep(3) |
| 299 |
assert c.has("foo") is False |
| 300 |
|
| 301 |
|
| 302 |
class TestNullCache(CacheTestsBase): |
| 303 |
@pytest.fixture(scope="class", autouse=True) |
| 304 |
@classmethod |
| 305 |
def make_cache(cls): |
| 306 |
return backends.NullCache |
| 307 |
|
| 308 |
def test_has(self, c): |
| 309 |
assert not c.has("foo") |