Software /
code /
prosody
Comparison
spec/util_async_spec.lua @ 8609:9f6ab206d741
util.async: Ensure runner is left in correct state after out-of-main-loop error (+tests)
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 16 Mar 2018 22:26:15 +0000 |
parent | 8608:a2e6caf5848d |
child | 8614:bfbafeced0c4 |
comparison
equal
deleted
inserted
replaced
8608:a2e6caf5848d | 8609:9f6ab206d741 |
---|---|
33 end); | 33 end); |
34 | 34 |
35 describe("#errors", function () | 35 describe("#errors", function () |
36 local last_processed_item, last_error; | 36 local last_processed_item, last_error; |
37 local r; | 37 local r; |
38 local wait, done; | |
38 r = async.runner(function (item) | 39 r = async.runner(function (item) |
39 if item == "error" then | 40 if item == "error" then |
40 error({ e = "test error" }); | 41 error({ e = "test error" }); |
42 elseif item == "wait" then | |
43 wait, done = async.waiter(); | |
44 wait(); | |
45 error({ e = "post wait error" }); | |
41 end | 46 end |
42 last_processed_item = item; | 47 last_processed_item = item; |
43 end, { | 48 end, { |
44 error = function (runner, err) | 49 error = function (runner, err) |
45 assert.equal(r, runner); | 50 assert.equal(r, runner); |
79 it("should not be fatal to the runner", function () | 84 it("should not be fatal to the runner", function () |
80 r:run("world"); | 85 r:run("world"); |
81 assert.equal(r.state, "ready"); | 86 assert.equal(r.state, "ready"); |
82 assert.equal(last_processed_item, "world"); | 87 assert.equal(last_processed_item, "world"); |
83 end); | 88 end); |
89 it("should work despite a #waiter", function () | |
90 -- This test covers an important case where a runner | |
91 -- throws an error while being executed outside of the | |
92 -- main loop. This happens when it was blocked ('waiting'), | |
93 -- and then released (via a call to done()). | |
94 last_error = nil; | |
95 r:run("wait"); | |
96 assert.equal(r.state, "waiting"); | |
97 done(); | |
98 -- At this point an error happens (state goes error->ready) | |
99 assert.equal(r.state, "ready"); | |
100 assert.is_table(last_error); | |
101 assert.equal(last_error.e, "post wait error"); | |
102 last_error = nil; | |
103 r:run("hello again"); | |
104 assert.equal(r.state, "ready"); | |
105 assert.equal(last_processed_item, "hello again"); | |
106 end); | |
84 end); | 107 end); |
85 end); | 108 end); |
86 describe("#waiter", function() | 109 describe("#waiter", function() |
87 it("should error outside of async context", function () | 110 it("should error outside of async context", function () |
88 assert.has_error(function () | 111 assert.has_error(function () |