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 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
|
from __future__ import annotations
import time
from math import ceil
import pytest
from limits.aio.strategies import (
FixedWindowElasticExpiryRateLimiter,
FixedWindowRateLimiter,
MovingWindowRateLimiter,
SlidingWindowCounterRateLimiter,
)
from limits.limits import (
RateLimitItemPerHour,
RateLimitItemPerMinute,
RateLimitItemPerSecond,
)
from limits.storage import storage_from_string
from limits.storage.base import TimestampedSlidingWindow
from tests.utils import (
async_all_storage,
async_fixed_start,
async_moving_window_storage,
async_sliding_window_counter_storage,
async_window,
timestamp_based_key_ttl,
)
@pytest.mark.asyncio
@async_all_storage
class TestAsyncFixedWindow:
@async_fixed_start
async def test_fixed_window(self, uri, args, fixture):
storage = storage_from_string(uri, **args)
limiter = FixedWindowRateLimiter(storage)
limit = RateLimitItemPerSecond(10, 2)
async with async_window(1) as (start, _):
assert all([await limiter.hit(limit) for _ in range(0, 10)])
assert not await limiter.hit(limit)
assert (await limiter.get_window_stats(limit)).remaining == 0
assert (await limiter.get_window_stats(limit)).reset_time == pytest.approx(
start + 2, 1e-2
)
@async_fixed_start
async def test_fixed_window_empty_stats(self, uri, args, fixture):
storage = storage_from_string(uri, **args)
limiter = FixedWindowRateLimiter(storage)
limit = RateLimitItemPerSecond(10, 2)
assert (await limiter.get_window_stats(limit)).remaining == 10
assert (await limiter.get_window_stats(limit)).reset_time == pytest.approx(
time.time(), 1e-2
)
@async_fixed_start
async 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 await limiter.hit(limit, "k1", cost=11)
assert await limiter.hit(limit, "k2", cost=5)
assert (await limiter.get_window_stats(limit, "k2")).remaining == 5
assert not await limiter.test(limit, "k2", cost=6)
assert not await limiter.hit(limit, "k2", cost=6)
@async_fixed_start
async 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)
async with async_window(1) as (start, end):
assert all([await limiter.hit(limit) for _ in range(0, 10)])
assert not await limiter.hit(limit)
assert (await limiter.get_window_stats(limit)).remaining == 0
assert (await limiter.get_window_stats(limit)).reset_time == pytest.approx(
start + 2, 1e-2
)
async with async_window(3) as (start, end):
assert not await limiter.hit(limit)
assert await limiter.hit(limit)
assert (await limiter.get_window_stats(limit)).remaining == 9
assert (await limiter.get_window_stats(limit)).reset_time == pytest.approx(
end + 2, 1e-2
)
@async_fixed_start
async 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 await limiter.hit(limit, "k1", cost=11)
async with async_window(0) as (_, end):
assert await limiter.hit(limit, "k2", cost=5)
assert (await limiter.get_window_stats(limit, "k2")).remaining == 5
assert (
await limiter.get_window_stats(limit, "k2")
).reset_time == pytest.approx(end + 2, 1e-2)
assert not await limiter.hit(limit, "k2", cost=6)
@async_fixed_start
@pytest.mark.flaky
async def test_test_fixed_window(self, uri, args, fixture):
storage = storage_from_string(uri, **args)
limiter = FixedWindowRateLimiter(storage)
limit = RateLimitItemPerHour(2, 1)
assert await limiter.hit(limit)
assert await limiter.test(limit)
assert await limiter.hit(limit)
assert not await limiter.test(limit)
assert not await limiter.hit(limit)
@pytest.mark.asyncio
@async_moving_window_storage
class TestAsyncMovingWindow:
async def test_moving_window_stats(self, uri, args, fixture):
storage = storage_from_string(uri, **args)
limiter = MovingWindowRateLimiter(storage)
limit = RateLimitItemPerMinute(2)
assert await limiter.hit(limit, "key")
time.sleep(1)
assert await limiter.hit(limit, "key")
time.sleep(1)
assert not await limiter.hit(limit, "key")
assert (await limiter.get_window_stats(limit, "key")).remaining == 0
assert (
await limiter.get_window_stats(limit, "key")
).reset_time - time.time() == pytest.approx(58, 1e-2)
async 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
async with async_window(0.5):
assert all([await limiter.hit(limit) for i in range(5)])
# 5 hits in the last 200ms
async with async_window(2, delay=1.3):
assert all([await limiter.hit(limit) for i in range(5)])
# 11th fails
assert not await limiter.hit(limit)
# 5 more succeed since there were only 5 in the last 2 seconds
assert all([await limiter.hit(limit) for i in range(5)])
assert (await limiter.get_window_stats(limit)).remaining == 0
async def test_moving_window_empty_stats(self, uri, args, fixture):
storage = storage_from_string(uri, **args)
limiter = MovingWindowRateLimiter(storage)
limit = RateLimitItemPerSecond(10, 2)
assert (await limiter.get_window_stats(limit)).remaining == 10
assert (await limiter.get_window_stats(limit)).reset_time == pytest.approx(
time.time() + 2, 1e-2
)
async 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 await limiter.hit(limit, "k1", cost=11)
# 5 hits in the first 100ms
async with async_window(0.1):
assert await limiter.hit(limit, "k2", cost=5)
# 5 hits in the last 100ms
async with async_window(2, delay=1.8):
assert all([await limiter.hit(limit, "k2") for i in range(4)])
assert not await limiter.test(limit, "k2", cost=2)
assert not await limiter.hit(limit, "k2", cost=2)
assert await limiter.hit(limit, "k2")
assert all([await limiter.hit(limit, "k2") for i in range(5)])
assert (await limiter.get_window_stats(limit, "k2")).remaining == 0
assert not await limiter.hit(limit, "k2", cost=2)
async def test_moving_window_varying_cost(self, uri, args, fixture):
storage = storage_from_string(uri, **args)
limiter = MovingWindowRateLimiter(storage)
five_per_min = RateLimitItemPerMinute(5)
await limiter.hit(five_per_min, cost=5)
assert not await limiter.hit(five_per_min, cost=2)
await limiter.clear(five_per_min)
assert await limiter.hit(five_per_min)
async def test_moving_window_huge_cost_async(self, uri, args, fixture):
storage = storage_from_string(uri, **args)
limiter = MovingWindowRateLimiter(storage)
many_per_min = RateLimitItemPerMinute(1_000_000)
await limiter.hit(many_per_min, cost=999_999)
assert not await limiter.hit(many_per_min, cost=2)
await limiter.clear(many_per_min)
assert await limiter.hit(many_per_min)
async def test_test_moving_window(self, uri, args, fixture):
storage = storage_from_string(uri, **args)
limit = RateLimitItemPerHour(2, 1)
limiter = MovingWindowRateLimiter(storage)
assert await limiter.hit(limit)
assert await limiter.test(limit)
assert await limiter.hit(limit)
assert not await limiter.test(limit)
assert not await limiter.hit(limit)
@pytest.mark.asyncio
@async_sliding_window_counter_storage
class TestAsyncSlidingWindow:
@async_fixed_start
async 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):
# Avoid testing the behaviour when the window is about to be reset
ttl = timestamp_based_key_ttl(limit)
if ttl < 1:
time.sleep(ttl)
async with async_window(1) as (start, _):
assert all([await limiter.hit(limit) for _ in range(0, 10)])
assert not await limiter.hit(limit)
assert (await limiter.get_window_stats(limit)).remaining == 0
assert (await limiter.get_window_stats(limit)).reset_time == pytest.approx(
start + 2, 1e-2
)
@pytest.mark.flaky
async 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 (await 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 await limiter.hit(limit, cost=multiple)
assert not await limiter.hit(limit)
assert (await limiter.get_window_stats(limit)).remaining == 0
time.sleep(period * 2)
assert (await limiter.get_window_stats(limit)).remaining == multiple
assert (await limiter.get_window_stats(limit)).reset_time == pytest.approx(
time.time(), abs=1e-2
)
async 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 await 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 (await limiter.get_window_stats(limit)).reset_time == pytest.approx(
expected_reset_time, 1e-2
)
assert (await limiter.get_window_stats(limit)).remaining == 1
assert await limiter.hit(limit)
assert not await limiter.hit(limit)
@pytest.mark.flaky(max_runs=3)
async 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)
limit = RateLimitItemPerSecond(5, 1)
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)
t0 = time.time()
previous_window_hits = 3
await limiter.hit(limit)
t1 = time.time()
for i in range(previous_window_hits - 1):
await limiter.hit(limit)
# Check the stats: only the current window is filled
if isinstance(storage, TimestampedSlidingWindow):
expected_reset_time = t0 + timestamp_based_key_ttl(limit, t0)
else:
expected_reset_time = t1 + 1
reset_time = (await limiter.get_window_stats(limit)).reset_time
assert reset_time == pytest.approx(expected_reset_time, abs=0.03)
assert (await limiter.get_window_stats(limit)).remaining == 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 = (await 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 (await limiter.get_window_stats(limit)).remaining == 3
assert await limiter.hit(limit)
assert await 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 (await limiter.get_window_stats(limit)).remaining == 1
assert await limiter.hit(limit)
assert (await limiter.get_window_stats(limit)).remaining == 0
assert not await limiter.hit(limit)
# The previous window has 4 hits. The reset time should be in a 1/4 of the window expiry
reset_time = (await 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)
@async_fixed_start
async 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 (await limiter.get_window_stats(limit)).remaining == 10
assert (await limiter.get_window_stats(limit)).reset_time == pytest.approx(
time.time(), 1e-2
)
@async_fixed_start
@pytest.mark.flaky
async def test_sliding_window_counter_stats(self, uri, args, fixture):
storage = storage_from_string(uri, **args)
limiter = SlidingWindowCounterRateLimiter(storage)
limit = RateLimitItemPerMinute(2)
if isinstance(storage, TimestampedSlidingWindow):
next_second_from_now = ceil(time.time())
assert await limiter.hit(limit, "key")
time.sleep(1)
assert await limiter.hit(limit, "key")
time.sleep(1)
assert not await limiter.hit(limit, "key")
assert (await limiter.get_window_stats(limit, "key")).remaining == 0
if isinstance(storage, TimestampedSlidingWindow):
# With timestamp-based key implementation,
# the reset time is periodic according to the worker's timestamp
reset_time = (await limiter.get_window_stats(limit, "key")).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 (
await limiter.get_window_stats(limit, "key")
).reset_time - time.time() == pytest.approx(58, 1e-2)
@async_fixed_start
async 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 await limiter.hit(limit, "k1", cost=11)
assert await limiter.hit(limit, "k2", cost=5)
assert (await limiter.get_window_stats(limit, "k2")).remaining == 5
assert not await limiter.test(limit, "k2", cost=6)
assert not await limiter.hit(limit, "k2", cost=6)
async def test_test_sliding_window_counter(self, uri, args, fixture):
storage = storage_from_string(uri, **args)
limit = RateLimitItemPerHour(2, 1)
limiter = SlidingWindowCounterRateLimiter(storage)
assert await limiter.hit(limit)
assert await limiter.test(limit)
assert await limiter.hit(limit)
assert not await limiter.test(limit)
assert not await limiter.hit(limit)
|