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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
 * Tests the "filter", "filterSeries", and "filterLimit" functions
 */
 
var mod_util = require('util');
 
var mod_tap = require('tap');
var mod_vasync = require('..');
 
mod_tap.test('filterSeries', function (test) {
    var inputs = [0, 1, 2, 3, 4, 5];
    var curTasks = 0;
    var maxTasks = 0;
    // filterSeries has an implicit limit of 1 concurrent operation
    var limit = 1;
 
    function filterFunc(input, cb) {
        curTasks++;
        if (curTasks > maxTasks) {
            maxTasks = curTasks;
        }
        test.ok(curTasks <= limit, mod_util.format(
            'input %d: current tasks %d <= %d',
            input, curTasks, limit));
 
        setTimeout(function () {
            curTasks--;
            cb(null, input < 2 || input === 4);
        }, 50);
    }
 
    mod_vasync.filterSeries(inputs, filterFunc,
        function filterDone(err, results) {
 
        test.ok(!err, 'error unset');
        test.equal(maxTasks, limit, 'max tasks reached limit');
        test.deepEqual(results, [0, 1, 4], 'results array correct');
        test.end();
    });
});
 
mod_tap.test('filterLimit', function (test) {
    var inputs = [0, 1, 2, 3, 4, 5];
    var curTasks = 0;
    var maxTasks = 0;
    var limit = 2;
 
    function filterFunc(input, cb) {
        curTasks++;
        if (curTasks > maxTasks) {
            maxTasks = curTasks;
        }
        test.ok(curTasks <= limit, mod_util.format(
            'input %d: current tasks %d <= %d',
            input, curTasks, limit));
 
        setTimeout(function () {
            curTasks--;
            cb(null, input < 2 || input === 4);
        }, 50);
    }
 
    mod_vasync.filterLimit(inputs, limit, filterFunc,
        function filterDone(err, results) {
 
        test.ok(!err, 'error unset');
        test.equal(maxTasks, limit, 'max tasks reached limit');
        test.deepEqual(results, [0, 1, 4], 'results array correct');
        test.end();
    });
});
 
mod_tap.test('filter (maintain order)', function (test) {
    var inputs = [0, 1, 2, 3, 4, 5];
    var limit = inputs.length;
    var storedValues = [];
 
    function filterFunc(input, cb) {
        /*
         * Hold every callback in an array to be called when all
         * filterFunc's have run.  This way, we can ensure that all
         * tasks have started without waiting for any others to finish.
         */
        storedValues.push({
            input: input,
            cb: cb
        });
 
        test.ok(storedValues.length <= limit, mod_util.format(
            'input %d: current tasks %d <= %d',
            input, storedValues.length, limit));
 
        /*
         * When this constraint is true, all filterFunc's have run for
         * each input.  We now call all callbacks in a pre-determined
         * order (out of order of the original) to ensure the final
         * array is in the correct order.
         */
        if (storedValues.length === inputs.length) {
            [5, 2, 0, 1, 4, 3].forEach(function (i) {
                var o = storedValues[i];
                o.cb(null, o.input < 2 || o.input === 4);
            });
        }
    }
 
    mod_vasync.filter(inputs, filterFunc,
        function filterDone(err, results) {
 
        test.ok(!err, 'error unset');
        test.equal(storedValues.length, inputs.length,
            'max tasks reached limit');
        test.deepEqual(results, [0, 1, 4], 'results array correct');
        test.end();
    });
});
 
mod_tap.test('filterSeries error handling', function (test) {
    /*
     * We will error half way through the list of inputs to ensure that
     * first half are processed while the second half are ignored.
     */
    var inputs = [0, 1, 2, 3, 4, 5];
 
    function filterFunc(input, cb) {
        switch (input) {
        case 0:
        case 1:
        case 2:
            cb(null, true);
            break;
        case 3:
            cb(new Error('error on ' + input));
            break;
        case 4:
        case 5:
            test.ok(false, 'processed too many inputs');
            cb(new Error('processed too many inputs'));
            break;
        default:
            test.ok(false, 'unexpected input: ' + input);
            cb(new Error('unexpected input'));
            break;
        }
    }
 
    mod_vasync.filterSeries(inputs, filterFunc,
        function filterDone(err, results) {
 
        test.ok(err, 'error set');
        test.ok(err.message === 'error on 3', 'error on input 3');
        test.ok(results === undefined, 'results is unset');
        test.end();
    });
});
 
mod_tap.test('filterSeries double callback', function (test) {
    var inputs = [0, 1, 2, 3, 4, 5];
 
    function filterFunc(input, cb) {
        switch (input) {
        case 0:
        case 1:
        case 2:
            cb(null, true);
            break;
        case 3:
            /*
             * The first call to cb() should "win" - meaning this
             * value will be filtered out of the final array of
             * results.
             */
            cb(null, false);
            test.throws(function () {
                cb(null, true);
            });
            break;
        case 4:
            /*
             * Like input 3, all subsequent calls to cb() will
             * throw an error and not affect the original call to
             * cb().
             */
            cb(null, true);
            test.throws(function () {
                cb(new Error('uh oh'));
            });
            break;
        case 5:
            cb(null, true);
            break;
        default:
            test.ok(false, 'unexpected input: ' + input);
            cb(new Error('unexpected input'));
            break;
        }
    }
 
    mod_vasync.filterSeries(inputs, filterFunc,
        function filterDone(err, results) {
 
        test.ok(!err, 'error not set');
        test.deepEqual(results, [0, 1, 2, 4, 5],
            'results array correct');
        test.end();
    });
});
 
mod_tap.test('filter push to queue object error', function (test) {
    var inputs = [0, 1, 2, 3, 4, 5];
 
    function filterFunc(input, cb) {
        cb(null, true);
    }
 
    var q = mod_vasync.filterSeries(inputs, filterFunc,
        function filterDone(err, results) {
 
        test.end();
    });
 
    test.equal(q.closed, true, 'queue is closed');
    test.throws(function () {
        q.push(6);
    });
});