Skip to content

test(auth): freeze cache clock in auth prefetch behavior tests - #41060

Closed
deepanshupal wants to merge 1 commit into
BerriAI:mainfrom
deepanshupal:fix/prefetch-test-frozen-clock
Closed

deepanshupal wants to merge 1 commit into
BerriAI:mainfrom
deepanshupal:fix/prefetch-test-frozen-clock

Conversation

@deepanshupal

Copy link
Copy Markdown

What

Fixes the proxy-behavior flake from #41051: test_join_binds_the_membership_to_the_requested_team fails on slow CI runners with TypeError: object MagicMock can't be used in 'await' expression from the get_org_object fallback. Currently red on main (push run 34737619378: 1 failed / 847 passed, test call took 5.48s).

Root cause

The test asserts that after prefetch_auth_objects, all four getters are served from the in-memory cache. But the prefetched org entries (org_id:*, org_id:*:with_budget) are written with the 5s DEFAULT_IN_MEMORY_TTL, while user/team entries get the 60s management TTL and the membership read is backstopped by the never-expiring reservation key. The org keys are the only prefetched entries with no no-expiry backstop.

The getters normally run milliseconds after the prefetch write, but the first getter call can take seconds (3.2s measured on a quiet local machine - event-loop warmup right after the prefetch await; the CI run shows a 5.48s call). On a loaded runner the gap crosses 5s, the org entry expires, and get_org_object falls through to the mocked dead prisma client.

Reproduced locally: sleeping 6s between prefetch and the getters produces the exact CI error, with membership/team/user still served from cache.

Fix

Test-only: the test's InMemoryCache gets a frozen clock (clock=lambda: 0.0), so TTL expiry can't race the assertions.

Production behavior is unchanged on purpose. The 5s org TTL is intentional - the org getter's own cache write uses it too, and bumping the prefetch TTL to the management TTL breaks 45 management tests that rely on org freshness.

Verification

  • tests/proxy_behavior (848 tests) and tests/test_litellm/proxy/auth/test_auth_object_prefetch.py (11 tests) pass against a real Postgres 16, seeded with the same prisma db push the CI job performs.
  • The 6s-sleep reproduction fails with the exact CI error without this change and passes with it.

The proxy-behavior test asserts the getters are served from the prefetched in-memory cache, but the prefetched org entries carry the 5s DEFAULT_IN_MEMORY_TTL while user/team entries get the 60s management TTL and the membership read is backstopped by the never-expiring reservation key. On a slow CI runner the first getter call alone can take seconds (3.2s observed on a quiet local machine), so by the time get_org_object runs the two org keys - the only ones without a no-expiry backstop - can have expired, and the getter falls through to the mocked dead prisma client (TypeError: object MagicMock can't be used in 'await' expression, seen on main push run 34737619378).

Freeze the test cache's clock so TTL expiry cannot race the assertions. Production behavior is unchanged: the 5s org TTL is intentional (the org getter's own write uses it, and bumping the prefetch TTL to the management TTL breaks 45 management tests that rely on org freshness).

Verified against a real Postgres 16 with the same prisma db push the CI job performs: tests/proxy_behavior (848 tests) and the fake-based prefetch unit tests (11 tests) all pass, and a 6s sleep between prefetch and the getters reproduces the exact CI failure without the fix and passes with it.
@deepanshupal
deepanshupal requested a review from a team September 14, 2026 09:28
@greptile-apps

greptile-apps Bot commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR makes three auth-object prefetch behavior tests deterministic by freezing their test-local cache clocks.

  • Prevents short cache TTLs from expiring during slow test execution
  • Leaves production cache behavior unchanged
  • Preserves the existing cache-hit and data-mapping assertions

Confidence Score: 5/5

The PR appears safe to merge because the deterministic clocks only affect tests and do not weaken their current behavioral assertions.

The changed tests verify immediate cache-hit behavior rather than TTL expiration, and the injected clock removes an unrelated timing race without changing production code.

Important Files Changed

Filename Overview
tests/proxy_behavior/auth/test_auth_object_prefetch.py Injects frozen clocks into test-local caches to prevent wall-clock timing from making immediate prefetch assertions flaky.

Reviews (1): Last reviewed commit: "test(auth): freeze cache clock in auth p..." | Re-trigger Greptile

@codecov

codecov Bot commented Sep 14, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@deepanshupal

Copy link
Copy Markdown
Author

Closing - main got the same fix in #40996 (frozen cache clock for these tests). Nothing left here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant