| 1 |
import queue |
| 2 |
import threading |
| 3 |
|
| 4 |
import pytest |
| 5 |
from clear import ClearTests |
| 6 |
from common import CommonTests |
| 7 |
from delete_many_with_prefix import DeleteManyWithPrefixTests |
| 8 |
from has import HasTests |
| 9 |
|
| 10 |
from cachelib import MemcachedCache |
| 11 |
|
| 12 |
|
| 13 |
def skip_unless_pooled(cache): |
| 14 |
# only pylibmc's ClientPool has reserve()/queue.Empty semantics; |
| 15 |
# python-memcached and libmc have no capped reservation pool |
| 16 |
if not hasattr(cache._client, "reserve"): |
| 17 |
pytest.skip("memcached client library does not support connection pooling") |
| 18 |
|
| 19 |
|
| 20 |
@pytest.fixture( |
| 21 |
params=["pylibmc", "memcache"], |
| 22 |
ids=["pylibmc", "python-memcached"], |
| 23 |
) |
| 24 |
def memcache_client_lib(request): |
| 25 |
pytest.importorskip(request.param, reason=f"{request.param} not installed") |
| 26 |
return request.param |
| 27 |
|
| 28 |
|
| 29 |
@pytest.fixture(autouse=True) |
| 30 |
def cache_factory(request, key_prefix, memcache_client_lib): |
| 31 |
caches = [] |
| 32 |
|
| 33 |
def _factory(self, *args, **kwargs): |
| 34 |
kwargs.setdefault("servers", ["127.0.0.1:11212"]) |
| 35 |
kwargs.setdefault("key_prefix", key_prefix) |
| 36 |
kwargs.setdefault("memcache_client_lib", memcache_client_lib) |
| 37 |
mc = MemcachedCache(*args, **kwargs) |
| 38 |
mc.clear() |
| 39 |
caches.append(mc) |
| 40 |
return mc |
| 41 |
|
| 42 |
request.cls.cache_factory = _factory |
| 43 |
yield |
| 44 |
for mc in caches: |
| 45 |
# close python-memcached sockets to avoid ResourceWarning noise |
| 46 |
disconnect = getattr(mc._client, "disconnect_all", None) |
| 47 |
if disconnect is not None: |
| 48 |
disconnect() |
| 49 |
|
| 50 |
|
| 51 |
@pytest.mark.network |
| 52 |
@pytest.mark.usefixtures("memcached_server") |
| 53 |
class TestMemcachedCache(CommonTests, ClearTests, HasTests, DeleteManyWithPrefixTests): |
| 54 |
def test_bool_roundtrip(self): |
| 55 |
# pylibmc/libmc flag bool as int on the wire so bools round-trip |
| 56 |
# as 1/0; python-memcached pickles them, preserving bool. Either |
| 57 |
# way the value must compare equal to 1/0 and be int-like. |
| 58 |
cache = self.cache_factory() |
| 59 |
assert cache.set("true-key", True) |
| 60 |
value = cache.get("true-key") |
| 61 |
assert value == 1 |
| 62 |
assert isinstance(value, int) |
| 63 |
assert cache.set("false-key", False) |
| 64 |
value = cache.get("false-key") |
| 65 |
assert value == 0 |
| 66 |
assert isinstance(value, int) |
| 67 |
|
| 68 |
def test_pool_enforces_capacity_and_blocking_waits_for_release(self): |
| 69 |
cache = self.cache_factory(pool_size=2, pool_blocking=True) |
| 70 |
skip_unless_pooled(cache) |
| 71 |
|
| 72 |
acquired = [threading.Event(), threading.Event()] |
| 73 |
release = threading.Event() |
| 74 |
held_clients = [None, None] |
| 75 |
|
| 76 |
def hold_a_slot(index): |
| 77 |
with cache._client_context() as client: |
| 78 |
held_clients[index] = client |
| 79 |
acquired[index].set() |
| 80 |
release.wait(timeout=5) |
| 81 |
|
| 82 |
holders = [threading.Thread(target=hold_a_slot, args=(i,)) for i in range(2)] |
| 83 |
for holder in holders: |
| 84 |
holder.start() |
| 85 |
for event in acquired: |
| 86 |
# wait until both holders have acquired their slots |
| 87 |
assert event.wait(timeout=5), "holder never acquired its slot" |
| 88 |
|
| 89 |
# Two reservations are different from each other, |
| 90 |
# not the same client shared. |
| 91 |
assert held_clients[0] is not held_clients[1] |
| 92 |
|
| 93 |
# pool_size=2 is now fully exhausted, a non-blocking reservation must |
| 94 |
# fail immediately with queue.Empty. |
| 95 |
with pytest.raises(queue.Empty): |
| 96 |
with cache._client.reserve(block=False): |
| 97 |
pass |
| 98 |
|
| 99 |
# With block=True reservation should wait rather than raise or return |
| 100 |
waiter_acquired = threading.Event() |
| 101 |
|
| 102 |
def wait_for_a_slot(): |
| 103 |
with cache._client_context(): |
| 104 |
waiter_acquired.set() |
| 105 |
|
| 106 |
waiter = threading.Thread(target=wait_for_a_slot) |
| 107 |
waiter.start() |
| 108 |
|
| 109 |
# check waiter can't acquire a client before it is released. |
| 110 |
assert not waiter_acquired.wait(timeout=0.2), ( |
| 111 |
"waiter acquired a client before any slot was released" |
| 112 |
) |
| 113 |
|
| 114 |
release.set() # free both held slots |
| 115 |
# wait until the waiter has acquired a slot after the release |
| 116 |
assert waiter_acquired.wait(timeout=5), "waiter never acquired after release" |
| 117 |
|
| 118 |
for holder in holders: |
| 119 |
holder.join() |
| 120 |
waiter.join() |
| 121 |
|
| 122 |
def test_non_blocking_pool_raises_when_exhausted(self): |
| 123 |
cache = self.cache_factory(pool_size=1, pool_blocking=False) |
| 124 |
skip_unless_pooled(cache) |
| 125 |
|
| 126 |
holder_acquired = threading.Event() |
| 127 |
released = threading.Event() |
| 128 |
|
| 129 |
def hold_the_only_client(): |
| 130 |
with cache._client_context(): |
| 131 |
holder_acquired.set() |
| 132 |
released.wait(timeout=5) # keep the single slot busy |
| 133 |
|
| 134 |
holder = threading.Thread(target=hold_the_only_client) |
| 135 |
holder.start() |
| 136 |
holder_acquired.wait( |
| 137 |
timeout=5 |
| 138 |
) # wait until the holder has acquired the only slot |
| 139 |
|
| 140 |
try: |
| 141 |
with pytest.raises(queue.Empty): |
| 142 |
with cache._client_context(): |
| 143 |
pass # 0 free slots left, will raise queue.Empty immediately |
| 144 |
finally: |
| 145 |
released.set() |
| 146 |
holder.join() |
| 147 |
|
| 148 |
# After the holder releases the only slot, this should work |
| 149 |
with cache._client_context() as client: |
| 150 |
assert client is not None |
| 151 |
|
| 152 |
|
| 153 |
class TestMemcachedCacheCheckConnection: |
| 154 |
# the connection check is only implemented for the pylibmc client |
| 155 |
def test_unreachable_server_raises_when_enabled(self): |
| 156 |
pytest.importorskip("pylibmc") |
| 157 |
with pytest.raises(RuntimeError, match="could not connect to memcached"): |
| 158 |
MemcachedCache(servers=["127.0.0.1:1"], check_connection=True) |
| 159 |
|
| 160 |
def test_unreachable_server_is_lazy_by_default(self): |
| 161 |
cache = MemcachedCache(servers=["127.0.0.1:1"]) |
| 162 |
assert isinstance(cache, MemcachedCache) |
| 163 |
|
| 164 |
|
| 165 |
@pytest.mark.network |
| 166 |
@pytest.mark.usefixtures("memcached_server") |
| 167 |
class TestMemcachedCacheCheckConnectionLive: |
| 168 |
def test_reachable_server_constructs_and_works(self): |
| 169 |
cache = MemcachedCache(servers=["127.0.0.1:11212"], check_connection=True) |
| 170 |
assert cache.set("check-connection-key", "value") |
| 171 |
assert cache.get("check-connection-key") == "value" |