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