From d2cfdfa8e8885d820d3076772745264a2dfc36e9 Mon Sep 17 00:00:00 2001 From: Ryan Ward Date: Mon, 24 Apr 2023 00:15:35 -0400 Subject: [PATCH] Writing tests for system threading --- integration/lanesManager/init.lua | 25 ++++---- integration/lanesManager/threads.lua | 5 ++ integration/loveManager/threads.lua | 4 ++ integration/pseudoManager/threads.lua | 4 ++ tests/threadtests.lua | 89 +++++++++++++++++++++++++++ 5 files changed, 116 insertions(+), 11 deletions(-) create mode 100644 tests/threadtests.lua diff --git a/integration/lanesManager/init.lua b/integration/lanesManager/init.lua index f9da3c4..e9178b9 100644 --- a/integration/lanesManager/init.lua +++ b/integration/lanesManager/init.lua @@ -78,19 +78,22 @@ function multi:newSystemThread(name, func, ...) c.alive = true c.priority = THREAD.Priority_Normal local multi_settings = multi.defaultSettings - for i,v in pairs(multi_settings) do - print(i,v) + local globe = { + THREAD_NAME = name, + THREAD_ID = count, + THREAD = THREAD, + GLOBAL = GLOBAL, + _Console = __ConsoleLinda + } + if GLOBAL["__env"] then + for i,v in pairs(GLOBAL["__env"]) do + globe[i] = v + end end c.thread = lanes.gen("*", { - globals={ -- Set up some globals - THREAD_NAME = name, - THREAD_ID = count, - THREAD = THREAD, - GLOBAL = GLOBAL, - _Console = __ConsoleLinda - }, - priority=c.priority + globals = globe, + priority = c.priority },function(...) require("multi"):init(multi_settings) require("multi.integration.lanesManager.extensions") @@ -168,7 +171,7 @@ function multi.InitSystemThreadErrorHandler() end) end -multi.print("Integrated Lanes!") +multi.print("Integrated Lanes Threading!") multi.integration = {} -- for module creators multi.integration.GLOBAL = GLOBAL multi.integration.THREAD = THREAD diff --git a/integration/lanesManager/threads.lua b/integration/lanesManager/threads.lua index f1b8da1..eadbf80 100644 --- a/integration/lanesManager/threads.lua +++ b/integration/lanesManager/threads.lua @@ -134,6 +134,11 @@ local function INIT(__GlobalLinda, __SleepingLinda, __StatusLinda, __Console) __GlobalLinda:set(k, v) end }) + + function THREAD.setENV(env) + GLOBAL["__env"] = env + end + return GLOBAL, THREAD end diff --git a/integration/loveManager/threads.lua b/integration/loveManager/threads.lua index 74b4954..2629dd5 100644 --- a/integration/loveManager/threads.lua +++ b/integration/loveManager/threads.lua @@ -139,6 +139,10 @@ function threads.getGlobal() ) end +function THREAD.setENV(env) + (threads.getGlobal())["__env"] = env +end + function threads.createTable(n) local _proxy = {} local function set(name,val) diff --git a/integration/pseudoManager/threads.lua b/integration/pseudoManager/threads.lua index b8ee6aa..55e1313 100644 --- a/integration/pseudoManager/threads.lua +++ b/integration/pseudoManager/threads.lua @@ -102,6 +102,10 @@ local function INIT(thread) THREAD.hold = thread.hold + function THREAD.setENV(env) + GLOBAL["__env"] = env + end + return GLOBAL, THREAD end diff --git a/tests/threadtests.lua b/tests/threadtests.lua new file mode 100644 index 0000000..a04c367 --- /dev/null +++ b/tests/threadtests.lua @@ -0,0 +1,89 @@ +package.path = "../?/init.lua;../?.lua;"..package.path +local multi, thread = require("multi"):init{print=true}--{priority=true} +local proc = multi:newProcessor("Test",true) +local LANES, LOVE, PSEUDO = 1, 2, 3 +local env + +if love then + GLOBAL, THREAD = require("multi.integration.loveManager"):init() + env = LOVE +else + io.write("Test Pseudo(p), Lanes(l), or love(Run in love environment) Threading: ") + choice = io.read() + if choice == "p" then + GLOBAL, THREAD = require("multi.integration.pseudoManager"):init() + env = PSEUDO + elseif choice == "l" then + GLOBAL, THREAD = require("multi.integration.lanesManager"):init() + env = LANES + else + error("Invalid threading choice") + end +end + +THREAD.setENV({ + multi_assert = function(expected, actual, s) + print("Testing") + if expected ~= actual then + error(s .. " Expected: '".. expected .."' Actual: '".. actual .."'") + end + end +}) + +multi:newThread("Scheduler Thread",function() + print("Test 1: Thread Spawning, THREAD namaspace in threads, global's working, and queues for passing data.") + + queue = multi:newSystemThreadedQueue("Test_Queue"):init() + + th1 = multi:newSystemThread("Test_Thread_2", function(a,b,c,d,e,f) + queue = THREAD.waitFor("Test_Queue"):init() + print("!") + multi_assert("Test_Thread_1", THREAD.getName(), "Thread name does not match!") + print("!") + multi_assert("Passing some args", a, "First argument is not as expected 'Passing some args'") + multi_assert(true, e, "Argument e is not true!") + multi_assert("table", type(f), "Argument f is not a table!") + queue:push("done") + end,"Passing some args", 1, 2, 3, true, {"Table"}).OnError(print) + + if thread.hold(function() + return queue:pop() == "done" + end,{sleep=1}) == nil then + thread.kill() + end + + print("Test 1: Ok") + + print("Test 2: Threaded Functions, arg passing, return passing, holding.") + + func = THREAD:newFunction(function(a,b,c) + assert(a == 3, "First argument expected '3' got '".. a .."'!") + assert(b == 2, "Second argument expected '2' got '".. a .."'!") + assert(c == 1, "Third argument expected '1' got '".. a .."'!") + return 1, 2, 3, {"a table"} + end, true) -- Hold this + + a, b, c, d = func(3,2,1) + + print("Returns passed from function", a, b, c, d) + + if not a then print(b) end + + assert(a == 1, "First return was not '1'!") + assert(b == 2, "Second return was not '2'!") + assert(c == 3, "Third return was not '3'!") + assert(d[1] == "a table", "Fourth return is not table, or doesn't contain 'a table'!") + + print("Test 2: Ok") + + print("Test 3: SystemThreadedTables") + + os.exit() +end).OnError(function(self, err) + print(err) + os.exit() +end) + + + +multi:mainloop() \ No newline at end of file