Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid warning during lua unit test #4313

Merged
merged 1 commit into from
Jul 11, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion rootfs/etc/nginx/lua/test/run.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,39 @@
local busted_runner
do
-- avoid warning during test runs caused by
-- https://github.com/openresty/lua-nginx-module/blob/2524330e59f0a385a9c77d4d1b957476dce7cb33/src/ngx_http_lua_util.c#L810

local traceback = require "debug".traceback

setmetatable(_G, { __newindex = function(table, key, value) rawset(table, key, value) end })
busted_runner = require "busted.runner"

-- if there's more constants need to be whitelisted for test runs, add here.
local GLOBALS_ALLOWED_IN_TEST = {
_TEST = true,
}
local newindex = function(table, key, value)
rawset(table, key, value)

local phase = ngx.get_phase()
if phase == "init_worker" or phase == "init" then
return
end

-- we check only timer phase because resty-cli runs everything in timer phase
if phase == "timer" and GLOBALS_ALLOWED_IN_TEST[key] then
return
end

local message = "writing a global lua variable " .. key ..
" which may lead to race conditions between concurrent requests, so prefer the use of 'local' variables " .. traceback('', 2)
-- it's important to do print here because ngx.log is mocked below
print(message)
end
setmetatable(_G, { __newindex = newindex })
end


local ffi = require("ffi")
local lua_ingress = require("lua_ingress")

Expand Down Expand Up @@ -35,4 +71,4 @@ ngx.print = function(...) end

lua_ingress.init_worker()

require "busted.runner"({ standalone = false })
busted_runner({ standalone = false })