Add more tests for task spawn
This commit is contained in:
parent
eaa40ff3bc
commit
85ed0d8318
@ -36,6 +36,13 @@ describe("spawn", function()
|
|||||||
assert(res.n == 4 and res[1] == 1 and res[2] == 2 and res[3] == nil and res[4] == 3)
|
assert(res.n == 4 and res[1] == 1 and res[2] == 2 and res[3] == nil and res[4] == 3)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
test("handles invalid args", function()
|
||||||
|
assert(not pcall(spawn))
|
||||||
|
assert(not pcall(spawn, 123))
|
||||||
|
assert(not pcall(spawn, 1, 2, 3))
|
||||||
|
assert(not pcall(spawn, {}, 2, 3))
|
||||||
|
end)
|
||||||
|
|
||||||
test("callback args and results", function()
|
test("callback args and results", function()
|
||||||
local res = table.pack(spawn(function(...)
|
local res = table.pack(spawn(function(...)
|
||||||
assert(select("#", ...) == 5)
|
assert(select("#", ...) == 5)
|
||||||
@ -46,6 +53,25 @@ describe("spawn", function()
|
|||||||
assert(res.n == 3 and res[1] == 1 and res[2] == 3 and res[3] == nil)
|
assert(res.n == 3 and res[1] == 1 and res[2] == 3 and res[3] == nil)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
test("large number of args", function()
|
||||||
|
local args = {}
|
||||||
|
for i = 1, 1000 do
|
||||||
|
args[i] = i
|
||||||
|
end
|
||||||
|
local res = table.pack(spawn(function(...)
|
||||||
|
return ...
|
||||||
|
end, table.unpack(args)):await())
|
||||||
|
assert(res.n == 1000 and res[1] == 1 and res[1000] == 1000)
|
||||||
|
end)
|
||||||
|
|
||||||
|
test("callback closes over upvalues", function()
|
||||||
|
local x = 42
|
||||||
|
local function f()
|
||||||
|
return x
|
||||||
|
end
|
||||||
|
assert(spawn(f):await() == 42)
|
||||||
|
end)
|
||||||
|
|
||||||
test("order is consistent", function()
|
test("order is consistent", function()
|
||||||
-- all tasks spawned in one batch should be resumed in the spawn order
|
-- all tasks spawned in one batch should be resumed in the spawn order
|
||||||
local tasks, nums = {}, {}
|
local tasks, nums = {}, {}
|
||||||
@ -65,6 +91,23 @@ describe("spawn", function()
|
|||||||
assert(nums[i] == i)
|
assert(nums[i] == i)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
test("nested spawns", function()
|
||||||
|
local result = {}
|
||||||
|
local function inner()
|
||||||
|
table.insert(result, "inner")
|
||||||
|
return "done"
|
||||||
|
end
|
||||||
|
local function outer()
|
||||||
|
table.insert(result, "outer")
|
||||||
|
local v = spawn(inner):await()
|
||||||
|
table.insert(result, v)
|
||||||
|
return v
|
||||||
|
end
|
||||||
|
local v = spawn(outer):await()
|
||||||
|
assert(v == "done")
|
||||||
|
assert(result[1] == "outer" and result[2] == "inner" and result[3] == "done")
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe("sleep", function()
|
describe("sleep", function()
|
||||||
@ -77,6 +120,44 @@ describe("sleep", function()
|
|||||||
task.sleep(100) -- implicit await: if it's synchronous, value wouldn't change
|
task.sleep(100) -- implicit await: if it's synchronous, value wouldn't change
|
||||||
assert(value == "value")
|
assert(value == "value")
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
test("sleep in nested spawns", function()
|
||||||
|
local value1, value2, value3 = nil, nil, nil
|
||||||
|
local results = {}
|
||||||
|
local function inner()
|
||||||
|
task.sleep(30)
|
||||||
|
value1 = "set by inner"
|
||||||
|
table.insert(results, "inner")
|
||||||
|
return value1
|
||||||
|
end
|
||||||
|
local function middle()
|
||||||
|
task.sleep(20)
|
||||||
|
value2 = "set by middle"
|
||||||
|
local v = spawn(inner):await()
|
||||||
|
table.insert(results, v)
|
||||||
|
table.insert(results, "middle")
|
||||||
|
return v, value2
|
||||||
|
end
|
||||||
|
local function outer()
|
||||||
|
task.sleep(10)
|
||||||
|
value3 = "set by outer"
|
||||||
|
local v1, v2 = spawn(middle):await()
|
||||||
|
table.insert(results, v1)
|
||||||
|
table.insert(results, v2)
|
||||||
|
table.insert(results, "outer")
|
||||||
|
return v1, v2, value3
|
||||||
|
end
|
||||||
|
assert(value1 == nil and value2 == nil and value3 == nil)
|
||||||
|
local r1, r2, r3 = spawn(outer):await()
|
||||||
|
assert(r1 == "set by inner" and r2 == "set by middle" and r3 == "set by outer")
|
||||||
|
assert(value1 == "set by inner" and value2 == "set by middle" and value3 == "set by outer")
|
||||||
|
assert(results[1] == "inner")
|
||||||
|
assert(results[2] == "set by inner")
|
||||||
|
assert(results[3] == "middle")
|
||||||
|
assert(results[4] == "set by inner")
|
||||||
|
assert(results[5] == "set by middle")
|
||||||
|
assert(results[6] == "outer")
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe("task", function()
|
describe("task", function()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user