Skip to content

Commit

Permalink
tests(helpers) extend function wait_timer
Browse files Browse the repository at this point in the history
  • Loading branch information
ADD-SP committed Jun 24, 2022
1 parent d7ce17f commit 54a01cf
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 32 deletions.
2 changes: 1 addition & 1 deletion spec/02-integration/03-db/09-query-semaphore_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe("#postgres Postgres query locks", function()
assert.res_status(204 , res)

-- wait for zero-delay timer
helpers.wait_timer("slow-query", true, true)
helpers.wait_timer("slow-query", true, "any-running")

res = assert(client:send {
method = "GET",
Expand Down
34 changes: 17 additions & 17 deletions spec/03-plugins/23-rate-limiting/04-access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ for _, strategy in helpers.each_strategy() do
assert.equal(true, reset <= 60 and reset >= 0)

-- wait for zero-delay timer
helpers.wait_timer("rate-limiting", 0.5)
helpers.wait_timer("rate-limiting", true, "any-finish")
end

-- Additonal request, while limit is 6/minute
Expand Down Expand Up @@ -407,7 +407,7 @@ for _, strategy in helpers.each_strategy() do
assert.equal(true, reset <= 60 and reset > 0)

-- wait for zero-delay timer
helpers.wait_timer("rate-limiting", 0.5)
helpers.wait_timer("rate-limiting", true, "any-finish")
end

-- Try a different path on the same host. This should reset the timers
Expand All @@ -424,7 +424,7 @@ for _, strategy in helpers.each_strategy() do
assert.equal(true, reset <= 60 and reset > 0)

-- wait for zero-delay timer
helpers.wait_timer("rate-limiting", 0.5)
helpers.wait_timer("rate-limiting", true, "any-finish")
end

-- Continue doing requests on the path which "blocks"
Expand All @@ -441,7 +441,7 @@ for _, strategy in helpers.each_strategy() do
assert.equal(true, reset <= 60 and reset > 0)

-- wait for zero-delay timer
helpers.wait_timer("rate-limiting", 0.5)
helpers.wait_timer("rate-limiting", true, "any-finish")
end

-- Additonal request, while limit is 6/minute
Expand Down Expand Up @@ -476,7 +476,7 @@ for _, strategy in helpers.each_strategy() do
assert.equal(true, reset <= 60 and reset > 0)

-- wait for zero-delay timer
helpers.wait_timer("rate-limiting", 0.5)
helpers.wait_timer("rate-limiting", true, "any-finish")
end

for i = 4, 6 do
Expand All @@ -492,7 +492,7 @@ for _, strategy in helpers.each_strategy() do
assert.equal(true, reset <= 60 and reset > 0)

-- wait for zero-delay timer
helpers.wait_timer("rate-limiting", 0.5)
helpers.wait_timer("rate-limiting", true, "any-finish")
end

-- Additonal request, while limit is 6/minute
Expand Down Expand Up @@ -534,7 +534,7 @@ for _, strategy in helpers.each_strategy() do
assert.equal(true, reset <= 60 and reset > 0)

-- wait for zero-delay timer
helpers.wait_timer("rate-limiting", 0.5)
helpers.wait_timer("rate-limiting", true, "any-finish")
end

local res, body = GET("/status/200", {
Expand Down Expand Up @@ -577,7 +577,7 @@ for _, strategy in helpers.each_strategy() do
assert.equal(true, reset <= 60 and reset >= 0)

-- wait for zero-delay timer
helpers.wait_timer("rate-limiting", 0.5)
helpers.wait_timer("rate-limiting", true, "any-finish")
end

-- Additonal request, while limit is 6/minute
Expand Down Expand Up @@ -617,7 +617,7 @@ for _, strategy in helpers.each_strategy() do
assert.equal(true, reset <= 60 and reset > 0)

-- wait for zero-delay timer
helpers.wait_timer("rate-limiting", 0.5)
helpers.wait_timer("rate-limiting", true, "any-finish")
end

-- Third query, while limit is 2/minute
Expand Down Expand Up @@ -658,7 +658,7 @@ for _, strategy in helpers.each_strategy() do
assert.equal(true, reset <= 60 and reset > 0)

-- wait for zero-delay timer
helpers.wait_timer("rate-limiting", 0.5)
helpers.wait_timer("rate-limiting", true, "any-finish")
end

local res, body = GET("/status/200?apikey=apikey122", {
Expand Down Expand Up @@ -692,7 +692,7 @@ for _, strategy in helpers.each_strategy() do
assert.equal(true, reset <= 60 and reset > 0)

-- wait for zero-delay timer
helpers.wait_timer("rate-limiting", 0.5)
helpers.wait_timer("rate-limiting", true, "any-finish")
end

local res, body = GET("/status/200?apikey=apikey122", {
Expand Down Expand Up @@ -1031,7 +1031,7 @@ for _, strategy in helpers.each_strategy() do
assert.equal(true, reset <= 60 and reset > 0)

-- wait for zero-delay timer
helpers.wait_timer("rate-limiting", 0.5)
helpers.wait_timer("rate-limiting", true, "any-finish")
end

-- Additonal request, while limit is 6/minute
Expand Down Expand Up @@ -1114,7 +1114,7 @@ for _, strategy in helpers.each_strategy() do
assert.equal(true, reset <= 60 and reset > 0)

-- wait for zero-delay timer
helpers.wait_timer("rate-limiting", 0.5)
helpers.wait_timer("rate-limiting", true, "any-finish")
end

-- Additonal request, while limit is 6/minute
Expand Down Expand Up @@ -1198,7 +1198,7 @@ for _, strategy in helpers.each_strategy() do
assert.equal(true, reset <= 60 and reset > 0)

-- wait for zero-delay timer
helpers.wait_timer("rate-limiting", 0.5)
helpers.wait_timer("rate-limiting", true, "any-finish")
end

for i = 1, 6 do
Expand All @@ -1212,7 +1212,7 @@ for _, strategy in helpers.each_strategy() do
assert.equal(true, reset <= 60 and reset > 0)

-- wait for zero-delay timer
helpers.wait_timer("rate-limiting", 0.5)
helpers.wait_timer("rate-limiting", true, "any-finish")
end

-- Additonal request, while limit is 6/minute
Expand Down Expand Up @@ -1289,7 +1289,7 @@ for _, strategy in helpers.each_strategy() do
assert.equal(true, reset <= 60 and reset > 0)

-- wait for zero-delay timer
helpers.wait_timer("rate-limiting", 0.5)
helpers.wait_timer("rate-limiting", true, "any-finish")
end

-- Additonal request, while limit is 6/minute
Expand Down Expand Up @@ -1372,7 +1372,7 @@ for _, strategy in helpers.each_strategy() do
assert.equal(true, reset <= 60 and reset > 0)

-- wait for zero-delay timer
helpers.wait_timer("rate-limiting", 0.5)
helpers.wait_timer("rate-limiting", true, "any-finish")
end

-- Additonal request, while limit is 6/minute
Expand Down
6 changes: 3 additions & 3 deletions spec/03-plugins/34-zipkin/zipkin_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ for _, strategy in helpers.each_strategy() do
assert.res_status(200, res)

-- wait for zero-delay timer
helpers.wait_timer("zipkin", 0.5)
helpers.wait_timer("zipkin", true, "any-finish")

assert.logfile().has.line("reporter flush failed to request: timeout", false, 2)
end)
Expand All @@ -400,7 +400,7 @@ for _, strategy in helpers.each_strategy() do
assert.res_status(200, res)

-- wait for zero-delay timer
helpers.wait_timer("zipkin", 0.5)
helpers.wait_timer("zipkin", true, "any-finish")

assert.logfile().has.line("reporter flush failed to request: timeout", false, 2)
end)
Expand All @@ -416,7 +416,7 @@ for _, strategy in helpers.each_strategy() do
assert.res_status(200, res)

-- wait for zero-delay timer
helpers.wait_timer("zipkin", 0.5)
helpers.wait_timer("zipkin", true, "any-finish")

assert.logfile().has.line("reporter flush failed to request: connection refused", false, 2)
end)
Expand Down
105 changes: 94 additions & 11 deletions spec/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1393,17 +1393,24 @@ end
---wait for some timer
---@param timer_name_pattern string
---@param plain boolean
---@param running? boolean optional, wait for timer running or exit (default = false)
---@param mode string all-finish | all-running | any-finish | any-running | worker-wide-all-finish
---@param timeout? number optional, maximum time to wait (default = 2)
---@param admin_client_timeout? number optional, to override the default timeout setting
---@param forced_admin_port? number optional, to override the default port of admin API
local function wait_timer(timer_name_pattern, plain, running, timeout, admin_client_timeout, forced_admin_port)
local function wait_timer(timer_name_pattern, plain,
mode, timeout,
admin_client_timeout, forced_admin_port)
if not timeout then
timeout = 2
end

local _admin_client

local all_running_each_worker = nil
local all_finish_each_worker = nil
local any_running_each_worker = nil
local any_finish_each_worker = nil

wait_until(function ()
if _admin_client then
_admin_client:close()
Expand All @@ -1413,22 +1420,98 @@ local function wait_timer(timer_name_pattern, plain, running, timeout, admin_cli
local res = assert(_admin_client:get("/timers"))
local body = luassert.res_status(200, res)
local json = assert(cjson.decode(body))
local worker_id = json.worker.id
local worker_count = json.worker.count

if not all_running_each_worker then
all_running_each_worker = {}
all_finish_each_worker = {}
any_running_each_worker = {}
any_finish_each_worker = {}

for i = 0, worker_count - 1 do
all_running_each_worker[i] = false
all_finish_each_worker[i] = false
any_running_each_worker[i] = false
any_finish_each_worker[i] = false
end
end

for timer_name, timer in pairs(json.timers) do
if string.find(timer_name, timer_name_pattern, 1, plain) then
if not running then
return false, "failed to wait " .. timer_name_pattern
local is_matched = false

elseif timer.is_running then
return true
all_finish_each_worker[worker_id] = true

else
return false, "failed to wait " .. timer_name_pattern .. " running"
for timer_name, timer in pairs(json.stats.timers) do
if string.find(timer_name, timer_name_pattern, 1, plain) then
is_matched = true

if timer.is_running then
all_running_each_worker[worker_id] = true
any_running_each_worker[worker_id] = true
all_finish_each_worker[worker_id] = false
goto continue
end

all_running_each_worker[worker_id] = false
any_finish_each_worker[worker_id] = true

goto continue
end

::continue::
end

return true
if not is_matched then
any_finish_each_worker[worker_id] = true
all_finish_each_worker[worker_id] = true
end

local all_running = true

local all_finish = true
local all_finish_worker_wide = true

local any_running = false
local any_finish = false

for _, v in pairs(all_running_each_worker) do
all_running = all_running or v
end

for _, v in pairs(all_finish_each_worker) do
all_finish = all_finish or v
all_finish_worker_wide = all_finish_worker_wide and v
end

for _, v in pairs(any_running_each_worker) do
any_running = any_running or v
end

for _, v in pairs(any_finish_each_worker) do
any_finish = any_finish or v
end

if mode == "all-running" then
return all_running
end

if mode == "all-finish" then
return all_finish
end

if mode == "worker-wide-all-finish" then
return all_finish_worker_wide
end

if mode == "any-finish" then
return any_finish
end

if mode == "any-running" then
return any_running
end

error("unexpected error")
end, timeout)
end

Expand Down

0 comments on commit 54a01cf

Please sign in to comment.