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