Software /
code /
prosody
Comparison
spec/util_async_spec.lua @ 8605:3a1efc0532cb
util.async: Add tests
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 16 Mar 2018 17:13:57 +0000 |
child | 8607:62bb06cf8a43 |
comparison
equal
deleted
inserted
replaced
8604:1c8c7fd259c8 | 8605:3a1efc0532cb |
---|---|
1 local async = require "util.async"; | |
2 | |
3 describe("util.async", function() | |
4 local debug = false; | |
5 local print = print; | |
6 if debug then | |
7 require "util.logger".add_simple_sink(print); | |
8 else | |
9 print = function () end | |
10 end | |
11 local function new(func, name) | |
12 local log = {}; | |
13 return async.runner(func, setmetatable({}, { | |
14 __index = function (_, event) | |
15 return function (runner, err) | |
16 print(name, "event", event, err) | |
17 print "--" | |
18 table.insert(log, { event = event, err = err }); | |
19 end; | |
20 end; | |
21 })), log; | |
22 end | |
23 describe("#runner", function() | |
24 it("should work", function() | |
25 local r, l = new(function (item) assert(type(item) == "number") end); | |
26 r:run(1); | |
27 r:run(2); | |
28 end); | |
29 end); | |
30 describe("#waiter", function() | |
31 it("should work", function () | |
32 local wait, done; | |
33 | |
34 local r, l = new(function (item) | |
35 assert(type(item) == "number") | |
36 if item == 3 then | |
37 wait, done = async.waiter(); | |
38 wait(); | |
39 end | |
40 end); | |
41 | |
42 r:run(1); | |
43 assert(r.state == "ready"); | |
44 r:run(2); | |
45 assert(r.state == "ready"); | |
46 r:run(3); | |
47 assert(r.state == "waiting"); | |
48 done(); | |
49 assert(r.state == "ready"); | |
50 --for k, v in ipairs(l) do print(k,v) end | |
51 end); | |
52 | |
53 it("should work", function () | |
54 -------------------- | |
55 local wait, done; | |
56 local last_item = 0; | |
57 local r, l = new(function (item) | |
58 assert(type(item) == "number") | |
59 assert(item == last_item + 1); | |
60 last_item = item; | |
61 if item == 3 then | |
62 wait, done = async.waiter(); | |
63 wait(); | |
64 end | |
65 end); | |
66 | |
67 r:run(1); | |
68 assert(r.state == "ready"); | |
69 r:run(2); | |
70 assert(r.state == "ready"); | |
71 r:run(3); | |
72 assert(r.state == "waiting"); | |
73 r:run(4); | |
74 assert(r.state == "waiting"); | |
75 done(); | |
76 assert(r.state == "ready"); | |
77 --for k, v in ipairs(l) do print(k,v) end | |
78 end); | |
79 it("should work", function () | |
80 -------------------- | |
81 local wait, done; | |
82 local last_item = 0; | |
83 local r, l = new(function (item) | |
84 assert(type(item) == "number") | |
85 assert((item == last_item + 1) or item == 3); | |
86 last_item = item; | |
87 if item == 3 then | |
88 wait, done = async.waiter(); | |
89 wait(); | |
90 end | |
91 end); | |
92 | |
93 r:run(1); | |
94 assert(r.state == "ready"); | |
95 r:run(2); | |
96 assert(r.state == "ready"); | |
97 | |
98 local dones = {}; | |
99 r:run(3); | |
100 assert(r.state == "waiting"); | |
101 r:run(3); | |
102 assert(r.state == "waiting"); | |
103 r:run(3); | |
104 assert(r.state == "waiting"); | |
105 r:run(4); | |
106 assert(r.state == "waiting"); | |
107 | |
108 for i = 1, 3 do | |
109 done(); | |
110 if i < 3 then | |
111 assert(r.state == "waiting"); | |
112 end | |
113 end | |
114 | |
115 assert(r.state == "ready"); | |
116 --for k, v in ipairs(l) do print(k,v) end | |
117 end); | |
118 it("should work", function () | |
119 -------------------- | |
120 local wait, done; | |
121 local last_item = 0; | |
122 local r, l = new(function (item) | |
123 assert(type(item) == "number") | |
124 assert((item == last_item + 1) or item == 3); | |
125 last_item = item; | |
126 if item == 3 then | |
127 wait, done = async.waiter(); | |
128 wait(); | |
129 end | |
130 end); | |
131 | |
132 r:run(1); | |
133 assert(r.state == "ready"); | |
134 r:run(2); | |
135 assert(r.state == "ready"); | |
136 | |
137 local dones = {}; | |
138 r:run(3); | |
139 assert(r.state == "waiting"); | |
140 r:run(3); | |
141 assert(r.state == "waiting"); | |
142 | |
143 for i = 1, 2 do | |
144 done(); | |
145 if i < 2 then | |
146 assert(r.state == "waiting"); | |
147 end | |
148 end | |
149 | |
150 assert(r.state == "ready"); | |
151 r:run(4); | |
152 assert(r.state == "ready"); | |
153 | |
154 assert(r.state == "ready"); | |
155 --for k, v in ipairs(l) do print(k,v) end | |
156 end); | |
157 it("should work with multiple runners in parallel", function () | |
158 -- Now with multiple runners | |
159 -------------------- | |
160 local wait1, done1; | |
161 local last_item1 = 0; | |
162 local r1, l1 = new(function (item) | |
163 assert(type(item) == "number") | |
164 assert((item == last_item1 + 1) or item == 3); | |
165 last_item1 = item; | |
166 if item == 3 then | |
167 wait1, done1 = async.waiter(); | |
168 wait1(); | |
169 end | |
170 end, "r1"); | |
171 | |
172 local wait2, done2; | |
173 local last_item2 = 0; | |
174 local r2, l2 = new(function (item) | |
175 assert(type(item) == "number") | |
176 assert((item == last_item2 + 1) or item == 3); | |
177 last_item2 = item; | |
178 if item == 3 then | |
179 wait2, done2 = async.waiter(); | |
180 wait2(); | |
181 end | |
182 end, "r2"); | |
183 | |
184 r1:run(1); | |
185 assert(r1.state == "ready"); | |
186 r1:run(2); | |
187 assert(r1.state == "ready"); | |
188 | |
189 local dones = {}; | |
190 r1:run(3); | |
191 assert(r1.state == "waiting"); | |
192 r1:run(3); | |
193 assert(r1.state == "waiting"); | |
194 | |
195 r2:run(1); | |
196 assert(r1.state == "waiting"); | |
197 assert(r2.state == "ready"); | |
198 | |
199 r2:run(2); | |
200 assert(r1.state == "waiting"); | |
201 assert(r2.state == "ready"); | |
202 | |
203 r2:run(3); | |
204 assert(r1.state == "waiting"); | |
205 assert(r2.state == "waiting"); | |
206 done2(); | |
207 | |
208 r2:run(3); | |
209 assert(r1.state == "waiting"); | |
210 assert(r2.state == "waiting"); | |
211 done2(); | |
212 | |
213 r2:run(4); | |
214 assert(r1.state == "waiting"); | |
215 assert(r2.state == "ready"); | |
216 | |
217 for i = 1, 2 do | |
218 done1(); | |
219 if i < 2 then | |
220 assert(r1.state == "waiting"); | |
221 end | |
222 end | |
223 | |
224 assert(r1.state == "ready"); | |
225 r1:run(4); | |
226 assert(r1.state == "ready"); | |
227 | |
228 assert(r1.state == "ready"); | |
229 --for k, v in ipairs(l1) do print(k,v) end | |
230 end); | |
231 it("should work work with multiple runners in parallel", function () | |
232 -------------------- | |
233 local wait1, done1; | |
234 local last_item1 = 0; | |
235 local r1, l1 = new(function (item) | |
236 print("r1 processing ", item); | |
237 assert(type(item) == "number") | |
238 assert((item == last_item1 + 1) or item == 3); | |
239 last_item1 = item; | |
240 if item == 3 then | |
241 wait1, done1 = async.waiter(); | |
242 wait1(); | |
243 end | |
244 end, "r1"); | |
245 | |
246 local wait2, done2; | |
247 local last_item2 = 0; | |
248 local r2, l2 = new(function (item) | |
249 print("r2 processing ", item); | |
250 assert.is_number(item); | |
251 assert((item == last_item2 + 1) or item == 3); | |
252 last_item2 = item; | |
253 if item == 3 then | |
254 wait2, done2 = async.waiter(); | |
255 wait2(); | |
256 end | |
257 end, "r2"); | |
258 | |
259 r1:run(1); | |
260 assert.equal(r1.state, "ready"); | |
261 r1:run(2); | |
262 assert.equal(r1.state, "ready"); | |
263 | |
264 r1:run(5); | |
265 assert.equal(r1.state, "ready"); | |
266 | |
267 local dones = {}; | |
268 r1:run(3); | |
269 assert.equal(r1.state, "waiting"); | |
270 r1:run(5); -- Will error, when we get to it | |
271 assert.equal(r1.state, "waiting"); | |
272 done1(); | |
273 assert.equal(r1.state, "ready"); | |
274 r1:run(3); | |
275 assert.equal(r1.state, "waiting"); | |
276 | |
277 r2:run(1); | |
278 assert.equal(r1.state, "waiting"); | |
279 assert.equal(r2.state, "ready"); | |
280 | |
281 r2:run(2); | |
282 assert.equal(r1.state, "waiting"); | |
283 assert.equal(r2.state, "ready"); | |
284 | |
285 r2:run(3); | |
286 assert.equal(r1.state, "waiting"); | |
287 assert.equal(r2.state, "waiting"); | |
288 | |
289 done2(); | |
290 assert.equal(r1.state, "waiting"); | |
291 assert.equal(r2.state, "ready"); | |
292 | |
293 r2:run(3); | |
294 assert.equal(r1.state, "waiting"); | |
295 assert.equal(r2.state, "waiting"); | |
296 | |
297 done2(); | |
298 assert.equal(r1.state, "waiting"); | |
299 assert.equal(r2.state, "ready"); | |
300 | |
301 r2:run(4); | |
302 assert.equal(r1.state, "waiting"); | |
303 assert.equal(r2.state, "ready"); | |
304 | |
305 done1(); | |
306 | |
307 assert.equal(r1.state, "ready"); | |
308 r1:run(4); | |
309 assert.equal(r1.state, "ready"); | |
310 | |
311 assert.equal(r1.state, "ready"); | |
312 --for k, v in ipairs(l1) do print(k,v) end | |
313 end); | |
314 end); | |
315 end); |