Improving tests

This commit is contained in:
2026-01-12 23:15:31 -08:00
parent f8265fbb3b
commit 40de9a8d22
6 changed files with 308 additions and 160 deletions
+3 -5
View File
@@ -1,8 +1,6 @@
# Multi Version: 16.1.0 - The Flow State # Multi Version: 16.3.0 -
**Key Changes** **Key Changes**
- Updated Processors to have more controlled over scheduled processes - Fixed connection multiplying
- Forwarding connections
- New Timeout handling
Refer to the [Change Log](https://github.com/rayaman/multi/blob/master/docs/changes.md) for more infromation Refer to the [Change Log](https://github.com/rayaman/multi/blob/master/docs/changes.md) for more infromation
@@ -12,7 +10,7 @@ My multitasking library for lua. It is a pure lua binding, with exceptions of th
</br> </br>
Progress is being made in [v16.2.0](https://github.com/rayaman/multi/tree/v16.2.0) Progress is being made in [v16.3.0](https://github.com/rayaman/multi/tree/v16.3.0)
--- ---
</br> </br>
+19 -6
View File
@@ -1,7 +1,8 @@
# Changelog # Changelog
Table of contents Table of contents
--- ---
[Update 16.2.0 - TimeStamps and UUIDs](#update-1620---timestamps-and-uuids-and-bugfixes)</br> [Update 16.3.0 - Bug Fixes](#update-1620---bug-fixes)</br>
[Update 16.2.0 - TimeStamps and UUIDs](#update-1620---timestamps-and-uuids)</br>
[Update 16.1.0 - The Flow State](#update-1610---the-flow-state)</br> [Update 16.1.0 - The Flow State](#update-1610---the-flow-state)</br>
[Update 16.0.1 - Bug fix](#update-1601---bug-fix)</br> [Update 16.0.1 - Bug fix](#update-1601---bug-fix)</br>
[Update 16.0.0 - Connecting the dots](#update-1600---getting-the-priorities-straight)</br> [Update 16.0.0 - Connecting the dots](#update-1600---getting-the-priorities-straight)</br>
@@ -61,6 +62,20 @@ Table of contents
[Update: EventManager 1.0.0 - Error checking](#update-eventmanager-100---error-checking)</br> [Update: EventManager 1.0.0 - Error checking](#update-eventmanager-100---error-checking)</br>
[Version: EventManager 0.0.1 - In The Beginning things were very different](#version-eventmanager-001---in-the-beginning-things-were-very-different) [Version: EventManager 0.0.1 - In The Beginning things were very different](#version-eventmanager-001---in-the-beginning-things-were-very-different)
# Update 16.3.0 - Bug Fixes
Added
---
Changed
---
- `thread.kill(msg)` now accepts a message. If omitted will use default `thread killed!`
Fixed
---
- [Issue](https://github.com/rayaman/multi/issues/77) with concat of connections where conn .. func did not work properly
- [Issue](https://github.com/rayaman/multi/issues/67) with connection multiplying
# Update 16.2.0 - TimeStamps and UUIDs and bugfixes # Update 16.2.0 - TimeStamps and UUIDs and bugfixes
Added Added
--- ---
@@ -629,15 +644,13 @@ Added
Output: Output:
``` ```
I run before all and control if execution should continue! I run before all and control if things go!
Hi 3 Hi 3
Hi 1 Hi 1
I run before all and control if execution should continue!
Hi 3
Hi 2
Test 1 Test 1
Test 2 Test 2
Test 3 Test 3
I run after it all!
``` ```
**Note:** Concat of connections does modify internal events on both connections depending on the direction func .. conn or conn .. func See implemention below: **Note:** Concat of connections does modify internal events on both connections depending on the direction func .. conn or conn .. func See implemention below:
@@ -3672,4 +3685,4 @@ end
event:Manager() event:Manager()
--event:CManager() --event:CManager()
--event:UManager() -- One of the few things that lived in name and spirit the u just became lowercase haha --event:UManager() -- One of the few things that lived in name and spirit the u just became lowercase haha
``` ```
+6 -16
View File
@@ -92,7 +92,7 @@ function multi.getTypes()
return types return types
end end
multi.Version = "16.2.0" multi.Version = "16.3.0"
multi.Name = "root" multi.Name = "root"
multi.NIL = {Type="NIL"} multi.NIL = {Type="NIL"}
local NIL = multi.NIL local NIL = multi.NIL
@@ -298,7 +298,7 @@ function multi:newConnection(protect,func,kill)
local ref local ref
if type(obj1) == "function" and type(obj2) == "table" then if type(obj1) == "function" and type(obj2) == "table" then
cn(function(...) cn(function(...)
if obj1(...) then if obj1(...) == true then
obj2:Fire(...) obj2:Fire(...)
end end
end) end)
@@ -320,7 +320,7 @@ function multi:newConnection(protect,func,kill)
end end
end) end)
end end
return obj1 return cn
elseif type(obj1) == "table" and type(obj2) == "table" then elseif type(obj1) == "table" and type(obj2) == "table" then
-- --
else else
@@ -367,8 +367,6 @@ function multi:newConnection(protect,func,kill)
if cn.__count[1] == cn.__hasInstances[1] then if cn.__count[1] == cn.__hasInstances[1] then
cn:Fire(...) cn:Fire(...)
cn.__count[1] = 0 cn.__count[1] = 0
c1:Unlock(ref1)
c2:Unlock(ref2)
end end
end) end)
return cn return cn
@@ -698,15 +696,6 @@ function multi:isDone()
return self.Active~=true return self.Active~=true
end end
local time = os.time
local ok, chronos = pcall(require, "chronos") -- hpc
if ok then
math.randomseed(chronos.nanotime()*100000000)
else
math.randomseed(time())
end
function multi:create(ref) function multi:create(ref)
ref.UID = multi.generate_uuid7() ref.UID = multi.generate_uuid7()
self.OnObjectCreated:Fire(ref, self) self.OnObjectCreated:Fire(ref, self)
@@ -1517,8 +1506,9 @@ function thread.skip(n)
return yield(CMD, t_skip, n or 1) return yield(CMD, t_skip, n or 1)
end end
function thread.kill() function thread.kill(msg)
multi.error("thread killed!") msg = msg or "thread killed!"
multi.error(msg)
end end
function thread.yield() function thread.yield()
+208 -132
View File
@@ -1,9 +1,74 @@
package.path = "../?/init.lua;../?.lua;./init.lua;./?.lua;"..package.path package.path = "../?/init.lua;../?.lua;./init.lua;./?.lua;" .. package.path
local multi, thread = require("multi"):init{print=true,warn=true,error=true}--{priority=true} local multi, thread = require("multi"):init{print=true,warn=true,error=true}--{priority=true}
local good = false local good = false
local proc = multi:newProcessor("Test") local proc = multi:newProcessor("Test")
local testing = {}
function testing.False(value)
if value ~= false then
thread.kill("Expected value to be false")
end
end
function testing.True(value)
if value ~= true then
thread.kill("Expected value to be true")
end
end
function testing.IsNil(value)
if value ~= nil then
thread.kill("Expected nil, but got: " .. tostring(value) .."")
end
end
function testing.NotNil(value)
if value == nil then
thread.kill("Expected value not to be nil.")
end
end
function testing.Equal(expected, actual)
if expected ~= actual then
thread.kill("Not equal: \n" ..
"expected: " .. tostring(expected) .. "\n" ..
"actual : " .. tostring(actual) .. "\n")
end
end
function testing.NotEqual(expected, actual)
if expected == actual then
thread.kill("Should not be: values are equal")
end
end
function getOS()
-- ask LuaJIT first
if jit then
return jit.os
end
-- Unix, Linux variants
local fh,err = assert(io.popen("uname -o 2>/dev/null","r"))
if fh then
osname = fh:read()
end
return osname or "Windows"
end
function ListFiles(path)
files = {}
if getOS() == "Windows" then
for dir in io.popen([[dir /A-d "]] .. path .. [[" /b]]):lines() do table.insert(files,dir) end
else
for dir in io.popen([[ls -pa "]] .. path .. [[" | grep -v /]]):lines() do table.insert(files,dir) end
end
return files
end
print("Version: "..multi.Version) print("Version: "..multi.Version)
proc.Start() proc.Start()
@@ -13,147 +78,158 @@ proc:newAlarm(3):OnRing(function()
end) end)
runTest = thread:newFunction(function() runTest = thread:newFunction(function()
local alarms,tsteps,steps,loops,tloops,updaters,events=false,0,0,0,0,0,false -- local alarms,tsteps,steps,loops,tloops,updaters,events=false,0,0,0,0,0,false
multi.print("Testing Basic Features. If this fails most other features will probably not work!") -- multi.print("Testing Basic Features. If this fails most other features will probably not work!")
proc:newAlarm(2):OnRing(function(a) -- proc:newAlarm(2):OnRing(function(a)
alarms = true -- alarms = true
a:Destroy() -- a:Destroy()
end) -- end)
proc:newTStep(1,10,1,.1):OnStep(function(t) -- proc:newTStep(1,10,1,.1):OnStep(function(t)
tsteps = tsteps + 1 -- tsteps = tsteps + 1
end):OnEnd(function(step) -- end):OnEnd(function(step)
step:Destroy() -- step:Destroy()
end) -- end)
proc:newStep(1,10):OnStep(function(s) -- proc:newStep(1,10):OnStep(function(s)
steps = steps + 1 -- steps = steps + 1
end):OnEnd(function(step) -- end):OnEnd(function(step)
step:Destroy() -- step:Destroy()
end) -- end)
local loop = proc:newLoop(function(l) -- local loop = proc:newLoop(function(l)
loops = loops + 1 -- loops = loops + 1
end) -- end)
proc:newTLoop(function(t) -- proc:newTLoop(function(t)
tloops = tloops + 1 -- tloops = tloops + 1
end,.1) -- end,.1)
local updater = proc:newUpdater(1):OnUpdate(function() -- local updater = proc:newUpdater(1):OnUpdate(function()
updaters = updaters + 1 -- updaters = updaters + 1
end) -- end)
local event = proc:newEvent(function() -- local event = proc:newEvent(function()
return alarms -- return alarms
end) -- end)
event.OnEvent(function(evnt) -- event.OnEvent(function(evnt)
evnt:Destroy() -- evnt:Destroy()
events = true -- events = true
multi.success("Alarms: Ok") -- multi.success("Alarms: Ok")
multi.success("Events: Ok") -- multi.success("Events: Ok")
if tsteps == 10 then multi.success("TSteps: Ok") else multi.error("TSteps: Bad!") end -- if tsteps == 10 then multi.success("TSteps: Ok") else multi.error("TSteps: Bad!") end
if steps == 10 then multi.success("Steps: Ok") else multi.error("Steps: Bad!") end -- if steps == 10 then multi.success("Steps: Ok") else multi.error("Steps: Bad!") end
if loops > 100 then multi.success("Loops: Ok") else multi.error("Loops: Bad!") end -- if loops > 100 then multi.success("Loops: Ok") else multi.error("Loops: Bad!") end
if tloops > 10 then multi.success("TLoops: Ok") else multi.error("TLoops: Bad!") end -- if tloops > 10 then multi.success("TLoops: Ok") else multi.error("TLoops: Bad!") end
if updaters > 100 then multi.success("Updaters: Ok") else multi.error("Updaters: Bad!") end -- if updaters > 100 then multi.success("Updaters: Ok") else multi.error("Updaters: Bad!") end
end) -- end)
thread.hold(event.OnEvent) -- thread.hold(event.OnEvent)
multi.print("Starting Connection and Thread tests!") -- multi.print("Starting Connection and Thread tests!")
func = thread:newFunction(function(count) -- func = thread:newFunction(function(count)
multi.print("Starting Status test: ",count) -- multi.print("Starting Status test: ",count)
local a = 0 -- local a = 0
while true do -- while true do
a = a + 1 -- a = a + 1
thread.sleep(.1) -- thread.sleep(.1)
thread.pushStatus(a,count) -- thread.pushStatus(a,count)
if a == count then break end -- if a == count then break end
end -- end
return "Done", true, math.random(1,10000) -- return "Done", true, math.random(1,10000)
end) -- end)
local ret = func(10) -- local ret = func(10)
local ret2 = func(15) -- local ret2 = func(15)
local ret3 = func(20) -- local ret3 = func(20)
local s1,s2,s3 = 0,0,0 -- local s1,s2,s3 = 0,0,0
ret.OnError(function(...) -- ret.OnError(function(...)
multi.error("Func 1:",...) -- multi.error("Func 1:",...)
end) -- end)
ret2.OnError(function(...) -- ret2.OnError(function(...)
multi.error("Func 2:",...) -- multi.error("Func 2:",...)
end) -- end)
ret3.OnError(function(...) -- ret3.OnError(function(...)
multi.error("Func 3:",...) -- multi.error("Func 3:",...)
end) -- end)
ret.OnStatus(function(part,whole) -- ret.OnStatus(function(part,whole)
s1 = math.ceil((part/whole)*1000)/10 -- s1 = math.ceil((part/whole)*1000)/10
end) -- end)
ret2.OnStatus(function(part,whole) -- ret2.OnStatus(function(part,whole)
s2 = math.ceil((part/whole)*1000)/10 -- s2 = math.ceil((part/whole)*1000)/10
end) -- end)
ret3.OnStatus(function(part,whole) -- ret3.OnStatus(function(part,whole)
s3 = math.ceil((part/whole)*1000)/10 -- s3 = math.ceil((part/whole)*1000)/10
end) -- end)
ret.OnReturn(function(...) -- ret.OnReturn(function(...)
multi.success("Done 1",...) -- multi.success("Done 1",...)
end) -- end)
ret2.OnReturn(function(...) -- ret2.OnReturn(function(...)
multi.success("Done 2",...) -- multi.success("Done 2",...)
end) -- end)
ret3.OnReturn(function(...) -- ret3.OnReturn(function(...)
multi.success("Done 3",...) -- multi.success("Done 3",...)
end) -- end)
local err, timeout = thread.hold(ret.OnReturn * ret2.OnReturn * ret3.OnReturn) -- local err, timeout = thread.hold(ret.OnReturn * ret2.OnReturn * ret3.OnReturn)
if s1 == 100 and s2 == 100 and s3 == 100 then -- if s1 == 100 and s2 == 100 and s3 == 100 then
multi.success("Threads: All tests Ok") -- multi.success("Threads: All tests Ok")
else -- else
if s1>0 and s2>0 and s3 > 0 then -- if s1>0 and s2>0 and s3 > 0 then
multi.success("Thread OnStatus: Ok") -- multi.success("Thread OnStatus: Ok")
else -- else
multi.error("Threads OnStatus or thread.hold(conn) Error!") -- multi.error("Threads OnStatus or thread.hold(conn) Error!")
end -- end
if timeout then -- if timeout then
multi.error("Connection Error!") -- multi.error("Connection Error!")
else -- else
multi.success("Connection Test 1: Ok") -- multi.success("Connection Test 1: Ok")
end -- end
multi.error("Connection holding Error!") -- multi.error("Connection holding Error!")
end -- end
conn1 = proc:newConnection() -- conn1 = proc:newConnection()
conn2 = proc:newConnection() -- conn2 = proc:newConnection()
conn3 = proc:newConnection() -- conn3 = proc:newConnection()
local c1,c2,c3,c4 = false,false,false,false -- local c1,c2,c3,c4 = false,false,false,false
local a = conn1(function() -- local a = conn1(function()
c1 = true -- c1 = true
end) -- end)
local b = conn2(function() -- local b = conn2(function()
c2 = true -- c2 = true
end) -- end)
local c = conn3(function() -- local c = conn3(function()
c3 = true -- c3 = true
end) -- end)
local d = conn3(function() -- local d = conn3(function()
c4 = true -- c4 = true
end) -- end)
conn1:Fire() -- conn1:Fire()
conn2:Fire() -- conn2:Fire()
conn3:Fire() -- conn3:Fire()
if c1 and c2 and c3 and c4 then -- if c1 and c2 and c3 and c4 then
multi.success("Connection Test 2: Ok") -- multi.success("Connection Test 2: Ok")
else -- else
multi.error("Connection Test 2: Error") -- multi.error("Connection Test 2: Error")
end -- end
c3 = false -- c3 = false
c4 = false -- c4 = false
conn3:Unconnect(d) -- conn3:Unconnect(d)
conn3:Fire() -- conn3:Fire()
if c3 and not(c4) then -- if c3 and not(c4) then
multi.success("Connection Test 3: Ok") -- multi.success("Connection Test 3: Ok")
else -- else
multi.error("Connection Test 3: Error removing connection") -- multi.error("Connection Test 3: Error removing connection")
-- end
units = ListFiles("tests/units")
for _, file in pairs(units) do
multi.print("Running tests in: " .. file)
unit = loadfile("tests/units/" .. file)()
function Test(...)
unit.Test(...)
multi.success(file)
end
func = thread:newFunction(Test, true)
func(multi:newProcessor(file), thread, testing)
end end
if not love then if not love then
local ec = 0 local ec = 0
+1 -1
View File
@@ -1,4 +1,4 @@
package.path = "D:/VSCWorkspace/?/init.lua;D:/VSCWorkspace/?.lua;"..package.path package.path = "../?/init.lua;../?.lua;./init.lua;./?.lua;" .. package.path
package.cpath = "C:/luaInstalls/lua5.4/lib/lua/5.4/?/core.dll;" .. package.cpath package.cpath = "C:/luaInstalls/lua5.4/lib/lua/5.4/?/core.dll;" .. package.cpath
multi, thread = require("multi"):init{error=true,warning=true,print=true, priority=true} multi, thread = require("multi"):init{error=true,warning=true,print=true, priority=true}
proc = multi:newProcessor("Thread Test",true) proc = multi:newProcessor("Thread Test",true)
+71
View File
@@ -0,0 +1,71 @@
function Test(multi, thread, t)
multi.print("Testing Connection operators")
do
multi.print("Testing: conn1 + conn2")
local conn1 = multi:newConnection()
local conn2 = multi:newConnection()
local conn3 = conn1 + conn2
local count = 0
conn3(function()
count = count + 1
end)
conn1:Fire()
t.Equal(1, count)
conn2:Fire()
t.Equal(2, count)
end
do
multi.print("Testing: conn1 * conn2")
local conn1 = multi:newConnection()
local conn2 = multi:newConnection()
local conn3 = conn1 * conn2
local count = 0
conn3(function()
count = count + 1
end)
conn1:Fire()
t.Equal(0, count)
conn2:Fire()
t.Equal(1, count)
end
do
multi.print("Testing: conn .. function")
local called = false
local conn1 = multi:newConnection()
local conn2 = conn1 .. function() called = true end
conn1(function()
t.False(called)
end)
conn2:Fire()
t.True(called)
end
do
multi.print("function .. conn")
local status = false
local conn1 = multi:newConnection()
local conn2 = function(test) return test end .. conn1
conn1(function()
status = true
end)
conn2:Fire(false)
t.False(status)
conn2:Fire(true)
t.True(status)
end
end
return {
Test = Test
}