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