Software /
code /
prosody
Annotate
spec/util_async_spec.lua @ 12327:4d6f4f6d0814
prosody.cfg.lua.dist: Remove mod_http_files
Serving web pages outside of specialized modules seems like a rare use
case that doesn't warrant a spot in the default config file.
Many users seem to have it confused with mod_http_upload, so removing it
should help with that.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Thu, 10 Feb 2022 18:35:39 +0100 |
parent | 11964:563ee7969f6c |
rev | line source |
---|---|
8605 | 1 local async = require "util.async"; |
11964
563ee7969f6c
util.async tests: Explicitly import match from luassert (luacheck)
Matthew Wild <mwild1@gmail.com>
parents:
11962
diff
changeset
|
2 local match = require "luassert.match"; |
8605 | 3 |
4 describe("util.async", function() | |
5 local debug = false; | |
6 local print = print; | |
7 if debug then | |
8 require "util.logger".add_simple_sink(print); | |
9 else | |
10 print = function () end | |
11 end | |
8623
ab242c513bf4
util.async: tests: Add helper function to create mock watcher callbacks
Matthew Wild <mwild1@gmail.com>
parents:
8622
diff
changeset
|
12 |
8630
deade38ffbbd
util.async: tests: slight modifications to allow more code reuse in tests
Matthew Wild <mwild1@gmail.com>
parents:
8628
diff
changeset
|
13 local function mock_watchers(event_log) |
deade38ffbbd
util.async: tests: slight modifications to allow more code reuse in tests
Matthew Wild <mwild1@gmail.com>
parents:
8628
diff
changeset
|
14 local function generic_logging_watcher(name) |
deade38ffbbd
util.async: tests: slight modifications to allow more code reuse in tests
Matthew Wild <mwild1@gmail.com>
parents:
8628
diff
changeset
|
15 return function (...) |
deade38ffbbd
util.async: tests: slight modifications to allow more code reuse in tests
Matthew Wild <mwild1@gmail.com>
parents:
8628
diff
changeset
|
16 table.insert(event_log, { name = name, n = select("#", ...)-1, select(2, ...) }); |
deade38ffbbd
util.async: tests: slight modifications to allow more code reuse in tests
Matthew Wild <mwild1@gmail.com>
parents:
8628
diff
changeset
|
17 end; |
deade38ffbbd
util.async: tests: slight modifications to allow more code reuse in tests
Matthew Wild <mwild1@gmail.com>
parents:
8628
diff
changeset
|
18 end; |
8623
ab242c513bf4
util.async: tests: Add helper function to create mock watcher callbacks
Matthew Wild <mwild1@gmail.com>
parents:
8622
diff
changeset
|
19 return setmetatable(mock{ |
8630
deade38ffbbd
util.async: tests: slight modifications to allow more code reuse in tests
Matthew Wild <mwild1@gmail.com>
parents:
8628
diff
changeset
|
20 ready = generic_logging_watcher("ready"); |
deade38ffbbd
util.async: tests: slight modifications to allow more code reuse in tests
Matthew Wild <mwild1@gmail.com>
parents:
8628
diff
changeset
|
21 waiting = generic_logging_watcher("waiting"); |
deade38ffbbd
util.async: tests: slight modifications to allow more code reuse in tests
Matthew Wild <mwild1@gmail.com>
parents:
8628
diff
changeset
|
22 error = generic_logging_watcher("error"); |
8623
ab242c513bf4
util.async: tests: Add helper function to create mock watcher callbacks
Matthew Wild <mwild1@gmail.com>
parents:
8622
diff
changeset
|
23 }, { |
ab242c513bf4
util.async: tests: Add helper function to create mock watcher callbacks
Matthew Wild <mwild1@gmail.com>
parents:
8622
diff
changeset
|
24 __index = function (_, event) |
ab242c513bf4
util.async: tests: Add helper function to create mock watcher callbacks
Matthew Wild <mwild1@gmail.com>
parents:
8622
diff
changeset
|
25 -- Unexpected watcher called |
8632
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
26 assert(false, "unexpected watcher called: "..event); |
8623
ab242c513bf4
util.async: tests: Add helper function to create mock watcher callbacks
Matthew Wild <mwild1@gmail.com>
parents:
8622
diff
changeset
|
27 end; |
ab242c513bf4
util.async: tests: Add helper function to create mock watcher callbacks
Matthew Wild <mwild1@gmail.com>
parents:
8622
diff
changeset
|
28 }) |
ab242c513bf4
util.async: tests: Add helper function to create mock watcher callbacks
Matthew Wild <mwild1@gmail.com>
parents:
8622
diff
changeset
|
29 end |
ab242c513bf4
util.async: tests: Add helper function to create mock watcher callbacks
Matthew Wild <mwild1@gmail.com>
parents:
8622
diff
changeset
|
30 |
8632
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
31 local function new(func) |
8630
deade38ffbbd
util.async: tests: slight modifications to allow more code reuse in tests
Matthew Wild <mwild1@gmail.com>
parents:
8628
diff
changeset
|
32 local event_log = {}; |
deade38ffbbd
util.async: tests: slight modifications to allow more code reuse in tests
Matthew Wild <mwild1@gmail.com>
parents:
8628
diff
changeset
|
33 local spy_func = spy.new(func); |
8633
8ec18a002c30
util.async: tests: more code re-use
Matthew Wild <mwild1@gmail.com>
parents:
8632
diff
changeset
|
34 return async.runner(spy_func, mock_watchers(event_log)), spy_func, event_log; |
8605 | 35 end |
36 describe("#runner", function() | |
8622
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8618
diff
changeset
|
37 it("should work", function() |
8633
8ec18a002c30
util.async: tests: more code re-use
Matthew Wild <mwild1@gmail.com>
parents:
8632
diff
changeset
|
38 local r = new(function (item) assert(type(item) == "number") end); |
8605 | 39 r:run(1); |
40 r:run(2); | |
41 end); | |
8607
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8605
diff
changeset
|
42 |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8605
diff
changeset
|
43 it("should be ready after creation", function () |
8633
8ec18a002c30
util.async: tests: more code re-use
Matthew Wild <mwild1@gmail.com>
parents:
8632
diff
changeset
|
44 local r = new(function () end); |
8607
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8605
diff
changeset
|
45 assert.equal(r.state, "ready"); |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8605
diff
changeset
|
46 end); |
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8605
diff
changeset
|
47 |
8614
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8609
diff
changeset
|
48 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
|
49 local did_run; |
8633
8ec18a002c30
util.async: tests: more code re-use
Matthew Wild <mwild1@gmail.com>
parents:
8632
diff
changeset
|
50 local r = new(function () did_run = true end); |
8614
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.equal(r.state, "ready"); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8609
diff
changeset
|
53 assert.is_nil(did_run); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8609
diff
changeset
|
54 r:run("hello"); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8609
diff
changeset
|
55 assert.is_true(did_run); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8609
diff
changeset
|
56 end); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8609
diff
changeset
|
57 |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8609
diff
changeset
|
58 it("should support queuing work items without running", function () |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8609
diff
changeset
|
59 local did_run; |
8633
8ec18a002c30
util.async: tests: more code re-use
Matthew Wild <mwild1@gmail.com>
parents:
8632
diff
changeset
|
60 local r = new(function () did_run = true end); |
8614
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8609
diff
changeset
|
61 r:enqueue("hello"); |
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 assert.is_nil(did_run); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8609
diff
changeset
|
64 r:run(); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8609
diff
changeset
|
65 assert.is_true(did_run); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8609
diff
changeset
|
66 end); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8609
diff
changeset
|
67 |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8609
diff
changeset
|
68 it("should support queuing multiple work items", function () |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8609
diff
changeset
|
69 local last_item; |
8633
8ec18a002c30
util.async: tests: more code re-use
Matthew Wild <mwild1@gmail.com>
parents:
8632
diff
changeset
|
70 local r, s = new(function (item) last_item = item; end); |
8614
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8609
diff
changeset
|
71 r:enqueue("hello"); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8609
diff
changeset
|
72 r:enqueue("there"); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8609
diff
changeset
|
73 r:enqueue("world"); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8609
diff
changeset
|
74 assert.equal(r.state, "ready"); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8609
diff
changeset
|
75 r:run(); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8609
diff
changeset
|
76 assert.equal(r.state, "ready"); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8609
diff
changeset
|
77 assert.spy(s).was.called(3); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8609
diff
changeset
|
78 assert.equal(last_item, "world"); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8609
diff
changeset
|
79 end); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8609
diff
changeset
|
80 |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8609
diff
changeset
|
81 it("should support all simple data types", function () |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8609
diff
changeset
|
82 local last_item; |
8633
8ec18a002c30
util.async: tests: more code re-use
Matthew Wild <mwild1@gmail.com>
parents:
8632
diff
changeset
|
83 local r, s = new(function (item) last_item = item; end); |
8614
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8609
diff
changeset
|
84 local values = { {}, 123, "hello", true, false }; |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8609
diff
changeset
|
85 for i = 1, #values do |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8609
diff
changeset
|
86 r:enqueue(values[i]); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8609
diff
changeset
|
87 end |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8609
diff
changeset
|
88 assert.equal(r.state, "ready"); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8609
diff
changeset
|
89 r:run(); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8609
diff
changeset
|
90 assert.equal(r.state, "ready"); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8609
diff
changeset
|
91 assert.spy(s).was.called(#values); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8609
diff
changeset
|
92 for i = 1, #values do |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8609
diff
changeset
|
93 assert.spy(s).was.called_with(values[i]); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8609
diff
changeset
|
94 end |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8609
diff
changeset
|
95 assert.equal(last_item, values[#values]); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8609
diff
changeset
|
96 end); |
bfbafeced0c4
util.async: Yet more tests
Matthew Wild <mwild1@gmail.com>
parents:
8609
diff
changeset
|
97 |
8681
0c077800cd70
util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents:
8669
diff
changeset
|
98 it("should work with no parameters", function () |
0c077800cd70
util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents:
8669
diff
changeset
|
99 local item = "fail"; |
0c077800cd70
util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents:
8669
diff
changeset
|
100 local r = async.runner(); |
0c077800cd70
util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents:
8669
diff
changeset
|
101 local f = spy.new(function () item = "success"; end); |
0c077800cd70
util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents:
8669
diff
changeset
|
102 r:run(f); |
0c077800cd70
util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents:
8669
diff
changeset
|
103 assert.spy(f).was.called(); |
0c077800cd70
util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents:
8669
diff
changeset
|
104 assert.equal(item, "success"); |
0c077800cd70
util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents:
8669
diff
changeset
|
105 end); |
0c077800cd70
util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents:
8669
diff
changeset
|
106 |
0c077800cd70
util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents:
8669
diff
changeset
|
107 it("supports a default error handler", function () |
0c077800cd70
util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents:
8669
diff
changeset
|
108 local item = "fail"; |
0c077800cd70
util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents:
8669
diff
changeset
|
109 local r = async.runner(); |
0c077800cd70
util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents:
8669
diff
changeset
|
110 local f = spy.new(function () error("test error"); end); |
0c077800cd70
util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents:
8669
diff
changeset
|
111 assert.error_matches(function () |
0c077800cd70
util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents:
8669
diff
changeset
|
112 r:run(f); |
0c077800cd70
util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents:
8669
diff
changeset
|
113 end, "test error"); |
0c077800cd70
util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents:
8669
diff
changeset
|
114 assert.spy(f).was.called(); |
0c077800cd70
util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents:
8669
diff
changeset
|
115 assert.equal(item, "fail"); |
0c077800cd70
util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents:
8669
diff
changeset
|
116 end); |
0c077800cd70
util.async: Make parameters to async.runner() optional
Matthew Wild <mwild1@gmail.com>
parents:
8669
diff
changeset
|
117 |
8607
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8605
diff
changeset
|
118 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
|
119 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
|
120 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
|
121 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
|
122 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
|
123 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
|
124 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
|
125 end |
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_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
|
127 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
|
128 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
|
129 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
|
130 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
|
131 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
|
132 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
|
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 }); |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8617
diff
changeset
|
135 |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8617
diff
changeset
|
136 -- 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
|
137 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
|
138 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
|
139 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
|
140 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
|
141 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
|
142 |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8617
diff
changeset
|
143 -- 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
|
144 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
|
145 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
|
146 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
|
147 -- 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
|
148 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
|
149 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
|
150 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
|
151 end); |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8617
diff
changeset
|
152 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
|
153 -- 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
|
154 -- 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
|
155 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
|
156 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
|
157 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
|
158 end); |
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.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
|
160 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
|
161 end); |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8617
diff
changeset
|
162 |
8632
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
163 do |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
164 local last_processed_item, last_error; |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
165 local r; |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
166 local wait, done; |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
167 r = async.runner(function (item) |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
168 if item == "error" then |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
169 error({ e = "test error" }); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
170 elseif item == "wait" then |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
171 wait, done = async.waiter(); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
172 wait(); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
173 error({ e = "post wait error" }); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
174 end |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
175 last_processed_item = item; |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
176 end, mock({ |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
177 ready = function () end; |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
178 waiting = function () end; |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
179 error = function (runner, err) |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
180 assert.equal(r, runner); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
181 last_error = err; |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
182 end; |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
183 })); |
8618
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8617
diff
changeset
|
184 |
8632
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
185 randomize(false); --luacheck: ignore 113/randomize |
8607
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8605
diff
changeset
|
186 |
8632
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
187 it("should not be fatal to the runner", function () |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
188 r:run("world"); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
189 assert.equal(r.state, "ready"); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
190 assert.spy(r.watchers.ready).was_not.called(); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
191 assert.equal(last_processed_item, "world"); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
192 end); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
193 it("should work despite a #waiter", function () |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
194 -- This test covers an important case where a runner |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
195 -- throws an error while being executed outside of the |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
196 -- main loop. This happens when it was blocked ('waiting'), |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
197 -- and then released (via a call to done()). |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
198 last_error = nil; |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
199 r:run("wait"); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
200 assert.equal(r.state, "waiting"); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
201 assert.spy(r.watchers.waiting).was.called(1); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
202 done(); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
203 -- At this point an error happens (state goes error->ready) |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
204 assert.equal(r.state, "ready"); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
205 assert.spy(r.watchers.error).was.called(1); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
206 assert.spy(r.watchers.ready).was.called(1); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
207 assert.is_table(last_error); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
208 assert.equal(last_error.e, "post wait error"); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
209 last_error = nil; |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
210 r:run("hello again"); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
211 assert.spy(r.watchers.ready).was.called(1); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
212 assert.spy(r.watchers.waiting).was.called(1); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
213 assert.spy(r.watchers.error).was.called(1); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
214 assert.equal(r.state, "ready"); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
215 assert.equal(last_processed_item, "hello again"); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
216 end); |
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
217 end |
8615
e77b37de482e
util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents:
8614
diff
changeset
|
218 |
e77b37de482e
util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents:
8614
diff
changeset
|
219 it("should continue to process work items", function () |
8632
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
220 local last_item; |
8633
8ec18a002c30
util.async: tests: more code re-use
Matthew Wild <mwild1@gmail.com>
parents:
8632
diff
changeset
|
221 local runner, runner_func = new(function (item) |
8615
e77b37de482e
util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents:
8614
diff
changeset
|
222 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
|
223 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
|
224 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 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
|
226 end); |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8617
diff
changeset
|
227 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
|
228 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
|
229 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
|
230 runner:run(); |
8632
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
231 assert.equal(runner.state, "ready"); |
8618
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8617
diff
changeset
|
232 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
|
233 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
|
234 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
|
235 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
|
236 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
|
237 end); |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8617
diff
changeset
|
238 |
680b1caa2dea
util.async: tests: replace peeking at internal state with monitoring correct callback behaviour
Matthew Wild <mwild1@gmail.com>
parents:
8617
diff
changeset
|
239 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
|
240 local wait, done, last_item; |
8633
8ec18a002c30
util.async: tests: more code re-use
Matthew Wild <mwild1@gmail.com>
parents:
8632
diff
changeset
|
241 local runner, runner_func = new(function (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
|
242 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
|
243 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
|
244 wait(); |
e77b37de482e
util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents:
8614
diff
changeset
|
245 error("test error"); |
e77b37de482e
util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents:
8614
diff
changeset
|
246 end |
e77b37de482e
util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents:
8614
diff
changeset
|
247 last_item = item; |
e77b37de482e
util.async: Behaviour change: continue to process queued items after errors
Matthew Wild <mwild1@gmail.com>
parents:
8614
diff
changeset
|
248 end); |
8616
a15c891c6232
util.async: ensure change in e77b37de482e applies after out-of-loop resume also
Matthew Wild <mwild1@gmail.com>
parents:
8615
diff
changeset
|
249 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
|
250 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
|
251 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
|
252 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
|
253 done(); |
8632
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
254 assert.equal(runner.state, "ready"); |
8616
a15c891c6232
util.async: ensure change in e77b37de482e applies after out-of-loop resume also
Matthew Wild <mwild1@gmail.com>
parents:
8615
diff
changeset
|
255 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
|
256 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
|
257 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
|
258 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
|
259 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
|
260 end); |
8607
62bb06cf8a43
util.async: Add tests to specifically cover error handling
Matthew Wild <mwild1@gmail.com>
parents:
8605
diff
changeset
|
261 end); |
8605 | 262 end); |
263 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
|
264 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
|
265 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
|
266 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
|
267 end); |
a2e6caf5848d
util.async: Add test to ensure waiters throw an error outside async contexts
Matthew Wild <mwild1@gmail.com>
parents:
8607
diff
changeset
|
268 end); |
8605 | 269 it("should work", function () |
270 local wait, done; | |
8622
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8618
diff
changeset
|
271 |
8633
8ec18a002c30
util.async: tests: more code re-use
Matthew Wild <mwild1@gmail.com>
parents:
8632
diff
changeset
|
272 local r = new(function (item) |
8605 | 273 assert(type(item) == "number") |
274 if item == 3 then | |
275 wait, done = async.waiter(); | |
276 wait(); | |
277 end | |
278 end); | |
8622
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8618
diff
changeset
|
279 |
8605 | 280 r:run(1); |
281 assert(r.state == "ready"); | |
282 r:run(2); | |
283 assert(r.state == "ready"); | |
284 r:run(3); | |
285 assert(r.state == "waiting"); | |
286 done(); | |
287 assert(r.state == "ready"); | |
288 --for k, v in ipairs(l) do print(k,v) end | |
289 end); | |
8622
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8618
diff
changeset
|
290 |
8605 | 291 it("should work", function () |
292 -------------------- | |
293 local wait, done; | |
294 local last_item = 0; | |
8633
8ec18a002c30
util.async: tests: more code re-use
Matthew Wild <mwild1@gmail.com>
parents:
8632
diff
changeset
|
295 local r = new(function (item) |
8605 | 296 assert(type(item) == "number") |
297 assert(item == last_item + 1); | |
298 last_item = item; | |
299 if item == 3 then | |
300 wait, done = async.waiter(); | |
301 wait(); | |
302 end | |
303 end); | |
8622
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8618
diff
changeset
|
304 |
8605 | 305 r:run(1); |
306 assert(r.state == "ready"); | |
307 r:run(2); | |
308 assert(r.state == "ready"); | |
309 r:run(3); | |
310 assert(r.state == "waiting"); | |
311 r:run(4); | |
312 assert(r.state == "waiting"); | |
313 done(); | |
314 assert(r.state == "ready"); | |
315 --for k, v in ipairs(l) do print(k,v) end | |
316 end); | |
317 it("should work", function () | |
318 -------------------- | |
319 local wait, done; | |
320 local last_item = 0; | |
8633
8ec18a002c30
util.async: tests: more code re-use
Matthew Wild <mwild1@gmail.com>
parents:
8632
diff
changeset
|
321 local r = new(function (item) |
8605 | 322 assert(type(item) == "number") |
323 assert((item == last_item + 1) or item == 3); | |
324 last_item = item; | |
325 if item == 3 then | |
326 wait, done = async.waiter(); | |
327 wait(); | |
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 r:run(1); |
332 assert(r.state == "ready"); | |
333 r:run(2); | |
334 assert(r.state == "ready"); | |
8622
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8618
diff
changeset
|
335 |
8605 | 336 r:run(3); |
337 assert(r.state == "waiting"); | |
338 r:run(3); | |
339 assert(r.state == "waiting"); | |
340 r:run(3); | |
341 assert(r.state == "waiting"); | |
342 r:run(4); | |
343 assert(r.state == "waiting"); | |
8622
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8618
diff
changeset
|
344 |
8605 | 345 for i = 1, 3 do |
346 done(); | |
347 if i < 3 then | |
348 assert(r.state == "waiting"); | |
349 end | |
350 end | |
8622
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8618
diff
changeset
|
351 |
8605 | 352 assert(r.state == "ready"); |
353 --for k, v in ipairs(l) do print(k,v) end | |
354 end); | |
355 it("should work", function () | |
356 -------------------- | |
357 local wait, done; | |
358 local last_item = 0; | |
8633
8ec18a002c30
util.async: tests: more code re-use
Matthew Wild <mwild1@gmail.com>
parents:
8632
diff
changeset
|
359 local r = new(function (item) |
8605 | 360 assert(type(item) == "number") |
361 assert((item == last_item + 1) or item == 3); | |
362 last_item = item; | |
363 if item == 3 then | |
364 wait, done = async.waiter(); | |
365 wait(); | |
366 end | |
367 end); | |
8622
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8618
diff
changeset
|
368 |
8605 | 369 r:run(1); |
370 assert(r.state == "ready"); | |
371 r:run(2); | |
372 assert(r.state == "ready"); | |
8622
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8618
diff
changeset
|
373 |
8605 | 374 r:run(3); |
375 assert(r.state == "waiting"); | |
376 r:run(3); | |
377 assert(r.state == "waiting"); | |
8622
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8618
diff
changeset
|
378 |
8605 | 379 for i = 1, 2 do |
380 done(); | |
381 if i < 2 then | |
382 assert(r.state == "waiting"); | |
383 end | |
384 end | |
8622
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8618
diff
changeset
|
385 |
8605 | 386 assert(r.state == "ready"); |
387 r:run(4); | |
388 assert(r.state == "ready"); | |
8622
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8618
diff
changeset
|
389 |
8605 | 390 assert(r.state == "ready"); |
391 --for k, v in ipairs(l) do print(k,v) end | |
392 end); | |
393 it("should work with multiple runners in parallel", function () | |
394 -- Now with multiple runners | |
395 -------------------- | |
396 local wait1, done1; | |
397 local last_item1 = 0; | |
8633
8ec18a002c30
util.async: tests: more code re-use
Matthew Wild <mwild1@gmail.com>
parents:
8632
diff
changeset
|
398 local r1 = new(function (item) |
8605 | 399 assert(type(item) == "number") |
400 assert((item == last_item1 + 1) or item == 3); | |
401 last_item1 = item; | |
402 if item == 3 then | |
403 wait1, done1 = async.waiter(); | |
404 wait1(); | |
405 end | |
406 end, "r1"); | |
8622
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8618
diff
changeset
|
407 |
8605 | 408 local wait2, done2; |
409 local last_item2 = 0; | |
8633
8ec18a002c30
util.async: tests: more code re-use
Matthew Wild <mwild1@gmail.com>
parents:
8632
diff
changeset
|
410 local r2 = new(function (item) |
8605 | 411 assert(type(item) == "number") |
412 assert((item == last_item2 + 1) or item == 3); | |
413 last_item2 = item; | |
414 if item == 3 then | |
415 wait2, done2 = async.waiter(); | |
416 wait2(); | |
417 end | |
418 end, "r2"); | |
8622
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8618
diff
changeset
|
419 |
8605 | 420 r1:run(1); |
421 assert(r1.state == "ready"); | |
422 r1:run(2); | |
423 assert(r1.state == "ready"); | |
8622
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8618
diff
changeset
|
424 |
8605 | 425 r1:run(3); |
426 assert(r1.state == "waiting"); | |
427 r1:run(3); | |
428 assert(r1.state == "waiting"); | |
8622
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8618
diff
changeset
|
429 |
8605 | 430 r2:run(1); |
431 assert(r1.state == "waiting"); | |
432 assert(r2.state == "ready"); | |
8622
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8618
diff
changeset
|
433 |
8605 | 434 r2:run(2); |
435 assert(r1.state == "waiting"); | |
436 assert(r2.state == "ready"); | |
8622
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8618
diff
changeset
|
437 |
8605 | 438 r2:run(3); |
439 assert(r1.state == "waiting"); | |
440 assert(r2.state == "waiting"); | |
441 done2(); | |
8622
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8618
diff
changeset
|
442 |
8605 | 443 r2:run(3); |
444 assert(r1.state == "waiting"); | |
445 assert(r2.state == "waiting"); | |
446 done2(); | |
8622
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8618
diff
changeset
|
447 |
8605 | 448 r2:run(4); |
449 assert(r1.state == "waiting"); | |
450 assert(r2.state == "ready"); | |
8622
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8618
diff
changeset
|
451 |
8605 | 452 for i = 1, 2 do |
453 done1(); | |
454 if i < 2 then | |
455 assert(r1.state == "waiting"); | |
456 end | |
457 end | |
8622
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8618
diff
changeset
|
458 |
8605 | 459 assert(r1.state == "ready"); |
460 r1:run(4); | |
461 assert(r1.state == "ready"); | |
8622
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8618
diff
changeset
|
462 |
8605 | 463 assert(r1.state == "ready"); |
464 --for k, v in ipairs(l1) do print(k,v) end | |
465 end); | |
466 it("should work work with multiple runners in parallel", function () | |
467 -------------------- | |
468 local wait1, done1; | |
469 local last_item1 = 0; | |
8633
8ec18a002c30
util.async: tests: more code re-use
Matthew Wild <mwild1@gmail.com>
parents:
8632
diff
changeset
|
470 local r1 = new(function (item) |
8605 | 471 print("r1 processing ", item); |
472 assert(type(item) == "number") | |
473 assert((item == last_item1 + 1) or item == 3); | |
474 last_item1 = item; | |
475 if item == 3 then | |
476 wait1, done1 = async.waiter(); | |
477 wait1(); | |
478 end | |
479 end, "r1"); | |
480 | |
481 local wait2, done2; | |
482 local last_item2 = 0; | |
8633
8ec18a002c30
util.async: tests: more code re-use
Matthew Wild <mwild1@gmail.com>
parents:
8632
diff
changeset
|
483 local r2 = new(function (item) |
8605 | 484 print("r2 processing ", item); |
485 assert.is_number(item); | |
486 assert((item == last_item2 + 1) or item == 3); | |
487 last_item2 = item; | |
488 if item == 3 then | |
489 wait2, done2 = async.waiter(); | |
490 wait2(); | |
491 end | |
492 end, "r2"); | |
493 | |
494 r1:run(1); | |
495 assert.equal(r1.state, "ready"); | |
496 r1:run(2); | |
497 assert.equal(r1.state, "ready"); | |
498 | |
499 r1:run(5); | |
500 assert.equal(r1.state, "ready"); | |
501 | |
502 r1:run(3); | |
503 assert.equal(r1.state, "waiting"); | |
504 r1:run(5); -- Will error, when we get to it | |
505 assert.equal(r1.state, "waiting"); | |
506 done1(); | |
507 assert.equal(r1.state, "ready"); | |
508 r1:run(3); | |
509 assert.equal(r1.state, "waiting"); | |
510 | |
511 r2:run(1); | |
512 assert.equal(r1.state, "waiting"); | |
513 assert.equal(r2.state, "ready"); | |
514 | |
515 r2:run(2); | |
516 assert.equal(r1.state, "waiting"); | |
517 assert.equal(r2.state, "ready"); | |
518 | |
519 r2:run(3); | |
520 assert.equal(r1.state, "waiting"); | |
521 assert.equal(r2.state, "waiting"); | |
522 | |
523 done2(); | |
524 assert.equal(r1.state, "waiting"); | |
525 assert.equal(r2.state, "ready"); | |
526 | |
527 r2:run(3); | |
528 assert.equal(r1.state, "waiting"); | |
529 assert.equal(r2.state, "waiting"); | |
530 | |
531 done2(); | |
532 assert.equal(r1.state, "waiting"); | |
533 assert.equal(r2.state, "ready"); | |
534 | |
535 r2:run(4); | |
536 assert.equal(r1.state, "waiting"); | |
537 assert.equal(r2.state, "ready"); | |
538 | |
539 done1(); | |
8622
92fee8a6c988
util.async: Trim trailing whitespace in tests [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8618
diff
changeset
|
540 |
8605 | 541 assert.equal(r1.state, "ready"); |
542 r1:run(4); | |
543 assert.equal(r1.state, "ready"); | |
544 | |
545 assert.equal(r1.state, "ready"); | |
546 end); | |
8624
5325f0e1791b
util.async: tests: Ensure done() can be called before wait()
Matthew Wild <mwild1@gmail.com>
parents:
8623
diff
changeset
|
547 |
10541
6c6ff4509082
tests: Silence [luacheck] warnings
Kim Alvefur <zash@zash.se>
parents:
8683
diff
changeset
|
548 -- luacheck: ignore 211/rf |
6c6ff4509082
tests: Silence [luacheck] warnings
Kim Alvefur <zash@zash.se>
parents:
8683
diff
changeset
|
549 -- FIXME what's rf? |
8628
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8624
diff
changeset
|
550 it("should support multiple done() calls", function () |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8624
diff
changeset
|
551 local processed_item; |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8624
diff
changeset
|
552 local wait, done; |
8633
8ec18a002c30
util.async: tests: more code re-use
Matthew Wild <mwild1@gmail.com>
parents:
8632
diff
changeset
|
553 local r, rf = new(function (item) |
8628
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8624
diff
changeset
|
554 wait, done = async.waiter(4); |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8624
diff
changeset
|
555 wait(); |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8624
diff
changeset
|
556 processed_item = item; |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8624
diff
changeset
|
557 end); |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8624
diff
changeset
|
558 r:run("test"); |
8632
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
559 for _ = 1, 3 do |
8628
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8624
diff
changeset
|
560 done(); |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8624
diff
changeset
|
561 assert.equal(r.state, "waiting"); |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8624
diff
changeset
|
562 assert.is_nil(processed_item); |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8624
diff
changeset
|
563 end |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8624
diff
changeset
|
564 done(); |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8624
diff
changeset
|
565 assert.equal(r.state, "ready"); |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8624
diff
changeset
|
566 assert.equal(processed_item, "test"); |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8624
diff
changeset
|
567 assert.spy(r.watchers.error).was_not.called(); |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8624
diff
changeset
|
568 end); |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8624
diff
changeset
|
569 |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8624
diff
changeset
|
570 it("should not allow done() to be called more than specified", function () |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8624
diff
changeset
|
571 local processed_item; |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8624
diff
changeset
|
572 local wait, done; |
8633
8ec18a002c30
util.async: tests: more code re-use
Matthew Wild <mwild1@gmail.com>
parents:
8632
diff
changeset
|
573 local r, rf = new(function (item) |
8628
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8624
diff
changeset
|
574 wait, done = async.waiter(4); |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8624
diff
changeset
|
575 wait(); |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8624
diff
changeset
|
576 processed_item = item; |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8624
diff
changeset
|
577 end); |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8624
diff
changeset
|
578 r:run("test"); |
8632
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
579 for _ = 1, 4 do |
8628
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8624
diff
changeset
|
580 done(); |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8624
diff
changeset
|
581 end |
8632
02b841ed03d1
util.async: tests: luacheck-clean, fixes some actual issues
Matthew Wild <mwild1@gmail.com>
parents:
8631
diff
changeset
|
582 assert.has_error(done); |
8628
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8624
diff
changeset
|
583 assert.equal(r.state, "ready"); |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8624
diff
changeset
|
584 assert.equal(processed_item, "test"); |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8624
diff
changeset
|
585 assert.spy(r.watchers.error).was_not.called(); |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8624
diff
changeset
|
586 end); |
e88744fa0985
util.async: Add some more tests for wait/done
Matthew Wild <mwild1@gmail.com>
parents:
8624
diff
changeset
|
587 |
8624
5325f0e1791b
util.async: tests: Ensure done() can be called before wait()
Matthew Wild <mwild1@gmail.com>
parents:
8623
diff
changeset
|
588 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
|
589 local processed_item; |
8633
8ec18a002c30
util.async: tests: more code re-use
Matthew Wild <mwild1@gmail.com>
parents:
8632
diff
changeset
|
590 local r, rf = new(function (item) |
8624
5325f0e1791b
util.async: tests: Ensure done() can be called before wait()
Matthew Wild <mwild1@gmail.com>
parents:
8623
diff
changeset
|
591 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
|
592 done(); |
5325f0e1791b
util.async: tests: Ensure done() can be called before wait()
Matthew Wild <mwild1@gmail.com>
parents:
8623
diff
changeset
|
593 wait(); |
5325f0e1791b
util.async: tests: Ensure done() can be called before wait()
Matthew Wild <mwild1@gmail.com>
parents:
8623
diff
changeset
|
594 processed_item = item; |
5325f0e1791b
util.async: tests: Ensure done() can be called before wait()
Matthew Wild <mwild1@gmail.com>
parents:
8623
diff
changeset
|
595 end); |
5325f0e1791b
util.async: tests: Ensure done() can be called before wait()
Matthew Wild <mwild1@gmail.com>
parents:
8623
diff
changeset
|
596 r:run("test"); |
5325f0e1791b
util.async: tests: Ensure done() can be called before wait()
Matthew Wild <mwild1@gmail.com>
parents:
8623
diff
changeset
|
597 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
|
598 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
|
599 -- 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
|
600 -- 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
|
601 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
|
602 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
|
603 end); |
8605 | 604 end); |
8648
ca710a71d730
util.async: Add ready() to check whether running in async context
Matthew Wild <mwild1@gmail.com>
parents:
8633
diff
changeset
|
605 |
ca710a71d730
util.async: Add ready() to check whether running in async context
Matthew Wild <mwild1@gmail.com>
parents:
8633
diff
changeset
|
606 describe("#ready()", function () |
ca710a71d730
util.async: Add ready() to check whether running in async context
Matthew Wild <mwild1@gmail.com>
parents:
8633
diff
changeset
|
607 it("should return false outside an async context", function () |
ca710a71d730
util.async: Add ready() to check whether running in async context
Matthew Wild <mwild1@gmail.com>
parents:
8633
diff
changeset
|
608 assert.falsy(async.ready()); |
ca710a71d730
util.async: Add ready() to check whether running in async context
Matthew Wild <mwild1@gmail.com>
parents:
8633
diff
changeset
|
609 end); |
ca710a71d730
util.async: Add ready() to check whether running in async context
Matthew Wild <mwild1@gmail.com>
parents:
8633
diff
changeset
|
610 it("should return true inside an async context", function () |
ca710a71d730
util.async: Add ready() to check whether running in async context
Matthew Wild <mwild1@gmail.com>
parents:
8633
diff
changeset
|
611 local r = new(function () |
ca710a71d730
util.async: Add ready() to check whether running in async context
Matthew Wild <mwild1@gmail.com>
parents:
8633
diff
changeset
|
612 assert.truthy(async.ready()); |
ca710a71d730
util.async: Add ready() to check whether running in async context
Matthew Wild <mwild1@gmail.com>
parents:
8633
diff
changeset
|
613 end); |
ca710a71d730
util.async: Add ready() to check whether running in async context
Matthew Wild <mwild1@gmail.com>
parents:
8633
diff
changeset
|
614 r:run(true); |
ca710a71d730
util.async: Add ready() to check whether running in async context
Matthew Wild <mwild1@gmail.com>
parents:
8633
diff
changeset
|
615 assert.spy(r.func).was.called(); |
ca710a71d730
util.async: Add ready() to check whether running in async context
Matthew Wild <mwild1@gmail.com>
parents:
8633
diff
changeset
|
616 assert.spy(r.watchers.error).was_not.called(); |
ca710a71d730
util.async: Add ready() to check whether running in async context
Matthew Wild <mwild1@gmail.com>
parents:
8633
diff
changeset
|
617 end); |
ca710a71d730
util.async: Add ready() to check whether running in async context
Matthew Wild <mwild1@gmail.com>
parents:
8633
diff
changeset
|
618 end); |
11961
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
619 |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
620 describe("#sleep()", function () |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
621 after_each(function () |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
622 -- Restore to default |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
623 async.set_schedule_function(nil); |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
624 end); |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
625 |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
626 it("should fail if no scheduler configured", function () |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
627 local r = new(function () |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
628 async.sleep(5); |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
629 end); |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
630 r:run(true); |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
631 assert.spy(r.watchers.error).was.called(); |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
632 |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
633 -- Set dummy scheduler |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
634 async.set_schedule_function(function () end); |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
635 |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
636 local r2 = new(function () |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
637 async.sleep(5); |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
638 end); |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
639 r2:run(true); |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
640 assert.spy(r2.watchers.error).was_not.called(); |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
641 end); |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
642 it("should work", function () |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
643 local queue = {}; |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
644 local add_task = spy.new(function (t, f) |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
645 table.insert(queue, { t, f }); |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
646 end); |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
647 async.set_schedule_function(add_task); |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
648 |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
649 local processed_item; |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
650 local r = new(function (item) |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
651 async.sleep(5); |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
652 processed_item = item; |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
653 end); |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
654 r:run("test"); |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
655 |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
656 -- Nothing happened, because the runner is sleeping |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
657 assert.is_nil(processed_item); |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
658 assert.equal(r.state, "waiting"); |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
659 assert.spy(add_task).was_called(1); |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
660 assert.spy(add_task).was_called_with(match.is_number(), match.is_function()); |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
661 assert.spy(r.watchers.waiting).was.called(); |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
662 assert.spy(r.watchers.ready).was_not.called(); |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
663 |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
664 -- Pretend the timer has triggered, call the handler |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
665 queue[1][2](); |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
666 |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
667 assert.equal(processed_item, "test"); |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
668 assert.equal(r.state, "ready"); |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
669 |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
670 assert.spy(r.watchers.ready).was.called(); |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
671 end); |
542a9a503073
util.async: Add sleep() method with configurable scheduling backend
Matthew Wild <mwild1@gmail.com>
parents:
10541
diff
changeset
|
672 end); |
11962
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
673 |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
674 describe("#set_nexttick()", function () |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
675 after_each(function () |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
676 -- Restore to default |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
677 async.set_nexttick(nil); |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
678 end); |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
679 it("should work", function () |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
680 local queue = {}; |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
681 local nexttick = spy.new(function (f) |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
682 assert.is_function(f); |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
683 table.insert(queue, f); |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
684 end); |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
685 async.set_nexttick(nexttick); |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
686 |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
687 local processed_item; |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
688 local wait, done; |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
689 local r = new(function (item) |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
690 wait, done = async.waiter(); |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
691 wait(); |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
692 processed_item = item; |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
693 end); |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
694 r:run("test"); |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
695 |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
696 -- Nothing happened, because the runner is waiting |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
697 assert.is_nil(processed_item); |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
698 assert.equal(r.state, "waiting"); |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
699 assert.spy(nexttick).was_called(0); |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
700 assert.spy(r.watchers.waiting).was.called(); |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
701 assert.spy(r.watchers.ready).was_not.called(); |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
702 |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
703 -- Mark the runner as ready, it should be scheduled for |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
704 -- the next tick |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
705 done(); |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
706 |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
707 assert.spy(nexttick).was_called(1); |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
708 assert.spy(nexttick).was_called_with(match.is_function()); |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
709 assert.equal(1, #queue); |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
710 |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
711 -- Pretend it's the next tick - call the pending function |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
712 queue[1](); |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
713 |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
714 assert.equal(processed_item, "test"); |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
715 assert.equal(r.state, "ready"); |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
716 assert.spy(r.watchers.ready).was.called(); |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
717 end); |
9a70a543c727
util.async: Add next-tick configuration
Matthew Wild <mwild1@gmail.com>
parents:
11961
diff
changeset
|
718 end); |
8605 | 719 end); |