Fix race condition in task state table unref
This commit is contained in:
@@ -19,7 +19,7 @@ local icons = {
|
||||
}
|
||||
|
||||
local function color(name, s)
|
||||
return colors[name] .. s .. colors.reset
|
||||
return ("%s %s %s"):format(colors[name], s, colors.reset)
|
||||
end
|
||||
|
||||
local function create_test(name, f, group)
|
||||
@@ -54,7 +54,7 @@ local function name_test(test)
|
||||
local name = test.name
|
||||
local group = test.group
|
||||
while group ~= nil do
|
||||
if group.name ~= "" then name = string.format("%s %s %s", group.name, icons.chevron, name) end
|
||||
if group.name ~= "" then name = ("%s %s %s"):format(group.name, icons.chevron, name) end
|
||||
group = group.parent
|
||||
end
|
||||
return name
|
||||
@@ -68,11 +68,12 @@ local function run_test(test)
|
||||
local ok, res = xpcall(test.f, trace, test)
|
||||
if ok then
|
||||
test.state = "pass"
|
||||
print("", string.format("%s %s", color("pass", "PASS"), name_test(test)))
|
||||
print("", ("%s %s"):format(color("pass", "PASS"), name_test(test)))
|
||||
else
|
||||
test.state = "fail"
|
||||
print("", string.format("%s %s\n\n%s\n", color("fail", "FAIL"), name_test(test), res))
|
||||
print("", ("%s %s\n\n%s\n"):format(color("fail", "FAIL"), name_test(test), res))
|
||||
end
|
||||
collectgarbage() -- gc after each test to test destructors
|
||||
return test
|
||||
end
|
||||
|
||||
@@ -86,6 +87,23 @@ local function start(cx, item)
|
||||
end
|
||||
end
|
||||
|
||||
local function check_unrefs()
|
||||
-- ensure all refs were properly unref'ed
|
||||
local registry = debug.getregistry()
|
||||
local count = #registry
|
||||
local ref = 0 -- FREELIST_REF
|
||||
while type(registry[ref]) == "number" do
|
||||
local next = registry[ref]
|
||||
registry[ref], ref = nil, next
|
||||
end
|
||||
for i = 1, count do
|
||||
local value = registry[i]
|
||||
if type(value) ~= "thread" then -- ignore threads pinned by the runtime
|
||||
assert(rawequal(registry[i], nil), ("ref %d not unref'ed: %s"):format(i, registry[i]))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function main(item)
|
||||
local cx = { tasks = {} }
|
||||
local pass, fail = 0, 0
|
||||
@@ -97,17 +115,23 @@ local function main(item)
|
||||
fail = fail + 1
|
||||
end
|
||||
end
|
||||
local code = 1
|
||||
if fail == 0 then
|
||||
print("", color("pass", string.format("%s %d tests passed", icons.check, pass)))
|
||||
return 0
|
||||
print("", color("pass", ("%s %d tests passed"):format(icons.check, pass)))
|
||||
code = 0
|
||||
else
|
||||
print(
|
||||
"",
|
||||
("%s, %s"):format(
|
||||
color("pass", ("%s %d tests passed"):format(icons.check, pass)),
|
||||
color("fail", ("%s %d tests failed"):format(icons.cross, fail))
|
||||
)
|
||||
)
|
||||
end
|
||||
print(
|
||||
"",
|
||||
color("pass", string.format("%s %d tests passed", icons.check, pass))
|
||||
.. ", "
|
||||
.. color("fail", string.format("%s %d tests failed", icons.cross, fail))
|
||||
)
|
||||
return 1 -- report error to cargo
|
||||
cx = nil
|
||||
collectgarbage()
|
||||
check_unrefs()
|
||||
return code -- report error to cargo
|
||||
end
|
||||
|
||||
return main(create_group("", function()
|
||||
|
||||
Reference in New Issue
Block a user