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