blob: 756bb5fb148de9274bc5ecb94f9b2929395cee8c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
|