From 5c8bd73ddb589bc4c3906269a23e23a0e8b75f82 Mon Sep 17 00:00:00 2001 From: Thijs Schreijer Date: Thu, 31 Dec 2020 08:30:53 +0100 Subject: [PATCH] chore(tests) add GC tests (#108) --- rbusted | 3 +++ spec/balancer/generic_spec.lua | 48 +++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/rbusted b/rbusted index 6c5045a9169..a0b879d8447 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 e3d595277f4..94cb0a3dd4f 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