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