Test that task properly unrefs its table

This commit is contained in:
lumi 2025-06-27 04:24:10 +10:00
parent 72b3afaeea
commit 8443c44671
Signed by: luaneko
GPG Key ID: 406809B8763FF07A

View File

@ -3,60 +3,46 @@ if not ok then return end
describe("spawn", function() describe("spawn", function()
test("callback receives args", function() test("callback receives args", function()
task spawn(function(...)
:spawn(function(...)
assert(select("#", ...) == 0) assert(select("#", ...) == 0)
end) end):await()
:await()
task spawn(function(...)
:spawn(function(...)
assert(select("#", ...) == 1) assert(select("#", ...) == 1)
assert((...) == nil) assert((...) == nil)
end, nil) end, nil):await()
:await()
task spawn(function(...)
:spawn(function(...)
assert(select("#", ...) == 4) assert(select("#", ...) == 4)
local args = table.pack(...) local args = table.pack(...)
assert(args[1] == 1 and args[2] == 2 and args[3] == nil and args[4] == 3) assert(args[1] == 1 and args[2] == 2 and args[3] == nil and args[4] == 3)
end, 1, 2, nil, 3) end, 1, 2, nil, 3):await()
:await()
end) end)
test("await returns callback results", function() test("await returns callback results", function()
local res = table.pack(task local res = table.pack(spawn(function()
:spawn(function()
-- no returns -- no returns
end) end):await())
:await())
assert(res.n == 0) assert(res.n == 0)
local res = table.pack(task local res = table.pack(spawn(function()
:spawn(function()
return nil return nil
end) end):await())
:await())
assert(res.n == 1 and res[1] == nil) assert(res.n == 1 and res[1] == nil)
local res = table.pack(task local res = table.pack(spawn(function()
:spawn(function()
return 1, 2, nil, 3 return 1, 2, nil, 3
end) end):await())
:await())
assert(res.n == 4 and res[1] == 1 and res[2] == 2 and res[3] == nil and res[4] == 3) assert(res.n == 4 and res[1] == 1 and res[2] == 2 and res[3] == nil and res[4] == 3)
end) end)
test("callback args and results", function() test("callback args and results", function()
local res = table.pack(task local res = table.pack(spawn(function(...)
:spawn(function(...)
assert(select("#", ...) == 5) assert(select("#", ...) == 5)
local args = table.pack(...) local args = table.pack(...)
assert(args[1] == 5 and args[2] == 4 and args[3] == nil and args[4] == 3, args[5] == nil) assert(args[1] == 5 and args[2] == 4 and args[3] == nil and args[4] == 3, args[5] == nil)
return 1, 3, nil return 1, 3, nil
end, 5, 4, nil, 3, nil) end, 5, 4, nil, 3, nil):await())
:await())
assert(res.n == 3 and res[1] == 1 and res[2] == 3 and res[3] == nil) assert(res.n == 3 and res[1] == 1 and res[2] == 3 and res[3] == nil)
end) end)
end) end)
@ -68,7 +54,30 @@ describe("sleep", function()
value = "value" value = "value"
end) end)
assert(value == nil) assert(value == nil)
sleep(100) -- implicit await: if it's synchronous, value wouldn't change task:sleep(100) -- implicit await: if it's synchronous, value wouldn't change
assert(value == "value") assert(value == "value")
end) end)
end) end)
describe("task", function()
test("properly unrefs arg and ret table", function()
local registry = debug.getregistry()
local ref, t
local cb = function()
return "my ret"
end
do
local task = spawn(cb, "my arg")
ref = task.__ref
t = registry[ref]
assert(type(t) == "table")
assert(t[1] == cb)
assert(t[2] == "my arg")
task:await()
assert(registry[task.__ref] == t)
assert(t[1] == "my ret")
end
collectgarbage()
assert(registry[ref] ~= t) -- if task unref'ed it, it should be either nil or the next freelist number
end)
end)