Software / code / prosody
Annotate
spec/util_async_spec.lua @ 8614:bfbafeced0c4
util.async: Yet more tests
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Sat, 17 Mar 2018 11:47:07 +0000 |
| parent | 8609:9f6ab206d741 |
| child | 8615:e77b37de482e |
| 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) | |
| 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); | |
|
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); |
|
8607
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8605
diff
changeset
|
159 end); |
| 8605 | 160 end); |
| 161 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
|
162 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
|
163 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
|
164 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
|
165 end); |
|
a2e6caf5848d
util.async: Add test to ensure waiters throw an error outside async contexts
Matthew Wild <mwild1@gmail.com>
parents:
8607
diff
changeset
|
166 end); |
| 8605 | 167 it("should work", function () |
| 168 local wait, done; | |
| 169 | |
| 170 local r, l = new(function (item) | |
| 171 assert(type(item) == "number") | |
| 172 if item == 3 then | |
| 173 wait, done = async.waiter(); | |
| 174 wait(); | |
| 175 end | |
| 176 end); | |
| 177 | |
| 178 r:run(1); | |
| 179 assert(r.state == "ready"); | |
| 180 r:run(2); | |
| 181 assert(r.state == "ready"); | |
| 182 r:run(3); | |
| 183 assert(r.state == "waiting"); | |
| 184 done(); | |
| 185 assert(r.state == "ready"); | |
| 186 --for k, v in ipairs(l) do print(k,v) end | |
| 187 end); | |
| 188 | |
| 189 it("should work", function () | |
| 190 -------------------- | |
| 191 local wait, done; | |
| 192 local last_item = 0; | |
| 193 local r, l = new(function (item) | |
| 194 assert(type(item) == "number") | |
| 195 assert(item == last_item + 1); | |
| 196 last_item = item; | |
| 197 if item == 3 then | |
| 198 wait, done = async.waiter(); | |
| 199 wait(); | |
| 200 end | |
| 201 end); | |
| 202 | |
| 203 r:run(1); | |
| 204 assert(r.state == "ready"); | |
| 205 r:run(2); | |
| 206 assert(r.state == "ready"); | |
| 207 r:run(3); | |
| 208 assert(r.state == "waiting"); | |
| 209 r:run(4); | |
| 210 assert(r.state == "waiting"); | |
| 211 done(); | |
| 212 assert(r.state == "ready"); | |
| 213 --for k, v in ipairs(l) do print(k,v) end | |
| 214 end); | |
| 215 it("should work", function () | |
| 216 -------------------- | |
| 217 local wait, done; | |
| 218 local last_item = 0; | |
| 219 local r, l = new(function (item) | |
| 220 assert(type(item) == "number") | |
| 221 assert((item == last_item + 1) or item == 3); | |
| 222 last_item = item; | |
| 223 if item == 3 then | |
| 224 wait, done = async.waiter(); | |
| 225 wait(); | |
| 226 end | |
| 227 end); | |
| 228 | |
| 229 r:run(1); | |
| 230 assert(r.state == "ready"); | |
| 231 r:run(2); | |
| 232 assert(r.state == "ready"); | |
| 233 | |
| 234 local dones = {}; | |
| 235 r:run(3); | |
| 236 assert(r.state == "waiting"); | |
| 237 r:run(3); | |
| 238 assert(r.state == "waiting"); | |
| 239 r:run(3); | |
| 240 assert(r.state == "waiting"); | |
| 241 r:run(4); | |
| 242 assert(r.state == "waiting"); | |
| 243 | |
| 244 for i = 1, 3 do | |
| 245 done(); | |
| 246 if i < 3 then | |
| 247 assert(r.state == "waiting"); | |
| 248 end | |
| 249 end | |
| 250 | |
| 251 assert(r.state == "ready"); | |
| 252 --for k, v in ipairs(l) do print(k,v) end | |
| 253 end); | |
| 254 it("should work", function () | |
| 255 -------------------- | |
| 256 local wait, done; | |
| 257 local last_item = 0; | |
| 258 local r, l = new(function (item) | |
| 259 assert(type(item) == "number") | |
| 260 assert((item == last_item + 1) or item == 3); | |
| 261 last_item = item; | |
| 262 if item == 3 then | |
| 263 wait, done = async.waiter(); | |
| 264 wait(); | |
| 265 end | |
| 266 end); | |
| 267 | |
| 268 r:run(1); | |
| 269 assert(r.state == "ready"); | |
| 270 r:run(2); | |
| 271 assert(r.state == "ready"); | |
| 272 | |
| 273 local dones = {}; | |
| 274 r:run(3); | |
| 275 assert(r.state == "waiting"); | |
| 276 r:run(3); | |
| 277 assert(r.state == "waiting"); | |
| 278 | |
| 279 for i = 1, 2 do | |
| 280 done(); | |
| 281 if i < 2 then | |
| 282 assert(r.state == "waiting"); | |
| 283 end | |
| 284 end | |
| 285 | |
| 286 assert(r.state == "ready"); | |
| 287 r:run(4); | |
| 288 assert(r.state == "ready"); | |
| 289 | |
| 290 assert(r.state == "ready"); | |
| 291 --for k, v in ipairs(l) do print(k,v) end | |
| 292 end); | |
| 293 it("should work with multiple runners in parallel", function () | |
| 294 -- Now with multiple runners | |
| 295 -------------------- | |
| 296 local wait1, done1; | |
| 297 local last_item1 = 0; | |
| 298 local r1, l1 = new(function (item) | |
| 299 assert(type(item) == "number") | |
| 300 assert((item == last_item1 + 1) or item == 3); | |
| 301 last_item1 = item; | |
| 302 if item == 3 then | |
| 303 wait1, done1 = async.waiter(); | |
| 304 wait1(); | |
| 305 end | |
| 306 end, "r1"); | |
| 307 | |
| 308 local wait2, done2; | |
| 309 local last_item2 = 0; | |
| 310 local r2, l2 = new(function (item) | |
| 311 assert(type(item) == "number") | |
| 312 assert((item == last_item2 + 1) or item == 3); | |
| 313 last_item2 = item; | |
| 314 if item == 3 then | |
| 315 wait2, done2 = async.waiter(); | |
| 316 wait2(); | |
| 317 end | |
| 318 end, "r2"); | |
| 319 | |
| 320 r1:run(1); | |
| 321 assert(r1.state == "ready"); | |
| 322 r1:run(2); | |
| 323 assert(r1.state == "ready"); | |
| 324 | |
| 325 local dones = {}; | |
| 326 r1:run(3); | |
| 327 assert(r1.state == "waiting"); | |
| 328 r1:run(3); | |
| 329 assert(r1.state == "waiting"); | |
| 330 | |
| 331 r2:run(1); | |
| 332 assert(r1.state == "waiting"); | |
| 333 assert(r2.state == "ready"); | |
| 334 | |
| 335 r2:run(2); | |
| 336 assert(r1.state == "waiting"); | |
| 337 assert(r2.state == "ready"); | |
| 338 | |
| 339 r2:run(3); | |
| 340 assert(r1.state == "waiting"); | |
| 341 assert(r2.state == "waiting"); | |
| 342 done2(); | |
| 343 | |
| 344 r2:run(3); | |
| 345 assert(r1.state == "waiting"); | |
| 346 assert(r2.state == "waiting"); | |
| 347 done2(); | |
| 348 | |
| 349 r2:run(4); | |
| 350 assert(r1.state == "waiting"); | |
| 351 assert(r2.state == "ready"); | |
| 352 | |
| 353 for i = 1, 2 do | |
| 354 done1(); | |
| 355 if i < 2 then | |
| 356 assert(r1.state == "waiting"); | |
| 357 end | |
| 358 end | |
| 359 | |
| 360 assert(r1.state == "ready"); | |
| 361 r1:run(4); | |
| 362 assert(r1.state == "ready"); | |
| 363 | |
| 364 assert(r1.state == "ready"); | |
| 365 --for k, v in ipairs(l1) do print(k,v) end | |
| 366 end); | |
| 367 it("should work work with multiple runners in parallel", function () | |
| 368 -------------------- | |
| 369 local wait1, done1; | |
| 370 local last_item1 = 0; | |
| 371 local r1, l1 = new(function (item) | |
| 372 print("r1 processing ", item); | |
| 373 assert(type(item) == "number") | |
| 374 assert((item == last_item1 + 1) or item == 3); | |
| 375 last_item1 = item; | |
| 376 if item == 3 then | |
| 377 wait1, done1 = async.waiter(); | |
| 378 wait1(); | |
| 379 end | |
| 380 end, "r1"); | |
| 381 | |
| 382 local wait2, done2; | |
| 383 local last_item2 = 0; | |
| 384 local r2, l2 = new(function (item) | |
| 385 print("r2 processing ", item); | |
| 386 assert.is_number(item); | |
| 387 assert((item == last_item2 + 1) or item == 3); | |
| 388 last_item2 = item; | |
| 389 if item == 3 then | |
| 390 wait2, done2 = async.waiter(); | |
| 391 wait2(); | |
| 392 end | |
| 393 end, "r2"); | |
| 394 | |
| 395 r1:run(1); | |
| 396 assert.equal(r1.state, "ready"); | |
| 397 r1:run(2); | |
| 398 assert.equal(r1.state, "ready"); | |
| 399 | |
| 400 r1:run(5); | |
| 401 assert.equal(r1.state, "ready"); | |
| 402 | |
| 403 local dones = {}; | |
| 404 r1:run(3); | |
| 405 assert.equal(r1.state, "waiting"); | |
| 406 r1:run(5); -- Will error, when we get to it | |
| 407 assert.equal(r1.state, "waiting"); | |
| 408 done1(); | |
| 409 assert.equal(r1.state, "ready"); | |
| 410 r1:run(3); | |
| 411 assert.equal(r1.state, "waiting"); | |
| 412 | |
| 413 r2:run(1); | |
| 414 assert.equal(r1.state, "waiting"); | |
| 415 assert.equal(r2.state, "ready"); | |
| 416 | |
| 417 r2:run(2); | |
| 418 assert.equal(r1.state, "waiting"); | |
| 419 assert.equal(r2.state, "ready"); | |
| 420 | |
| 421 r2:run(3); | |
| 422 assert.equal(r1.state, "waiting"); | |
| 423 assert.equal(r2.state, "waiting"); | |
| 424 | |
| 425 done2(); | |
| 426 assert.equal(r1.state, "waiting"); | |
| 427 assert.equal(r2.state, "ready"); | |
| 428 | |
| 429 r2:run(3); | |
| 430 assert.equal(r1.state, "waiting"); | |
| 431 assert.equal(r2.state, "waiting"); | |
| 432 | |
| 433 done2(); | |
| 434 assert.equal(r1.state, "waiting"); | |
| 435 assert.equal(r2.state, "ready"); | |
| 436 | |
| 437 r2:run(4); | |
| 438 assert.equal(r1.state, "waiting"); | |
| 439 assert.equal(r2.state, "ready"); | |
| 440 | |
| 441 done1(); | |
| 442 | |
| 443 assert.equal(r1.state, "ready"); | |
| 444 r1:run(4); | |
| 445 assert.equal(r1.state, "ready"); | |
| 446 | |
| 447 assert.equal(r1.state, "ready"); | |
| 448 --for k, v in ipairs(l1) do print(k,v) end | |
| 449 end); | |
| 450 end); | |
| 451 end); |