|
| 1 | +import threading |
| 2 | +import time |
| 3 | +import unittest |
| 4 | +from datetime import timedelta |
| 5 | + |
| 6 | +import pydantic |
| 7 | + |
| 8 | +from app.lib.cache.cache import BackgroundCache |
| 9 | + |
| 10 | + |
| 11 | +class DummyModel(pydantic.BaseModel): |
| 12 | + value: int |
| 13 | + |
| 14 | + |
| 15 | +class BackgroundCacheTest(unittest.TestCase): |
| 16 | + def test_happy_path_no_change(self): |
| 17 | + value = DummyModel(value=1) |
| 18 | + |
| 19 | + def refresh(): |
| 20 | + return value |
| 21 | + |
| 22 | + cache = BackgroundCache( |
| 23 | + name="test_cache", |
| 24 | + refresh_func=refresh, |
| 25 | + refresh_frequency=timedelta(seconds=0.1), |
| 26 | + refresh_timeout=timedelta(seconds=1), |
| 27 | + ) |
| 28 | + self.assertEqual(cache.get().value, 1) |
| 29 | + t = threading.Thread(target=cache.run, daemon=True) |
| 30 | + t.start() |
| 31 | + time.sleep(0.35) |
| 32 | + for _ in range(3): |
| 33 | + self.assertEqual(cache.get().value, 1) |
| 34 | + cache.stop() |
| 35 | + t.join(timeout=1) |
| 36 | + self.assertEqual(cache.get().value, 1) |
| 37 | + |
| 38 | + def test_happy_path_with_change(self): |
| 39 | + state = {"v": 0} |
| 40 | + |
| 41 | + def refresh(): |
| 42 | + return DummyModel(value=state["v"]) |
| 43 | + |
| 44 | + cache = BackgroundCache( |
| 45 | + name="test_cache", |
| 46 | + refresh_func=refresh, |
| 47 | + refresh_frequency=timedelta(seconds=0.05), |
| 48 | + refresh_timeout=timedelta(seconds=1), |
| 49 | + ) |
| 50 | + self.assertEqual(cache.get().value, 0) |
| 51 | + t = threading.Thread(target=cache.run, daemon=True) |
| 52 | + t.start() |
| 53 | + self.assertEqual(cache.get().value, 0) |
| 54 | + state["v"] = 1 |
| 55 | + time.sleep(0.15) |
| 56 | + self.assertEqual(cache.get().value, 1) |
| 57 | + state["v"] = 2 |
| 58 | + time.sleep(0.15) |
| 59 | + self.assertEqual(cache.get().value, 2) |
| 60 | + cache.stop() |
| 61 | + t.join(timeout=1) |
| 62 | + |
| 63 | + def test_refresh_fails_then_restores_value_unchanged(self): |
| 64 | + call_count = 0 |
| 65 | + |
| 66 | + def refresh(): |
| 67 | + nonlocal call_count |
| 68 | + call_count += 1 |
| 69 | + if call_count == 2: |
| 70 | + raise RuntimeError("transient failure") |
| 71 | + return DummyModel(value=42) |
| 72 | + |
| 73 | + cache = BackgroundCache( |
| 74 | + name="test_cache", |
| 75 | + refresh_func=refresh, |
| 76 | + refresh_frequency=timedelta(seconds=0.05), |
| 77 | + refresh_timeout=timedelta(seconds=1), |
| 78 | + ) |
| 79 | + self.assertEqual(cache.get().value, 42) |
| 80 | + t = threading.Thread(target=cache.run, daemon=True) |
| 81 | + t.start() |
| 82 | + time.sleep(0.2) |
| 83 | + self.assertEqual(cache.get().value, 42) |
| 84 | + cache.stop() |
| 85 | + t.join(timeout=1) |
| 86 | + self.assertEqual(cache.get().value, 42) |
| 87 | + |
| 88 | + def test_refresh_fails_during_init_raises(self): |
| 89 | + def refresh(): |
| 90 | + raise ValueError("init failed") |
| 91 | + |
| 92 | + with self.assertRaises(ValueError) as ctx: |
| 93 | + BackgroundCache( |
| 94 | + name="test_cache", |
| 95 | + refresh_func=refresh, |
| 96 | + refresh_frequency=timedelta(seconds=1), |
| 97 | + refresh_timeout=timedelta(seconds=1), |
| 98 | + ) |
| 99 | + self.assertIn("init failed", str(ctx.exception)) |
0 commit comments