Software /
code /
prosody
Annotate
tests/test_util_async.lua @ 7822:4caaa0c185f1
Merge 0.10->trunk
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Fri, 13 Jan 2017 18:30:18 +0100 |
parent | 7437:ff74188488ab |
child | 8209:39f24de4f53f |
rev | line source |
---|---|
7437
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 -- Test passing nil to runner |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 -- Test runners work correctly after errors (coroutine gets recreated) |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 -- What happens if an error is thrown, but more items are in the queue? (I think runner might stall) |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 -- Test errors thrown halfway through a queue |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 -- Multiple runners |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 function runner(new_runner, async) |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 local function new(func) |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 local log = {}; |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 return new_runner(func, setmetatable({}, { |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 __index = function (_, event) |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 return function (runner, err) |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 print("event", event, err) |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 table.insert(log, { event = event, err = err }); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 end; |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 end; |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 })), log; |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 end |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 -------------------- |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 local r, l = new(function (item) assert(type(item) == "number") end); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 r:run(1); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 r:run(2); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 for k, v in ipairs(l) do print(k,v) end |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 -------------------- |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 local wait, done; |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 local r, l = new(function (item) |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 assert(type(item) == "number") |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 if item == 3 then |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 wait, done = async.waiter(); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 wait(); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 end |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 end); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 r:run(1); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 assert(r.state == "ready"); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 r:run(2); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 assert(r.state == "ready"); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 r:run(3); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 assert(r.state == "waiting"); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 done(); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 assert(r.state == "ready"); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 for k, v in ipairs(l) do print(k,v) end |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 -------------------- |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 local wait, done; |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 local last_item = 0; |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 local r, l = new(function (item) |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 assert(type(item) == "number") |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 assert(item == last_item + 1); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 last_item = item; |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 if item == 3 then |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 wait, done = async.waiter(); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 wait(); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 end |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 end); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 r:run(1); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 assert(r.state == "ready"); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 r:run(2); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 assert(r.state == "ready"); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 r:run(3); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 assert(r.state == "waiting"); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 r:run(4); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 assert(r.state == "waiting"); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 done(); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 assert(r.state == "ready"); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 for k, v in ipairs(l) do print(k,v) end |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 -------------------- |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 local wait, done; |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 local last_item = 0; |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 local r, l = new(function (item) |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 assert(type(item) == "number") |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 assert((item == last_item + 1) or item == 3); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 last_item = item; |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 if item == 3 then |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 wait, done = async.waiter(); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 wait(); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 end |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 end); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 r:run(1); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 assert(r.state == "ready"); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 r:run(2); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 assert(r.state == "ready"); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 local dones = {}; |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 r:run(3); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 assert(r.state == "waiting"); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
94 r:run(3); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 assert(r.state == "waiting"); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
96 r:run(3); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
97 assert(r.state == "waiting"); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 r:run(4); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 assert(r.state == "waiting"); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
100 |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
101 for i = 1, 3 do |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 done(); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
103 if i < 3 then |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 assert(r.state == "waiting"); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 end |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 end |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
107 |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
108 assert(r.state == "ready"); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
109 for k, v in ipairs(l) do print(k,v) end |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
111 -------------------- |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
112 local wait, done; |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 local last_item = 0; |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
114 local r, l = new(function (item) |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 assert(type(item) == "number") |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
116 assert((item == last_item + 1) or item == 3); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
117 last_item = item; |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
118 if item == 3 then |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
119 wait, done = async.waiter(); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
120 wait(); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
121 end |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 end); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
123 |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
124 r:run(1); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
125 assert(r.state == "ready"); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
126 r:run(2); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
127 assert(r.state == "ready"); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
128 |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
129 local dones = {}; |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
130 r:run(3); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
131 assert(r.state == "waiting"); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
132 r:run(3); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
133 assert(r.state == "waiting"); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
134 |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
135 for i = 1, 2 do |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
136 done(); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 if i < 2 then |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
138 assert(r.state == "waiting"); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
139 end |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
140 end |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
141 |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
142 assert(r.state == "ready"); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
143 r:run(4); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
144 assert(r.state == "ready"); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
145 |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
146 assert(r.state == "ready"); |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
147 for k, v in ipairs(l) do print(k,v) end |
ff74188488ab
tests: Add initial tests for util.async
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
148 end |