author | Kim Alvefur <zash@zash.se> |
Mon, 25 Nov 2019 23:51:41 +0100 | |
changeset 10452 | cbe524ed1a6a |
parent 8686 | 867ac771fb6e |
child 10545 | 6c6ff4509082 |
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 |
|
8626
ab242c513bf4
util.async: tests: Add helper function to create mock watcher callbacks
Matthew Wild <mwild1@gmail.com>
parents:
8625
diff
changeset
|
11 |
|
8633
deade38ffbbd
util.async: tests: slight modifications to allow more code reuse in tests
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
12 |
local function mock_watchers(event_log) |
deade38ffbbd
util.async: tests: slight modifications to allow more code reuse in tests
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
13 |
local function generic_logging_watcher(name) |
deade38ffbbd
util.async: tests: slight modifications to allow more code reuse in tests
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
14 |
return function (...) |
deade38ffbbd
util.async: tests: slight modifications to allow more code reuse in tests
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
15 |
table.insert(event_log, { name = name, n = select("#", ...)-1, select(2, ...) }); |
deade38ffbbd
util.async: tests: slight modifications to allow more code reuse in tests
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
16 |
end; |
deade38ffbbd
util.async: tests: slight modifications to allow more code reuse in tests
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
17 |
end; |
8626
ab242c513bf4
util.async: tests: Add helper function to create mock watcher callbacks
Matthew Wild <mwild1@gmail.com>
parents:
8625
diff
changeset
|
18 |
return setmetatable(mock{ |
8633
deade38ffbbd
util.async: tests: slight modifications to allow more code reuse in tests
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
19 |
ready = generic_logging_watcher("ready"); |
deade38ffbbd
util.async: tests: slight modifications to allow more code reuse in tests
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
20 |
waiting = generic_logging_watcher("waiting"); |
deade38ffbbd
util.async: tests: slight modifications to allow more code reuse in tests
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
21 |
error = generic_logging_watcher("error"); |
8626
ab242c513bf4
util.async: tests: Add helper function to create mock watcher callbacks
Matthew Wild <mwild1@gmail.com>
parents:
8625
diff
changeset
|
22 |
}, { |
ab242c513bf4
util.async: tests: Add helper function to create mock watcher callbacks
Matthew Wild <mwild1@gmail.com>
parents:
8625
diff
changeset
|
23 |
__index = function (_, event) |
ab242c513bf4
util.async: tests: Add helper function to create mock watcher callbacks
Matthew Wild <mwild1@gmail.com>
parents:
8625
diff
changeset
|
24 |
-- Unexpected watcher called |
8635
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
25 |
assert(false, "unexpected watcher called: "..event); |
8626
ab242c513bf4
util.async: tests: Add helper function to create mock watcher callbacks
Matthew Wild <mwild1@gmail.com>
parents:
8625
diff
changeset
|
26 |
end; |
ab242c513bf4
util.async: tests: Add helper function to create mock watcher callbacks
Matthew Wild <mwild1@gmail.com>
parents:
8625
diff
changeset
|
27 |
}) |
ab242c513bf4
util.async: tests: Add helper function to create mock watcher callbacks
Matthew Wild <mwild1@gmail.com>
parents:
8625
diff
changeset
|
28 |
end |
ab242c513bf4
util.async: tests: Add helper function to create mock watcher callbacks
Matthew Wild <mwild1@gmail.com>
parents:
8625
diff
changeset
|
29 |
|
8635
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
30 |
local function new(func) |
8633
deade38ffbbd
util.async: tests: slight modifications to allow more code reuse in tests
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
31 |
local event_log = {}; |
deade38ffbbd
util.async: tests: slight modifications to allow more code reuse in tests
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
32 |
local spy_func = spy.new(func); |
8636
8ec18a002c30
util.async: tests: more code re-use
Matthew Wild <mwild1@gmail.com>
parents:
8635
diff
changeset
|
33 |
return async.runner(spy_func, mock_watchers(event_log)), spy_func, event_log; |
8608 | 34 |
end |
35 |
describe("#runner", function() |
|
8625
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8621
diff
changeset
|
36 |
it("should work", function() |
8636
8ec18a002c30
util.async: tests: more code re-use
Matthew Wild <mwild1@gmail.com>
parents:
8635
diff
changeset
|
37 |
local r = new(function (item) assert(type(item) == "number") end); |
8608 | 38 |
r:run(1); |
39 |
r:run(2); |
|
40 |
end); |
|
8610
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
41 |
|
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
42 |
it("should be ready after creation", function () |
8636
8ec18a002c30
util.async: tests: more code re-use
Matthew Wild <mwild1@gmail.com>
parents:
8635
diff
changeset
|
43 |
local r = new(function () end); |
8610
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
44 |
assert.equal(r.state, "ready"); |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
45 |
end); |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
46 |
|
8617
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
47 |
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
|
48 |
local did_run; |
8636
8ec18a002c30
util.async: tests: more code re-use
Matthew Wild <mwild1@gmail.com>
parents:
8635
diff
changeset
|
49 |
local r = new(function () did_run = true end); |
8617
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
50 |
r:run(); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
51 |
assert.equal(r.state, "ready"); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
52 |
assert.is_nil(did_run); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
53 |
r:run("hello"); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
54 |
assert.is_true(did_run); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
55 |
end); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
56 |
|
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
57 |
it("should support queuing work items without running", function () |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
58 |
local did_run; |
8636
8ec18a002c30
util.async: tests: more code re-use
Matthew Wild <mwild1@gmail.com>
parents:
8635
diff
changeset
|
59 |
local r = new(function () did_run = true end); |
8617
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
60 |
r:enqueue("hello"); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
61 |
assert.equal(r.state, "ready"); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
62 |
assert.is_nil(did_run); |
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.is_true(did_run); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
65 |
end); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
66 |
|
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
67 |
it("should support queuing multiple work items", function () |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
68 |
local last_item; |
8636
8ec18a002c30
util.async: tests: more code re-use
Matthew Wild <mwild1@gmail.com>
parents:
8635
diff
changeset
|
69 |
local r, s = new(function (item) last_item = item; end); |
8617
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
70 |
r:enqueue("hello"); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
71 |
r:enqueue("there"); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
72 |
r:enqueue("world"); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
73 |
assert.equal(r.state, "ready"); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
74 |
r:run(); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
75 |
assert.equal(r.state, "ready"); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
76 |
assert.spy(s).was.called(3); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
77 |
assert.equal(last_item, "world"); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
78 |
end); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
79 |
|
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
80 |
it("should support all simple data types", function () |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
81 |
local last_item; |
8636
8ec18a002c30
util.async: tests: more code re-use
Matthew Wild <mwild1@gmail.com>
parents:
8635
diff
changeset
|
82 |
local r, s = new(function (item) last_item = item; end); |
8617
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
83 |
local values = { {}, 123, "hello", true, false }; |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
84 |
for i = 1, #values do |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
85 |
r:enqueue(values[i]); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
86 |
end |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
87 |
assert.equal(r.state, "ready"); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
88 |
r:run(); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
89 |
assert.equal(r.state, "ready"); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
90 |
assert.spy(s).was.called(#values); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
91 |
for i = 1, #values do |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
92 |
assert.spy(s).was.called_with(values[i]); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
93 |
end |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
94 |
assert.equal(last_item, values[#values]); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
95 |
end); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8612
diff
changeset
|
96 |
|
8684
0c077800cd70
util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents:
8672
diff
changeset
|
97 |
it("should work with no parameters", function () |
0c077800cd70
util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents:
8672
diff
changeset
|
98 |
local item = "fail"; |
0c077800cd70
util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents:
8672
diff
changeset
|
99 |
local r = async.runner(); |
0c077800cd70
util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents:
8672
diff
changeset
|
100 |
local f = spy.new(function () item = "success"; end); |
0c077800cd70
util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents:
8672
diff
changeset
|
101 |
r:run(f); |
0c077800cd70
util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents:
8672
diff
changeset
|
102 |
assert.spy(f).was.called(); |
0c077800cd70
util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents:
8672
diff
changeset
|
103 |
assert.equal(item, "success"); |
0c077800cd70
util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents:
8672
diff
changeset
|
104 |
end); |
0c077800cd70
util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents:
8672
diff
changeset
|
105 |
|
0c077800cd70
util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents:
8672
diff
changeset
|
106 |
it("supports a default error handler", function () |
0c077800cd70
util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents:
8672
diff
changeset
|
107 |
local item = "fail"; |
0c077800cd70
util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents:
8672
diff
changeset
|
108 |
local r = async.runner(); |
0c077800cd70
util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents:
8672
diff
changeset
|
109 |
local f = spy.new(function () error("test error"); end); |
0c077800cd70
util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents:
8672
diff
changeset
|
110 |
assert.error_matches(function () |
0c077800cd70
util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents:
8672
diff
changeset
|
111 |
r:run(f); |
0c077800cd70
util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents:
8672
diff
changeset
|
112 |
end, "test error"); |
0c077800cd70
util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents:
8672
diff
changeset
|
113 |
assert.spy(f).was.called(); |
0c077800cd70
util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents:
8672
diff
changeset
|
114 |
assert.equal(item, "fail"); |
0c077800cd70
util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents:
8672
diff
changeset
|
115 |
end); |
0c077800cd70
util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents:
8672
diff
changeset
|
116 |
|
8610
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
117 |
describe("#errors", function () |
8621
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
118 |
describe("should notify", function () |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
119 |
local last_processed_item, last_error; |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
120 |
local r; |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
121 |
r = async.runner(function (item) |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
122 |
if item == "error" then |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
123 |
error({ e = "test error" }); |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
124 |
end |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
125 |
last_processed_item = item; |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
126 |
end, mock{ |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
127 |
ready = function () end; |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
128 |
waiting = function () end; |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
129 |
error = function (runner, err) |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
130 |
assert.equal(r, runner); |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
131 |
last_error = err; |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
132 |
end; |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
133 |
}); |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
134 |
|
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
135 |
-- Simple item, no error |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
136 |
r:run("hello"); |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
137 |
assert.equal(r.state, "ready"); |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
138 |
assert.equal(last_processed_item, "hello"); |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
139 |
assert.spy(r.watchers.ready).was_not.called(); |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
140 |
assert.spy(r.watchers.error).was_not.called(); |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
141 |
|
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
142 |
-- Trigger an error inside the runner |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
143 |
assert.equal(last_error, nil); |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
144 |
r:run("error"); |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
145 |
test("the correct watcher functions", function () |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
146 |
-- Only the error watcher should have been called |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
147 |
assert.spy(r.watchers.ready).was_not.called(); |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
148 |
assert.spy(r.watchers.waiting).was_not.called(); |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
149 |
assert.spy(r.watchers.error).was.called(1); |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
150 |
end); |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
151 |
test("with the correct error", function () |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
152 |
-- The error watcher state should be correct, to |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
153 |
-- demonstrate the error was passed correctly |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
154 |
assert.is_table(last_error); |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
155 |
assert.equal(last_error.e, "test error"); |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
156 |
last_error = nil; |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
157 |
end); |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
158 |
assert.equal(r.state, "ready"); |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
159 |
assert.equal(last_processed_item, "hello"); |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
160 |
end); |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
161 |
|
8635
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
162 |
do |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
163 |
local last_processed_item, last_error; |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
164 |
local r; |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
165 |
local wait, done; |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
166 |
r = async.runner(function (item) |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
167 |
if item == "error" then |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
168 |
error({ e = "test error" }); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
169 |
elseif item == "wait" then |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
170 |
wait, done = async.waiter(); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
171 |
wait(); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
172 |
error({ e = "post wait error" }); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
173 |
end |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
174 |
last_processed_item = item; |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
175 |
end, mock({ |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
176 |
ready = function () end; |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
177 |
waiting = function () end; |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
178 |
error = function (runner, err) |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
179 |
assert.equal(r, runner); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
180 |
last_error = err; |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
181 |
end; |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
182 |
})); |
8621
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
183 |
|
8635
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
184 |
randomize(false); --luacheck: ignore 113/randomize |
8610
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
185 |
|
8635
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
186 |
it("should not be fatal to the runner", function () |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
187 |
r:run("world"); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
188 |
assert.equal(r.state, "ready"); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
189 |
assert.spy(r.watchers.ready).was_not.called(); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
190 |
assert.equal(last_processed_item, "world"); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
191 |
end); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
192 |
it("should work despite a #waiter", function () |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
193 |
-- This test covers an important case where a runner |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
194 |
-- throws an error while being executed outside of the |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
195 |
-- main loop. This happens when it was blocked ('waiting'), |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
196 |
-- and then released (via a call to done()). |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
197 |
last_error = nil; |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
198 |
r:run("wait"); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
199 |
assert.equal(r.state, "waiting"); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
200 |
assert.spy(r.watchers.waiting).was.called(1); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
201 |
done(); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
202 |
-- At this point an error happens (state goes error->ready) |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
203 |
assert.equal(r.state, "ready"); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
204 |
assert.spy(r.watchers.error).was.called(1); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
205 |
assert.spy(r.watchers.ready).was.called(1); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
206 |
assert.is_table(last_error); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
207 |
assert.equal(last_error.e, "post wait error"); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
208 |
last_error = nil; |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
209 |
r:run("hello again"); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
210 |
assert.spy(r.watchers.ready).was.called(1); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
211 |
assert.spy(r.watchers.waiting).was.called(1); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
212 |
assert.spy(r.watchers.error).was.called(1); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
213 |
assert.equal(r.state, "ready"); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
214 |
assert.equal(last_processed_item, "hello again"); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
215 |
end); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
216 |
end |
8618
e77b37de482e
util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents:
8617
diff
changeset
|
217 |
|
e77b37de482e
util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents:
8617
diff
changeset
|
218 |
it("should continue to process work items", function () |
8635
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
219 |
local last_item; |
8636
8ec18a002c30
util.async: tests: more code re-use
Matthew Wild <mwild1@gmail.com>
parents:
8635
diff
changeset
|
220 |
local runner, runner_func = new(function (item) |
8618
e77b37de482e
util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents:
8617
diff
changeset
|
221 |
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
|
222 |
error("test error"); |
8621
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
223 |
end |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
224 |
last_item = item; |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
225 |
end); |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
226 |
runner:enqueue("one"); |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
227 |
runner:enqueue("error"); |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
228 |
runner:enqueue("two"); |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
229 |
runner:run(); |
8635
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
230 |
assert.equal(runner.state, "ready"); |
8621
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
231 |
assert.spy(runner_func).was.called(3); |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
232 |
assert.spy(runner.watchers.error).was.called(1); |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
233 |
assert.spy(runner.watchers.ready).was.called(0); |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
234 |
assert.spy(runner.watchers.waiting).was.called(0); |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
235 |
assert.equal(last_item, "two"); |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
236 |
end); |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
237 |
|
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
238 |
it("should continue to process work items during resume", function () |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
239 |
local wait, done, last_item; |
8636
8ec18a002c30
util.async: tests: more code re-use
Matthew Wild <mwild1@gmail.com>
parents:
8635
diff
changeset
|
240 |
local runner, runner_func = new(function (item) |
8621
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
241 |
if item == "wait-error" then |
8618
e77b37de482e
util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents:
8617
diff
changeset
|
242 |
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
|
243 |
wait(); |
e77b37de482e
util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents:
8617
diff
changeset
|
244 |
error("test error"); |
e77b37de482e
util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents:
8617
diff
changeset
|
245 |
end |
e77b37de482e
util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents:
8617
diff
changeset
|
246 |
last_item = item; |
e77b37de482e
util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents:
8617
diff
changeset
|
247 |
end); |
8619
a15c891c6232
util.async: ensure change in e77b37de482e applies after out-of-loop resume also
Matthew Wild <mwild1@gmail.com>
parents:
8618
diff
changeset
|
248 |
runner:enqueue("one"); |
a15c891c6232
util.async: ensure change in e77b37de482e applies after out-of-loop resume also
Matthew Wild <mwild1@gmail.com>
parents:
8618
diff
changeset
|
249 |
runner:enqueue("wait-error"); |
a15c891c6232
util.async: ensure change in e77b37de482e applies after out-of-loop resume also
Matthew Wild <mwild1@gmail.com>
parents:
8618
diff
changeset
|
250 |
runner:enqueue("two"); |
a15c891c6232
util.async: ensure change in e77b37de482e applies after out-of-loop resume also
Matthew Wild <mwild1@gmail.com>
parents:
8618
diff
changeset
|
251 |
runner:run(); |
a15c891c6232
util.async: ensure change in e77b37de482e applies after out-of-loop resume also
Matthew Wild <mwild1@gmail.com>
parents:
8618
diff
changeset
|
252 |
done(); |
8635
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
253 |
assert.equal(runner.state, "ready"); |
8619
a15c891c6232
util.async: ensure change in e77b37de482e applies after out-of-loop resume also
Matthew Wild <mwild1@gmail.com>
parents:
8618
diff
changeset
|
254 |
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:
8618
diff
changeset
|
255 |
assert.spy(runner.watchers.error).was.called(1); |
8621
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
256 |
assert.spy(runner.watchers.waiting).was.called(1); |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8620
diff
changeset
|
257 |
assert.spy(runner.watchers.ready).was.called(1); |
8619
a15c891c6232
util.async: ensure change in e77b37de482e applies after out-of-loop resume also
Matthew Wild <mwild1@gmail.com>
parents:
8618
diff
changeset
|
258 |
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:
8618
diff
changeset
|
259 |
end); |
8610
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8608
diff
changeset
|
260 |
end); |
8608 | 261 |
end); |
262 |
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
|
263 |
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
|
264 |
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
|
265 |
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
|
266 |
end); |
a2e6caf5848d
util.async: Add test to ensure waiters throw an error outside async contexts
Matthew Wild <mwild1@gmail.com>
parents:
8610
diff
changeset
|
267 |
end); |
8608 | 268 |
it("should work", function () |
269 |
local wait, done; |
|
8625
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8621
diff
changeset
|
270 |
|
8636
8ec18a002c30
util.async: tests: more code re-use
Matthew Wild <mwild1@gmail.com>
parents:
8635
diff
changeset
|
271 |
local r = new(function (item) |
8608 | 272 |
assert(type(item) == "number") |
273 |
if item == 3 then |
|
274 |
wait, done = async.waiter(); |
|
275 |
wait(); |
|
276 |
end |
|
277 |
end); |
|
8625
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8621
diff
changeset
|
278 |
|
8608 | 279 |
r:run(1); |
280 |
assert(r.state == "ready"); |
|
281 |
r:run(2); |
|
282 |
assert(r.state == "ready"); |
|
283 |
r:run(3); |
|
284 |
assert(r.state == "waiting"); |
|
285 |
done(); |
|
286 |
assert(r.state == "ready"); |
|
287 |
--for k, v in ipairs(l) do print(k,v) end |
|
288 |
end); |
|
8625
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8621
diff
changeset
|
289 |
|
8608 | 290 |
it("should work", function () |
291 |
-------------------- |
|
292 |
local wait, done; |
|
293 |
local last_item = 0; |
|
8636
8ec18a002c30
util.async: tests: more code re-use
Matthew Wild <mwild1@gmail.com>
parents:
8635
diff
changeset
|
294 |
local r = new(function (item) |
8608 | 295 |
assert(type(item) == "number") |
296 |
assert(item == last_item + 1); |
|
297 |
last_item = item; |
|
298 |
if item == 3 then |
|
299 |
wait, done = async.waiter(); |
|
300 |
wait(); |
|
301 |
end |
|
302 |
end); |
|
8625
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8621
diff
changeset
|
303 |
|
8608 | 304 |
r:run(1); |
305 |
assert(r.state == "ready"); |
|
306 |
r:run(2); |
|
307 |
assert(r.state == "ready"); |
|
308 |
r:run(3); |
|
309 |
assert(r.state == "waiting"); |
|
310 |
r:run(4); |
|
311 |
assert(r.state == "waiting"); |
|
312 |
done(); |
|
313 |
assert(r.state == "ready"); |
|
314 |
--for k, v in ipairs(l) do print(k,v) end |
|
315 |
end); |
|
316 |
it("should work", function () |
|
317 |
-------------------- |
|
318 |
local wait, done; |
|
319 |
local last_item = 0; |
|
8636
8ec18a002c30
util.async: tests: more code re-use
Matthew Wild <mwild1@gmail.com>
parents:
8635
diff
changeset
|
320 |
local r = new(function (item) |
8608 | 321 |
assert(type(item) == "number") |
322 |
assert((item == last_item + 1) or item == 3); |
|
323 |
last_item = item; |
|
324 |
if item == 3 then |
|
325 |
wait, done = async.waiter(); |
|
326 |
wait(); |
|
327 |
end |
|
328 |
end); |
|
8625
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8621
diff
changeset
|
329 |
|
8608 | 330 |
r:run(1); |
331 |
assert(r.state == "ready"); |
|
332 |
r:run(2); |
|
333 |
assert(r.state == "ready"); |
|
8625
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8621
diff
changeset
|
334 |
|
8608 | 335 |
r:run(3); |
336 |
assert(r.state == "waiting"); |
|
337 |
r:run(3); |
|
338 |
assert(r.state == "waiting"); |
|
339 |
r:run(3); |
|
340 |
assert(r.state == "waiting"); |
|
341 |
r:run(4); |
|
342 |
assert(r.state == "waiting"); |
|
8625
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8621
diff
changeset
|
343 |
|
8608 | 344 |
for i = 1, 3 do |
345 |
done(); |
|
346 |
if i < 3 then |
|
347 |
assert(r.state == "waiting"); |
|
348 |
end |
|
349 |
end |
|
8625
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8621
diff
changeset
|
350 |
|
8608 | 351 |
assert(r.state == "ready"); |
352 |
--for k, v in ipairs(l) do print(k,v) end |
|
353 |
end); |
|
354 |
it("should work", function () |
|
355 |
-------------------- |
|
356 |
local wait, done; |
|
357 |
local last_item = 0; |
|
8636
8ec18a002c30
util.async: tests: more code re-use
Matthew Wild <mwild1@gmail.com>
parents:
8635
diff
changeset
|
358 |
local r = new(function (item) |
8608 | 359 |
assert(type(item) == "number") |
360 |
assert((item == last_item + 1) or item == 3); |
|
361 |
last_item = item; |
|
362 |
if item == 3 then |
|
363 |
wait, done = async.waiter(); |
|
364 |
wait(); |
|
365 |
end |
|
366 |
end); |
|
8625
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8621
diff
changeset
|
367 |
|
8608 | 368 |
r:run(1); |
369 |
assert(r.state == "ready"); |
|
370 |
r:run(2); |
|
371 |
assert(r.state == "ready"); |
|
8625
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8621
diff
changeset
|
372 |
|
8608 | 373 |
r:run(3); |
374 |
assert(r.state == "waiting"); |
|
375 |
r:run(3); |
|
376 |
assert(r.state == "waiting"); |
|
8625
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8621
diff
changeset
|
377 |
|
8608 | 378 |
for i = 1, 2 do |
379 |
done(); |
|
380 |
if i < 2 then |
|
381 |
assert(r.state == "waiting"); |
|
382 |
end |
|
383 |
end |
|
8625
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8621
diff
changeset
|
384 |
|
8608 | 385 |
assert(r.state == "ready"); |
386 |
r:run(4); |
|
387 |
assert(r.state == "ready"); |
|
8625
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8621
diff
changeset
|
388 |
|
8608 | 389 |
assert(r.state == "ready"); |
390 |
--for k, v in ipairs(l) do print(k,v) end |
|
391 |
end); |
|
392 |
it("should work with multiple runners in parallel", function () |
|
393 |
-- Now with multiple runners |
|
394 |
-------------------- |
|
395 |
local wait1, done1; |
|
396 |
local last_item1 = 0; |
|
8636
8ec18a002c30
util.async: tests: more code re-use
Matthew Wild <mwild1@gmail.com>
parents:
8635
diff
changeset
|
397 |
local r1 = new(function (item) |
8608 | 398 |
assert(type(item) == "number") |
399 |
assert((item == last_item1 + 1) or item == 3); |
|
400 |
last_item1 = item; |
|
401 |
if item == 3 then |
|
402 |
wait1, done1 = async.waiter(); |
|
403 |
wait1(); |
|
404 |
end |
|
405 |
end, "r1"); |
|
8625
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8621
diff
changeset
|
406 |
|
8608 | 407 |
local wait2, done2; |
408 |
local last_item2 = 0; |
|
8636
8ec18a002c30
util.async: tests: more code re-use
Matthew Wild <mwild1@gmail.com>
parents:
8635
diff
changeset
|
409 |
local r2 = new(function (item) |
8608 | 410 |
assert(type(item) == "number") |
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"); |
|
8625
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8621
diff
changeset
|
418 |
|
8608 | 419 |
r1:run(1); |
420 |
assert(r1.state == "ready"); |
|
421 |
r1:run(2); |
|
422 |
assert(r1.state == "ready"); |
|
8625
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8621
diff
changeset
|
423 |
|
8608 | 424 |
r1:run(3); |
425 |
assert(r1.state == "waiting"); |
|
426 |
r1:run(3); |
|
427 |
assert(r1.state == "waiting"); |
|
8625
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8621
diff
changeset
|
428 |
|
8608 | 429 |
r2:run(1); |
430 |
assert(r1.state == "waiting"); |
|
431 |
assert(r2.state == "ready"); |
|
8625
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8621
diff
changeset
|
432 |
|
8608 | 433 |
r2:run(2); |
434 |
assert(r1.state == "waiting"); |
|
435 |
assert(r2.state == "ready"); |
|
8625
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8621
diff
changeset
|
436 |
|
8608 | 437 |
r2:run(3); |
438 |
assert(r1.state == "waiting"); |
|
439 |
assert(r2.state == "waiting"); |
|
440 |
done2(); |
|
8625
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8621
diff
changeset
|
441 |
|
8608 | 442 |
r2:run(3); |
443 |
assert(r1.state == "waiting"); |
|
444 |
assert(r2.state == "waiting"); |
|
445 |
done2(); |
|
8625
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8621
diff
changeset
|
446 |
|
8608 | 447 |
r2:run(4); |
448 |
assert(r1.state == "waiting"); |
|
449 |
assert(r2.state == "ready"); |
|
8625
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8621
diff
changeset
|
450 |
|
8608 | 451 |
for i = 1, 2 do |
452 |
done1(); |
|
453 |
if i < 2 then |
|
454 |
assert(r1.state == "waiting"); |
|
455 |
end |
|
456 |
end |
|
8625
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8621
diff
changeset
|
457 |
|
8608 | 458 |
assert(r1.state == "ready"); |
459 |
r1:run(4); |
|
460 |
assert(r1.state == "ready"); |
|
8625
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8621
diff
changeset
|
461 |
|
8608 | 462 |
assert(r1.state == "ready"); |
463 |
--for k, v in ipairs(l1) do print(k,v) end |
|
464 |
end); |
|
465 |
it("should work work with multiple runners in parallel", function () |
|
466 |
-------------------- |
|
467 |
local wait1, done1; |
|
468 |
local last_item1 = 0; |
|
8636
8ec18a002c30
util.async: tests: more code re-use
Matthew Wild <mwild1@gmail.com>
parents:
8635
diff
changeset
|
469 |
local r1 = new(function (item) |
8608 | 470 |
print("r1 processing ", item); |
471 |
assert(type(item) == "number") |
|
472 |
assert((item == last_item1 + 1) or item == 3); |
|
473 |
last_item1 = item; |
|
474 |
if item == 3 then |
|
475 |
wait1, done1 = async.waiter(); |
|
476 |
wait1(); |
|
477 |
end |
|
478 |
end, "r1"); |
|
479 |
||
480 |
local wait2, done2; |
|
481 |
local last_item2 = 0; |
|
8636
8ec18a002c30
util.async: tests: more code re-use
Matthew Wild <mwild1@gmail.com>
parents:
8635
diff
changeset
|
482 |
local r2 = new(function (item) |
8608 | 483 |
print("r2 processing ", item); |
484 |
assert.is_number(item); |
|
485 |
assert((item == last_item2 + 1) or item == 3); |
|
486 |
last_item2 = item; |
|
487 |
if item == 3 then |
|
488 |
wait2, done2 = async.waiter(); |
|
489 |
wait2(); |
|
490 |
end |
|
491 |
end, "r2"); |
|
492 |
||
493 |
r1:run(1); |
|
494 |
assert.equal(r1.state, "ready"); |
|
495 |
r1:run(2); |
|
496 |
assert.equal(r1.state, "ready"); |
|
497 |
||
498 |
r1:run(5); |
|
499 |
assert.equal(r1.state, "ready"); |
|
500 |
||
501 |
r1:run(3); |
|
502 |
assert.equal(r1.state, "waiting"); |
|
503 |
r1:run(5); -- Will error, when we get to it |
|
504 |
assert.equal(r1.state, "waiting"); |
|
505 |
done1(); |
|
506 |
assert.equal(r1.state, "ready"); |
|
507 |
r1:run(3); |
|
508 |
assert.equal(r1.state, "waiting"); |
|
509 |
||
510 |
r2:run(1); |
|
511 |
assert.equal(r1.state, "waiting"); |
|
512 |
assert.equal(r2.state, "ready"); |
|
513 |
||
514 |
r2:run(2); |
|
515 |
assert.equal(r1.state, "waiting"); |
|
516 |
assert.equal(r2.state, "ready"); |
|
517 |
||
518 |
r2:run(3); |
|
519 |
assert.equal(r1.state, "waiting"); |
|
520 |
assert.equal(r2.state, "waiting"); |
|
521 |
||
522 |
done2(); |
|
523 |
assert.equal(r1.state, "waiting"); |
|
524 |
assert.equal(r2.state, "ready"); |
|
525 |
||
526 |
r2:run(3); |
|
527 |
assert.equal(r1.state, "waiting"); |
|
528 |
assert.equal(r2.state, "waiting"); |
|
529 |
||
530 |
done2(); |
|
531 |
assert.equal(r1.state, "waiting"); |
|
532 |
assert.equal(r2.state, "ready"); |
|
533 |
||
534 |
r2:run(4); |
|
535 |
assert.equal(r1.state, "waiting"); |
|
536 |
assert.equal(r2.state, "ready"); |
|
537 |
||
538 |
done1(); |
|
8625
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8621
diff
changeset
|
539 |
|
8608 | 540 |
assert.equal(r1.state, "ready"); |
541 |
r1:run(4); |
|
542 |
assert.equal(r1.state, "ready"); |
|
543 |
||
544 |
assert.equal(r1.state, "ready"); |
|
545 |
end); |
|
8627
5325f0e1791b
util.async: tests: Ensure done() can be called before wait()
Matthew Wild <mwild1@gmail.com>
parents:
8626
diff
changeset
|
546 |
|
8631
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8627
diff
changeset
|
547 |
it("should support multiple done() calls", function () |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8627
diff
changeset
|
548 |
local processed_item; |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8627
diff
changeset
|
549 |
local wait, done; |
8636
8ec18a002c30
util.async: tests: more code re-use
Matthew Wild <mwild1@gmail.com>
parents:
8635
diff
changeset
|
550 |
local r, rf = new(function (item) |
8631
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8627
diff
changeset
|
551 |
wait, done = async.waiter(4); |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8627
diff
changeset
|
552 |
wait(); |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8627
diff
changeset
|
553 |
processed_item = item; |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8627
diff
changeset
|
554 |
end); |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8627
diff
changeset
|
555 |
r:run("test"); |
8635
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
556 |
for _ = 1, 3 do |
8631
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8627
diff
changeset
|
557 |
done(); |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8627
diff
changeset
|
558 |
assert.equal(r.state, "waiting"); |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8627
diff
changeset
|
559 |
assert.is_nil(processed_item); |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8627
diff
changeset
|
560 |
end |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8627
diff
changeset
|
561 |
done(); |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8627
diff
changeset
|
562 |
assert.equal(r.state, "ready"); |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8627
diff
changeset
|
563 |
assert.equal(processed_item, "test"); |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8627
diff
changeset
|
564 |
assert.spy(r.watchers.error).was_not.called(); |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8627
diff
changeset
|
565 |
end); |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8627
diff
changeset
|
566 |
|
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8627
diff
changeset
|
567 |
it("should not allow done() to be called more than specified", function () |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8627
diff
changeset
|
568 |
local processed_item; |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8627
diff
changeset
|
569 |
local wait, done; |
8636
8ec18a002c30
util.async: tests: more code re-use
Matthew Wild <mwild1@gmail.com>
parents:
8635
diff
changeset
|
570 |
local r, rf = new(function (item) |
8631
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8627
diff
changeset
|
571 |
wait, done = async.waiter(4); |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8627
diff
changeset
|
572 |
wait(); |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8627
diff
changeset
|
573 |
processed_item = item; |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8627
diff
changeset
|
574 |
end); |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8627
diff
changeset
|
575 |
r:run("test"); |
8635
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
576 |
for _ = 1, 4 do |
8631
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8627
diff
changeset
|
577 |
done(); |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8627
diff
changeset
|
578 |
end |
8635
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8634
diff
changeset
|
579 |
assert.has_error(done); |
8631
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8627
diff
changeset
|
580 |
assert.equal(r.state, "ready"); |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8627
diff
changeset
|
581 |
assert.equal(processed_item, "test"); |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8627
diff
changeset
|
582 |
assert.spy(r.watchers.error).was_not.called(); |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8627
diff
changeset
|
583 |
end); |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8627
diff
changeset
|
584 |
|
8627
5325f0e1791b
util.async: tests: Ensure done() can be called before wait()
Matthew Wild <mwild1@gmail.com>
parents:
8626
diff
changeset
|
585 |
it("should allow done() to be called before wait()", function () |
5325f0e1791b
util.async: tests: Ensure done() can be called before wait()
Matthew Wild <mwild1@gmail.com>
parents:
8626
diff
changeset
|
586 |
local processed_item; |
8636
8ec18a002c30
util.async: tests: more code re-use
Matthew Wild <mwild1@gmail.com>
parents:
8635
diff
changeset
|
587 |
local r, rf = new(function (item) |
8627
5325f0e1791b
util.async: tests: Ensure done() can be called before wait()
Matthew Wild <mwild1@gmail.com>
parents:
8626
diff
changeset
|
588 |
local wait, done = async.waiter(); |
5325f0e1791b
util.async: tests: Ensure done() can be called before wait()
Matthew Wild <mwild1@gmail.com>
parents:
8626
diff
changeset
|
589 |
done(); |
5325f0e1791b
util.async: tests: Ensure done() can be called before wait()
Matthew Wild <mwild1@gmail.com>
parents:
8626
diff
changeset
|
590 |
wait(); |
5325f0e1791b
util.async: tests: Ensure done() can be called before wait()
Matthew Wild <mwild1@gmail.com>
parents:
8626
diff
changeset
|
591 |
processed_item = item; |
5325f0e1791b
util.async: tests: Ensure done() can be called before wait()
Matthew Wild <mwild1@gmail.com>
parents:
8626
diff
changeset
|
592 |
end); |
5325f0e1791b
util.async: tests: Ensure done() can be called before wait()
Matthew Wild <mwild1@gmail.com>
parents:
8626
diff
changeset
|
593 |
r:run("test"); |
5325f0e1791b
util.async: tests: Ensure done() can be called before wait()
Matthew Wild <mwild1@gmail.com>
parents:
8626
diff
changeset
|
594 |
assert.equal(processed_item, "test"); |
5325f0e1791b
util.async: tests: Ensure done() can be called before wait()
Matthew Wild <mwild1@gmail.com>
parents:
8626
diff
changeset
|
595 |
assert.equal(r.state, "ready"); |
5325f0e1791b
util.async: tests: Ensure done() can be called before wait()
Matthew Wild <mwild1@gmail.com>
parents:
8626
diff
changeset
|
596 |
-- Since the observable state did not change, |
5325f0e1791b
util.async: tests: Ensure done() can be called before wait()
Matthew Wild <mwild1@gmail.com>
parents:
8626
diff
changeset
|
597 |
-- the watchers should not have been called |
5325f0e1791b
util.async: tests: Ensure done() can be called before wait()
Matthew Wild <mwild1@gmail.com>
parents:
8626
diff
changeset
|
598 |
assert.spy(r.watchers.waiting).was_not.called(); |
5325f0e1791b
util.async: tests: Ensure done() can be called before wait()
Matthew Wild <mwild1@gmail.com>
parents:
8626
diff
changeset
|
599 |
assert.spy(r.watchers.ready).was_not.called(); |
5325f0e1791b
util.async: tests: Ensure done() can be called before wait()
Matthew Wild <mwild1@gmail.com>
parents:
8626
diff
changeset
|
600 |
end); |
8608 | 601 |
end); |
8651
ca710a71d730
util.async: Add ready() to check whether running in async context
Matthew Wild <mwild1@gmail.com>
parents:
8636
diff
changeset
|
602 |
|
ca710a71d730
util.async: Add ready() to check whether running in async context
Matthew Wild <mwild1@gmail.com>
parents:
8636
diff
changeset
|
603 |
describe("#ready()", function () |
ca710a71d730
util.async: Add ready() to check whether running in async context
Matthew Wild <mwild1@gmail.com>
parents:
8636
diff
changeset
|
604 |
it("should return false outside an async context", function () |
ca710a71d730
util.async: Add ready() to check whether running in async context
Matthew Wild <mwild1@gmail.com>
parents:
8636
diff
changeset
|
605 |
assert.falsy(async.ready()); |
ca710a71d730
util.async: Add ready() to check whether running in async context
Matthew Wild <mwild1@gmail.com>
parents:
8636
diff
changeset
|
606 |
end); |
ca710a71d730
util.async: Add ready() to check whether running in async context
Matthew Wild <mwild1@gmail.com>
parents:
8636
diff
changeset
|
607 |
it("should return true inside an async context", function () |
ca710a71d730
util.async: Add ready() to check whether running in async context
Matthew Wild <mwild1@gmail.com>
parents:
8636
diff
changeset
|
608 |
local r = new(function () |
ca710a71d730
util.async: Add ready() to check whether running in async context
Matthew Wild <mwild1@gmail.com>
parents:
8636
diff
changeset
|
609 |
assert.truthy(async.ready()); |
ca710a71d730
util.async: Add ready() to check whether running in async context
Matthew Wild <mwild1@gmail.com>
parents:
8636
diff
changeset
|
610 |
end); |
ca710a71d730
util.async: Add ready() to check whether running in async context
Matthew Wild <mwild1@gmail.com>
parents:
8636
diff
changeset
|
611 |
r:run(true); |
ca710a71d730
util.async: Add ready() to check whether running in async context
Matthew Wild <mwild1@gmail.com>
parents:
8636
diff
changeset
|
612 |
assert.spy(r.func).was.called(); |
ca710a71d730
util.async: Add ready() to check whether running in async context
Matthew Wild <mwild1@gmail.com>
parents:
8636
diff
changeset
|
613 |
assert.spy(r.watchers.error).was_not.called(); |
ca710a71d730
util.async: Add ready() to check whether running in async context
Matthew Wild <mwild1@gmail.com>
parents:
8636
diff
changeset
|
614 |
end); |
ca710a71d730
util.async: Add ready() to check whether running in async context
Matthew Wild <mwild1@gmail.com>
parents:
8636
diff
changeset
|
615 |
end); |
8608 | 616 |
end); |