-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_tests.lua
More file actions
149 lines (127 loc) · 4.78 KB
/
task_tests.lua
File metadata and controls
149 lines (127 loc) · 4.78 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
local task = require("@task")
testing:test("task spawn", function(t)
local task_h = task.spawn(function(delay)
task.sleep(delay)
return "done"
end, "10ms")
t.assert_ne(task_h.id, nil, "task id should be set")
t.assert_eq(task_h.name, nil, "task name should be nil by default")
task.sleep("5ms")
local elapsed = task_h:elapsed():as_secs()
t.assert_ge(elapsed, 0.005, "elapsed time should be >= 5ms")
t.assert_lt(elapsed, 0.01, "elapsed time should be < 10ms")
task.sleep("10ms")
t.assert(task_h:is_finished(), "task should be finished after total 10ms")
local result = task_h:join()
t.assert_eq(result, "done", "task result should be 'done'")
-- One more join
local _, err = task_h:join()
t.assert_match(err, "task already joined")
end)
testing:test("task abort", function(t)
local task_h = task.spawn(function()
task.sleep("50ms")
return "should not reach here"
end)
task.sleep("5ms")
task_h:abort()
task.sleep("5ms")
t.assert(task_h:is_finished(), "task should be finished after abort")
local elapsed1 = task_h:elapsed():as_secs()
t.assert_ge(elapsed1, 0.005, "elapsed time should be >= 5ms")
t.assert_lt(elapsed1, 0.01, "elapsed time should be < 10ms")
task.sleep("5ms")
local elapsed2 = task_h:elapsed():as_secs()
t.assert_eq(elapsed1, elapsed2, "elapsed time should not change after abort")
local _, err = task_h:join()
t.assert_match(err, "task %d+ was cancelled")
end)
testing:test("task error", function(t)
local task_h = task.spawn(function()
error("task error occurred")
end)
task.sleep("10ms")
t.assert(task_h:is_finished(), "task should be finished after error")
local _, err = task_h:join()
t.assert_match(err, "task error occurred")
end)
testing:test("task spawn_every", function(t)
local count = 0
local task_h = task.spawn_every("10ms", function()
count = count + 1
end)
task.sleep("35ms")
task_h:abort()
t.assert_eq(count, 3, "task should have run 3 times")
local _, err = task_h:join()
t.assert_match(err, "task %d+ was cancelled")
end)
testing:test("task spawn with timeout", function(t)
local task_h = task.spawn(task.create(function()
task.yield()
task.sleep("50ms")
return "should not reach here"
end, { name = "my_task", timeout = "20ms" }))
t.assert_ne(task_h.id, nil, "task id should be set")
t.assert_eq(task_h.name, "my_task")
task.sleep("30ms")
t.assert(task_h:is_finished(), "task should be finished after timeout")
local elapsed = task_h:elapsed():as_secs()
t.assert_ge(elapsed, 0.02, "elapsed time should be >= 20ms")
t.assert_lt(elapsed, 0.03, "elapsed time should be < 30ms")
local _, err = task_h:join()
t.assert_match(err, "task exceeded timeout")
end)
testing:test("task spawn_every with timeout", function(t)
local count = 0
local task_h = task.spawn_every(
"10ms",
task.create(function()
count = count + 1
end, { name = "my_task", timeout = "35ms" })
)
t.assert_ne(task_h.id, nil, "task id should be set")
t.assert_eq(task_h.name, "my_task")
task.sleep("50ms")
t.assert(task_h:is_finished(), "task should be finished after timeout")
local elapsed = task_h:elapsed():as_secs()
t.assert_ge(elapsed, 0.035, "elapsed time should be >= 35ms")
t.assert_lt(elapsed, 0.04, "elapsed time should be < 40ms")
local _, err = task_h:join()
t.assert_match(err, "task exceeded timeout")
end)
testing:test("task group", function(t)
local group = task.group()
local task1 = group:spawn(function()
task.sleep("10ms")
return "task1 done"
end)
local task2 = group:spawn(function()
task.sleep("20ms")
return "task2 done"
end)
t.assert_eq(#group, 2, "group should have 2 tasks")
task.sleep("15ms")
t.assert(task1:is_finished(), "task1 should be finished after 15ms")
t.assert(task1:elapsed():as_secs() >= 0.01, "task1 elapsed should be at least 10ms")
t.assert(not task2:is_finished(), "task2 should not be finished after 15ms")
local results = group:join_all()
t.assert_same(results, { "task1 done", "task2 done" }, "group results should match")
t.assert_eq(#group, 0, "group should have 0 tasks after joins")
end)
testing:test("task group with abort", function(t)
local group = task.group()
group:spawn(function()
task.sleep("10ms")
return "task1 done"
end)
local task2 = group:spawn(function()
task.sleep("20ms")
return "task2 done"
end)
task.sleep("15ms")
task2:abort()
local results = group:join_all()
t.assert_eq(results[1], "task1 done", "task1 result should match")
t.assert_match(results[2], "task %d+ was cancelled")
end)