Skip to content

Commit 7e95b86

Browse files
test(django): Fix flaky cache tests by using uuid for cache LOCATION (#7444)
## 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](https://github.com/getsentry/sentry-python/actions/runs/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 <!-- junior-request-attribution:start --> via **neel**. <!-- junior-request-attribution:end --> <!-- junior-session-footer:start --> <!-- junior-conversation-id:agent-dispatch%3Adispatch_0edf7c52a0265c4f71de640db2fe7c12 --> -- [View Junior Session](https://junior-prod.sentry.dev/conversations/agent-dispatch%3Adispatch_0edf7c52a0265c4f71de640db2fe7c12) [[Sentry]](https://sentry.sentry.io/explore/conversations/agent-dispatch%3Adispatch_0edf7c52a0265c4f71de640db2fe7c12/?project=4510944073809921) <!-- junior-session-footer:end --> Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com> Co-authored-by: Neel Shah <6536764+sl0thentr0py@users.noreply.github.com>
1 parent 1e758fa commit 7e95b86

1 file changed

Lines changed: 2 additions & 3 deletions

File tree

‎tests/integrations/django/test_cache_module.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import os
2-
import random
32
import uuid
43

54
import pytest
@@ -30,7 +29,7 @@ def use_django_caching(settings):
3029
settings.CACHES = {
3130
"default": {
3231
"BACKEND": "django.core.cache.backends.locmem.LocMemCache",
33-
"LOCATION": "unique-snowflake-%s" % random.randint(1, 1000000),
32+
"LOCATION": "unique-snowflake-%s" % uuid.uuid4(),
3433
}
3534
}
3635

@@ -40,7 +39,7 @@ def use_django_caching_with_middlewares(settings):
4039
settings.CACHES = {
4140
"default": {
4241
"BACKEND": "django.core.cache.backends.locmem.LocMemCache",
43-
"LOCATION": "unique-snowflake-%s" % random.randint(1, 1000000),
42+
"LOCATION": "unique-snowflake-%s" % uuid.uuid4(),
4443
}
4544
}
4645
if hasattr(settings, "MIDDLEWARE"):

0 commit comments

Comments
 (0)