2023-05-14 21:16:58 +01:00
|
|
|
local task = require("@lune/task")
|
|
|
|
|
2023-01-22 22:06:35 +00:00
|
|
|
-- Spawning a task should return the thread that can then be cancelled
|
|
|
|
|
|
|
|
local thread = task.spawn(function() end)
|
|
|
|
assert(type(thread) == "thread", "Spawn should return the thread spawned")
|
|
|
|
|
2023-01-21 20:07:18 +00:00
|
|
|
-- Spawned functions should run right away
|
|
|
|
|
|
|
|
local flag: boolean = false
|
|
|
|
task.spawn(function()
|
|
|
|
flag = true
|
|
|
|
end)
|
2023-01-22 01:11:17 +00:00
|
|
|
assert(flag, "Spawn should run instantly")
|
2023-01-21 20:07:18 +00:00
|
|
|
|
|
|
|
-- Spawned functions should work with yielding
|
|
|
|
|
|
|
|
local flag2: boolean = false
|
|
|
|
task.spawn(function()
|
2023-02-13 14:28:18 +00:00
|
|
|
task.wait(0.05)
|
2023-01-21 20:07:18 +00:00
|
|
|
flag2 = true
|
|
|
|
end)
|
2023-01-22 01:11:17 +00:00
|
|
|
assert(not flag2, "Spawn should work with yielding (1)")
|
2023-02-13 14:28:18 +00:00
|
|
|
task.wait(0.1)
|
2023-01-22 01:11:17 +00:00
|
|
|
assert(flag2, "Spawn should work with yielding (2)")
|
2023-01-21 20:07:18 +00:00
|
|
|
|
2023-01-24 00:13:18 +00:00
|
|
|
-- Spawned functions should be able to run threads created with the coroutine global
|
|
|
|
|
|
|
|
local flag3: boolean = false
|
|
|
|
local thread2 = coroutine.create(function()
|
|
|
|
flag3 = true
|
|
|
|
end)
|
|
|
|
task.spawn(thread2)
|
|
|
|
assert(flag3, "Spawn should run threads made from coroutine.create")
|
|
|
|
|
2023-02-16 15:19:58 +00:00
|
|
|
-- Spawn should be able to be nested
|
|
|
|
|
|
|
|
local flag4: boolean = false
|
|
|
|
task.spawn(function()
|
|
|
|
local function nested3()
|
|
|
|
task.spawn(function()
|
|
|
|
task.wait(0.05)
|
|
|
|
flag4 = true
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
local function nested2()
|
|
|
|
task.spawn(function()
|
|
|
|
task.wait(0.05)
|
|
|
|
nested3()
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
local function nested1()
|
|
|
|
task.spawn(function()
|
|
|
|
task.wait(0.05)
|
|
|
|
nested2()
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
task.wait(0.05)
|
|
|
|
nested1()
|
|
|
|
end)
|
|
|
|
task.wait(0.25)
|
|
|
|
assert(flag4, "Spawn should work with nesting")
|
|
|
|
|
2023-01-21 20:07:18 +00:00
|
|
|
-- Varargs should get passed correctly
|
|
|
|
|
2023-01-24 17:52:41 +00:00
|
|
|
local fcheck = require("./fcheck")
|
2023-01-24 17:24:57 +00:00
|
|
|
|
|
|
|
local function f(...: any)
|
|
|
|
fcheck(1, "string", select(1, ...))
|
|
|
|
fcheck(2, "number", select(2, ...))
|
|
|
|
fcheck(3, "function", select(3, ...))
|
2023-01-21 20:07:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
task.spawn(f, "", 1, f)
|
|
|
|
task.spawn(f, "inf", math.huge, f)
|
|
|
|
task.spawn(f, "NaN", 0 / 0, f)
|