|
| 1 | +# (C) 2025 GoodData Corporation |
| 2 | + |
| 3 | +import time |
| 4 | +import pytest |
| 5 | +from gooddata_pipelines.utils.rate_limiter import RateLimiter |
| 6 | + |
| 7 | + |
| 8 | +# --------------------------- |
| 9 | +# Core wait + reset behavior |
| 10 | +# --------------------------- |
| 11 | + |
| 12 | + |
| 13 | +def test_rate_limiter_no_wait_needed(): |
| 14 | + limiter = RateLimiter(calls_per_second=1000.0) # Very fast limit |
| 15 | + waited = limiter.wait_if_needed() |
| 16 | + assert waited == pytest.approx(0.0, abs=0.001) |
| 17 | + |
| 18 | + |
| 19 | +def test_rate_limiter_enforces_delay(): |
| 20 | + limiter = RateLimiter(calls_per_second=2.0) |
| 21 | + limiter.wait_if_needed() |
| 22 | + start = time.time() |
| 23 | + waited = limiter.wait_if_needed() |
| 24 | + duration = time.time() - start |
| 25 | + |
| 26 | + assert waited >= 0.49 |
| 27 | + assert duration < 0.65 |
| 28 | + |
| 29 | + |
| 30 | +def test_rate_limiter_respects_reset(): |
| 31 | + limiter = RateLimiter(calls_per_second=1.0) |
| 32 | + limiter.wait_if_needed() |
| 33 | + limiter.reset() |
| 34 | + waited = limiter.wait_if_needed() |
| 35 | + assert waited == pytest.approx(0.0, abs=0.001) |
| 36 | + |
| 37 | + |
| 38 | +def test_rate_limiter_min_interval_property(): |
| 39 | + limiter = RateLimiter(calls_per_second=4.0) |
| 40 | + assert limiter.min_interval == pytest.approx(0.25, abs=1e-9) |
| 41 | + |
| 42 | + |
| 43 | +# ----------------------------------------- |
| 44 | +# Decorator: shared instance (@limiter) |
| 45 | +# ----------------------------------------- |
| 46 | + |
| 47 | + |
| 48 | +def test_rate_limiter_as_decorator_enforces_delay_shared_instance(): |
| 49 | + limiter = RateLimiter(calls_per_second=2.0) |
| 50 | + ts = [] |
| 51 | + |
| 52 | + @limiter |
| 53 | + def func(): |
| 54 | + ts.append(time.time()) |
| 55 | + |
| 56 | + func() |
| 57 | + func() |
| 58 | + |
| 59 | + assert len(ts) == 2 |
| 60 | + assert ts[1] - ts[0] >= 0.49 |
| 61 | + |
| 62 | + |
| 63 | +def test_rate_limiter_decorator_shared_state_across_functions(): |
| 64 | + limiter = RateLimiter(calls_per_second=2.0) |
| 65 | + ts = [] |
| 66 | + |
| 67 | + @limiter |
| 68 | + def func_a(): |
| 69 | + ts.append(time.time()) |
| 70 | + |
| 71 | + @limiter |
| 72 | + def func_b(): |
| 73 | + ts.append(time.time()) |
| 74 | + |
| 75 | + func_a() |
| 76 | + func_b() # should be throttled by the *same* limiter |
| 77 | + assert len(ts) == 2 |
| 78 | + assert ts[1] - ts[0] >= 0.49 |
| 79 | + |
| 80 | + |
| 81 | +def test_multiple_limiters_independent_state_shared_instance_mode(): |
| 82 | + limiter_a = RateLimiter(calls_per_second=2.0) |
| 83 | + limiter_b = RateLimiter(calls_per_second=2.0) |
| 84 | + |
| 85 | + ts_a = [] |
| 86 | + ts_b = [] |
| 87 | + |
| 88 | + @limiter_a |
| 89 | + def func_a(): |
| 90 | + ts_a.append(time.time()) |
| 91 | + |
| 92 | + @limiter_b |
| 93 | + def func_b(): |
| 94 | + ts_b.append(time.time()) |
| 95 | + |
| 96 | + func_a() |
| 97 | + func_b() |
| 98 | + |
| 99 | + # They should be ~simultaneous since they use different instances |
| 100 | + assert abs(ts_a[0] - ts_b[0]) < 0.05 |
| 101 | + |
| 102 | + |
| 103 | +# ------------------------------------------------------- |
| 104 | +# Decorator: per-function instance (@RateLimiter(...)) |
| 105 | +# ------------------------------------------------------- |
| 106 | + |
| 107 | + |
| 108 | +def test_per_function_decorator_enforces_delay_per_function(): |
| 109 | + # Each function decorated this way gets its *own* limiter instance. |
| 110 | + ts = [] |
| 111 | + |
| 112 | + @RateLimiter(calls_per_second=2.0) # 0.5s |
| 113 | + def func(): |
| 114 | + ts.append(time.time()) |
| 115 | + |
| 116 | + func() |
| 117 | + func() |
| 118 | + assert len(ts) == 2 |
| 119 | + assert ts[1] - ts[0] >= 0.49 |
| 120 | + |
| 121 | + |
| 122 | +def test_per_function_decorator_independent_state_between_functions(): |
| 123 | + ts_a = [] |
| 124 | + ts_b = [] |
| 125 | + |
| 126 | + @RateLimiter(calls_per_second=2.0) |
| 127 | + def func_a(): |
| 128 | + ts_a.append(time.time()) |
| 129 | + |
| 130 | + @RateLimiter(calls_per_second=2.0) |
| 131 | + def func_b(): |
| 132 | + ts_b.append(time.time()) |
| 133 | + |
| 134 | + func_a() |
| 135 | + func_b() # independent limiter, so no enforced delay between A and B |
| 136 | + assert abs(ts_a[0] - ts_b[0]) < 0.05 |
| 137 | + |
| 138 | + |
| 139 | +# ----------------------------- |
| 140 | +# Context manager usage |
| 141 | +# ----------------------------- |
| 142 | + |
| 143 | + |
| 144 | +def test_context_manager_waits_on_enter(): |
| 145 | + limiter = RateLimiter(calls_per_second=2.0) |
| 146 | + with limiter: |
| 147 | + t1 = time.time() |
| 148 | + with limiter: |
| 149 | + t2 = time.time() |
| 150 | + |
| 151 | + # The second 'with' should be at least 0.5s after the first 'with' enter |
| 152 | + assert t2 - t1 >= 0.49 |
| 153 | + |
| 154 | + |
| 155 | +def test_context_manager_multiple_uses_same_instance(): |
| 156 | + limiter = RateLimiter(calls_per_second=3.0) |
| 157 | + times = [] |
| 158 | + |
| 159 | + for _ in range(3): |
| 160 | + with limiter: |
| 161 | + times.append(time.time()) |
| 162 | + |
| 163 | + intervals = [b - a for a, b in zip(times, times[1:])] |
| 164 | + for iv in intervals: |
| 165 | + assert iv >= 0.30 # a bit of slack for timing variance |
| 166 | + |
| 167 | + |
| 168 | +def test_context_manager_propagates_exceptions(): |
| 169 | + limiter = RateLimiter(calls_per_second=10.0) |
| 170 | + |
| 171 | + class Boom(Exception): |
| 172 | + pass |
| 173 | + |
| 174 | + with pytest.raises(Boom): |
| 175 | + with limiter: |
| 176 | + raise Boom("fail") |
0 commit comments