-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
parallel.js
91 lines (82 loc) · 2.87 KB
/
parallel.js
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
/*!
* each-promise <https://github.com/tunnckoCore/each-promise>
*
* Copyright (c) Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)
* Released under the MIT license.
*/
/* jshint asi:true */
'use strict'
var test = require('mukla')
var eachPromise = require('../index')
var Bluebird = require('bluebird')
var semver = require('semver')
var extend = require('extend-shallow')
var delay = function (fn, ms) {
return new Bluebird(function (resolve) {
setTimeout(resolve, ms)
})
}
var specialError = new Error('errfoo')
var fixtureTwo = function () {
return [
delay(900).then(function () { return 1 }),
function () { return delay(770).then(function () { throw specialError }) },
function () { return delay(620).then(function () { return 3 }) },
delay(800).then(function () { return 4 }),
function () { return delay(700).then(function () { return 5 }) }
]
}
function factory (fnParallel) {
test('should `.parallel` run with concurrency default to `iterable` length', function (done) {
fnParallel(fixtureTwo()).then(function (res) {
test.strictEqual(res.length, 5)
// test.deepEqual(res, [1, 4, specialError, 3, 5]) // may not work
test.strictEqual(res.indexOf(3) > -1, true)
test.strictEqual(res.indexOf(5) > -1, true)
test.strictEqual(res.indexOf(4) > -1, true)
test.strictEqual(res.indexOf(1) > -1, true)
test.strictEqual(res.indexOf(specialError) > -1, true)
// test.strictEqual(res[0], 3)
// test.strictEqual(res[1], 5)
// test.strictEqual(res[2].name, 'Error')
// test.strictEqual(res[2].message, 'errfoo')
// test.strictEqual(res[3], 4)
// test.strictEqual(res[4], 1)
done()
}, done).catch(done)
})
test('should `.parallel` stop after first error if settle:false', function (done) {
fnParallel(fixtureTwo(), { settle: false }).catch(function (err) {
test.strictEqual(err !== null, true, 'err should be Error object')
test.strictEqual(err.message, 'errfoo')
test.strictEqual(err.name, 'Error')
done()
}).catch(done)
})
test('should `.parallel` with custom concurrenc: 2 and `mapper`', function (done) {
var concurrency = 2
var mapper = function (item) {
return (item.reason && item.reason.message) || item.value
}
fnParallel(fixtureTwo(), {
concurrency: concurrency,
mapper: mapper
}).then(function (res) {
test.strictEqual(res.length, 5)
test.strictEqual(res.indexOf(1) > -1, true)
test.strictEqual(res.indexOf(3) > -1, true)
test.strictEqual(res.indexOf(4) > -1, true)
test.strictEqual(res.indexOf(5) > -1, true)
done()
}, done).catch(done)
})
}
if (semver.lt(process.version, '0.11.13')) {
factory(function (val, opts) {
return eachPromise.parallel(val, extend({
Promise: Bluebird
}, opts))
})
} else {
factory(eachPromise.parallel)
}