|
| 1 | +import json |
| 2 | +import time |
| 3 | +import threading |
| 4 | +from contextlib import contextmanager |
| 5 | + |
| 6 | +import pytest |
| 7 | +from redis.exceptions import LockError |
| 8 | + |
| 9 | +import common.utils as cu |
| 10 | +from common.utils import ( |
| 11 | + get_contentprovider_records, |
| 12 | + get_or_create_contentprovider_lookup, |
| 13 | + CONTENTPROVIDER_RECORDS_KEY, |
| 14 | +) |
| 15 | + |
| 16 | +RECORDS = [ |
| 17 | + {"name": "Université de Lausanne", "internal_name": "ftunivlausanne"}, |
| 18 | + {"name": "Some Repo", "internal_name": "ftsomerepo"}, |
| 19 | +] |
| 20 | + |
| 21 | + |
| 22 | +# --- Minimal in-memory Redis double supporting get / set(ex) / lock ----------- |
| 23 | + |
| 24 | +class FakeRedis: |
| 25 | + def __init__(self, fail_lock=False): |
| 26 | + self.store = {} |
| 27 | + self.last_ex = None |
| 28 | + self._fail_lock = fail_lock |
| 29 | + self._lock = threading.Lock() |
| 30 | + |
| 31 | + def get(self, key): |
| 32 | + return self.store.get(key) |
| 33 | + |
| 34 | + def set(self, key, value, ex=None): |
| 35 | + self.store[key] = value |
| 36 | + self.last_ex = ex |
| 37 | + |
| 38 | + @contextmanager |
| 39 | + def lock(self, name, timeout=None, blocking_timeout=None): |
| 40 | + if self._fail_lock: |
| 41 | + raise LockError("could not acquire lock") |
| 42 | + acquired = self._lock.acquire( |
| 43 | + timeout=blocking_timeout if blocking_timeout is not None else -1 |
| 44 | + ) |
| 45 | + if not acquired: |
| 46 | + raise LockError("lock acquisition timed out") |
| 47 | + try: |
| 48 | + yield |
| 49 | + finally: |
| 50 | + self._lock.release() |
| 51 | + |
| 52 | + |
| 53 | +# --- get_contentprovider_records --------------------------------------------- |
| 54 | + |
| 55 | +def test_cache_hit_skips_producer(): |
| 56 | + r = FakeRedis() |
| 57 | + r.set(CONTENTPROVIDER_RECORDS_KEY, json.dumps(RECORDS)) |
| 58 | + calls = [] |
| 59 | + |
| 60 | + out = get_contentprovider_records(r, lambda: calls.append(1) or []) |
| 61 | + |
| 62 | + assert out == RECORDS |
| 63 | + assert calls == [] # producer never called on a warm cache |
| 64 | + |
| 65 | + |
| 66 | +def test_cache_miss_produces_and_caches_with_ttl(): |
| 67 | + r = FakeRedis() |
| 68 | + calls = [] |
| 69 | + |
| 70 | + def produce(): |
| 71 | + calls.append(1) |
| 72 | + return RECORDS |
| 73 | + |
| 74 | + out = get_contentprovider_records(r, produce, ttl=1234) |
| 75 | + |
| 76 | + assert out == RECORDS |
| 77 | + assert calls == [1] |
| 78 | + assert json.loads(r.store[CONTENTPROVIDER_RECORDS_KEY]) == RECORDS |
| 79 | + assert r.last_ex == 1234 |
| 80 | + |
| 81 | + |
| 82 | +def test_single_producer_under_contention(): |
| 83 | + r = FakeRedis() |
| 84 | + counter = {"n": 0} |
| 85 | + counter_lock = threading.Lock() |
| 86 | + |
| 87 | + def produce(): |
| 88 | + with counter_lock: |
| 89 | + counter["n"] += 1 |
| 90 | + time.sleep(0.2) # hold the lock long enough for others to contend |
| 91 | + return RECORDS |
| 92 | + |
| 93 | + results = [] |
| 94 | + |
| 95 | + def worker(): |
| 96 | + results.append(get_contentprovider_records(r, produce)) |
| 97 | + |
| 98 | + threads = [threading.Thread(target=worker) for _ in range(5)] |
| 99 | + for t in threads: |
| 100 | + t.start() |
| 101 | + for t in threads: |
| 102 | + t.join() |
| 103 | + |
| 104 | + assert counter["n"] == 1 # exactly one fetch despite 5 concurrent callers |
| 105 | + assert all(res == RECORDS for res in results) |
| 106 | + |
| 107 | + |
| 108 | +def test_lock_contention_waits_for_published_cache(): |
| 109 | + r = FakeRedis(fail_lock=True) # this caller can never acquire the lock |
| 110 | + |
| 111 | + def publish_later(): |
| 112 | + time.sleep(0.1) |
| 113 | + r.set(CONTENTPROVIDER_RECORDS_KEY, json.dumps(RECORDS)) |
| 114 | + |
| 115 | + t = threading.Thread(target=publish_later) |
| 116 | + t.start() |
| 117 | + |
| 118 | + def produce_should_not_run(): |
| 119 | + raise AssertionError("producer must not run when the lock is held elsewhere") |
| 120 | + |
| 121 | + out = get_contentprovider_records(r, produce_should_not_run, poll_timeout=5) |
| 122 | + t.join() |
| 123 | + |
| 124 | + assert out == RECORDS |
| 125 | + |
| 126 | + |
| 127 | +def test_producer_error_falls_back_to_bundled(): |
| 128 | + r = FakeRedis() |
| 129 | + |
| 130 | + def produce(): |
| 131 | + raise RuntimeError("boom") |
| 132 | + |
| 133 | + out = get_contentprovider_records(r, produce) |
| 134 | + |
| 135 | + assert isinstance(out, list) and len(out) > 0 |
| 136 | + assert {"name", "internal_name"} <= set(out[0].keys()) |
| 137 | + |
| 138 | + |
| 139 | +def test_corrupt_cache_value_is_reproduced(): |
| 140 | + r = FakeRedis() |
| 141 | + r.set(CONTENTPROVIDER_RECORDS_KEY, "not-json{") |
| 142 | + |
| 143 | + out = get_contentprovider_records(r, lambda: RECORDS) |
| 144 | + |
| 145 | + assert out == RECORDS |
| 146 | + |
| 147 | + |
| 148 | +def test_cache_hit_logs_debug_trace(caplog): |
| 149 | + r = FakeRedis() |
| 150 | + r.set(CONTENTPROVIDER_RECORDS_KEY, json.dumps(RECORDS)) |
| 151 | + |
| 152 | + with caplog.at_level("DEBUG"): |
| 153 | + get_contentprovider_records(r, lambda: []) |
| 154 | + |
| 155 | + assert any( |
| 156 | + "contentprovider-cache: served" in rec.message for rec in caplog.records |
| 157 | + ) |
| 158 | + |
| 159 | + |
| 160 | +def test_fallback_logs_warning(caplog): |
| 161 | + r = FakeRedis() |
| 162 | + |
| 163 | + def produce(): |
| 164 | + raise RuntimeError("boom") |
| 165 | + |
| 166 | + with caplog.at_level("WARNING"): |
| 167 | + get_contentprovider_records(r, produce) |
| 168 | + |
| 169 | + assert any( |
| 170 | + rec.levelname == "WARNING" and "bundled fallback" in rec.message |
| 171 | + for rec in caplog.records |
| 172 | + ) |
| 173 | + |
| 174 | + |
| 175 | +# --- get_or_create_contentprovider_lookup (API forward map) ------------------ |
| 176 | + |
| 177 | +def test_get_or_create_builds_forward_lookup(monkeypatch): |
| 178 | + monkeypatch.setattr( |
| 179 | + cu, "get_contentprovider_records", lambda store, fn, **kw: RECORDS |
| 180 | + ) |
| 181 | + |
| 182 | + lookup = get_or_create_contentprovider_lookup() |
| 183 | + |
| 184 | + assert lookup == { |
| 185 | + "ftunivlausanne": "Université de Lausanne", |
| 186 | + "ftsomerepo": "Some Repo", |
| 187 | + } |
0 commit comments