|
| 1 | +""" |
| 2 | +Cache-behavior tests for the nested-access-group membership map (#28032). |
| 3 | +
|
| 4 | +Hot-path callers go through get_cached_group_memberships() which TTL-caches |
| 5 | +get_group_memberships_from_db() and is invalidated by every membership |
| 6 | +write. These tests pin the cache hit/miss/invalidation semantics so the |
| 7 | +optimization can't silently break later. |
| 8 | +""" |
| 9 | + |
| 10 | +import os |
| 11 | +import sys |
| 12 | +from types import SimpleNamespace |
| 13 | +from unittest.mock import AsyncMock, MagicMock |
| 14 | + |
| 15 | +sys.path.insert(0, os.path.abspath("../../..")) |
| 16 | + |
| 17 | +import pytest |
| 18 | + |
| 19 | +import litellm.proxy.management_endpoints.model_access_group_management_endpoints as mgmt |
| 20 | +from litellm.proxy.management_endpoints.model_access_group_management_endpoints import ( |
| 21 | + delete_group_membership_edges, |
| 22 | + get_cached_group_memberships, |
| 23 | + invalidate_group_memberships_cache, |
| 24 | + upsert_group_memberships, |
| 25 | +) |
| 26 | + |
| 27 | + |
| 28 | +def _row(parent: str, child: str) -> SimpleNamespace: |
| 29 | + return SimpleNamespace(parent_group=parent, child_group=child) |
| 30 | + |
| 31 | + |
| 32 | +def _make_prisma(membership_rows=None): |
| 33 | + membership_rows = membership_rows or [] |
| 34 | + db = MagicMock() |
| 35 | + db.litellm_accessgroupmembership = MagicMock() |
| 36 | + db.litellm_accessgroupmembership.find_many = AsyncMock(return_value=membership_rows) |
| 37 | + db.litellm_accessgroupmembership.create_many = AsyncMock(return_value=0) |
| 38 | + db.litellm_accessgroupmembership.delete_many = AsyncMock(return_value=0) |
| 39 | + client = MagicMock() |
| 40 | + client.db = db |
| 41 | + return client |
| 42 | + |
| 43 | + |
| 44 | +@pytest.fixture(autouse=True) |
| 45 | +def _reset_cache_between_tests(): |
| 46 | + """Module-level cache state must not leak between tests.""" |
| 47 | + invalidate_group_memberships_cache() |
| 48 | + yield |
| 49 | + invalidate_group_memberships_cache() |
| 50 | + |
| 51 | + |
| 52 | +@pytest.mark.asyncio |
| 53 | +async def test_cache_miss_then_hit_avoids_second_db_query(): |
| 54 | + prisma = _make_prisma(membership_rows=[_row("project-x", "image")]) |
| 55 | + |
| 56 | + first = await get_cached_group_memberships(prisma_client=prisma) |
| 57 | + second = await get_cached_group_memberships(prisma_client=prisma) |
| 58 | + |
| 59 | + assert first == second == {"project-x": ["image"]} |
| 60 | + # Only the first call should hit the DB |
| 61 | + prisma.db.litellm_accessgroupmembership.find_many.assert_awaited_once() |
| 62 | + |
| 63 | + |
| 64 | +@pytest.mark.asyncio |
| 65 | +async def test_cache_invalidation_forces_db_refetch(): |
| 66 | + prisma = _make_prisma(membership_rows=[_row("project-x", "image")]) |
| 67 | + |
| 68 | + await get_cached_group_memberships(prisma_client=prisma) |
| 69 | + invalidate_group_memberships_cache() |
| 70 | + await get_cached_group_memberships(prisma_client=prisma) |
| 71 | + |
| 72 | + assert prisma.db.litellm_accessgroupmembership.find_many.await_count == 2 |
| 73 | + |
| 74 | + |
| 75 | +@pytest.mark.asyncio |
| 76 | +async def test_upsert_invalidates_cache(): |
| 77 | + """Writing edges must drop the cache so the next read sees the change.""" |
| 78 | + prisma = _make_prisma(membership_rows=[_row("project-x", "image")]) |
| 79 | + prisma.db.litellm_accessgroupmembership.create_many = AsyncMock(return_value=1) |
| 80 | + |
| 81 | + await get_cached_group_memberships(prisma_client=prisma) # populates cache |
| 82 | + await upsert_group_memberships( |
| 83 | + parent_group="project-x", |
| 84 | + child_groups=["reasoning"], |
| 85 | + prisma_client=prisma, |
| 86 | + ) |
| 87 | + await get_cached_group_memberships(prisma_client=prisma) # must re-fetch |
| 88 | + |
| 89 | + assert prisma.db.litellm_accessgroupmembership.find_many.await_count == 2 |
| 90 | + |
| 91 | + |
| 92 | +@pytest.mark.asyncio |
| 93 | +async def test_delete_edges_invalidates_cache(): |
| 94 | + """Deleting edges must drop the cache too.""" |
| 95 | + prisma = _make_prisma(membership_rows=[_row("project-x", "image")]) |
| 96 | + prisma.db.litellm_accessgroupmembership.delete_many = AsyncMock(return_value=1) |
| 97 | + |
| 98 | + await get_cached_group_memberships(prisma_client=prisma) |
| 99 | + await delete_group_membership_edges(access_group="project-x", prisma_client=prisma) |
| 100 | + await get_cached_group_memberships(prisma_client=prisma) |
| 101 | + |
| 102 | + assert prisma.db.litellm_accessgroupmembership.find_many.await_count == 2 |
| 103 | + |
| 104 | + |
| 105 | +@pytest.mark.asyncio |
| 106 | +async def test_cache_expires_after_ttl(monkeypatch): |
| 107 | + """When monotonic time advances past the TTL, the next read re-fetches.""" |
| 108 | + prisma = _make_prisma(membership_rows=[_row("project-x", "image")]) |
| 109 | + |
| 110 | + # Freeze time; advance past TTL between calls |
| 111 | + now = [1000.0] |
| 112 | + monkeypatch.setattr(mgmt.time, "monotonic", lambda: now[0]) |
| 113 | + |
| 114 | + await get_cached_group_memberships(prisma_client=prisma) |
| 115 | + now[0] += mgmt._MEMBERSHIPS_CACHE_TTL_SECONDS + 1 |
| 116 | + await get_cached_group_memberships(prisma_client=prisma) |
| 117 | + |
| 118 | + assert prisma.db.litellm_accessgroupmembership.find_many.await_count == 2 |
| 119 | + |
| 120 | + |
| 121 | +@pytest.mark.asyncio |
| 122 | +async def test_cache_within_ttl_does_not_refetch(monkeypatch): |
| 123 | + """Reads inside the TTL window stay served from cache.""" |
| 124 | + prisma = _make_prisma(membership_rows=[_row("project-x", "image")]) |
| 125 | + |
| 126 | + now = [1000.0] |
| 127 | + monkeypatch.setattr(mgmt.time, "monotonic", lambda: now[0]) |
| 128 | + |
| 129 | + await get_cached_group_memberships(prisma_client=prisma) |
| 130 | + now[0] += mgmt._MEMBERSHIPS_CACHE_TTL_SECONDS - 1 |
| 131 | + await get_cached_group_memberships(prisma_client=prisma) |
| 132 | + |
| 133 | + prisma.db.litellm_accessgroupmembership.find_many.assert_awaited_once() |
| 134 | + |
| 135 | + |
| 136 | +@pytest.mark.asyncio |
| 137 | +async def test_cache_falls_through_empty_dict_on_error_path(): |
| 138 | + """When the underlying helper returns {} due to a DB error, the cache |
| 139 | + still stores it - we don't want to retry on every single request.""" |
| 140 | + prisma = _make_prisma() |
| 141 | + prisma.db.litellm_accessgroupmembership.find_many = AsyncMock( |
| 142 | + side_effect=ConnectionError("postgres unreachable") |
| 143 | + ) |
| 144 | + |
| 145 | + first = await get_cached_group_memberships(prisma_client=prisma) |
| 146 | + second = await get_cached_group_memberships(prisma_client=prisma) |
| 147 | + |
| 148 | + assert first == second == {} |
| 149 | + # Only one DB attempt; subsequent calls served from the cached {} |
| 150 | + prisma.db.litellm_accessgroupmembership.find_many.assert_awaited_once() |
0 commit comments