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