|
| 1 | +describe("TradeQueryRateLimiter", function() |
| 2 | + describe("ParseHeader", function() |
| 3 | + -- Pass: Extracts keys/values correctly |
| 4 | + -- Fail: Nil/malformed values, indicating regex failure, breaking policy updates from API |
| 5 | + it("parses basic headers", function() |
| 6 | + local limiter = new("TradeQueryRateLimiter") |
| 7 | + local headers = limiter:ParseHeader("X-Rate-Limit-Policy: test\nRetry-After: 5\nContent-Type: json") |
| 8 | + assert.are.equal(headers["x-rate-limit-policy"], "test") |
| 9 | + assert.are.equal(headers["retry-after"], "5") |
| 10 | + assert.are.equal(headers["content-type"], "json") |
| 11 | + end) |
| 12 | + end) |
| 13 | + |
| 14 | + describe("ParsePolicy", function() |
| 15 | + -- Pass: Extracts rules/limits/states accurately |
| 16 | + -- Fail: Wrong buckets/windows, indicating parsing bug, enforcing incorrect rates |
| 17 | + it("parses full policy", function() |
| 18 | + local limiter = new("TradeQueryRateLimiter") |
| 19 | + local header = "X-Rate-Limit-Policy: trade-search-request-limit\nX-Rate-Limit-Rules: Ip,Account\nX-Rate-Limit-Ip: 8:10:60,15:60:120\nX-Rate-Limit-Ip-State: 7:10:60,14:60:120\nX-Rate-Limit-Account: 2:5:60\nX-Rate-Limit-Account-State: 1:5:60\nRetry-After: 10" |
| 20 | + local policies = limiter:ParsePolicy(header) |
| 21 | + local policy = policies["trade-search-request-limit"] |
| 22 | + assert.are.equal(policy.ip.limits[10].request, 8) |
| 23 | + assert.are.equal(policy.ip.limits[10].timeout, 60) |
| 24 | + assert.are.equal(policy.ip.state[10].request, 7) |
| 25 | + assert.are.equal(policy.account.limits[5].request, 2) |
| 26 | + end) |
| 27 | + end) |
| 28 | + |
| 29 | + describe("UpdateFromHeader", function() |
| 30 | + -- Pass: Reduces limits (e.g., 5 -> 4) |
| 31 | + -- Fail: Unchanged limits, indicating margin ignored, risking user over-requests |
| 32 | + it("applies margin to limits", function() |
| 33 | + local limiter = new("TradeQueryRateLimiter") |
| 34 | + limiter.limitMargin = 1 |
| 35 | + local header = "X-Rate-Limit-Policy: test\nX-Rate-Limit-Rules: Ip\nX-Rate-Limit-Ip: 5:10:60\nX-Rate-Limit-Ip-State: 4:10:60" |
| 36 | + limiter:UpdateFromHeader(header) |
| 37 | + assert.are.equal(limiter.policies["test"].ip.limits[10].request, 4) |
| 38 | + end) |
| 39 | + end) |
| 40 | + |
| 41 | + describe("NextRequestTime", function() |
| 42 | + -- Pass: Delays past timestamp |
| 43 | + -- Fail: Allows immediate request, indicating ignored cooldowns, causing 429 errors |
| 44 | + it("blocks on retry-after", function() |
| 45 | + local limiter = new("TradeQueryRateLimiter") |
| 46 | + local now = os.time() |
| 47 | + limiter.policies["test"] = {} |
| 48 | + limiter.retryAfter["test"] = now + 10 |
| 49 | + local nextTime = limiter:NextRequestTime("test", now) |
| 50 | + assert.is_true(nextTime > now) |
| 51 | + end) |
| 52 | + |
| 53 | + -- Pass: Calculates delay from timestamps |
| 54 | + -- Fail: Allows request in limit, indicating state misread, over-throttling or bans |
| 55 | + it("blocks on window limit", function() |
| 56 | + local limiter = new("TradeQueryRateLimiter") |
| 57 | + local now = os.time() |
| 58 | + limiter.policies["test"] = { ["ip"] = { ["limits"] = { ["10"] = { ["request"] = 1, ["timeout"] = 60 } }, ["state"] = { ["10"] = { ["request"] = 1, ["timeout"] = 0 } } } } |
| 59 | + limiter.requestHistory["test"] = { timestamps = {now - 5} } |
| 60 | + limiter.lastUpdate["test"] = now - 5 |
| 61 | + local nextTime = limiter:NextRequestTime("test", now) |
| 62 | + assert.is_true(nextTime > now) |
| 63 | + end) |
| 64 | + end) |
| 65 | + |
| 66 | + describe("AgeOutRequests", function() |
| 67 | + -- Pass: Removes old stamps, decrements to 1 |
| 68 | + -- Fail: Stale data persists, indicating aging bug, perpetual blocking |
| 69 | + it("cleans up timestamps and decrements", function() |
| 70 | + local limiter = new("TradeQueryRateLimiter") |
| 71 | + limiter.policies["test"] = { ["ip"] = { ["state"] = { ["10"] = { ["request"] = 2, ["timeout"] = 0, ["decremented"] = nil } } } } |
| 72 | + limiter.requestHistory["test"] = { timestamps = {os.time() - 15, os.time() - 5}, maxWindow=10, lastCheck=os.time() - 10 } |
| 73 | + limiter:AgeOutRequests("test", os.time()) |
| 74 | + assert.are.equal(limiter.policies["test"].ip.state["10"].request, 1) |
| 75 | + assert.are.equal(#limiter.requestHistory["test"].timestamps, 1) |
| 76 | + end) |
| 77 | + end) |
| 78 | +end) |
0 commit comments