1 |
|
2 |
|
3 |
|
4 |
|
5 | var mod_tap = require('tap');
|
6 | var mod_vasync = require('..');
|
7 |
|
8 | var count = 0;
|
9 | var st;
|
10 |
|
11 | mod_tap.test('empty waterfall', function (test) {
|
12 | st = mod_vasync.waterfall([], function (err) {
|
13 | test.ok(err === null);
|
14 | test.ok(st.ndone === 0);
|
15 | test.ok(st.nerrors === 0);
|
16 | test.ok(st.operations.length === 0);
|
17 | test.ok(st.successes.length === 0);
|
18 | test.equal(count, 1);
|
19 | test.end();
|
20 | });
|
21 | count++;
|
22 | test.ok(st.ndone === 0);
|
23 | test.ok(st.nerrors === 0);
|
24 | test.ok(st.operations.length === 0);
|
25 | test.ok(st.successes.length === 0);
|
26 | });
|
27 |
|
28 | mod_tap.test('normal 4-stage waterfall', function (test) {
|
29 | count = 0;
|
30 | st = mod_vasync.waterfall([
|
31 | function func1(cb) {
|
32 | test.ok(count === 0, 'func1: count === 0');
|
33 | test.ok(st.ndone === 0);
|
34 | count++;
|
35 | setTimeout(cb, 20, null, { 'hello': 'world' });
|
36 | },
|
37 | function func2(extra, cb) {
|
38 | test.equal(extra.hello, 'world', 'func2: extra arg');
|
39 | test.ok(count == 1, 'func2: count == 1');
|
40 | test.ok(st.ndone === 1);
|
41 | test.ok(st.operations[0].status == 'ok');
|
42 | test.ok(st.operations[1].status == 'pending');
|
43 | test.ok(st.operations[2].status == 'waiting');
|
44 | count++;
|
45 | setTimeout(cb, 20, null, 5, 6, 7);
|
46 | },
|
47 | function (five, six, seven, cb) {
|
48 | test.equal(five, 5, 'func3: extra arg');
|
49 | test.equal(six, 6, 'func3: extra arg');
|
50 | test.equal(seven, 7, 'func3: extra arg');
|
51 | test.ok(count == 2, 'func3: count == 2');
|
52 | test.ok(st.ndone === 2);
|
53 | count++;
|
54 | setTimeout(cb, 20);
|
55 | },
|
56 | function func4(cb) {
|
57 | test.ok(count == 3, 'func4: count == 2');
|
58 | test.ok(st.ndone === 3);
|
59 | count++;
|
60 | setTimeout(cb, 20, null, 8, 9);
|
61 | }
|
62 | ], function (err, eight, nine) {
|
63 | test.ok(count == 4, 'final: count == 4');
|
64 | test.ok(err === null, 'no error');
|
65 | test.ok(eight == 8);
|
66 | test.ok(nine == 9);
|
67 | test.ok(st.ndone === 4);
|
68 | test.ok(st.nerrors === 0);
|
69 | test.ok(st.operations.length === 4);
|
70 | test.ok(st.successes.length === 4);
|
71 | test.ok(st.operations[0].status == 'ok');
|
72 | test.ok(st.operations[1].status == 'ok');
|
73 | test.ok(st.operations[2].status == 'ok');
|
74 | test.ok(st.operations[3].status == 'ok');
|
75 | test.end();
|
76 | });
|
77 | test.ok(st.ndone === 0);
|
78 | test.ok(st.nerrors === 0);
|
79 | test.ok(st.operations.length === 4);
|
80 | test.ok(st.operations[0].funcname == 'func1', 'func1 name');
|
81 | test.ok(st.operations[0].status == 'pending');
|
82 | test.ok(st.operations[1].funcname == 'func2', 'func2 name');
|
83 | test.ok(st.operations[1].status == 'waiting');
|
84 | test.ok(st.operations[2].funcname == '(anon)', 'anon name');
|
85 | test.ok(st.operations[2].status == 'waiting');
|
86 | test.ok(st.operations[3].funcname == 'func4', 'func4 name');
|
87 | test.ok(st.operations[3].status == 'waiting');
|
88 | test.ok(st.successes.length === 0);
|
89 | });
|
90 |
|
91 | mod_tap.test('bailing out early', function (test) {
|
92 | count = 0;
|
93 | st = mod_vasync.waterfall([
|
94 | function func1(cb) {
|
95 | test.ok(count === 0, 'func1: count === 0');
|
96 | count++;
|
97 | setTimeout(cb, 20);
|
98 | },
|
99 | function func2(cb) {
|
100 | test.ok(count == 1, 'func2: count == 1');
|
101 | count++;
|
102 | setTimeout(cb, 20, new Error('boom!'));
|
103 | },
|
104 | function func3(cb) {
|
105 | test.ok(count == 2, 'func3: count == 2');
|
106 | count++;
|
107 | setTimeout(cb, 20);
|
108 | }
|
109 | ], function (err) {
|
110 | test.ok(count == 2, 'final: count == 3');
|
111 | test.equal(err.message, 'boom!');
|
112 | test.ok(st.ndone == 2);
|
113 | test.ok(st.nerrors == 1);
|
114 | test.ok(st.operations[0].status == 'ok');
|
115 | test.ok(st.operations[1].status == 'fail');
|
116 | test.ok(st.operations[2].status == 'waiting');
|
117 | test.ok(st.successes.length == 1);
|
118 | test.end();
|
119 | });
|
120 | });
|
121 |
|
122 | mod_tap.test('bad function', function (test) {
|
123 | count = 0;
|
124 | st = mod_vasync.waterfall([
|
125 | function func1(cb) {
|
126 | count++;
|
127 | cb();
|
128 | setTimeout(function () {
|
129 | test.throws(
|
130 | function () { cb(); process.abort(); },
|
131 | 'vasync.waterfall: ' +
|
132 | 'function 0 ("func1") invoked its ' +
|
133 | 'callback twice');
|
134 | test.equal(count, 2);
|
135 | test.end();
|
136 | }, 100);
|
137 | },
|
138 | function func2(cb) {
|
139 | count++;
|
140 |
|
141 | }
|
142 | ], function (err) {
|
143 |
|
144 | console.error('didn\'t expect to finish');
|
145 | process.abort();
|
146 | });
|
147 | });
|
148 |
|
149 | mod_tap.test('badargs', function (test) {
|
150 | test.throws(function () { mod_vasync.waterfall(); });
|
151 | test.throws(function () { mod_vasync.waterfall([], 'foo'); });
|
152 | test.throws(function () { mod_vasync.waterfall('foo', 'bar'); });
|
153 | test.end();
|
154 | });
|
155 |
|
156 | mod_tap.test('normal waterfall, no callback', function (test) {
|
157 | count = 0;
|
158 | st = mod_vasync.waterfall([
|
159 | function func1(cb) {
|
160 | test.ok(count === 0);
|
161 | count++;
|
162 | setImmediate(cb);
|
163 | },
|
164 | function func2(cb) {
|
165 | test.ok(count == 1);
|
166 | count++;
|
167 | setImmediate(cb);
|
168 | setTimeout(function () {
|
169 | test.ok(count == 2);
|
170 | test.end();
|
171 | }, 100);
|
172 | }
|
173 | ]);
|
174 | });
|
175 |
|
176 | mod_tap.test('empty waterfall, no callback', function (test) {
|
177 | st = mod_vasync.waterfall([]);
|
178 | setTimeout(function () { test.end(); }, 100);
|
179 | });
|