/
test.js
48 lines (42 loc) · 1.49 KB
/
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
var assert = require('assert');
var throttlePromises = require('./');
describe('throttle-promises', function() {
it("doesn't run more than `limit` promises in parallel", function(done) {
var passed = true;
var limit = 5;
var currentlyExecuting = 0;
var currentlyExecutingHistory = [currentlyExecuting];
var nextValue = 0;
var asyncFactory = function() {
return new Promise(function(resolve) {
var resolveWith = nextValue++;
currentlyExecuting++;
currentlyExecutingHistory.push(currentlyExecuting);
if (currentlyExecuting > limit) {
passed = false;
}
setTimeout(function() {
resolve(resolveWith + '!');
currentlyExecuting--;
currentlyExecutingHistory.push(currentlyExecuting);
}, Math.random() * 100);
});
};
var arr = [];
for (var i = 0; i < 100; i++) {
arr.push(asyncFactory);
}
throttlePromises(5, arr).then(function(results) {
var expectedResults = Array(101).join('.').split('').map(function(dot, index) {
return index + '!';
});
var expectedHistory = Array(202).join('.').split('').map(function(dot, index) {
return index < 5 ? index : index >= 200 - 5 ? 200 - index : index % 2 ? 5 : 4;
});
assert(passed, 'more than ' + limit + ' promises ran in parallel');
assert.deepEqual(results, expectedResults);
assert.deepEqual(currentlyExecutingHistory, expectedHistory);
done();
});
});
});