2023-05-14 22:16:58 +02:00
|
|
|
local task = require("@lune/task")
|
|
|
|
|
2023-01-22 17:06:35 -05:00
|
|
|
-- Delaying a task should return the thread that can then be cancelled
|
|
|
|
|
|
|
|
local thread = task.delay(0, function() end)
|
|
|
|
assert(type(thread) == "thread", "Delay should return the thread spawned")
|
|
|
|
|
2023-01-21 15:07:18 -05:00
|
|
|
-- Delayed functions should never run right away
|
|
|
|
|
|
|
|
local flag: boolean = false
|
|
|
|
task.delay(0, function()
|
|
|
|
flag = true
|
|
|
|
end)
|
2023-01-21 20:11:17 -05:00
|
|
|
assert(not flag, "Delay should not run instantly or block")
|
2023-02-13 15:28:18 +01:00
|
|
|
task.wait(0.05)
|
2023-01-21 20:11:17 -05:00
|
|
|
assert(flag, "Delay should run after the wanted duration")
|
2023-01-21 15:07:18 -05:00
|
|
|
|
|
|
|
-- Delayed functions should work with yielding
|
|
|
|
|
|
|
|
local flag2: boolean = false
|
2023-02-13 15:28:18 +01:00
|
|
|
task.delay(0.05, function()
|
2023-01-21 15:07:18 -05:00
|
|
|
flag2 = true
|
2023-02-13 15:28:18 +01:00
|
|
|
task.wait(0.1)
|
2023-01-21 15:07:18 -05:00
|
|
|
flag2 = false
|
|
|
|
end)
|
2023-02-13 15:28:18 +01:00
|
|
|
task.wait(0.1)
|
2023-01-21 20:11:17 -05:00
|
|
|
assert(flag, "Delay should work with yielding (1)")
|
2023-02-13 15:28:18 +01:00
|
|
|
task.wait(0.1)
|
2023-01-21 20:11:17 -05:00
|
|
|
assert(not flag2, "Delay should work with yielding (2)")
|
2023-01-21 15:07:18 -05:00
|
|
|
|
2023-02-16 16:19:58 +01:00
|
|
|
-- Defer should be able to be nested
|
|
|
|
|
|
|
|
local flag4: boolean = false
|
|
|
|
task.defer(function()
|
|
|
|
local function nested3()
|
|
|
|
task.defer(function()
|
|
|
|
task.wait(0.05)
|
|
|
|
flag4 = true
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
local function nested2()
|
|
|
|
task.defer(function()
|
|
|
|
task.wait(0.05)
|
|
|
|
nested3()
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
local function nested1()
|
|
|
|
task.defer(function()
|
|
|
|
task.wait(0.05)
|
|
|
|
nested2()
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
task.wait(0.05)
|
|
|
|
nested1()
|
|
|
|
end)
|
|
|
|
task.wait(0.25)
|
|
|
|
assert(flag4, "Defer should work with nesting")
|
|
|
|
|
2023-01-21 15:07:18 -05:00
|
|
|
-- Varargs should get passed correctly
|
|
|
|
|
2023-01-24 12:52:41 -05:00
|
|
|
local fcheck = require("./fcheck")
|
2023-01-24 12:24:57 -05:00
|
|
|
|
|
|
|
local function f(...: any)
|
|
|
|
fcheck(1, "string", select(1, ...))
|
|
|
|
fcheck(2, "number", select(2, ...))
|
|
|
|
fcheck(3, "function", select(3, ...))
|
2023-01-21 15:07:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
task.delay(0, f, "", 1, f)
|
|
|
|
task.delay(0, f, "inf", math.huge, f)
|
|
|
|
task.delay(0, f, "NaN", 0 / 0, f)
|