Skip to content

Commit 6216f63

Browse files
committed
fix(tests): Fix flaky test_ttl_jitter random collision
The test asserted two random TTL values were different, but with only 1801 possible integer values there was a ~1/1801 chance of collision per test run. Fix by sampling 20 values and asserting we get more than one distinct value. Fixes #108952
1 parent 9b71753 commit 6216f63

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

‎tests/sentry/sentry_metrics/test_indexer_cache.py‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,13 +210,15 @@ def test_ttl_jitter() -> None:
210210
base_ttl = 3600 * 2
211211
max_ttl = base_ttl + 1800 # 25% of base_ttl
212212

213-
ttl_1 = indexer_cache.randomized_ttl
214-
assert base_ttl <= ttl_1 <= max_ttl
215-
216-
ttl_2 = indexer_cache.randomized_ttl
217-
assert base_ttl <= ttl_2 <= max_ttl
218-
219-
assert not ttl_1 == ttl_2
213+
ttls = set()
214+
for _ in range(20):
215+
ttl = indexer_cache.randomized_ttl
216+
assert base_ttl <= ttl <= max_ttl
217+
ttls.add(ttl)
218+
219+
# With 20 samples from 1801 possible integer values, the probability
220+
# of all being identical is vanishingly small (~(1/1801)^19)
221+
assert len(ttls) > 1
220222

221223

222224
def test_separate_namespacing() -> None:

0 commit comments

Comments
 (0)