Add sleep test

This commit is contained in:
lumi 2025-06-27 04:11:31 +10:00
parent 503985269a
commit 72b3afaeea
Signed by: luaneko
GPG Key ID: 406809B8763FF07A

View File

@ -3,20 +3,26 @@ if not ok then return end
describe("spawn", function()
test("callback receives args", function()
spawn(function(...)
assert(select("#", ...) == 0)
end):await()
task
:spawn(function(...)
assert(select("#", ...) == 0)
end)
:await()
spawn(function(...)
assert(select("#", ...) == 1)
assert((...) == nil)
end, nil):await()
task
:spawn(function(...)
assert(select("#", ...) == 1)
assert((...) == nil)
end, nil)
: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()
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()
end)
test("await returns callback results", function()
@ -54,3 +60,15 @@ describe("spawn", function()
assert(res.n == 3 and res[1] == 1 and res[2] == 3 and res[3] == nil)
end)
end)
describe("sleep", function()
test("sleep is asynchronous", function()
local value
spawn(function()
value = "value"
end)
assert(value == nil)
sleep(100) -- implicit await: if it's synchronous, value wouldn't change
assert(value == "value")
end)
end)