Annotate

spec/util_async_spec.lua @ 8605:3a1efc0532cb

util.async: Add tests
author Matthew Wild <mwild1@gmail.com>
date Fri, 16 Mar 2018 17:13:57 +0000
child 8607:62bb06cf8a43
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8605
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local async = require "util.async";
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 describe("util.async", function()
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local debug = false;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local print = print;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 if debug then
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 require "util.logger".add_simple_sink(print);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 else
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 print = function () end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local function new(func, name)
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local log = {};
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 return async.runner(func, setmetatable({}, {
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 __index = function (_, event)
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 return function (runner, err)
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 print(name, "event", event, err)
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 print "--"
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 table.insert(log, { event = event, err = err });
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 end;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 end;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 })), log;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 describe("#runner", function()
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 it("should work", function()
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 local r, l = new(function (item) assert(type(item) == "number") end);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 r:run(1);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 r:run(2);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 end);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 end);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 describe("#waiter", function()
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 it("should work", function ()
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 local wait, done;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 local r, l = new(function (item)
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 assert(type(item) == "number")
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 if item == 3 then
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 wait, done = async.waiter();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 wait();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 end);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 r:run(1);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 assert(r.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 r:run(2);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 assert(r.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 r:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 assert(r.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 done();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 assert(r.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 --for k, v in ipairs(l) do print(k,v) end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 end);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 it("should work", function ()
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 --------------------
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 local wait, done;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 local last_item = 0;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 local r, l = new(function (item)
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 assert(type(item) == "number")
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 assert(item == last_item + 1);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 last_item = item;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 if item == 3 then
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 wait, done = async.waiter();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 wait();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 end);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 r:run(1);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 assert(r.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 r:run(2);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 assert(r.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 r:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 assert(r.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 r:run(4);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 assert(r.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 done();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 assert(r.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 --for k, v in ipairs(l) do print(k,v) end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 end);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 it("should work", function ()
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 --------------------
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 local wait, done;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 local last_item = 0;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 local r, l = new(function (item)
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 assert(type(item) == "number")
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 assert((item == last_item + 1) or item == 3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 last_item = item;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 if item == 3 then
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 wait, done = async.waiter();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 wait();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 end);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 r:run(1);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 assert(r.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 r:run(2);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 assert(r.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 local dones = {};
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 r:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 assert(r.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 r:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 assert(r.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 r:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 assert(r.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 r:run(4);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 assert(r.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 for i = 1, 3 do
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 done();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 if i < 3 then
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 assert(r.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 assert(r.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 --for k, v in ipairs(l) do print(k,v) end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 end);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 it("should work", function ()
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 --------------------
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 local wait, done;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 local last_item = 0;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 local r, l = new(function (item)
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 assert(type(item) == "number")
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 assert((item == last_item + 1) or item == 3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 last_item = item;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 if item == 3 then
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 wait, done = async.waiter();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 wait();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 end);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 r:run(1);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 assert(r.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 r:run(2);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 assert(r.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 local dones = {};
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 r:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 assert(r.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 r:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 assert(r.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 for i = 1, 2 do
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 done();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 if i < 2 then
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 assert(r.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 assert(r.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 r:run(4);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 assert(r.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154 assert(r.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 --for k, v in ipairs(l) do print(k,v) end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 end);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 it("should work with multiple runners in parallel", function ()
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 -- Now with multiple runners
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 --------------------
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 local wait1, done1;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 local last_item1 = 0;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 local r1, l1 = new(function (item)
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 assert(type(item) == "number")
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 assert((item == last_item1 + 1) or item == 3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 last_item1 = item;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 if item == 3 then
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 wait1, done1 = async.waiter();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 wait1();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 end, "r1");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172 local wait2, done2;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 local last_item2 = 0;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174 local r2, l2 = new(function (item)
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175 assert(type(item) == "number")
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 assert((item == last_item2 + 1) or item == 3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177 last_item2 = item;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178 if item == 3 then
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 wait2, done2 = async.waiter();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180 wait2();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182 end, "r2");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184 r1:run(1);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185 assert(r1.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186 r1:run(2);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187 assert(r1.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189 local dones = {};
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
190 r1:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191 assert(r1.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192 r1:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
193 assert(r1.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
195 r2:run(1);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196 assert(r1.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197 assert(r2.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
198
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
199 r2:run(2);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
200 assert(r1.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201 assert(r2.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203 r2:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204 assert(r1.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205 assert(r2.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
206 done2();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
207
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
208 r2:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
209 assert(r1.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
210 assert(r2.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
211 done2();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
212
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
213 r2:run(4);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
214 assert(r1.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
215 assert(r2.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
216
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
217 for i = 1, 2 do
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
218 done1();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
219 if i < 2 then
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
220 assert(r1.state == "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
221 end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
222 end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
223
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
224 assert(r1.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
225 r1:run(4);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
226 assert(r1.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
227
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
228 assert(r1.state == "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
229 --for k, v in ipairs(l1) do print(k,v) end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
230 end);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
231 it("should work work with multiple runners in parallel", function ()
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
232 --------------------
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
233 local wait1, done1;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
234 local last_item1 = 0;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
235 local r1, l1 = new(function (item)
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
236 print("r1 processing ", item);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
237 assert(type(item) == "number")
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
238 assert((item == last_item1 + 1) or item == 3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
239 last_item1 = item;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
240 if item == 3 then
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
241 wait1, done1 = async.waiter();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
242 wait1();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
243 end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
244 end, "r1");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
245
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
246 local wait2, done2;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
247 local last_item2 = 0;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
248 local r2, l2 = new(function (item)
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
249 print("r2 processing ", item);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
250 assert.is_number(item);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
251 assert((item == last_item2 + 1) or item == 3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
252 last_item2 = item;
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
253 if item == 3 then
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
254 wait2, done2 = async.waiter();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
255 wait2();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
256 end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
257 end, "r2");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
258
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
259 r1:run(1);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
260 assert.equal(r1.state, "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
261 r1:run(2);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
262 assert.equal(r1.state, "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
263
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
264 r1:run(5);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
265 assert.equal(r1.state, "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
266
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
267 local dones = {};
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
268 r1:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
269 assert.equal(r1.state, "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
270 r1:run(5); -- Will error, when we get to it
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
271 assert.equal(r1.state, "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
272 done1();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
273 assert.equal(r1.state, "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
274 r1:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
275 assert.equal(r1.state, "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
276
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
277 r2:run(1);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
278 assert.equal(r1.state, "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
279 assert.equal(r2.state, "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
280
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
281 r2:run(2);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
282 assert.equal(r1.state, "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
283 assert.equal(r2.state, "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
284
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
285 r2:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
286 assert.equal(r1.state, "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
287 assert.equal(r2.state, "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
288
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
289 done2();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
290 assert.equal(r1.state, "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
291 assert.equal(r2.state, "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
292
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
293 r2:run(3);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
294 assert.equal(r1.state, "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
295 assert.equal(r2.state, "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
296
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
297 done2();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
298 assert.equal(r1.state, "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
299 assert.equal(r2.state, "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
300
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
301 r2:run(4);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
302 assert.equal(r1.state, "waiting");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
303 assert.equal(r2.state, "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
304
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
305 done1();
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
306
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
307 assert.equal(r1.state, "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
308 r1:run(4);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
309 assert.equal(r1.state, "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
310
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
311 assert.equal(r1.state, "ready");
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
312 --for k, v in ipairs(l1) do print(k,v) end
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
313 end);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
314 end);
3a1efc0532cb util.async: Add tests
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
315 end);