Skip to content

Commit

Permalink
chore(tests) add GC tests (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tieske authored Dec 31, 2020
1 parent 877eee1 commit 5c8bd73
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
3 changes: 3 additions & 0 deletions rbusted
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
48 changes: 47 additions & 1 deletion spec/balancer/generic_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ for algorithm, balancer_module in helpers.balancer_types() do
})
snapshot = assert:snapshot()
assert:set_parameter("TableFormatLevel", 10)
collectgarbage()
collectgarbage()
end)


Expand Down Expand Up @@ -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
end

0 comments on commit 5c8bd73

Please sign in to comment.