-
Notifications
You must be signed in to change notification settings - Fork 5
/
index2.test.js
82 lines (69 loc) · 1.6 KB
/
index2.test.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
'use strict';
var expect = require('expect.js');
var series = require('./index');
function pTimeout(result, cb) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
resolve(result);
if (cb) cb();
}, 500);
});
}
function logTime() {
console.log((new Date()).getTime());
}
describe('series', function () {
this.timeout(5000);
it('pTimeout() should work', function () {
return pTimeout(logTime)
.then(function () {
return pTimeout(1, logTime);
})
.then(function () {
return pTimeout(2, logTime);
})
.then(function () {
return pTimeout(3, logTime);
});
});
it('Should run promises in series', function (done) {
var n = 0;
function count() {
n++;
logTime();
}
var p = series([
pTimeout(1, count),
pTimeout.bind(null, 2, count),
pTimeout.bind(null, 3, count)
])
.then(function (results) {
expect(n).to.be(3);
expect(results).to.eql([1, 2, 3]);
done();
})
.catch(done);
expect(p).to.be.a(Promise);
});
it('Should run promises in concurrency', function (done) {
var n = 0;
function count() {
n++;
logTime();
}
var p = series([
pTimeout(1, count),
pTimeout.bind(null, 2, count),
pTimeout.bind(null, 3, count),
pTimeout.bind(null, 4, count),
pTimeout.bind(null, 5, count)
], 2)
.then(function (results) {
expect(n).to.be(5);
expect(results).to.eql([1, 2, 3, 4, 5]);
done();
})
.catch(done);
expect(p).to.be.a(Promise);
});
});