author | Matthew Wild <mwild1@gmail.com> |
Sat, 17 Mar 2018 14:54:48 +0000 | |
changeset 8618 | e77b37de482e |
parent 8617 | bfbafeced0c4 |
child 8619 | a15c891c6232 |
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 |
|
8617
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
35 |
it("should do nothing if the queue is empty", function () |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
36 |
local did_run; |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
37 |
local r = async.runner(function (item) did_run = true end); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
38 |
r:run(); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
39 |
assert.equal(r.state, "ready"); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
40 |
assert.is_nil(did_run); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
41 |
r:run("hello"); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
42 |
assert.is_true(did_run); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
43 |
end); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
44 |
|
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
45 |
it("should support queuing work items without running", function () |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
46 |
local did_run; |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
47 |
local r = async.runner(function (item) did_run = true end); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
48 |
r:enqueue("hello"); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
49 |
assert.equal(r.state, "ready"); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
50 |
assert.is_nil(did_run); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
51 |
r:run(); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
52 |
assert.is_true(did_run); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
53 |
end); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
54 |
|
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
55 |
it("should support queuing multiple work items", function () |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
56 |
local last_item; |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
57 |
local s = spy(function (item) last_item = item; end); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
58 |
local r = async.runner(s); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
59 |
r:enqueue("hello"); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
60 |
r:enqueue("there"); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
61 |
r:enqueue("world"); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
62 |
assert.equal(r.state, "ready"); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
63 |
r:run(); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
64 |
assert.equal(r.state, "ready"); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
65 |
assert.spy(s).was.called(3); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
66 |
assert.equal(last_item, "world"); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
67 |
end); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
68 |
|
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
69 |
it("should support all simple data types", function () |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
70 |
local last_item; |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
71 |
local s = spy(function (item) last_item = item; end); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
72 |
local r = async.runner(s); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
73 |
local values = { {}, 123, "hello", true, false }; |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
74 |
for i = 1, #values do |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
75 |
r:enqueue(values[i]); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
76 |
end |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
77 |
assert.equal(r.state, "ready"); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
78 |
r:run(); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
79 |
assert.equal(r.state, "ready"); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
80 |
assert.spy(s).was.called(#values); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
81 |
for i = 1, #values do |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
82 |
assert.spy(s).was.called_with(values[i]); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
83 |
end |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
84 |
assert.equal(last_item, values[#values]); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
85 |
end); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
86 |
|
8610
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
87 |
describe("#errors", function () |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
88 |
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
|
89 |
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
|
90 |
local wait, done; |
8610
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
91 |
r = async.runner(function (item) |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
92 |
if item == "error" then |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
93 |
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
|
94 |
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
|
95 |
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
|
96 |
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
|
97 |
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
|
98 |
end |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
99 |
last_processed_item = item; |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
100 |
end, { |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
101 |
error = function (runner, err) |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
102 |
assert.equal(r, runner); |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
103 |
last_error = err; |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
104 |
end; |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
105 |
}); |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
106 |
|
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
107 |
randomize(false); |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
108 |
|
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
109 |
it("should notify", function () |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
110 |
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
|
111 |
local r; |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
112 |
r = async.runner(function (item) |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
113 |
if item == "error" then |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
114 |
error({ e = "test error" }); |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
115 |
end |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
116 |
last_processed_item = item; |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
117 |
end, { |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
118 |
error = function (runner, err) |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
119 |
assert.equal(r, runner); |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
120 |
last_error = err; |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
121 |
end; |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
122 |
}); |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
123 |
|
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
124 |
r:run("hello"); |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
125 |
assert.equal(r.state, "ready"); |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
126 |
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
|
127 |
|
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
128 |
assert.equal(last_error, nil); |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
129 |
r:run("error"); |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
130 |
assert.is_table(last_error); |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
131 |
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
|
132 |
last_error = nil; |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
133 |
assert.equal(r.state, "ready"); |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
134 |
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
|
135 |
end); |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
136 |
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
|
137 |
r:run("world"); |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
138 |
assert.equal(r.state, "ready"); |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
139 |
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
|
140 |
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
|
141 |
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
|
142 |
-- 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
|
143 |
-- 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
|
144 |
-- 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
|
145 |
-- 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
|
146 |
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
|
147 |
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
|
148 |
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
|
149 |
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
|
150 |
-- 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
|
151 |
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
|
152 |
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
|
153 |
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
|
154 |
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
|
155 |
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
|
156 |
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
|
157 |
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
|
158 |
end); |
8618
e77b37de482e
util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents:
8617
diff
changeset
|
159 |
|
e77b37de482e
util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents:
8617
diff
changeset
|
160 |
it("should continue to process work items", function () |
e77b37de482e
util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents:
8617
diff
changeset
|
161 |
local wait, done, last_item; |
e77b37de482e
util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents:
8617
diff
changeset
|
162 |
local runner_func = spy.new(function (item) |
e77b37de482e
util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents:
8617
diff
changeset
|
163 |
if item == "error" then |
e77b37de482e
util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents:
8617
diff
changeset
|
164 |
error("test error"); |
e77b37de482e
util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents:
8617
diff
changeset
|
165 |
elseif item == "wait-error" then |
e77b37de482e
util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents:
8617
diff
changeset
|
166 |
wait, done = async.waiter(); |
e77b37de482e
util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents:
8617
diff
changeset
|
167 |
wait(); |
e77b37de482e
util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents:
8617
diff
changeset
|
168 |
error("test error"); |
e77b37de482e
util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents:
8617
diff
changeset
|
169 |
end |
e77b37de482e
util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents:
8617
diff
changeset
|
170 |
last_item = item; |
e77b37de482e
util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents:
8617
diff
changeset
|
171 |
end); |
e77b37de482e
util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents:
8617
diff
changeset
|
172 |
local runner = async.runner(runner_func, { error = spy.new(function () end) }); |
e77b37de482e
util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents:
8617
diff
changeset
|
173 |
runner:enqueue("one"); |
e77b37de482e
util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents:
8617
diff
changeset
|
174 |
runner:enqueue("error"); |
e77b37de482e
util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents:
8617
diff
changeset
|
175 |
runner:enqueue("two"); |
e77b37de482e
util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents:
8617
diff
changeset
|
176 |
runner:run(); |
e77b37de482e
util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents:
8617
diff
changeset
|
177 |
assert.equal(r.state, "ready"); |
e77b37de482e
util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents:
8617
diff
changeset
|
178 |
assert.equal(r.state, r.notified_state); |
e77b37de482e
util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents:
8617
diff
changeset
|
179 |
assert.spy(runner_func).was.called(3); |
e77b37de482e
util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents:
8617
diff
changeset
|
180 |
assert.spy(runner.watchers.error).was.called(1); |
e77b37de482e
util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents:
8617
diff
changeset
|
181 |
assert.equal(last_item, "two"); |
e77b37de482e
util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents:
8617
diff
changeset
|
182 |
end); |
8610
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
183 |
end); |
8608 | 184 |
end); |
185 |
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
|
186 |
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
|
187 |
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
|
188 |
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
|
189 |
end); |
a2e6caf5848d
util.async: Add test to ensure waiters throw an error outside async contexts
Matthew Wild <mwild1@gmail.com>
parents:
8610
diff
changeset
|
190 |
end); |
8608 | 191 |
it("should work", function () |
192 |
local wait, done; |
|
193 |
||
194 |
local r, l = new(function (item) |
|
195 |
assert(type(item) == "number") |
|
196 |
if item == 3 then |
|
197 |
wait, done = async.waiter(); |
|
198 |
wait(); |
|
199 |
end |
|
200 |
end); |
|
201 |
||
202 |
r:run(1); |
|
203 |
assert(r.state == "ready"); |
|
204 |
r:run(2); |
|
205 |
assert(r.state == "ready"); |
|
206 |
r:run(3); |
|
207 |
assert(r.state == "waiting"); |
|
208 |
done(); |
|
209 |
assert(r.state == "ready"); |
|
210 |
--for k, v in ipairs(l) do print(k,v) end |
|
211 |
end); |
|
212 |
||
213 |
it("should work", function () |
|
214 |
-------------------- |
|
215 |
local wait, done; |
|
216 |
local last_item = 0; |
|
217 |
local r, l = new(function (item) |
|
218 |
assert(type(item) == "number") |
|
219 |
assert(item == last_item + 1); |
|
220 |
last_item = item; |
|
221 |
if item == 3 then |
|
222 |
wait, done = async.waiter(); |
|
223 |
wait(); |
|
224 |
end |
|
225 |
end); |
|
226 |
||
227 |
r:run(1); |
|
228 |
assert(r.state == "ready"); |
|
229 |
r:run(2); |
|
230 |
assert(r.state == "ready"); |
|
231 |
r:run(3); |
|
232 |
assert(r.state == "waiting"); |
|
233 |
r:run(4); |
|
234 |
assert(r.state == "waiting"); |
|
235 |
done(); |
|
236 |
assert(r.state == "ready"); |
|
237 |
--for k, v in ipairs(l) do print(k,v) end |
|
238 |
end); |
|
239 |
it("should work", function () |
|
240 |
-------------------- |
|
241 |
local wait, done; |
|
242 |
local last_item = 0; |
|
243 |
local r, l = new(function (item) |
|
244 |
assert(type(item) == "number") |
|
245 |
assert((item == last_item + 1) or item == 3); |
|
246 |
last_item = item; |
|
247 |
if item == 3 then |
|
248 |
wait, done = async.waiter(); |
|
249 |
wait(); |
|
250 |
end |
|
251 |
end); |
|
252 |
||
253 |
r:run(1); |
|
254 |
assert(r.state == "ready"); |
|
255 |
r:run(2); |
|
256 |
assert(r.state == "ready"); |
|
257 |
||
258 |
local dones = {}; |
|
259 |
r:run(3); |
|
260 |
assert(r.state == "waiting"); |
|
261 |
r:run(3); |
|
262 |
assert(r.state == "waiting"); |
|
263 |
r:run(3); |
|
264 |
assert(r.state == "waiting"); |
|
265 |
r:run(4); |
|
266 |
assert(r.state == "waiting"); |
|
267 |
||
268 |
for i = 1, 3 do |
|
269 |
done(); |
|
270 |
if i < 3 then |
|
271 |
assert(r.state == "waiting"); |
|
272 |
end |
|
273 |
end |
|
274 |
||
275 |
assert(r.state == "ready"); |
|
276 |
--for k, v in ipairs(l) do print(k,v) end |
|
277 |
end); |
|
278 |
it("should work", function () |
|
279 |
-------------------- |
|
280 |
local wait, done; |
|
281 |
local last_item = 0; |
|
282 |
local r, l = new(function (item) |
|
283 |
assert(type(item) == "number") |
|
284 |
assert((item == last_item + 1) or item == 3); |
|
285 |
last_item = item; |
|
286 |
if item == 3 then |
|
287 |
wait, done = async.waiter(); |
|
288 |
wait(); |
|
289 |
end |
|
290 |
end); |
|
291 |
||
292 |
r:run(1); |
|
293 |
assert(r.state == "ready"); |
|
294 |
r:run(2); |
|
295 |
assert(r.state == "ready"); |
|
296 |
||
297 |
local dones = {}; |
|
298 |
r:run(3); |
|
299 |
assert(r.state == "waiting"); |
|
300 |
r:run(3); |
|
301 |
assert(r.state == "waiting"); |
|
302 |
||
303 |
for i = 1, 2 do |
|
304 |
done(); |
|
305 |
if i < 2 then |
|
306 |
assert(r.state == "waiting"); |
|
307 |
end |
|
308 |
end |
|
309 |
||
310 |
assert(r.state == "ready"); |
|
311 |
r:run(4); |
|
312 |
assert(r.state == "ready"); |
|
313 |
||
314 |
assert(r.state == "ready"); |
|
315 |
--for k, v in ipairs(l) do print(k,v) end |
|
316 |
end); |
|
317 |
it("should work with multiple runners in parallel", function () |
|
318 |
-- Now with multiple runners |
|
319 |
-------------------- |
|
320 |
local wait1, done1; |
|
321 |
local last_item1 = 0; |
|
322 |
local r1, l1 = new(function (item) |
|
323 |
assert(type(item) == "number") |
|
324 |
assert((item == last_item1 + 1) or item == 3); |
|
325 |
last_item1 = item; |
|
326 |
if item == 3 then |
|
327 |
wait1, done1 = async.waiter(); |
|
328 |
wait1(); |
|
329 |
end |
|
330 |
end, "r1"); |
|
331 |
||
332 |
local wait2, done2; |
|
333 |
local last_item2 = 0; |
|
334 |
local r2, l2 = new(function (item) |
|
335 |
assert(type(item) == "number") |
|
336 |
assert((item == last_item2 + 1) or item == 3); |
|
337 |
last_item2 = item; |
|
338 |
if item == 3 then |
|
339 |
wait2, done2 = async.waiter(); |
|
340 |
wait2(); |
|
341 |
end |
|
342 |
end, "r2"); |
|
343 |
||
344 |
r1:run(1); |
|
345 |
assert(r1.state == "ready"); |
|
346 |
r1:run(2); |
|
347 |
assert(r1.state == "ready"); |
|
348 |
||
349 |
local dones = {}; |
|
350 |
r1:run(3); |
|
351 |
assert(r1.state == "waiting"); |
|
352 |
r1:run(3); |
|
353 |
assert(r1.state == "waiting"); |
|
354 |
||
355 |
r2:run(1); |
|
356 |
assert(r1.state == "waiting"); |
|
357 |
assert(r2.state == "ready"); |
|
358 |
||
359 |
r2:run(2); |
|
360 |
assert(r1.state == "waiting"); |
|
361 |
assert(r2.state == "ready"); |
|
362 |
||
363 |
r2:run(3); |
|
364 |
assert(r1.state == "waiting"); |
|
365 |
assert(r2.state == "waiting"); |
|
366 |
done2(); |
|
367 |
||
368 |
r2:run(3); |
|
369 |
assert(r1.state == "waiting"); |
|
370 |
assert(r2.state == "waiting"); |
|
371 |
done2(); |
|
372 |
||
373 |
r2:run(4); |
|
374 |
assert(r1.state == "waiting"); |
|
375 |
assert(r2.state == "ready"); |
|
376 |
||
377 |
for i = 1, 2 do |
|
378 |
done1(); |
|
379 |
if i < 2 then |
|
380 |
assert(r1.state == "waiting"); |
|
381 |
end |
|
382 |
end |
|
383 |
||
384 |
assert(r1.state == "ready"); |
|
385 |
r1:run(4); |
|
386 |
assert(r1.state == "ready"); |
|
387 |
||
388 |
assert(r1.state == "ready"); |
|
389 |
--for k, v in ipairs(l1) do print(k,v) end |
|
390 |
end); |
|
391 |
it("should work work with multiple runners in parallel", function () |
|
392 |
-------------------- |
|
393 |
local wait1, done1; |
|
394 |
local last_item1 = 0; |
|
395 |
local r1, l1 = new(function (item) |
|
396 |
print("r1 processing ", item); |
|
397 |
assert(type(item) == "number") |
|
398 |
assert((item == last_item1 + 1) or item == 3); |
|
399 |
last_item1 = item; |
|
400 |
if item == 3 then |
|
401 |
wait1, done1 = async.waiter(); |
|
402 |
wait1(); |
|
403 |
end |
|
404 |
end, "r1"); |
|
405 |
||
406 |
local wait2, done2; |
|
407 |
local last_item2 = 0; |
|
408 |
local r2, l2 = new(function (item) |
|
409 |
print("r2 processing ", item); |
|
410 |
assert.is_number(item); |
|
411 |
assert((item == last_item2 + 1) or item == 3); |
|
412 |
last_item2 = item; |
|
413 |
if item == 3 then |
|
414 |
wait2, done2 = async.waiter(); |
|
415 |
wait2(); |
|
416 |
end |
|
417 |
end, "r2"); |
|
418 |
||
419 |
r1:run(1); |
|
420 |
assert.equal(r1.state, "ready"); |
|
421 |
r1:run(2); |
|
422 |
assert.equal(r1.state, "ready"); |
|
423 |
||
424 |
r1:run(5); |
|
425 |
assert.equal(r1.state, "ready"); |
|
426 |
||
427 |
local dones = {}; |
|
428 |
r1:run(3); |
|
429 |
assert.equal(r1.state, "waiting"); |
|
430 |
r1:run(5); -- Will error, when we get to it |
|
431 |
assert.equal(r1.state, "waiting"); |
|
432 |
done1(); |
|
433 |
assert.equal(r1.state, "ready"); |
|
434 |
r1:run(3); |
|
435 |
assert.equal(r1.state, "waiting"); |
|
436 |
||
437 |
r2:run(1); |
|
438 |
assert.equal(r1.state, "waiting"); |
|
439 |
assert.equal(r2.state, "ready"); |
|
440 |
||
441 |
r2:run(2); |
|
442 |
assert.equal(r1.state, "waiting"); |
|
443 |
assert.equal(r2.state, "ready"); |
|
444 |
||
445 |
r2:run(3); |
|
446 |
assert.equal(r1.state, "waiting"); |
|
447 |
assert.equal(r2.state, "waiting"); |
|
448 |
||
449 |
done2(); |
|
450 |
assert.equal(r1.state, "waiting"); |
|
451 |
assert.equal(r2.state, "ready"); |
|
452 |
||
453 |
r2:run(3); |
|
454 |
assert.equal(r1.state, "waiting"); |
|
455 |
assert.equal(r2.state, "waiting"); |
|
456 |
||
457 |
done2(); |
|
458 |
assert.equal(r1.state, "waiting"); |
|
459 |
assert.equal(r2.state, "ready"); |
|
460 |
||
461 |
r2:run(4); |
|
462 |
assert.equal(r1.state, "waiting"); |
|
463 |
assert.equal(r2.state, "ready"); |
|
464 |
||
465 |
done1(); |
|
466 |
||
467 |
assert.equal(r1.state, "ready"); |
|
468 |
r1:run(4); |
|
469 |
assert.equal(r1.state, "ready"); |
|
470 |
||
471 |
assert.equal(r1.state, "ready"); |
|
472 |
--for k, v in ipairs(l1) do print(k,v) end |
|
473 |
end); |
|
474 |
end); |
|
475 |
end); |