lune/tests/task/cancel.luau

35 lines
770 B
Text
Raw Normal View History

local task = require("@lune/task")
2023-01-21 22:23:39 +00:00
-- Cancel should cancel any deferred or delayed threads
local flag: boolean = false
local thread = task.defer(function()
flag = true
end)
2023-01-24 00:21:53 +00:00
task.cancel(thread)
task.wait(0.1)
assert(not flag, "Cancel should handle deferred threads")
local flag2: boolean = false
local thread2 = task.delay(0.1, function()
2023-01-24 00:21:53 +00:00
flag2 = true
2023-01-21 22:23:39 +00:00
end)
task.wait(0)
2023-01-21 22:23:39 +00:00
task.cancel(thread2)
task.wait(0.2)
assert(not flag2, "Cancel should handle delayed threads")
2023-01-21 22:23:39 +00:00
2023-01-24 00:21:53 +00:00
-- Cancellation should work with yields in spawned threads
2023-01-21 22:23:39 +00:00
2023-01-24 00:21:53 +00:00
local flag3: number = 1
local thread3 = task.spawn(function()
task.wait(0.1)
2023-01-24 00:21:53 +00:00
flag3 = 2
task.wait(0.2)
2023-01-24 00:21:53 +00:00
flag3 = 3
2023-01-21 22:23:39 +00:00
end)
task.wait(0.2)
2023-01-24 00:21:53 +00:00
task.cancel(thread3)
task.wait(0.2)
assert(flag3 == 2, "Cancel should properly handle yielding threads")