67 lines
2.9 KiB
Python
67 lines
2.9 KiB
Python
|
|
"""Tests for randomized timing functionality"""
|
||
|
|
import pytest
|
||
|
|
import random
|
||
|
|
|
||
|
|
|
||
|
|
def get_randomized_interval(base_interval: int = 150, variance: int = 30) -> int:
|
||
|
|
"""Get check interval with random variance to avoid bot detection patterns."""
|
||
|
|
variance_value = random.randint(-variance, variance)
|
||
|
|
interval = base_interval + variance_value
|
||
|
|
# Ensure minimum of 60 seconds to avoid excessive load
|
||
|
|
return max(60, interval)
|
||
|
|
|
||
|
|
|
||
|
|
class TestRandomizedTiming:
|
||
|
|
"""Test randomized check interval functionality"""
|
||
|
|
|
||
|
|
def test_randomized_interval_within_bounds(self):
|
||
|
|
"""Test that randomized interval stays within expected bounds"""
|
||
|
|
# Test multiple intervals to ensure randomization
|
||
|
|
intervals = [get_randomized_interval(150, 30) for _ in range(100)]
|
||
|
|
|
||
|
|
# All intervals should be between 120 and 180 (150 ± 30)
|
||
|
|
assert all(120 <= i <= 180 for i in intervals), f"Some intervals outside bounds"
|
||
|
|
|
||
|
|
# Should have some variance (not all the same)
|
||
|
|
assert len(set(intervals)) > 1, "No randomization detected - all intervals are identical"
|
||
|
|
|
||
|
|
def test_minimum_interval_enforced(self):
|
||
|
|
"""Test that minimum 60s interval is enforced even with high variance"""
|
||
|
|
# Test multiple times with extreme variance
|
||
|
|
intervals = [get_randomized_interval(90, 50) for _ in range(100)]
|
||
|
|
|
||
|
|
# All intervals should be >= 60
|
||
|
|
assert all(i >= 60 for i in intervals), f"Some intervals below 60s minimum: {min(intervals)}"
|
||
|
|
|
||
|
|
def test_zero_variance_possible(self):
|
||
|
|
"""Test that variance can be disabled by setting to 0"""
|
||
|
|
# All intervals should be exactly 150
|
||
|
|
intervals = [get_randomized_interval(150, 0) for _ in range(20)]
|
||
|
|
assert all(i == 150 for i in intervals), "Variance=0 should produce constant interval"
|
||
|
|
|
||
|
|
def test_interval_distribution(self):
|
||
|
|
"""Test that randomization produces reasonable distribution"""
|
||
|
|
# Generate many samples
|
||
|
|
intervals = [get_randomized_interval(150, 30) for _ in range(1000)]
|
||
|
|
|
||
|
|
avg = sum(intervals) / len(intervals)
|
||
|
|
|
||
|
|
# Average should be close to 150 (within 5 seconds)
|
||
|
|
assert 145 <= avg <= 155, f"Average {avg} too far from expected 150"
|
||
|
|
|
||
|
|
# Should have good spread (at least 20 different values)
|
||
|
|
assert len(set(intervals)) >= 20, "Not enough variance in intervals"
|
||
|
|
|
||
|
|
def test_custom_parameters(self):
|
||
|
|
"""Test with various custom base intervals and variances"""
|
||
|
|
test_cases = [
|
||
|
|
(120, 20, 100, 140), # base, variance, min_expected, max_expected
|
||
|
|
(180, 40, 140, 220),
|
||
|
|
(200, 10, 190, 210),
|
||
|
|
]
|
||
|
|
|
||
|
|
for base, variance, min_exp, max_exp in test_cases:
|
||
|
|
intervals = [get_randomized_interval(base, variance) for _ in range(50)]
|
||
|
|
assert all(min_exp <= i <= max_exp for i in intervals), \
|
||
|
|
f"Failed for base={base}, variance={variance}"
|