source: trip-planner-front/node_modules/fastq/test/test.js@ ceaed42

Last change on this file since ceaed42 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 11.4 KB
Line 
1'use strict'
2
3/* eslint-disable no-var */
4
5var test = require('tape')
6var buildQueue = require('../')
7
8test('concurrency', function (t) {
9 t.plan(2)
10 t.throws(buildQueue.bind(null, worker, 0))
11 t.doesNotThrow(buildQueue.bind(null, worker, 1))
12
13 function worker (arg, cb) {
14 cb(null, true)
15 }
16})
17
18test('worker execution', function (t) {
19 t.plan(3)
20
21 var queue = buildQueue(worker, 1)
22
23 queue.push(42, function (err, result) {
24 t.error(err, 'no error')
25 t.equal(result, true, 'result matches')
26 })
27
28 function worker (arg, cb) {
29 t.equal(arg, 42)
30 cb(null, true)
31 }
32})
33
34test('limit', function (t) {
35 t.plan(4)
36
37 var expected = [10, 0]
38 var queue = buildQueue(worker, 1)
39
40 queue.push(10, result)
41 queue.push(0, result)
42
43 function result (err, arg) {
44 t.error(err, 'no error')
45 t.equal(arg, expected.shift(), 'the result matches')
46 }
47
48 function worker (arg, cb) {
49 setTimeout(cb, arg, null, arg)
50 }
51})
52
53test('multiple executions', function (t) {
54 t.plan(15)
55
56 var queue = buildQueue(worker, 1)
57 var toExec = [1, 2, 3, 4, 5]
58 var count = 0
59
60 toExec.forEach(function (task) {
61 queue.push(task, done)
62 })
63
64 function done (err, result) {
65 t.error(err, 'no error')
66 t.equal(result, toExec[count - 1], 'the result matches')
67 }
68
69 function worker (arg, cb) {
70 t.equal(arg, toExec[count], 'arg matches')
71 count++
72 setImmediate(cb, null, arg)
73 }
74})
75
76test('multiple executions, one after another', function (t) {
77 t.plan(15)
78
79 var queue = buildQueue(worker, 1)
80 var toExec = [1, 2, 3, 4, 5]
81 var count = 0
82
83 queue.push(toExec[0], done)
84
85 function done (err, result) {
86 t.error(err, 'no error')
87 t.equal(result, toExec[count - 1], 'the result matches')
88 if (count < toExec.length) {
89 queue.push(toExec[count], done)
90 }
91 }
92
93 function worker (arg, cb) {
94 t.equal(arg, toExec[count], 'arg matches')
95 count++
96 setImmediate(cb, null, arg)
97 }
98})
99
100test('set this', function (t) {
101 t.plan(3)
102
103 var that = {}
104 var queue = buildQueue(that, worker, 1)
105
106 queue.push(42, function (err, result) {
107 t.error(err, 'no error')
108 t.equal(this, that, 'this matches')
109 })
110
111 function worker (arg, cb) {
112 t.equal(this, that, 'this matches')
113 cb(null, true)
114 }
115})
116
117test('drain', function (t) {
118 t.plan(4)
119
120 var queue = buildQueue(worker, 1)
121 var worked = false
122
123 queue.push(42, function (err, result) {
124 t.error(err, 'no error')
125 t.equal(result, true, 'result matches')
126 })
127
128 queue.drain = function () {
129 t.equal(true, worked, 'drained')
130 }
131
132 function worker (arg, cb) {
133 t.equal(arg, 42)
134 worked = true
135 setImmediate(cb, null, true)
136 }
137})
138
139test('pause && resume', function (t) {
140 t.plan(7)
141
142 var queue = buildQueue(worker, 1)
143 var worked = false
144
145 t.notOk(queue.paused, 'it should not be paused')
146
147 queue.pause()
148
149 queue.push(42, function (err, result) {
150 t.error(err, 'no error')
151 t.equal(result, true, 'result matches')
152 })
153
154 t.notOk(worked, 'it should be paused')
155 t.ok(queue.paused, 'it should be paused')
156
157 queue.resume()
158 queue.resume() // second resume is a no-op
159
160 t.notOk(queue.paused, 'it should not be paused')
161
162 function worker (arg, cb) {
163 t.equal(arg, 42)
164 worked = true
165 cb(null, true)
166 }
167})
168
169test('pause in flight && resume', function (t) {
170 t.plan(9)
171
172 var queue = buildQueue(worker, 1)
173 var expected = [42, 24]
174
175 t.notOk(queue.paused, 'it should not be paused')
176
177 queue.push(42, function (err, result) {
178 t.error(err, 'no error')
179 t.equal(result, true, 'result matches')
180 t.ok(queue.paused, 'it should be paused')
181 process.nextTick(function () { queue.resume() })
182 })
183
184 queue.push(24, function (err, result) {
185 t.error(err, 'no error')
186 t.equal(result, true, 'result matches')
187 t.notOk(queue.paused, 'it should not be paused')
188 })
189
190 queue.pause()
191
192 function worker (arg, cb) {
193 t.equal(arg, expected.shift())
194 process.nextTick(function () { cb(null, true) })
195 }
196})
197
198test('altering concurrency', function (t) {
199 t.plan(7)
200
201 var queue = buildQueue(worker, 1)
202 var count = 0
203
204 queue.pause()
205
206 queue.push(24, workDone)
207 queue.push(24, workDone)
208
209 queue.concurrency = 2
210
211 queue.resume()
212
213 t.equal(queue.running(), 2, '2 jobs running')
214
215 function workDone (err, result) {
216 t.error(err, 'no error')
217 t.equal(result, true, 'result matches')
218 }
219
220 function worker (arg, cb) {
221 t.equal(0, count, 'works in parallel')
222 setImmediate(function () {
223 count++
224 cb(null, true)
225 })
226 }
227})
228
229test('idle()', function (t) {
230 t.plan(12)
231
232 var queue = buildQueue(worker, 1)
233
234 t.ok(queue.idle(), 'queue is idle')
235
236 queue.push(42, function (err, result) {
237 t.error(err, 'no error')
238 t.equal(result, true, 'result matches')
239 t.notOk(queue.idle(), 'queue is not idle')
240 })
241
242 queue.push(42, function (err, result) {
243 t.error(err, 'no error')
244 t.equal(result, true, 'result matches')
245 // it will go idle after executing this function
246 setImmediate(function () {
247 t.ok(queue.idle(), 'queue is now idle')
248 })
249 })
250
251 t.notOk(queue.idle(), 'queue is not idle')
252
253 function worker (arg, cb) {
254 t.notOk(queue.idle(), 'queue is not idle')
255 t.equal(arg, 42)
256 setImmediate(cb, null, true)
257 }
258})
259
260test('saturated', function (t) {
261 t.plan(9)
262
263 var queue = buildQueue(worker, 1)
264 var preworked = 0
265 var worked = 0
266
267 queue.saturated = function () {
268 t.pass('saturated')
269 t.equal(preworked, 1, 'started 1 task')
270 t.equal(worked, 0, 'worked zero task')
271 }
272
273 queue.push(42, done)
274 queue.push(42, done)
275
276 function done (err, result) {
277 t.error(err, 'no error')
278 t.equal(result, true, 'result matches')
279 }
280
281 function worker (arg, cb) {
282 t.equal(arg, 42)
283 preworked++
284 setImmediate(function () {
285 worked++
286 cb(null, true)
287 })
288 }
289})
290
291test('length', function (t) {
292 t.plan(7)
293
294 var queue = buildQueue(worker, 1)
295
296 t.equal(queue.length(), 0, 'nothing waiting')
297 queue.push(42, done)
298 t.equal(queue.length(), 0, 'nothing waiting')
299 queue.push(42, done)
300 t.equal(queue.length(), 1, 'one task waiting')
301 queue.push(42, done)
302 t.equal(queue.length(), 2, 'two tasks waiting')
303
304 function done (err, result) {
305 t.error(err, 'no error')
306 }
307
308 function worker (arg, cb) {
309 setImmediate(function () {
310 cb(null, true)
311 })
312 }
313})
314
315test('getQueue', function (t) {
316 t.plan(10)
317
318 var queue = buildQueue(worker, 1)
319
320 t.equal(queue.getQueue().length, 0, 'nothing waiting')
321 queue.push(42, done)
322 t.equal(queue.getQueue().length, 0, 'nothing waiting')
323 queue.push(42, done)
324 t.equal(queue.getQueue().length, 1, 'one task waiting')
325 t.equal(queue.getQueue()[0], 42, 'should be equal')
326 queue.push(43, done)
327 t.equal(queue.getQueue().length, 2, 'two tasks waiting')
328 t.equal(queue.getQueue()[0], 42, 'should be equal')
329 t.equal(queue.getQueue()[1], 43, 'should be equal')
330
331 function done (err, result) {
332 t.error(err, 'no error')
333 }
334
335 function worker (arg, cb) {
336 setImmediate(function () {
337 cb(null, true)
338 })
339 }
340})
341
342test('unshift', function (t) {
343 t.plan(8)
344
345 var queue = buildQueue(worker, 1)
346 var expected = [1, 2, 3, 4]
347
348 queue.push(1, done)
349 queue.push(4, done)
350 queue.unshift(3, done)
351 queue.unshift(2, done)
352
353 function done (err, result) {
354 t.error(err, 'no error')
355 }
356
357 function worker (arg, cb) {
358 t.equal(expected.shift(), arg, 'tasks come in order')
359 setImmediate(function () {
360 cb(null, true)
361 })
362 }
363})
364
365test('unshift && empty', function (t) {
366 t.plan(2)
367
368 var queue = buildQueue(worker, 1)
369 var completed = false
370
371 queue.pause()
372
373 queue.empty = function () {
374 t.notOk(completed, 'the task has not completed yet')
375 }
376
377 queue.unshift(1, done)
378
379 queue.resume()
380
381 function done (err, result) {
382 completed = true
383 t.error(err, 'no error')
384 }
385
386 function worker (arg, cb) {
387 setImmediate(function () {
388 cb(null, true)
389 })
390 }
391})
392
393test('push && empty', function (t) {
394 t.plan(2)
395
396 var queue = buildQueue(worker, 1)
397 var completed = false
398
399 queue.pause()
400
401 queue.empty = function () {
402 t.notOk(completed, 'the task has not completed yet')
403 }
404
405 queue.push(1, done)
406
407 queue.resume()
408
409 function done (err, result) {
410 completed = true
411 t.error(err, 'no error')
412 }
413
414 function worker (arg, cb) {
415 setImmediate(function () {
416 cb(null, true)
417 })
418 }
419})
420
421test('kill', function (t) {
422 t.plan(5)
423
424 var queue = buildQueue(worker, 1)
425 var expected = [1]
426
427 var predrain = queue.drain
428
429 queue.drain = function drain () {
430 t.fail('drain should never be called')
431 }
432
433 queue.push(1, done)
434 queue.push(4, done)
435 queue.unshift(3, done)
436 queue.unshift(2, done)
437 queue.kill()
438
439 function done (err, result) {
440 t.error(err, 'no error')
441 setImmediate(function () {
442 t.equal(queue.length(), 0, 'no queued tasks')
443 t.equal(queue.running(), 0, 'no running tasks')
444 t.equal(queue.drain, predrain, 'drain is back to default')
445 })
446 }
447
448 function worker (arg, cb) {
449 t.equal(expected.shift(), arg, 'tasks come in order')
450 setImmediate(function () {
451 cb(null, true)
452 })
453 }
454})
455
456test('killAndDrain', function (t) {
457 t.plan(6)
458
459 var queue = buildQueue(worker, 1)
460 var expected = [1]
461
462 var predrain = queue.drain
463
464 queue.drain = function drain () {
465 t.pass('drain has been called')
466 }
467
468 queue.push(1, done)
469 queue.push(4, done)
470 queue.unshift(3, done)
471 queue.unshift(2, done)
472 queue.killAndDrain()
473
474 function done (err, result) {
475 t.error(err, 'no error')
476 setImmediate(function () {
477 t.equal(queue.length(), 0, 'no queued tasks')
478 t.equal(queue.running(), 0, 'no running tasks')
479 t.equal(queue.drain, predrain, 'drain is back to default')
480 })
481 }
482
483 function worker (arg, cb) {
484 t.equal(expected.shift(), arg, 'tasks come in order')
485 setImmediate(function () {
486 cb(null, true)
487 })
488 }
489})
490
491test('pause && idle', function (t) {
492 t.plan(11)
493
494 var queue = buildQueue(worker, 1)
495 var worked = false
496
497 t.notOk(queue.paused, 'it should not be paused')
498 t.ok(queue.idle(), 'should be idle')
499
500 queue.pause()
501
502 queue.push(42, function (err, result) {
503 t.error(err, 'no error')
504 t.equal(result, true, 'result matches')
505 })
506
507 t.notOk(worked, 'it should be paused')
508 t.ok(queue.paused, 'it should be paused')
509 t.notOk(queue.idle(), 'should not be idle')
510
511 queue.resume()
512
513 t.notOk(queue.paused, 'it should not be paused')
514 t.notOk(queue.idle(), 'it should not be idle')
515
516 function worker (arg, cb) {
517 t.equal(arg, 42)
518 worked = true
519 process.nextTick(cb.bind(null, null, true))
520 process.nextTick(function () {
521 t.ok(queue.idle(), 'is should be idle')
522 })
523 }
524})
525
526test('push without cb', function (t) {
527 t.plan(1)
528
529 var queue = buildQueue(worker, 1)
530
531 queue.push(42)
532
533 function worker (arg, cb) {
534 t.equal(arg, 42)
535 cb()
536 }
537})
538
539test('unshift without cb', function (t) {
540 t.plan(1)
541
542 var queue = buildQueue(worker, 1)
543
544 queue.unshift(42)
545
546 function worker (arg, cb) {
547 t.equal(arg, 42)
548 cb()
549 }
550})
551
552test('push with worker throwing error', function (t) {
553 t.plan(5)
554 var q = buildQueue(function (task, cb) {
555 cb(new Error('test error'), null)
556 }, 1)
557 q.error(function (err, task) {
558 t.ok(err instanceof Error, 'global error handler should catch the error')
559 t.match(err.message, /test error/, 'error message should be "test error"')
560 t.equal(task, 42, 'The task executed should be passed')
561 })
562 q.push(42, function (err) {
563 t.ok(err instanceof Error, 'push callback should catch the error')
564 t.match(err.message, /test error/, 'error message should be "test error"')
565 })
566})
Note: See TracBrowser for help on using the repository browser.