Skip to content

test(django): Fix flaky cache tests by using uuid for cache LOCATION - #7444

Merged
sl0thentr0py merged 1 commit into
masterfrom
fix/cache-module-test-flakiness
Sep 9, 2026
Merged

test(django): Fix flaky cache tests by using uuid for cache LOCATION#7444
sl0thentr0py merged 1 commit into
masterfrom
fix/cache-module-test-flakiness

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Root cause

test_cache_spans_item_size[True] and test_cache_spans_decorator[True] have been intermittently failing on master (observed 3x each in the last ~150 CI runs, e.g. run 34096255656) with:

tests/integrations/django/test_cache_module.py:764: in test_cache_spans_item_size
    assert not spans[0]["attributes"]["cache.hit"]
E   assert not True

use_django_caching / use_django_caching_with_middlewares gave each test a "unique" Django LocMemCache namespace via "unique-snowflake-%s" % random.randint(1, 1_000_000). Django's LocMemCache backend keeps its data keyed by LOCATION in 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 same LOCATION. When that happens, a later test's very first cache.get() call sees a cache entry left behind by an earlier (unrelated) test, so cache.hit comes back True instead of the expected False, failing the assertion.

Fix

Use uuid.uuid4() (already imported in this file) instead of random.randint(1, 1_000_000) for the cache LOCATION, guaranteeing no collisions across the test session. Removed the now-unused random import.

Verification

  • pytest tests/integrations/django/test_cache_module.py — 37 passed
  • ruff check tests/integrations/django/test_cache_module.py — clean

via neel.

--

View Junior Session [Sentry]

…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>
@github-actions

github-actions Bot commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Codecov Results 📊

131905 passed | ⏭️ 7131 skipped | Total: 139036 | Pass Rate: 94.87% | Execution Time: 468m 40s

📊 Comparison with Base Branch

Metric Change
Total Tests 📈 +1330
Passed Tests 📈 +1321
Failed Tests
Skipped Tests 📈 +9

All tests are passing successfully.

✅ Patch coverage is 100.00%. Project has 2527 uncovered lines.
✅ Project coverage is 90.2%. Comparing base (1e758fa) to head (f94120a).

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
sl0thentr0py marked this pull request as ready for review September 9, 2026 12:46
@sl0thentr0py
sl0thentr0py requested a review from a team as a code owner September 9, 2026 12:46
@sl0thentr0py
sl0thentr0py enabled auto-merge (squash) September 9, 2026 12:46
@sl0thentr0py
sl0thentr0py merged commit 7e95b86 into master Sep 9, 2026
310 of 312 checks passed
@sl0thentr0py
sl0thentr0py deleted the fix/cache-module-test-flakiness branch September 9, 2026 12:55
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