From f94120a7282e4a21c70f1f083dd019931798ce27 Mon Sep 17 00:00:00 2001 From: "sentry-junior[bot]" <264270552+sentry-junior[bot]@users.noreply.github.com> Date: Wed, 9 Sep 2026 12:02:51 +0000 Subject: [PATCH] test(django): Use uuid for unique cache LOCATION to fix flaky cache tests 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> --- tests/integrations/django/test_cache_module.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/integrations/django/test_cache_module.py b/tests/integrations/django/test_cache_module.py index edde701994..3766006c9b 100644 --- a/tests/integrations/django/test_cache_module.py +++ b/tests/integrations/django/test_cache_module.py @@ -1,5 +1,4 @@ import os -import random import uuid import pytest @@ -30,7 +29,7 @@ def use_django_caching(settings): settings.CACHES = { "default": { "BACKEND": "django.core.cache.backends.locmem.LocMemCache", - "LOCATION": "unique-snowflake-%s" % random.randint(1, 1000000), + "LOCATION": "unique-snowflake-%s" % uuid.uuid4(), } } @@ -40,7 +39,7 @@ def use_django_caching_with_middlewares(settings): settings.CACHES = { "default": { "BACKEND": "django.core.cache.backends.locmem.LocMemCache", - "LOCATION": "unique-snowflake-%s" % random.randint(1, 1000000), + "LOCATION": "unique-snowflake-%s" % uuid.uuid4(), } } if hasattr(settings, "MIDDLEWARE"):