Skip to content

Commit

Permalink
tightened up tests for throttle-promises
Browse files Browse the repository at this point in the history
  • Loading branch information
kolodny committed Jun 12, 2015
1 parent 34dc2ca commit 6343113
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions throttle-promises/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@ describe('throttle-promises', function() {
var passed = true;
var limit = 5;
var currentlyExecuting = 0;
var maxCurrentlyExecuting = 0;
var currentlyExecutingHistory = [currentlyExecuting];
var nextValue = 0;
var asyncFactory = function() {
return new Promise(function(resolve) {
var resolveWith = nextValue++;
currentlyExecuting++;
maxCurrentlyExecuting = Math.max(maxCurrentlyExecuting, currentlyExecuting);
currentlyExecutingHistory.push(currentlyExecuting);
if (currentlyExecuting > limit) {
passed = false;
}
setTimeout(function() {
resolve(resolveWith + '!');
currentlyExecuting--;
currentlyExecutingHistory.push(currentlyExecuting);
}, Math.random() * 100);
});
};
Expand All @@ -32,9 +33,13 @@ describe('throttle-promises', function() {
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.equal(maxCurrentlyExecuting, limit);
assert.deepEqual(results, expectedResults);
assert.deepEqual(currentlyExecutingHistory, expectedHistory);
done();
});

Expand Down

0 comments on commit 6343113

Please sign in to comment.