Add Job System (#656)

* Add Job System

* Remove testing stuff

* changelog

* plugin work

* Convert a couple more jobs

* simplify callback

* assert

* Logging changes and rendezvous perf

* changelog

* sb segment viewed job

* clear cache, and few changes

* Lint fix

* a couple more jobs

* reduce rendezvous in web server

* refactor

* Lint fix

* no message

* test shutdown

* comment

* free fields

---------

Co-authored-by: github-action linter <githubaction@githubaction.com>
This commit is contained in:
Brahim Hadricheandgithub-action linter authored and GitHub committed 2025-06-17 20:13:48 -04:00
1 parent 45983a67af
commit d70a07049a
74 files changed
+1090 -298

No files matched your search

@@ -0,0 +1,152 @@
import "pkg:/components/JobSystem/Jobs.bs"
import "pkg:/components/JobSystem/JobSystem.bs"
namespace Tests
@suite
class JobSystemTests extends Rooibos.BaseTestSuite
protected override function setup()
m.logger = CreateObject("roSGNode", "Logger")
end function
protected override function beforeEach()
m.jobQueue = CreateObject("roSGNode", "JobQueue")
end function
protected override function afterEach()
m.jobQueue.shutdown = true
m.jobQueue = invalid
end function
@describe("JobSystem")
@it("Queues and processes jobs correctly")
function _()
successCalls = 0
errorCalls = 0
messagePort = CreateObject("roMessagePort")
for i = 1 to 100
callbackNode = CreateObject("roSGNode", "JobCallback")
callbackNode.observeField("done", messagePort, ["success"])
JobSystem.QueueJob(m.jobQueue, Jobs.SimpleTestJob, { sleep: 50, success: i mod 2 = 0 }, callbackNode)
end for
while true
msg = wait(1000, messagePort)
if msg = invalid
m.testSuite.fail("Waiting for job messages timed out after 1000ms")
exit while
end if
if type(msg) = "roSGNodeEvent"
if msg.getField() = "done"
info = msg.getInfo()
if info.success
successCalls += 1
else
errorCalls += 1
end if
end if
end if
if successCalls = 50 and errorCalls = 50
exit while
end if
end while
end function
@it("Cancels jobs correctly")
function _()
cancelCalls = 0
messagePort = CreateObject("roMessagePort")
callbacks = []
for i = 1 to 10
callbackNode = CreateObject("roSGNode", "JobCallback")
callbackNode.observeField("done", messagePort, ["success", "cancel"])
callbacks.push(callbackNode)
JobSystem.QueueJob(m.jobQueue, Jobs.SimpleTestJob, { sleep: 5000, success: true }, callbackNode)
end for
' Wait for jobs to start before we start cancelling them
sleep (1000)
for i = 1 to 10
callbackNode = callbacks[i - 1]
callbackNode.cancel = true
end for
while true
msg = wait(1000, messagePort)
if msg = invalid
m.testSuite.fail("Waiting for job messages timed out after 1000ms")
exit while
end if
if type(msg) = "roSGNodeEvent"
if msg.getField() = "done"
info = msg.getInfo()
if info.cancel and not info.success
cancelCalls += 1
else if info.success
m.testSuite.fail("Job should not have succeeded after cancellation")
exit while
end if
end if
end if
if cancelCalls = 10
exit while
end if
end while
end function
@it("Cancels jobs after they start immediately")
function _()
cancelCalls = 0
messagePort = CreateObject("roMessagePort")
callbacks = []
for i = 1 to 10
callbackNode = CreateObject("roSGNode", "JobCallback")
callbackNode.observeField("done", messagePort, ["success", "cancel"])
callbacks.push(callbackNode)
JobSystem.QueueJob(m.jobQueue, Jobs.SimpleTestJob, { sleep: 5000, success: true }, callbackNode)
callbackNode.cancel = true
end for
while true
msg = wait(1000, messagePort)
if msg = invalid
m.testSuite.fail("Waiting for job messages timed out after 1000ms")
exit while
end if
if type(msg) = "roSGNodeEvent"
if msg.getField() = "done"
info = msg.getInfo()
if info.cancel and not info.success
cancelCalls += 1
else if info.success
m.testSuite.fail("Job should not have succeeded after cancellation")
exit while
end if
end if
end if
if cancelCalls = 10
exit while
end if
end while
end function
end class
end namespace