lune/tests/task/cancel.luau
2023-02-05 19:13:58 -05:00

31 lines
720 B
Lua

-- Cancel should cancel any deferred or delayed threads
local flag: boolean = false
local thread = task.defer(function()
flag = true
end)
task.cancel(thread)
task.wait(0.1)
assert(not flag, "Cancel should handle deferred threads")
local flag2: boolean = false
local thread2 = task.delay(0, function()
flag2 = true
end)
task.cancel(thread2)
task.wait(0.1)
assert(not flag2, "Cancel should handle deferred threads")
-- Cancellation should work with yields in spawned threads
local flag3: number = 1
local thread3 = task.spawn(function()
task.wait(0.1)
flag3 = 2
task.wait(0.2)
flag3 = 3
end)
task.wait(0.2)
task.cancel(thread3)
task.wait(0.2)
assert(flag3 == 2, "Cancel should properly handle yielding threads")