test(django): Fix flaky cache tests by using uuid for cache LOCATION - #7444
Merged
Conversation
…ests use_django_caching and use_django_caching_with_middlewares generated a random LOCATION with random.randint(1, 1_000_000). Django's LocMemCache keeps its backing dict keyed by LOCATION for the life of the test process, so with ~1e6 possible values the birthday problem gives a real (and observed) chance of two tests colliding on the same cache namespace. When that happens, a later test's first cache.get() sees an entry written by an earlier test, so cache.hit is unexpectedly True and the item_size/decorator assertions fail. Use uuid.uuid4() instead, which is already imported in this file, to guarantee a unique cache namespace per test invocation and remove the now-unused random import. Co-Authored-By: Neel Shah <6536764+sl0thentr0py@users.noreply.github.com>
sl0thentr0py
approved these changes
Sep 9, 2026
Contributor
Codecov Results 📊✅ 131905 passed | ⏭️ 7131 skipped | Total: 139036 | Pass Rate: 94.87% | Execution Time: 468m 40s 📊 Comparison with Base Branch
All tests are passing successfully. ✅ Patch coverage is 100.00%. Project has 2527 uncovered lines. Coverage diff@@ Coverage Diff @@
## master #PR +/-##
==========================================
+ Coverage 90.20% 90.20% —%
==========================================
Files 193 193 —
Lines 25788 25788 —
Branches 9530 9530 —
==========================================
+ Hits 23261 23261 —
- Misses 2527 2527 —
- Partials 1432 1432 —Generated by Codecov Action |
sl0thentr0py
marked this pull request as ready for review
September 9, 2026 12:46
sl0thentr0py
enabled auto-merge (squash)
September 9, 2026 12:46
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Root cause
test_cache_spans_item_size[True]andtest_cache_spans_decorator[True]have been intermittently failing on master (observed 3x each in the last ~150 CI runs, e.g. run 34096255656) with:use_django_caching/use_django_caching_with_middlewaresgave each test a "unique" DjangoLocMemCachenamespace via"unique-snowflake-%s" % random.randint(1, 1_000_000). Django'sLocMemCachebackend keeps its data keyed byLOCATIONin a module-level dict for the lifetime of the test process. With only ~1e6 possible values and hundreds of tests using this fixture in a session, the birthday problem gives a non-trivial chance that two different test invocations pick the sameLOCATION. When that happens, a later test's very firstcache.get()call sees a cache entry left behind by an earlier (unrelated) test, socache.hitcomes backTrueinstead of the expectedFalse, failing the assertion.Fix
Use
uuid.uuid4()(already imported in this file) instead ofrandom.randint(1, 1_000_000)for the cacheLOCATION, guaranteeing no collisions across the test session. Removed the now-unusedrandomimport.Verification
pytest tests/integrations/django/test_cache_module.py— 37 passedruff check tests/integrations/django/test_cache_module.py— cleanvia neel.
--
View Junior Session [Sentry]