1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
|
from __future__ import annotations
import time
from math import ceil
import pytest
from limits.limits import (
RateLimitItemPerHour,
RateLimitItemPerMinute,
RateLimitItemPerSecond,
)
from limits.storage import storage_from_string
from limits.storage.base import TimestampedSlidingWindow
from limits.strategies import (
FixedWindowElasticExpiryRateLimiter,
FixedWindowRateLimiter,
MovingWindowRateLimiter,
SlidingWindowCounterRateLimiter,
)
from tests.utils import (
all_storage,
fixed_start,
moving_window_storage,
sliding_window_counter_storage,
timestamp_based_key_ttl,
window,
)
@all_storage
class TestFixedWindow:
@fixed_start
def test_fixed_window(self, uri, args, fixture):
storage = storage_from_string(uri, **args)
limiter = FixedWindowRateLimiter(storage)
limit = RateLimitItemPerSecond(10, 2)
with window(1) as (start, end):
assert all([limiter.hit(limit) for _ in range(0, 10)])
assert not limiter.hit(limit)
assert limiter.get_window_stats(limit).remaining == 0
assert limiter.get_window_stats(limit).reset_time == pytest.approx(
start + 2, 1e-2
)
@fixed_start
def test_fixed_window_empty_stats(self, uri, args, fixture):
storage = storage_from_string(uri, **args)
limiter = FixedWindowRateLimiter(storage)
limit = RateLimitItemPerSecond(10, 2)
assert limiter.get_window_stats(limit).remaining == 10
assert limiter.get_window_stats(limit).reset_time == pytest.approx(
time.time(), 1e-2
)
@fixed_start
def test_fixed_window_multiple_cost(self, uri, args, fixture):
storage = storage_from_string(uri, **args)
limiter = FixedWindowRateLimiter(storage)
limit = RateLimitItemPerMinute(10, 2)
assert not limiter.hit(limit, "k1", cost=11)
assert limiter.hit(limit, "k2", cost=5)
assert limiter.get_window_stats(limit, "k2").remaining == 5
assert not limiter.test(limit, "k2", cost=6)
assert not limiter.hit(limit, "k2", cost=6)
@fixed_start
def test_fixed_window_with_elastic_expiry(self, uri, args, fixture):
storage = storage_from_string(uri, **args)
with pytest.warns(DeprecationWarning):
limiter = FixedWindowElasticExpiryRateLimiter(storage)
limit = RateLimitItemPerSecond(10, 2)
with window(1) as (start, end):
assert all([limiter.hit(limit) for _ in range(0, 10)])
assert not limiter.hit(limit)
assert limiter.get_window_stats(limit).remaining == 0
assert limiter.get_window_stats(limit).reset_time == pytest.approx(
start + 2, 1e-2
)
with window(3) as (start, end):
assert not limiter.hit(limit)
assert limiter.hit(limit)
assert limiter.get_window_stats(limit).remaining == 9
assert limiter.get_window_stats(limit).reset_time == pytest.approx(
end + 2, 1e-2
)
@fixed_start
def test_fixed_window_with_elastic_expiry_multiple_cost(self, uri, args, fixture):
storage = storage_from_string(uri, **args)
with pytest.warns(DeprecationWarning):
limiter = FixedWindowElasticExpiryRateLimiter(storage)
limit = RateLimitItemPerSecond(10, 2)
assert not limiter.hit(limit, "k1", cost=11)
with window(0) as (start, end):
assert limiter.hit(limit, "k2", cost=5)
assert limiter.get_window_stats(limit, "k2").remaining == 5
assert limiter.get_window_stats(limit, "k2").reset_time == pytest.approx(
end + 2, 1e-2
)
assert not limiter.hit(limit, "k2", cost=6)
@fixed_start
@pytest.mark.flaky
def test_test_fixed_window(self, uri, args, fixture):
storage = storage_from_string(uri, **args)
limiter = FixedWindowRateLimiter(storage)
limit = RateLimitItemPerHour(2, 1)
assert limiter.hit(limit)
assert limiter.test(limit)
assert limiter.hit(limit)
assert not limiter.test(limit)
assert not limiter.hit(limit)
@sliding_window_counter_storage
class TestSlidingWindow:
@fixed_start
def test_sliding_window_counter(self, uri, args, fixture):
storage = storage_from_string(uri, **args)
limiter = SlidingWindowCounterRateLimiter(storage)
limit = RateLimitItemPerSecond(10, 2)
if isinstance(storage, TimestampedSlidingWindow):
next_second_from_now = ceil(time.time())
if next_second_from_now % 2 == 0:
# Next second is even, so the curent one is odd.
# Must wait a full period for memcached.
time.sleep(1)
next_second_from_now = ceil(time.time())
with window(1) as (start, end):
assert all([limiter.hit(limit) for _ in range(0, 10)])
assert not limiter.hit(limit)
assert limiter.get_window_stats(limit).remaining == 0
if isinstance(storage, TimestampedSlidingWindow):
# If the key is timestamp-based, the reset time is periodic according to the worker's timestamp
reset_time = limiter.get_window_stats(limit).reset_time
expected_reset = int(
limit.get_expiry() - (next_second_from_now % limit.get_expiry())
)
assert reset_time - next_second_from_now == pytest.approx(
expected_reset, abs=1e-2
)
else:
assert limiter.get_window_stats(limit).reset_time == pytest.approx(
start + 2, 1e-2
)
@pytest.mark.flaky
def test_sliding_window_counter_total_reset(self, uri, args, fixture):
storage = storage_from_string(uri, **args)
limiter = SlidingWindowCounterRateLimiter(storage)
multiple = 10
period = 1
limit = RateLimitItemPerSecond(multiple, period)
assert limiter.get_window_stats(limit).remaining == multiple
if isinstance(storage, TimestampedSlidingWindow):
# Avoid testing the behaviour when the window is about to be reset
ttl = timestamp_based_key_ttl(limit)
if ttl < 0.5:
time.sleep(ttl)
assert limiter.hit(limit, cost=multiple)
assert not limiter.hit(limit)
assert limiter.get_window_stats(limit).remaining == 0
time.sleep(period * 2)
assert limiter.get_window_stats(limit).remaining == multiple
assert limiter.get_window_stats(limit).reset_time == pytest.approx(
time.time(), abs=1e-2
)
def test_sliding_window_counter_current_window(self, uri, args, fixture):
"""Check the window stats when only the current window is filled"""
storage = storage_from_string(uri, **args)
limiter = SlidingWindowCounterRateLimiter(storage)
limit = RateLimitItemPerHour(2, 24)
if isinstance(storage, TimestampedSlidingWindow):
# Avoid testing the behaviour when the window is about to be reset
ttl = timestamp_based_key_ttl(limit)
if ttl < 0.5:
time.sleep(ttl)
assert limiter.hit(limit)
now = time.time()
if isinstance(storage, TimestampedSlidingWindow):
expected_reset_time = now + timestamp_based_key_ttl(limit, now)
else:
expected_reset_time = now + 24 * 3600
assert limiter.get_window_stats(limit).reset_time == pytest.approx(
expected_reset_time, 1e-2
)
assert limiter.get_window_stats(limit).remaining == 1
assert limiter.hit(limit)
assert not limiter.hit(limit)
@pytest.mark.flaky(max_runs=3)
def test_sliding_window_counter_previous_window(self, uri, args, fixture):
"""Check the window stats when the previous window is partially filled"""
storage = storage_from_string(uri, **args)
limiter = SlidingWindowCounterRateLimiter(storage)
period = 1
limit = RateLimitItemPerSecond(5, period)
sleep_margin = 0.001
if isinstance(storage, TimestampedSlidingWindow):
# Avoid testing the behaviour when the window is about to be reset
ttl = timestamp_based_key_ttl(limit)
if ttl < 0.3:
time.sleep(ttl + sleep_margin)
previous_window_hits = 3
for i in range(previous_window_hits):
limiter.hit(limit)
now = time.time()
# Check the stats: only the current window is filled
assert limiter.get_window_stats(limit).remaining == 2
if isinstance(storage, TimestampedSlidingWindow):
expected_reset_time = now + timestamp_based_key_ttl(limit, now)
else:
expected_reset_time = now + period
assert limiter.get_window_stats(limit).reset_time == pytest.approx(
expected_reset_time, 1e-2
)
# Wait for the next window
sleep_time = expected_reset_time - time.time() + sleep_margin
time.sleep(sleep_time)
# A new hit should be available immediately after window shift
# The limiter should reset in a fraction of a period, according to how many hits are in the previous window
reset_time = limiter.get_window_stats(limit).reset_time
reset_in = reset_time - time.time()
assert reset_in == pytest.approx(
limit.get_expiry() / previous_window_hits, abs=0.03
)
assert limiter.get_window_stats(limit).remaining == 3
assert limiter.hit(limit)
assert limiter.hit(limit)
for i in range(previous_window_hits):
# A new item hit should be freed by the previous window
t0 = time.time()
assert limiter.get_window_stats(limit).remaining == 1
assert limiter.hit(limit)
assert limiter.get_window_stats(limit).remaining == 0
assert not limiter.hit(limit)
# The previous window has 4 hits. The reset time should be in a 1/4 of the window expiry
reset_time = limiter.get_window_stats(limit).reset_time
t1 = time.time()
reset_in = reset_time - time.time()
assert reset_in == pytest.approx(
limit.get_expiry() / previous_window_hits - (t1 - t0), abs=0.03
)
# Wait for the next hit available
time.sleep(reset_in + sleep_margin)
@fixed_start
def test_sliding_window_counter_empty_stats(self, uri, args, fixture):
storage = storage_from_string(uri, **args)
limiter = SlidingWindowCounterRateLimiter(storage)
limit = RateLimitItemPerSecond(10, 2)
assert limiter.get_window_stats(limit).remaining == 10
assert limiter.get_window_stats(limit).reset_time == pytest.approx(
time.time(), 1e-2
)
@fixed_start
def test_sliding_window_counter_multiple_cost(self, uri, args, fixture):
storage = storage_from_string(uri, **args)
limiter = SlidingWindowCounterRateLimiter(storage)
limit = RateLimitItemPerMinute(10, 2)
if isinstance(storage, TimestampedSlidingWindow):
# Avoid testing the behaviour when the window is about to be reset
ttl = timestamp_based_key_ttl(limit)
if ttl < 0.5:
time.sleep(ttl)
assert not limiter.hit(limit, "k1", cost=11)
assert limiter.hit(limit, "k2", cost=5)
assert limiter.get_window_stats(limit, "k2").remaining == 5
assert not limiter.test(limit, "k2", cost=6)
assert not limiter.hit(limit, "k2", cost=6)
@fixed_start
@pytest.mark.flaky
def test_test_sliding_window_counter(self, uri, args, fixture):
storage = storage_from_string(uri, **args)
limiter = SlidingWindowCounterRateLimiter(storage)
limit = RateLimitItemPerHour(2, 1)
assert limiter.hit(limit)
assert limiter.test(limit)
assert limiter.hit(limit)
assert not limiter.test(limit)
assert not limiter.hit(limit)
@moving_window_storage
class TestMovingWindow:
def test_moving_window_empty_stats(self, uri, args, fixture):
storage = storage_from_string(uri, **args)
limiter = MovingWindowRateLimiter(storage)
limit = RateLimitItemPerSecond(10, 2)
assert limiter.get_window_stats(limit).remaining == 10
assert limiter.get_window_stats(limit).reset_time == pytest.approx(
time.time() + 2, 1e-2
)
def test_moving_window_stats(self, uri, args, fixture):
storage = storage_from_string(uri, **args)
limiter = MovingWindowRateLimiter(storage)
limit = RateLimitItemPerMinute(2)
assert limiter.hit(limit, "key")
time.sleep(1)
assert limiter.hit(limit, "key")
time.sleep(1)
assert not limiter.hit(limit, "key")
assert limiter.get_window_stats(limit, "key").remaining == 0
assert limiter.get_window_stats(
limit, "key"
).reset_time - time.time() == pytest.approx(58, 1e-2)
def test_moving_window(self, uri, args, fixture):
storage = storage_from_string(uri, **args)
limiter = MovingWindowRateLimiter(storage)
limit = RateLimitItemPerSecond(10, 2)
# 5 hits in the first 500ms
with window(0.5):
assert all(limiter.hit(limit) for i in range(5))
# 5 hits in the last 200ms
with window(2, delay=1.3):
assert all(limiter.hit(limit) for i in range(5))
# 11th fails
assert not limiter.hit(limit)
def test_moving_window_multiple_cost(self, uri, args, fixture):
storage = storage_from_string(uri, **args)
limiter = MovingWindowRateLimiter(storage)
limit = RateLimitItemPerSecond(10, 2)
assert not limiter.hit(limit, "k1", cost=11)
# 5 hits in the first 100ms
with window(0.1):
limiter.hit(limit, "k2", cost=5)
# 5 hits in the last 100ms
with window(2, delay=1.8):
assert all(limiter.hit(limit, "k2") for i in range(4))
assert not limiter.test(limit, "k2", cost=2)
assert not limiter.hit(limit, "k2", cost=2)
assert limiter.hit(limit, "k2")
# 5 more succeed since there were only 5 in the last 2 seconds
assert all([limiter.hit(limit, "k2") for i in range(5)])
assert limiter.get_window_stats(limit, "k2")[1] == 0
assert not limiter.hit(limit, "k2", cost=2)
def test_moving_window_varying_cost(self, uri, args, fixture):
storage = storage_from_string(uri, **args)
limiter = MovingWindowRateLimiter(storage)
five_per_min = RateLimitItemPerMinute(5)
limiter.hit(five_per_min, cost=5)
assert not limiter.hit(five_per_min, cost=2)
limiter.clear(five_per_min)
assert limiter.hit(five_per_min)
def test_moving_window_huge_cost_sync(self, uri, args, fixture):
storage = storage_from_string(uri, **args)
limiter = MovingWindowRateLimiter(storage)
many_per_min = RateLimitItemPerMinute(1_000_000)
limiter.hit(many_per_min, cost=1_000_000)
assert not limiter.hit(many_per_min, cost=2)
limiter.clear(many_per_min)
assert limiter.hit(many_per_min)
def test_test_moving_window(self, uri, args, fixture):
storage = storage_from_string(uri, **args)
limit = RateLimitItemPerHour(2, 1)
limiter = MovingWindowRateLimiter(storage)
assert limiter.hit(limit)
assert limiter.test(limit)
assert limiter.hit(limit)
assert not limiter.test(limit)
assert not limiter.hit(limit)
|