Repository navigation
Commit 7e95b86
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
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
3 | 2 | | |
4 | 3 | | |
5 | 4 | | |
| |||
30 | 29 | | |
31 | 30 | | |
32 | 31 | | |
33 | | - | |
| 32 | + | |
34 | 33 | | |
35 | 34 | | |
36 | 35 | | |
| |||
40 | 39 | | |
41 | 40 | | |
42 | 41 | | |
43 | | - | |
| 42 | + | |
44 | 43 | | |
45 | 44 | | |
46 | 45 | | |
| |||
0 commit comments