Software /
code /
prosody
Comparison
spec/util_async_spec.lua @ 8632:02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Mon, 19 Mar 2018 16:31:53 +0000 |
parent | 8631:1daabc077393 |
child | 8633:8ec18a002c30 |
comparison
equal
deleted
inserted
replaced
8631:1daabc077393 | 8632:02b841ed03d1 |
---|---|
20 waiting = generic_logging_watcher("waiting"); | 20 waiting = generic_logging_watcher("waiting"); |
21 error = generic_logging_watcher("error"); | 21 error = generic_logging_watcher("error"); |
22 }, { | 22 }, { |
23 __index = function (_, event) | 23 __index = function (_, event) |
24 -- Unexpected watcher called | 24 -- Unexpected watcher called |
25 assert(false); | 25 assert(false, "unexpected watcher called: "..event); |
26 end; | 26 end; |
27 }) | 27 }) |
28 end | 28 end |
29 | 29 |
30 local function new(func, name) | 30 local function new(func) |
31 local event_log = {}; | 31 local event_log = {}; |
32 local spy_func = spy.new(func); | 32 local spy_func = spy.new(func); |
33 return async.runner(spy_func, mock_watchers(event_log)), event_log, spy_func; | 33 return async.runner(spy_func, mock_watchers(event_log)), event_log, spy_func; |
34 end | 34 end |
35 describe("#runner", function() | 35 describe("#runner", function() |
38 r:run(1); | 38 r:run(1); |
39 r:run(2); | 39 r:run(2); |
40 end); | 40 end); |
41 | 41 |
42 it("should be ready after creation", function () | 42 it("should be ready after creation", function () |
43 local r = async.runner(function (item) end); | 43 local r = async.runner(function () end); |
44 assert.equal(r.state, "ready"); | 44 assert.equal(r.state, "ready"); |
45 end); | 45 end); |
46 | 46 |
47 it("should do nothing if the queue is empty", function () | 47 it("should do nothing if the queue is empty", function () |
48 local did_run; | 48 local did_run; |
49 local r = async.runner(function (item) did_run = true end); | 49 local r = async.runner(function () did_run = true end); |
50 r:run(); | 50 r:run(); |
51 assert.equal(r.state, "ready"); | 51 assert.equal(r.state, "ready"); |
52 assert.is_nil(did_run); | 52 assert.is_nil(did_run); |
53 r:run("hello"); | 53 r:run("hello"); |
54 assert.is_true(did_run); | 54 assert.is_true(did_run); |
55 end); | 55 end); |
56 | 56 |
57 it("should support queuing work items without running", function () | 57 it("should support queuing work items without running", function () |
58 local did_run; | 58 local did_run; |
59 local r = async.runner(function (item) did_run = true end); | 59 local r = async.runner(function () did_run = true end); |
60 r:enqueue("hello"); | 60 r:enqueue("hello"); |
61 assert.equal(r.state, "ready"); | 61 assert.equal(r.state, "ready"); |
62 assert.is_nil(did_run); | 62 assert.is_nil(did_run); |
63 r:run(); | 63 r:run(); |
64 assert.is_true(did_run); | 64 assert.is_true(did_run); |
139 end); | 139 end); |
140 assert.equal(r.state, "ready"); | 140 assert.equal(r.state, "ready"); |
141 assert.equal(last_processed_item, "hello"); | 141 assert.equal(last_processed_item, "hello"); |
142 end); | 142 end); |
143 | 143 |
144 | 144 do |
145 local last_processed_item, last_error; | 145 local last_processed_item, last_error; |
146 local r; | 146 local r; |
147 local wait, done; | 147 local wait, done; |
148 r = async.runner(function (item) | 148 r = async.runner(function (item) |
149 if item == "error" then | 149 if item == "error" then |
150 error({ e = "test error" }); | 150 error({ e = "test error" }); |
151 elseif item == "wait" then | 151 elseif item == "wait" then |
152 wait, done = async.waiter(); | 152 wait, done = async.waiter(); |
153 wait(); | 153 wait(); |
154 error({ e = "post wait error" }); | 154 error({ e = "post wait error" }); |
155 end | 155 end |
156 last_processed_item = item; | 156 last_processed_item = item; |
157 end, mock({ | 157 end, mock({ |
158 ready = function () end; | 158 ready = function () end; |
159 waiting = function () end; | 159 waiting = function () end; |
160 error = function (runner, err) | 160 error = function (runner, err) |
161 assert.equal(r, runner); | 161 assert.equal(r, runner); |
162 last_error = err; | 162 last_error = err; |
163 end; | 163 end; |
164 })); | 164 })); |
165 | 165 |
166 randomize(false); | 166 randomize(false); --luacheck: ignore 113/randomize |
167 | 167 |
168 it("should not be fatal to the runner", function () | 168 it("should not be fatal to the runner", function () |
169 r:run("world"); | 169 r:run("world"); |
170 assert.equal(r.state, "ready"); | 170 assert.equal(r.state, "ready"); |
171 assert.spy(r.watchers.ready).was_not.called(); | 171 assert.spy(r.watchers.ready).was_not.called(); |
172 assert.equal(last_processed_item, "world"); | 172 assert.equal(last_processed_item, "world"); |
173 end); | 173 end); |
174 it("should work despite a #waiter", function () | 174 it("should work despite a #waiter", function () |
175 -- This test covers an important case where a runner | 175 -- This test covers an important case where a runner |
176 -- throws an error while being executed outside of the | 176 -- throws an error while being executed outside of the |
177 -- main loop. This happens when it was blocked ('waiting'), | 177 -- main loop. This happens when it was blocked ('waiting'), |
178 -- and then released (via a call to done()). | 178 -- and then released (via a call to done()). |
179 last_error = nil; | 179 last_error = nil; |
180 r:run("wait"); | 180 r:run("wait"); |
181 assert.equal(r.state, "waiting"); | 181 assert.equal(r.state, "waiting"); |
182 assert.spy(r.watchers.waiting).was.called(1); | 182 assert.spy(r.watchers.waiting).was.called(1); |
183 done(); | 183 done(); |
184 -- At this point an error happens (state goes error->ready) | 184 -- At this point an error happens (state goes error->ready) |
185 assert.equal(r.state, "ready"); | 185 assert.equal(r.state, "ready"); |
186 assert.spy(r.watchers.error).was.called(1); | 186 assert.spy(r.watchers.error).was.called(1); |
187 assert.spy(r.watchers.ready).was.called(1); | 187 assert.spy(r.watchers.ready).was.called(1); |
188 assert.is_table(last_error); | 188 assert.is_table(last_error); |
189 assert.equal(last_error.e, "post wait error"); | 189 assert.equal(last_error.e, "post wait error"); |
190 last_error = nil; | 190 last_error = nil; |
191 r:run("hello again"); | 191 r:run("hello again"); |
192 assert.spy(r.watchers.ready).was.called(1); | 192 assert.spy(r.watchers.ready).was.called(1); |
193 assert.spy(r.watchers.waiting).was.called(1); | 193 assert.spy(r.watchers.waiting).was.called(1); |
194 assert.spy(r.watchers.error).was.called(1); | 194 assert.spy(r.watchers.error).was.called(1); |
195 assert.equal(r.state, "ready"); | 195 assert.equal(r.state, "ready"); |
196 assert.equal(last_processed_item, "hello again"); | 196 assert.equal(last_processed_item, "hello again"); |
197 end); | 197 end); |
198 end | |
198 | 199 |
199 it("should continue to process work items", function () | 200 it("should continue to process work items", function () |
200 local wait, done, last_item; | 201 local last_item; |
201 local runner_func = spy.new(function (item) | 202 local runner_func = spy.new(function (item) |
202 if item == "error" then | 203 if item == "error" then |
203 error("test error"); | 204 error("test error"); |
204 end | 205 end |
205 last_item = item; | 206 last_item = item; |
211 }); | 212 }); |
212 runner:enqueue("one"); | 213 runner:enqueue("one"); |
213 runner:enqueue("error"); | 214 runner:enqueue("error"); |
214 runner:enqueue("two"); | 215 runner:enqueue("two"); |
215 runner:run(); | 216 runner:run(); |
216 assert.equal(r.state, "ready"); | 217 assert.equal(runner.state, "ready"); |
217 assert.spy(runner_func).was.called(3); | 218 assert.spy(runner_func).was.called(3); |
218 assert.spy(runner.watchers.error).was.called(1); | 219 assert.spy(runner.watchers.error).was.called(1); |
219 assert.spy(runner.watchers.ready).was.called(0); | 220 assert.spy(runner.watchers.ready).was.called(0); |
220 assert.spy(runner.watchers.waiting).was.called(0); | 221 assert.spy(runner.watchers.waiting).was.called(0); |
221 assert.equal(last_item, "two"); | 222 assert.equal(last_item, "two"); |
239 runner:enqueue("one"); | 240 runner:enqueue("one"); |
240 runner:enqueue("wait-error"); | 241 runner:enqueue("wait-error"); |
241 runner:enqueue("two"); | 242 runner:enqueue("two"); |
242 runner:run(); | 243 runner:run(); |
243 done(); | 244 done(); |
244 assert.equal(r.state, "ready"); | 245 assert.equal(runner.state, "ready"); |
245 assert.spy(runner_func).was.called(3); | 246 assert.spy(runner_func).was.called(3); |
246 assert.spy(runner.watchers.error).was.called(1); | 247 assert.spy(runner.watchers.error).was.called(1); |
247 assert.spy(runner.watchers.waiting).was.called(1); | 248 assert.spy(runner.watchers.waiting).was.called(1); |
248 assert.spy(runner.watchers.ready).was.called(1); | 249 assert.spy(runner.watchers.ready).was.called(1); |
249 assert.equal(last_item, "two"); | 250 assert.equal(last_item, "two"); |
542 wait, done = async.waiter(4); | 543 wait, done = async.waiter(4); |
543 wait(); | 544 wait(); |
544 processed_item = item; | 545 processed_item = item; |
545 end); | 546 end); |
546 r:run("test"); | 547 r:run("test"); |
547 for i = 1, 3 do | 548 for _ = 1, 3 do |
548 done(); | 549 done(); |
549 assert.equal(r.state, "waiting"); | 550 assert.equal(r.state, "waiting"); |
550 assert.is_nil(processed_item); | 551 assert.is_nil(processed_item); |
551 end | 552 end |
552 done(); | 553 done(); |
562 wait, done = async.waiter(4); | 563 wait, done = async.waiter(4); |
563 wait(); | 564 wait(); |
564 processed_item = item; | 565 processed_item = item; |
565 end); | 566 end); |
566 r:run("test"); | 567 r:run("test"); |
567 for i = 1, 4 do | 568 for _ = 1, 4 do |
568 done(); | 569 done(); |
569 end | 570 end |
570 assert.has_error(done);; | 571 assert.has_error(done); |
571 assert.equal(r.state, "ready"); | 572 assert.equal(r.state, "ready"); |
572 assert.equal(processed_item, "test"); | 573 assert.equal(processed_item, "test"); |
573 assert.spy(r.watchers.error).was_not.called(); | 574 assert.spy(r.watchers.error).was_not.called(); |
574 end); | 575 end); |
575 | 576 |