diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-08 20:37:50 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-08 20:37:50 +0000 |
commit | c1f743ab2e4a7046d5500875a47d1f62c8624603 (patch) | |
tree | 709946d52f5f3bbaeb38be9e3f1d56d11f058237 /tests/config/worker.test.lua | |
parent | Initial commit. (diff) | |
download | knot-resolver-c1f743ab2e4a7046d5500875a47d1f62c8624603.tar.xz knot-resolver-c1f743ab2e4a7046d5500875a47d1f62c8624603.zip |
Adding upstream version 5.7.1.upstream/5.7.1
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.lua | 65 |
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 |