Skip to content

Commit

Permalink
tests(rate-limiting): refactoring sync rate tests
Browse files Browse the repository at this point in the history
  • Loading branch information
samugi committed Oct 27, 2023
1 parent cf58cb8 commit 9804330
Showing 1 changed file with 36 additions and 37 deletions.
73 changes: 36 additions & 37 deletions spec/03-plugins/23-rate-limiting/02-policies_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -176,42 +176,41 @@ describe("Plugin: rate-limiting (policies)", function()
end)
end

for _, sync_rate in ipairs{1, SYNC_RATE_REALTIME} do
describe("redis with sync rate: " .. sync_rate, function()
local identifier = uuid()
local conf = {
route_id = uuid(),
service_id = uuid(),
redis_host = helpers.redis_host,
redis_port = helpers.redis_port,
redis_database = 0,
sync_rate = sync_rate,
}

before_each(function()
local red = require "resty.redis"
local redis = assert(red:new())
redis:set_timeout(1000)
assert(redis:connect(conf.redis_host, conf.redis_port))
redis:flushall()
redis:close()
end)

it("increase & usage", function()
--[[
Just a simple test:
- increase 1
- check usage == 1
- increase 1
- check usage == 2
- increase 1 (beyond the limit)
- check usage == 3
--]]

local current_timestamp = 1424217600
local periods = timestamp.get_timestamps(current_timestamp)
for _, sync_rate in ipairs{0.5, SYNC_RATE_REALTIME} do
local current_timestamp = 1424217600
local periods = timestamp.get_timestamps(current_timestamp)

for period in pairs(periods) do
describe("redis with sync rate: " .. sync_rate .. " period: " .. period, function()
local identifier = uuid()
local conf = {
route_id = uuid(),
service_id = uuid(),
redis_host = helpers.redis_host,
redis_port = helpers.redis_port,
redis_database = 0,
sync_rate = sync_rate,
}

for period in pairs(periods) do
before_each(function()
local red = require "resty.redis"
local redis = assert(red:new())
redis:set_timeout(1000)
assert(redis:connect(conf.redis_host, conf.redis_port))
redis:flushall()
redis:close()
end)

it("increase & usage", function()
--[[
Just a simple test:
- increase 1
- check usage == 1
- increase 1
- check usage == 2
- increase 1 (beyond the limit)
- check usage == 3
--]]

local metric = assert(policies.redis.usage(conf, identifier, period, current_timestamp))
assert.equal(0, metric)
Expand All @@ -232,8 +231,8 @@ describe("Plugin: rate-limiting (policies)", function()
assert.equal(i, metric)
end
end
end
end)
end)
end)
end
end
end)

0 comments on commit 9804330

Please sign in to comment.