Improve task.cancel unit test

This commit is contained in:
Filip Tibell 2023-01-23 19:21:53 -05:00
parent bd162a9bd6
commit fcf59455db
No known key found for this signature in database

View file

@ -2,26 +2,30 @@
local flag: boolean = false local flag: boolean = false
local thread = task.defer(function() local thread = task.defer(function()
task.wait(0.1)
flag = true
end)
local thread2 = task.delay(0, function()
task.wait(0.1)
flag = true flag = true
end) end)
task.cancel(thread) task.cancel(thread)
task.cancel(thread2)
task.wait(0.2)
assert(not flag, "Cancel should handle non-immediate threads")
-- Cancellation should be as immediate as possible
local flag2: number = 1
task.spawn(function()
task.wait(0.1) task.wait(0.1)
flag2 = 2 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) task.wait(0.2)
flag2 = 3 flag3 = 3
end) end)
task.wait(0.2) task.wait(0.2)
assert(flag2 == 2, "Cancel should properly handle yielding threads") task.cancel(thread3)
task.wait(0.2)
assert(flag3 == 2, "Cancel should properly handle yielding threads")