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
|
import("core.base.scheduler")
function test_group(t)
local count = 0
local task = function (a)
t:are_equal(a, "xmake!")
count = count + 1
end
scheduler.co_group_begin("test", function ()
for i = 1, 100 do
scheduler.co_start(task, "xmake!")
end
end)
scheduler.co_group_wait("test")
t:are_equal(count, 100)
end
function test_sleep(t)
local count = 0
local task = function (a)
local dt = os.mclock()
os.sleep(500)
dt = os.mclock() - dt
t:require(dt > 100 and dt < 1000)
count = count + 1
end
for i = 1, 3 do
scheduler.co_start(task)
end
end
function test_yield(t)
local count = 0
local task = function (a)
scheduler.co_yield()
count = count + 1
end
scheduler.co_group_begin("test", function ()
for i = 1, 10 do
scheduler.co_start(task)
end
end)
scheduler.co_group_wait("test")
t:are_equal(count, 10)
end
function test_runjobs(t)
import("async.runjobs")
local total = 100
local comax = 6
local count = 0
runjobs("test", function (index)
t:require(index >= 1 and index <= total)
count = count + 1
end, {total = total, comax = comax})
t:are_equal(count, total)
end
|