diff --git a/rbusted b/rbusted index 6c5045a91693..a0b879d84477 100755 --- a/rbusted +++ b/rbusted @@ -9,5 +9,8 @@ if ngx ~= nil then ngx.exit = function() end end +-- disable globals warning +setmetatable(_G, nil) + -- Busted command-line runner require 'busted.runner'({ standalone = false }) diff --git a/spec/balancer/generic_spec.lua b/spec/balancer/generic_spec.lua index e3d595277f4f..94cb0a3dd4fb 100644 --- a/spec/balancer/generic_spec.lua +++ b/spec/balancer/generic_spec.lua @@ -27,6 +27,8 @@ for algorithm, balancer_module in helpers.balancer_types() do }) snapshot = assert:snapshot() assert:set_parameter("TableFormatLevel", 10) + collectgarbage() + collectgarbage() end) @@ -1843,6 +1845,50 @@ for algorithm, balancer_module in helpers.balancer_types() do end) + + + describe("GC:", function() + + it("removed Hosts get collected",function() + local b = balancer_module.new({ + dns = client, + }) + b:addHost("127.0.0.1", 8000, 100) + + local test_table = setmetatable({}, { __mode = "v" }) + test_table.key = b.hosts[1] + assert.not_nil(next(test_table)) + + -- destroy it + b:removeHost("127.0.0.1", 8000) + collectgarbage() + collectgarbage() + assert.is_nil(next(test_table)) + end) + + + it("dropped balancers get collected",function() + local b = balancer_module.new({ + dns = client, + }) + b:addHost("127.0.0.1", 8000, 100) + + local test_table = setmetatable({}, { __mode = "k" }) + test_table[b] = true + assert.not_nil(next(test_table)) + + -- destroy it + ngx.sleep(0) -- without this it fails, why, why, why? + b = nil -- luacheck: ignore + + collectgarbage() + collectgarbage() + --assert.is_nil(next(test_table)) -- doesn't work, hangs if failed, luassert bug + assert.equal("nil", tostring(next(test_table))) + end) + + end) + end) -end \ No newline at end of file +end