Skip to content

Commit 3203181

Browse files
fix(tests): Use fixed draws in the indexer cache TTL jitter test
Two random TTLs round to the same second about once in 1800 runs. Refs #108952 Co-Authored-By: Joshua Li <14209781+joshuarli@users.noreply.github.com>
1 parent f083f33 commit 3203181

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

‎tests/sentry/sentry_metrics/test_indexer_cache.py‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from datetime import timedelta
2+
from unittest.mock import patch
23

34
import pytest
45
from django.conf import settings
@@ -216,7 +217,14 @@ def test_ttl_jitter() -> None:
216217
ttl_2 = indexer_cache.randomized_ttl
217218
assert base_ttl <= ttl_2 <= max_ttl
218219

219-
assert not ttl_1 == ttl_2
220+
# Two real draws land on the same whole second about once in 1800 runs, so check the spread
221+
# with fixed draws instead.
222+
with patch("sentry.sentry_metrics.indexer.cache.random") as mock_random:
223+
mock_random.uniform.side_effect = [0.0, 0.1, 0.25]
224+
assert indexer_cache.randomized_ttl == base_ttl
225+
assert indexer_cache.randomized_ttl == base_ttl + 720
226+
assert indexer_cache.randomized_ttl == max_ttl
227+
mock_random.uniform.assert_called_with(0, 0.25)
220228

221229

222230
def test_separate_namespacing() -> None:

0 commit comments

Comments
 (0)