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