summaryrefslogtreecommitdiffstats
path: root/tests/config/worker.test.lua
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 15:26:00 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 15:26:00 +0000
commit830407e88f9d40d954356c3754f2647f91d5c06a (patch)
treed6a0ece6feea91f3c656166dbaa884ef8a29740e /tests/config/worker.test.lua
parentInitial commit. (diff)
downloadknot-resolver-830407e88f9d40d954356c3754f2647f91d5c06a.tar.xz
knot-resolver-830407e88f9d40d954356c3754f2647f91d5c06a.zip
Adding upstream version 5.6.0.upstream/5.6.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/config/worker.test.lua')
-rw-r--r--tests/config/worker.test.lua65
1 files changed, 65 insertions, 0 deletions
diff --git a/tests/config/worker.test.lua b/tests/config/worker.test.lua
new file mode 100644
index 0000000..756bb5f
--- /dev/null
+++ b/tests/config/worker.test.lua
@@ -0,0 +1,65 @@
+-- SPDX-License-Identifier: GPL-3.0-or-later
+-- check prerequisites
+if not worker.bg_worker then
+ -- skipping worker test because it doesnt support background worker
+ os.exit(77)
+else
+ -- import primitives for synchronisation
+ local monotime = require('cqueues').monotime
+
+ -- test whether sleeping works
+ local function test_worker_sleep()
+ local now = monotime()
+ ok(pcall(worker.sleep, 0.1), 'sleep works')
+ local elapsed = monotime() - now
+ ok(elapsed > 0, 'sleep takes non-zero time')
+ end
+
+ -- helper to track number of executions
+ local cv = require('cqueues.condition').new()
+ local tasks = 0
+ local function work ()
+ worker.sleep(0.1)
+ tasks = tasks - 1
+ if tasks == 0 then
+ cv:signal()
+ elseif tasks < 0 then
+ error('too many executions')
+ end
+ end
+
+ -- test whether coroutines work
+ local function test_worker_coroutine()
+ tasks = 2
+ worker.coroutine(work)
+ worker.coroutine(work)
+ -- Check if coroutines finish
+ local status = cv:wait(1)
+ same(tasks, 0, 'all coroutines finish')
+ ok(status, 'coroutines finish successfully')
+ -- Check if nesting coroutines works
+ local now = monotime()
+ tasks = 100
+ worker.coroutine(function ()
+ for _ = 1, 100 do
+ worker.coroutine(work)
+ end
+ end)
+ status = cv:wait(1)
+ local elapsed = monotime() - now
+ same(tasks, 0, 'all nested coroutines finish')
+ ok(status, 'nested coroutines finish successfully')
+ -- Test if 100 coroutines didnt execute synchronously
+ -- (the wait time would be 100 * 0.1 = 10s sleep time)
+ -- Concurrent sleep time should still be ~0.1s (added some safe margin)
+ ok(elapsed < 0.5, 'coroutines didnt block while sleeping')
+ end
+
+ -- plan tests
+ local tests = {
+ test_worker_sleep,
+ test_worker_coroutine
+ }
+
+ return tests
+end