Skip to content

Commit

Permalink
tests(*): shutdown timerng instance after test completion (#14005)
Browse files Browse the repository at this point in the history
  • Loading branch information
flrgh authored Dec 11, 2024
1 parent d40cedd commit 358fff3
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 35 deletions.
1 change: 1 addition & 0 deletions .busted
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ return {
lpath = "./?.lua;./?/init.lua;",
-- make setup() and teardown() behave like their lazy_ variants
lazy = true,
helper = "./spec/busted-ci-helper.lua",
}
}
1 change: 1 addition & 0 deletions kong-3.10.0-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ build = {
["kong.cmd.utils.prefix_handler"] = "kong/cmd/utils/prefix_handler.lua",
["kong.cmd.utils.process_secrets"] = "kong/cmd/utils/process_secrets.lua",
["kong.cmd.utils.inject_confs"] = "kong/cmd/utils/inject_confs.lua",
["kong.cmd.utils.timer"] = "kong/cmd/utils/timer.lua",

["kong.api"] = "kong/api/init.lua",
["kong.api.api_helpers"] = "kong/api/api_helpers.lua",
Expand Down
11 changes: 3 additions & 8 deletions kong/cmd/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@ math.randomseed() -- Generate PRNG seed

local pl_app = require "pl.lapp"
local log = require "kong.cmd.utils.log"

local function stop_timers()
-- shutdown lua-resty-timer-ng to allow the nginx worker to stop quickly
if _G.timerng then
_G.timerng:destroy()
end
end
local timer = require "kong.cmd.utils.timer"

return function(cmd_name, args)
local cmd = require("kong.cmd." .. cmd_name)
Expand Down Expand Up @@ -42,5 +36,6 @@ return function(cmd_name, args)
pl_app.quit(nil, true)
end)

stop_timers()
-- shutdown lua-resty-timer-ng to allow the nginx worker to stop quickly
timer.shutdown()
end
18 changes: 18 additions & 0 deletions kong/cmd/utils/timer.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
local _M = {}

function _M.shutdown()
if _G.timerng then
pcall(_G.timerng.destroy, _G.timerng)
end

-- kong.init_worker() stashes the timerng instance within the kong global and
-- removes the _G.timerng reference, so check there too
if _G.kong and _G.kong.timer and _G.kong.timer ~= _G.timerng then
pcall(_G.kong.timer.destroy, _G.kong.timer)
_G.kong.timer = nil
end

_G.timerng = nil
end

return _M
67 changes: 40 additions & 27 deletions spec/busted-ci-helper.lua
Original file line number Diff line number Diff line change
@@ -1,43 +1,55 @@
-- busted-ci-helper.lua
local busted = require 'busted'

do
local shutdown_timers = require("kong.cmd.utils.timer").shutdown
assert(type(shutdown_timers) == "function")

-- needed before requiring 'socket.unix'
require 'socket'
-- shutdown lua-resty-timer-ng to allow the nginx worker to stop quickly
busted.subscribe({ 'exit' }, function()
shutdown_timers()

local busted = require 'busted'
local cjson = require 'cjson'
local socket_unix = require 'socket.unix'
-- second return value must be `true`, or else all other callbacks for this
-- event will be skipped
return nil, true
end)
end

local busted_event_path = os.getenv("BUSTED_EVENT_PATH")
local BUSTED_EVENT_PATH = os.getenv("BUSTED_EVENT_PATH")
if BUSTED_EVENT_PATH then
-- needed before requiring 'socket.unix'
require 'socket'

-- Function to recursively copy a table, skipping keys associated with functions
local function copyTable(original, copied, cache, max_depth, current_depth)
copied = copied or {}
cache = cache or {}
max_depth = max_depth or 5
current_depth = current_depth or 1
local cjson = require 'cjson'
local socket_unix = require 'socket.unix'

if cache[original] then return cache[original] end
cache[original] = copied
-- Function to recursively copy a table, skipping keys associated with functions
local function copyTable(original, copied, cache, max_depth, current_depth)
copied = copied or {}
cache = cache or {}
max_depth = max_depth or 5
current_depth = current_depth or 1

for key, value in pairs(original) do
if type(value) == "table" then
if current_depth < max_depth then
copied[key] = copyTable(value, {}, cache, max_depth, current_depth + 1)
if cache[original] then return cache[original] end
cache[original] = copied

for key, value in pairs(original) do
if type(value) == "table" then
if current_depth < max_depth then
copied[key] = copyTable(value, {}, cache, max_depth, current_depth + 1)
end
elseif type(value) == "userdata" then
copied[key] = tostring(value)
elseif type(value) ~= "function" then
copied[key] = value
end
elseif type(value) == "userdata" then
copied[key] = tostring(value)
elseif type(value) ~= "function" then
copied[key] = value
end
end

return copied
end
return copied
end

if busted_event_path then
local sock = assert(socket_unix())
assert(sock:connect(busted_event_path))
assert(sock:connect(BUSTED_EVENT_PATH))

local events = {{ 'suite', 'reset' },
{ 'suite', 'start' },
Expand All @@ -51,6 +63,7 @@ if busted_event_path then
{ 'error', 'it' },
{ 'failure' },
{ 'error' }}

for _, event in ipairs(events) do
busted.subscribe(event, function (...)
local args = {}
Expand Down

1 comment on commit 358fff3

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bazel Build

Docker image available kong/kong:358fff3869ce330880827a947db8bac3b5066215
Artifacts available https://github.com/Kong/kong/actions/runs/12283746182

Please sign in to comment.