author | Matthew Wild <mwild1@gmail.com> |
Fri, 16 Mar 2018 22:26:15 +0000 | |
changeset 8612 | 9f6ab206d741 |
parent 8611 | a2e6caf5848d |
child 8617 | bfbafeced0c4 |
permissions | -rw-r--r-- |
8608 | 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); |
|
8610
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
29 |
|
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
30 |
it("should be ready after creation", function () |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
31 |
local r = async.runner(function (item) end); |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
32 |
assert.equal(r.state, "ready"); |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
33 |
end); |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
34 |
|
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
35 |
describe("#errors", function () |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
36 |
local last_processed_item, last_error; |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
37 |
local r; |
8612
9f6ab206d741
util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents:
8611
diff
changeset
|
38 |
local wait, done; |
8610
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
39 |
r = async.runner(function (item) |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
40 |
if item == "error" then |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
41 |
error({ e = "test error" }); |
8612
9f6ab206d741
util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents:
8611
diff
changeset
|
42 |
elseif item == "wait" then |
9f6ab206d741
util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents:
8611
diff
changeset
|
43 |
wait, done = async.waiter(); |
9f6ab206d741
util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents:
8611
diff
changeset
|
44 |
wait(); |
9f6ab206d741
util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents:
8611
diff
changeset
|
45 |
error({ e = "post wait error" }); |
8610
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
46 |
end |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
47 |
last_processed_item = item; |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
48 |
end, { |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
49 |
error = function (runner, err) |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
50 |
assert.equal(r, runner); |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
51 |
last_error = err; |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
52 |
end; |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
53 |
}); |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
54 |
|
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
55 |
randomize(false); |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
56 |
|
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
57 |
it("should notify", function () |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
58 |
local last_processed_item, last_error; |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
59 |
local r; |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
60 |
r = async.runner(function (item) |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
61 |
if item == "error" then |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
62 |
error({ e = "test error" }); |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
63 |
end |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
64 |
last_processed_item = item; |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
65 |
end, { |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
66 |
error = function (runner, err) |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
67 |
assert.equal(r, runner); |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
68 |
last_error = err; |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
69 |
end; |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
70 |
}); |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
71 |
|
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
72 |
r:run("hello"); |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
73 |
assert.equal(r.state, "ready"); |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
74 |
assert.equal(last_processed_item, "hello"); |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
75 |
|
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
76 |
assert.equal(last_error, nil); |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
77 |
r:run("error"); |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
78 |
assert.is_table(last_error); |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
79 |
assert.equal(last_error.e, "test error"); |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
80 |
last_error = nil; |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
81 |
assert.equal(r.state, "ready"); |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
82 |
assert.equal(last_processed_item, "hello"); |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
83 |
end); |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
84 |
it("should not be fatal to the runner", function () |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
85 |
r:run("world"); |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
86 |
assert.equal(r.state, "ready"); |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
87 |
assert.equal(last_processed_item, "world"); |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
88 |
end); |
8612
9f6ab206d741
util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents:
8611
diff
changeset
|
89 |
it("should work despite a #waiter", function () |
9f6ab206d741
util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents:
8611
diff
changeset
|
90 |
-- This test covers an important case where a runner |
9f6ab206d741
util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents:
8611
diff
changeset
|
91 |
-- throws an error while being executed outside of the |
9f6ab206d741
util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents:
8611
diff
changeset
|
92 |
-- main loop. This happens when it was blocked ('waiting'), |
9f6ab206d741
util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents:
8611
diff
changeset
|
93 |
-- and then released (via a call to done()). |
9f6ab206d741
util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents:
8611
diff
changeset
|
94 |
last_error = nil; |
9f6ab206d741
util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents:
8611
diff
changeset
|
95 |
r:run("wait"); |
9f6ab206d741
util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents:
8611
diff
changeset
|
96 |
assert.equal(r.state, "waiting"); |
9f6ab206d741
util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents:
8611
diff
changeset
|
97 |
done(); |
9f6ab206d741
util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents:
8611
diff
changeset
|
98 |
-- At this point an error happens (state goes error->ready) |
9f6ab206d741
util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents:
8611
diff
changeset
|
99 |
assert.equal(r.state, "ready"); |
9f6ab206d741
util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents:
8611
diff
changeset
|
100 |
assert.is_table(last_error); |
9f6ab206d741
util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents:
8611
diff
changeset
|
101 |
assert.equal(last_error.e, "post wait error"); |
9f6ab206d741
util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents:
8611
diff
changeset
|
102 |
last_error = nil; |
9f6ab206d741
util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents:
8611
diff
changeset
|
103 |
r:run("hello again"); |
9f6ab206d741
util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents:
8611
diff
changeset
|
104 |
assert.equal(r.state, "ready"); |
9f6ab206d741
util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents:
8611
diff
changeset
|
105 |
assert.equal(last_processed_item, "hello again"); |
9f6ab206d741
util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
Matthew Wild <mwild1@gmail.com>
parents:
8611
diff
changeset
|
106 |
end); |
8610
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
107 |
end); |
8608 | 108 |
end); |
109 |
describe("#waiter", function() |
|
8611
a2e6caf5848d
util.async: Add test to ensure waiters throw an error outside async contexts
Matthew Wild <mwild1@gmail.com>
parents:
8610
diff
changeset
|
110 |
it("should error outside of async context", function () |
a2e6caf5848d
util.async: Add test to ensure waiters throw an error outside async contexts
Matthew Wild <mwild1@gmail.com>
parents:
8610
diff
changeset
|
111 |
assert.has_error(function () |
a2e6caf5848d
util.async: Add test to ensure waiters throw an error outside async contexts
Matthew Wild <mwild1@gmail.com>
parents:
8610
diff
changeset
|
112 |
async.waiter(); |
a2e6caf5848d
util.async: Add test to ensure waiters throw an error outside async contexts
Matthew Wild <mwild1@gmail.com>
parents:
8610
diff
changeset
|
113 |
end); |
a2e6caf5848d
util.async: Add test to ensure waiters throw an error outside async contexts
Matthew Wild <mwild1@gmail.com>
parents:
8610
diff
changeset
|
114 |
end); |
8608 | 115 |
it("should work", function () |
116 |
local wait, done; |
|
117 |
||
118 |
local r, l = new(function (item) |
|
119 |
assert(type(item) == "number") |
|
120 |
if item == 3 then |
|
121 |
wait, done = async.waiter(); |
|
122 |
wait(); |
|
123 |
end |
|
124 |
end); |
|
125 |
||
126 |
r:run(1); |
|
127 |
assert(r.state == "ready"); |
|
128 |
r:run(2); |
|
129 |
assert(r.state == "ready"); |
|
130 |
r:run(3); |
|
131 |
assert(r.state == "waiting"); |
|
132 |
done(); |
|
133 |
assert(r.state == "ready"); |
|
134 |
--for k, v in ipairs(l) do print(k,v) end |
|
135 |
end); |
|
136 |
||
137 |
it("should work", function () |
|
138 |
-------------------- |
|
139 |
local wait, done; |
|
140 |
local last_item = 0; |
|
141 |
local r, l = new(function (item) |
|
142 |
assert(type(item) == "number") |
|
143 |
assert(item == last_item + 1); |
|
144 |
last_item = item; |
|
145 |
if item == 3 then |
|
146 |
wait, done = async.waiter(); |
|
147 |
wait(); |
|
148 |
end |
|
149 |
end); |
|
150 |
||
151 |
r:run(1); |
|
152 |
assert(r.state == "ready"); |
|
153 |
r:run(2); |
|
154 |
assert(r.state == "ready"); |
|
155 |
r:run(3); |
|
156 |
assert(r.state == "waiting"); |
|
157 |
r:run(4); |
|
158 |
assert(r.state == "waiting"); |
|
159 |
done(); |
|
160 |
assert(r.state == "ready"); |
|
161 |
--for k, v in ipairs(l) do print(k,v) end |
|
162 |
end); |
|
163 |
it("should work", function () |
|
164 |
-------------------- |
|
165 |
local wait, done; |
|
166 |
local last_item = 0; |
|
167 |
local r, l = new(function (item) |
|
168 |
assert(type(item) == "number") |
|
169 |
assert((item == last_item + 1) or item == 3); |
|
170 |
last_item = item; |
|
171 |
if item == 3 then |
|
172 |
wait, done = async.waiter(); |
|
173 |
wait(); |
|
174 |
end |
|
175 |
end); |
|
176 |
||
177 |
r:run(1); |
|
178 |
assert(r.state == "ready"); |
|
179 |
r:run(2); |
|
180 |
assert(r.state == "ready"); |
|
181 |
||
182 |
local dones = {}; |
|
183 |
r:run(3); |
|
184 |
assert(r.state == "waiting"); |
|
185 |
r:run(3); |
|
186 |
assert(r.state == "waiting"); |
|
187 |
r:run(3); |
|
188 |
assert(r.state == "waiting"); |
|
189 |
r:run(4); |
|
190 |
assert(r.state == "waiting"); |
|
191 |
||
192 |
for i = 1, 3 do |
|
193 |
done(); |
|
194 |
if i < 3 then |
|
195 |
assert(r.state == "waiting"); |
|
196 |
end |
|
197 |
end |
|
198 |
||
199 |
assert(r.state == "ready"); |
|
200 |
--for k, v in ipairs(l) do print(k,v) end |
|
201 |
end); |
|
202 |
it("should work", function () |
|
203 |
-------------------- |
|
204 |
local wait, done; |
|
205 |
local last_item = 0; |
|
206 |
local r, l = new(function (item) |
|
207 |
assert(type(item) == "number") |
|
208 |
assert((item == last_item + 1) or item == 3); |
|
209 |
last_item = item; |
|
210 |
if item == 3 then |
|
211 |
wait, done = async.waiter(); |
|
212 |
wait(); |
|
213 |
end |
|
214 |
end); |
|
215 |
||
216 |
r:run(1); |
|
217 |
assert(r.state == "ready"); |
|
218 |
r:run(2); |
|
219 |
assert(r.state == "ready"); |
|
220 |
||
221 |
local dones = {}; |
|
222 |
r:run(3); |
|
223 |
assert(r.state == "waiting"); |
|
224 |
r:run(3); |
|
225 |
assert(r.state == "waiting"); |
|
226 |
||
227 |
for i = 1, 2 do |
|
228 |
done(); |
|
229 |
if i < 2 then |
|
230 |
assert(r.state == "waiting"); |
|
231 |
end |
|
232 |
end |
|
233 |
||
234 |
assert(r.state == "ready"); |
|
235 |
r:run(4); |
|
236 |
assert(r.state == "ready"); |
|
237 |
||
238 |
assert(r.state == "ready"); |
|
239 |
--for k, v in ipairs(l) do print(k,v) end |
|
240 |
end); |
|
241 |
it("should work with multiple runners in parallel", function () |
|
242 |
-- Now with multiple runners |
|
243 |
-------------------- |
|
244 |
local wait1, done1; |
|
245 |
local last_item1 = 0; |
|
246 |
local r1, l1 = new(function (item) |
|
247 |
assert(type(item) == "number") |
|
248 |
assert((item == last_item1 + 1) or item == 3); |
|
249 |
last_item1 = item; |
|
250 |
if item == 3 then |
|
251 |
wait1, done1 = async.waiter(); |
|
252 |
wait1(); |
|
253 |
end |
|
254 |
end, "r1"); |
|
255 |
||
256 |
local wait2, done2; |
|
257 |
local last_item2 = 0; |
|
258 |
local r2, l2 = new(function (item) |
|
259 |
assert(type(item) == "number") |
|
260 |
assert((item == last_item2 + 1) or item == 3); |
|
261 |
last_item2 = item; |
|
262 |
if item == 3 then |
|
263 |
wait2, done2 = async.waiter(); |
|
264 |
wait2(); |
|
265 |
end |
|
266 |
end, "r2"); |
|
267 |
||
268 |
r1:run(1); |
|
269 |
assert(r1.state == "ready"); |
|
270 |
r1:run(2); |
|
271 |
assert(r1.state == "ready"); |
|
272 |
||
273 |
local dones = {}; |
|
274 |
r1:run(3); |
|
275 |
assert(r1.state == "waiting"); |
|
276 |
r1:run(3); |
|
277 |
assert(r1.state == "waiting"); |
|
278 |
||
279 |
r2:run(1); |
|
280 |
assert(r1.state == "waiting"); |
|
281 |
assert(r2.state == "ready"); |
|
282 |
||
283 |
r2:run(2); |
|
284 |
assert(r1.state == "waiting"); |
|
285 |
assert(r2.state == "ready"); |
|
286 |
||
287 |
r2:run(3); |
|
288 |
assert(r1.state == "waiting"); |
|
289 |
assert(r2.state == "waiting"); |
|
290 |
done2(); |
|
291 |
||
292 |
r2:run(3); |
|
293 |
assert(r1.state == "waiting"); |
|
294 |
assert(r2.state == "waiting"); |
|
295 |
done2(); |
|
296 |
||
297 |
r2:run(4); |
|
298 |
assert(r1.state == "waiting"); |
|
299 |
assert(r2.state == "ready"); |
|
300 |
||
301 |
for i = 1, 2 do |
|
302 |
done1(); |
|
303 |
if i < 2 then |
|
304 |
assert(r1.state == "waiting"); |
|
305 |
end |
|
306 |
end |
|
307 |
||
308 |
assert(r1.state == "ready"); |
|
309 |
r1:run(4); |
|
310 |
assert(r1.state == "ready"); |
|
311 |
||
312 |
assert(r1.state == "ready"); |
|
313 |
--for k, v in ipairs(l1) do print(k,v) end |
|
314 |
end); |
|
315 |
it("should work work with multiple runners in parallel", function () |
|
316 |
-------------------- |
|
317 |
local wait1, done1; |
|
318 |
local last_item1 = 0; |
|
319 |
local r1, l1 = new(function (item) |
|
320 |
print("r1 processing ", item); |
|
321 |
assert(type(item) == "number") |
|
322 |
assert((item == last_item1 + 1) or item == 3); |
|
323 |
last_item1 = item; |
|
324 |
if item == 3 then |
|
325 |
wait1, done1 = async.waiter(); |
|
326 |
wait1(); |
|
327 |
end |
|
328 |
end, "r1"); |
|
329 |
||
330 |
local wait2, done2; |
|
331 |
local last_item2 = 0; |
|
332 |
local r2, l2 = new(function (item) |
|
333 |
print("r2 processing ", item); |
|
334 |
assert.is_number(item); |
|
335 |
assert((item == last_item2 + 1) or item == 3); |
|
336 |
last_item2 = item; |
|
337 |
if item == 3 then |
|
338 |
wait2, done2 = async.waiter(); |
|
339 |
wait2(); |
|
340 |
end |
|
341 |
end, "r2"); |
|
342 |
||
343 |
r1:run(1); |
|
344 |
assert.equal(r1.state, "ready"); |
|
345 |
r1:run(2); |
|
346 |
assert.equal(r1.state, "ready"); |
|
347 |
||
348 |
r1:run(5); |
|
349 |
assert.equal(r1.state, "ready"); |
|
350 |
||
351 |
local dones = {}; |
|
352 |
r1:run(3); |
|
353 |
assert.equal(r1.state, "waiting"); |
|
354 |
r1:run(5); -- Will error, when we get to it |
|
355 |
assert.equal(r1.state, "waiting"); |
|
356 |
done1(); |
|
357 |
assert.equal(r1.state, "ready"); |
|
358 |
r1:run(3); |
|
359 |
assert.equal(r1.state, "waiting"); |
|
360 |
||
361 |
r2:run(1); |
|
362 |
assert.equal(r1.state, "waiting"); |
|
363 |
assert.equal(r2.state, "ready"); |
|
364 |
||
365 |
r2:run(2); |
|
366 |
assert.equal(r1.state, "waiting"); |
|
367 |
assert.equal(r2.state, "ready"); |
|
368 |
||
369 |
r2:run(3); |
|
370 |
assert.equal(r1.state, "waiting"); |
|
371 |
assert.equal(r2.state, "waiting"); |
|
372 |
||
373 |
done2(); |
|
374 |
assert.equal(r1.state, "waiting"); |
|
375 |
assert.equal(r2.state, "ready"); |
|
376 |
||
377 |
r2:run(3); |
|
378 |
assert.equal(r1.state, "waiting"); |
|
379 |
assert.equal(r2.state, "waiting"); |
|
380 |
||
381 |
done2(); |
|
382 |
assert.equal(r1.state, "waiting"); |
|
383 |
assert.equal(r2.state, "ready"); |
|
384 |
||
385 |
r2:run(4); |
|
386 |
assert.equal(r1.state, "waiting"); |
|
387 |
assert.equal(r2.state, "ready"); |
|
388 |
||
389 |
done1(); |
|
390 |
||
391 |
assert.equal(r1.state, "ready"); |
|
392 |
r1:run(4); |
|
393 |
assert.equal(r1.state, "ready"); |
|
394 |
||
395 |
assert.equal(r1.state, "ready"); |
|
396 |
--for k, v in ipairs(l1) do print(k,v) end |
|
397 |
end); |
|
398 |
end); |
|
399 |
end); |