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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/* vim: set ts=8 sts=8 sw=8 noet: */
 
var mod_tap = require('tap');
var mod_vasync = require('..');
 
function
immediate_worker(task, next)
{
    setImmediate(function () {
        next();
    });
}
 
function
sametick_worker(task, next)
{
    next();
}
 
function
random_delay_worker(task, next)
{
    setTimeout(function () {
        next();
    }, Math.floor(Math.random() * 250));
}
 
mod_tap.test('must not push after close', function (test) {
    test.plan(3);
 
    var q = mod_vasync.queuev({
        worker: immediate_worker,
        concurrency: 10
    });
    test.ok(q);
 
    test.doesNotThrow(function () {
        q.push({});
    }, 'push should not throw _before_ close()');
 
    q.close();
 
    /*
     * If we attempt to add tasks to the queue _after_ calling close(),
     * we should get an exception:
     */
    test.throws(function () {
        q.push({});
    }, 'push should throw _after_ close()');
 
    test.end();
});
 
mod_tap.test('get \'end\' event with close()', function (test) {
    var task_count = 45;
    var tasks_finished = 0;
    var seen_end = false;
    var seen_drain = false;
 
    test.plan(14 + task_count);
 
    var q = mod_vasync.queuev({
        worker: random_delay_worker,
        concurrency: 5
    });
    test.ok(q);
 
    /*
     * Enqueue a bunch of tasks; more than our concurrency:
     */
    for (var i = 0; i < 45; i++) {
        q.push({}, function () {
            tasks_finished++;
            test.ok(true);
        });
    }
 
    /*
     * Close the queue to signify that we're done now.
     */
    test.equal(q.ended, false);
    test.equal(q.closed, false);
    q.close();
    test.equal(q.closed, true);
    test.equal(q.ended, false);
 
    q.on('drain', function () {
        /*
         * 'drain' should fire before 'end':
         */
        test.notOk(seen_drain);
        test.notOk(seen_end);
        seen_drain = true;
    });
    q.on('end', function () {
        /*
         * 'end' should fire after 'drain':
         */
        test.ok(seen_drain);
        test.notOk(seen_end);
        seen_end = true;
 
        /*
         * Check the public state:
         */
        test.equal(q.closed, true);
        test.equal(q.ended, true);
 
        /*
         * We should have fired the callbacks for _all_ enqueued
         * tasks by now:
         */
        test.equal(task_count, tasks_finished);
        test.end();
    });
 
    /*
     * Check that we see neither the 'drain', nor the 'end' event before
     * the end of this tick:
     */
    test.notOk(seen_drain);
    test.notOk(seen_end);
});
 
mod_tap.test('get \'end\' event with close() and no tasks', function (test) {
    var seen_drain = false;
    var seen_end = false;
 
    test.plan(10);
 
    var q = mod_vasync.queuev({
        worker: immediate_worker,
        concurrency: 10
    });
 
    setImmediate(function () {
        test.notOk(seen_end);
    });
 
    test.equal(q.ended, false);
    test.equal(q.closed, false);
    q.close();
    test.equal(q.closed, true);
    test.equal(q.ended, false);
    test.notOk(seen_end);
 
    q.on('drain', function () {
        seen_drain = true;
    });
    q.on('end', function () {
        /*
         * We do not expect to see a 'drain' event, as there were no
         * tasks pushed onto the queue before we closed it.
         */
        test.notOk(seen_drain);
        test.notOk(seen_end);
        test.equal(q.closed, true);
        test.equal(q.ended, true);
        seen_end = true;
        test.end();
    });
});
 
/*
 * We want to ensure that both the 'drain' event and the q.drain() hook are
 * called the same number of times:
 */
mod_tap.test('equivalence of on(\'drain\') and q.drain()', function (test) {
    var enqcount = 4;
    var drains = 4;
    var ee_count = 0;
    var fn_count = 0;
 
    test.plan(enqcount + drains + 3);
 
    var q = mod_vasync.queuev({
        worker: immediate_worker,
        concurrency: 10
    });
 
    var enq = function () {
        if (--enqcount < 0)
            return;
 
        q.push({}, function () {
            test.ok(true, 'task completion');
        });
    };
 
    var draino = function () {
        test.ok(true, 'drain called');
        if (--drains === 0) {
            test.equal(q.closed, false, 'not closed');
            test.equal(q.ended, false, 'not ended');
            test.equal(fn_count, ee_count, 'same number of calls');
            test.end();
        }
    };
 
    enq();
    enq();
 
    q.on('drain', function () {
        ee_count++;
        enq();
        draino();
    });
    q.drain = function () {
        fn_count++;
        enq();
        draino();
    };
});
 
/*
 * In the past, we've only handed on the _first_ argument to the task completion
 * callback.  Make sure we hand on _all_ of the arguments now:
 */
mod_tap.test('ensure all arguments passed to push() callback', function (test) {
    test.plan(13);
 
    var q = mod_vasync.queuev({
        worker: function (task, callback) {
            if (task.fail) {
                callback(new Error('guru meditation'));
                return;
            }
            callback(null, 1, 2, 3, 5, 8);
        },
        concurrency: 1
    });
 
    q.push({ fail: true }, function (err, a, b, c, d, e) {
        test.ok(err, 'got the error');
        test.equal(err.message, 'guru meditation');
        test.type(a, 'undefined');
        test.type(b, 'undefined');
        test.type(c, 'undefined');
        test.type(d, 'undefined');
        test.type(e, 'undefined');
    });
 
    q.push({ fail: false }, function (err, a, b, c, d, e) {
        test.notOk(err, 'got no error');
        test.equal(a, 1);
        test.equal(b, 2);
        test.equal(c, 3);
        test.equal(d, 5);
        test.equal(e, 8);
    });
 
    q.drain = function () {
        test.end();
    };
});
 
mod_tap.test('queue kill', function (test) {
    // Derived from async queue.kill test
    var count = 0;
    var q = mod_vasync.queuev({
        worker: function (task, callback) {
            setImmediate(function () {
                test.ok(++count < 2,
                    'Function should be called once');
                callback();
            });
        },
        concurrency: 1
    });
    q.drain = function () {
        test.ok(false, 'Function should never be called');
    };
 
    // Queue twice, the first will exec immediately
    q.push(0);
    q.push(0);
 
    q.kill();
 
    q.on('end', function () {
        test.ok(q.killed);
        test.end();
    });
});