Skip to content

Commit 9804330

Browse files
committed
tests(rate-limiting): refactoring sync rate tests
1 parent cf58cb8 commit 9804330

File tree

1 file changed

+36
-37
lines changed

1 file changed

+36
-37
lines changed

spec/03-plugins/23-rate-limiting/02-policies_spec.lua

+36-37
Original file line numberDiff line numberDiff line change
@@ -176,42 +176,41 @@ describe("Plugin: rate-limiting (policies)", function()
176176
end)
177177
end
178178

179-
for _, sync_rate in ipairs{1, SYNC_RATE_REALTIME} do
180-
describe("redis with sync rate: " .. sync_rate, function()
181-
local identifier = uuid()
182-
local conf = {
183-
route_id = uuid(),
184-
service_id = uuid(),
185-
redis_host = helpers.redis_host,
186-
redis_port = helpers.redis_port,
187-
redis_database = 0,
188-
sync_rate = sync_rate,
189-
}
190-
191-
before_each(function()
192-
local red = require "resty.redis"
193-
local redis = assert(red:new())
194-
redis:set_timeout(1000)
195-
assert(redis:connect(conf.redis_host, conf.redis_port))
196-
redis:flushall()
197-
redis:close()
198-
end)
199-
200-
it("increase & usage", function()
201-
--[[
202-
Just a simple test:
203-
- increase 1
204-
- check usage == 1
205-
- increase 1
206-
- check usage == 2
207-
- increase 1 (beyond the limit)
208-
- check usage == 3
209-
--]]
210-
211-
local current_timestamp = 1424217600
212-
local periods = timestamp.get_timestamps(current_timestamp)
179+
for _, sync_rate in ipairs{0.5, SYNC_RATE_REALTIME} do
180+
local current_timestamp = 1424217600
181+
local periods = timestamp.get_timestamps(current_timestamp)
182+
183+
for period in pairs(periods) do
184+
describe("redis with sync rate: " .. sync_rate .. " period: " .. period, function()
185+
local identifier = uuid()
186+
local conf = {
187+
route_id = uuid(),
188+
service_id = uuid(),
189+
redis_host = helpers.redis_host,
190+
redis_port = helpers.redis_port,
191+
redis_database = 0,
192+
sync_rate = sync_rate,
193+
}
213194

214-
for period in pairs(periods) do
195+
before_each(function()
196+
local red = require "resty.redis"
197+
local redis = assert(red:new())
198+
redis:set_timeout(1000)
199+
assert(redis:connect(conf.redis_host, conf.redis_port))
200+
redis:flushall()
201+
redis:close()
202+
end)
203+
204+
it("increase & usage", function()
205+
--[[
206+
Just a simple test:
207+
- increase 1
208+
- check usage == 1
209+
- increase 1
210+
- check usage == 2
211+
- increase 1 (beyond the limit)
212+
- check usage == 3
213+
--]]
215214

216215
local metric = assert(policies.redis.usage(conf, identifier, period, current_timestamp))
217216
assert.equal(0, metric)
@@ -232,8 +231,8 @@ describe("Plugin: rate-limiting (policies)", function()
232231
assert.equal(i, metric)
233232
end
234233
end
235-
end
234+
end)
236235
end)
237-
end)
236+
end
238237
end
239238
end)

0 commit comments

Comments
 (0)