Test that task properly unrefs its table
This commit is contained in:
parent
72b3afaeea
commit
8443c44671
@ -3,60 +3,46 @@ if not ok then return end
|
||||
|
||||
describe("spawn", function()
|
||||
test("callback receives args", function()
|
||||
task
|
||||
:spawn(function(...)
|
||||
assert(select("#", ...) == 0)
|
||||
end)
|
||||
:await()
|
||||
spawn(function(...)
|
||||
assert(select("#", ...) == 0)
|
||||
end):await()
|
||||
|
||||
task
|
||||
:spawn(function(...)
|
||||
assert(select("#", ...) == 1)
|
||||
assert((...) == nil)
|
||||
end, nil)
|
||||
:await()
|
||||
spawn(function(...)
|
||||
assert(select("#", ...) == 1)
|
||||
assert((...) == nil)
|
||||
end, nil):await()
|
||||
|
||||
task
|
||||
:spawn(function(...)
|
||||
assert(select("#", ...) == 4)
|
||||
local args = table.pack(...)
|
||||
assert(args[1] == 1 and args[2] == 2 and args[3] == nil and args[4] == 3)
|
||||
end, 1, 2, nil, 3)
|
||||
:await()
|
||||
spawn(function(...)
|
||||
assert(select("#", ...) == 4)
|
||||
local args = table.pack(...)
|
||||
assert(args[1] == 1 and args[2] == 2 and args[3] == nil and args[4] == 3)
|
||||
end, 1, 2, nil, 3):await()
|
||||
end)
|
||||
|
||||
test("await returns callback results", function()
|
||||
local res = table.pack(task
|
||||
:spawn(function()
|
||||
-- no returns
|
||||
end)
|
||||
:await())
|
||||
local res = table.pack(spawn(function()
|
||||
-- no returns
|
||||
end):await())
|
||||
assert(res.n == 0)
|
||||
|
||||
local res = table.pack(task
|
||||
:spawn(function()
|
||||
return nil
|
||||
end)
|
||||
:await())
|
||||
local res = table.pack(spawn(function()
|
||||
return nil
|
||||
end):await())
|
||||
assert(res.n == 1 and res[1] == nil)
|
||||
|
||||
local res = table.pack(task
|
||||
:spawn(function()
|
||||
return 1, 2, nil, 3
|
||||
end)
|
||||
:await())
|
||||
local res = table.pack(spawn(function()
|
||||
return 1, 2, nil, 3
|
||||
end):await())
|
||||
assert(res.n == 4 and res[1] == 1 and res[2] == 2 and res[3] == nil and res[4] == 3)
|
||||
end)
|
||||
|
||||
test("callback args and results", function()
|
||||
local res = table.pack(task
|
||||
:spawn(function(...)
|
||||
assert(select("#", ...) == 5)
|
||||
local args = table.pack(...)
|
||||
assert(args[1] == 5 and args[2] == 4 and args[3] == nil and args[4] == 3, args[5] == nil)
|
||||
return 1, 3, nil
|
||||
end, 5, 4, nil, 3, nil)
|
||||
:await())
|
||||
local res = table.pack(spawn(function(...)
|
||||
assert(select("#", ...) == 5)
|
||||
local args = table.pack(...)
|
||||
assert(args[1] == 5 and args[2] == 4 and args[3] == nil and args[4] == 3, args[5] == nil)
|
||||
return 1, 3, nil
|
||||
end, 5, 4, nil, 3, nil):await())
|
||||
assert(res.n == 3 and res[1] == 1 and res[2] == 3 and res[3] == nil)
|
||||
end)
|
||||
end)
|
||||
@ -68,7 +54,30 @@ describe("sleep", function()
|
||||
value = "value"
|
||||
end)
|
||||
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")
|
||||
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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user