Add new test for require while a background task is running

This commit is contained in:
Filip Tibell 2023-08-22 14:59:27 -05:00
parent 58d64fc0c8
commit b18e1ed519
2 changed files with 52 additions and 0 deletions

View file

@ -67,6 +67,7 @@ create_tests! {
process_spawn: "process/spawn", process_spawn: "process/spawn",
require_async: "require/tests/async", require_async: "require/tests/async",
require_async_background: "require/tests/async_background",
require_async_concurrent: "require/tests/async_concurrent", require_async_concurrent: "require/tests/async_concurrent",
require_async_sequential: "require/tests/async_sequential", require_async_sequential: "require/tests/async_sequential",
require_builtins: "require/tests/builtins", require_builtins: "require/tests/builtins",

View file

@ -0,0 +1,51 @@
local net = require("@lune/net")
local process = require("@lune/process")
local stdio = require("@lune/stdio")
local task = require("@lune/task")
-- Spawn an asynchronous background task (eg. web server)
local PORT = 8082
task.delay(3, function()
stdio.ewrite("Test did not complete in time\n")
task.wait(1)
process.exit(1)
end)
local handle = net.serve(PORT, function(request)
return ""
end)
-- Require modules same way we did in the async_concurrent and async_sequential tests
local module3
local module4
task.defer(function()
module4 = require("./modules/async")
end)
task.spawn(function()
module3 = require("./modules/async")
end)
local _module1 = require("./modules/async")
local _module2 = require("./modules/async")
task.wait(1)
assert(type(module3) == "table", "Required module3 did not return a table")
assert(module3.Foo == "Bar", "Required module3 did not contain correct values")
assert(module3.Hello == "World", "Required module3 did not contain correct values")
assert(type(module4) == "table", "Required module4 did not return a table")
assert(module4.Foo == "Bar", "Required module4 did not contain correct values")
assert(module4.Hello == "World", "Required module4 did not contain correct values")
assert(module3 == module4, "Required modules should point to the same return value")
-- Stop the server and exit successfully
handle.stop()
process.exit(0)