Skip to content

Commit

Permalink
Merge branch 'issue-2-pr'
Browse files Browse the repository at this point in the history
  • Loading branch information
azproduction committed Jul 9, 2014
2 parents 5c9ad80 + 14e354f commit 76a5469
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 5 deletions.
4 changes: 0 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,4 @@ matrix:
allow_failures:
- node_js: "0.8"

branches:
only:
- master

script: make travis
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ Promise-based queue
npm install promise-queue
```

## Interface

- `new Queue(Number maxConcurrent, Number maxQueued): Queue`
- `Queue#add(Function generator): Promise` - adds function argument that generates a promise to the queue
- `Queue#getQueueLength(): Number` - returns current length of buffer(added but not started promise generators) `it <= maxQueued`
- `Queue#getPendingLength(): Number` - returns number of pending(concurrently running) promises `it <= maxConcurrent`

## Example

### Configure queue
Expand Down Expand Up @@ -55,4 +62,27 @@ app.get('/version/:user/:repo', function (req, res, next) {
});
```

### Getting number of pending promises and queue(buffered promises) length

```js
var maxConcurrent = 1;
var maxQueue = 1;
var queue = new Queue(maxConcurrent, maxQueue);

queue.add(function () {
queue.getQueueLength() === 0;
queue.getPendingLength() === 1;
return somePromise();
});

queue.add(function () {
queue.getQueueLength() === 0;
queue.getPendingLength() === 0;
return somePromise();
});

queue.getQueueLength() === 1;
queue.getPendingLength() === 1;
```

[Live example](http://jsfiddle.net/RVuEU/1/)
18 changes: 18 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,24 @@
});
};

/**
* Number of simultaneously running promises (which are resolving)
*
* @return {number}
*/
Queue.prototype.getPendingLength = function () {
return this.pendingPromises;
};

/**
* Number of queued promises (which are waiting)
*
* @return {number}
*/
Queue.prototype.getQueueLength = function () {
return this.queue.length;
};

/**
* @returns {boolean} true if first item removed from queue
* @private
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"author" : "Mikhail Davydov <i@azproduction.ru>",
"description" : "Promise-based queue",
"name" : "promise-queue",
"version" : "2.0.1",
"version" : "2.1.1",
"contributors" : [
{
"name": "Mikhail Davydov",
Expand Down
79 changes: 79 additions & 0 deletions test/test.queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,83 @@ describe('queue', function () {
.then(done, done);
});
});

describe('getPendingLength', function () {

it('returns number of pending promises', function (done) {
var expectedPendingLength = 10;
var pendingNumber = 10;
var queue = new Queue(expectedPendingLength);

function generator() {
return function () {
new vow.Promise(function (resolve) {
setTimeout(function () {
resolve();
}, 100);
});
};
}

function check() {
pendingNumber--;
if (pendingNumber < 0) {
return;
}
expect(queue.getPendingLength()).to.be.eql(pendingNumber);
if (pendingNumber === 0) {
done();
}
}

// Note: extra promises will be moved to a queue
for (var i = 0; i < expectedPendingLength * 2; i++) {
queue.add(generator()).then(check).then(null, done);
}

// Should synchronously increase pending counter
expect(queue.getPendingLength()).to.be.eql(expectedPendingLength);
});

});

describe('getQueueLength', function () {

it('returns number of queued promises', function (done) {
var maxPending = 1;
var expectedQueueLength = 9;
var queue = new Queue(maxPending);

function generator() {
return function () {
new vow.Promise(function (resolve) {
setTimeout(function () {
resolve();
}, 100);
});
};
}

function check() {
expectedQueueLength--;
if (expectedQueueLength < 0) {
return;
}
expect(queue.getQueueLength()).to.be.eql(expectedQueueLength);
if (expectedQueueLength === 0) {
return done();
}
}

// Note: extra promises will be moved to a queue
for (var i = 0; i <= expectedQueueLength; i++) {
queue.add(generator()).then(check).then(null, done);
}

// Should synchronously increase queue counter
expect(queue.getQueueLength()).to.be.eql(expectedQueueLength);
expectedQueueLength = expectedQueueLength - 1;
});

});
});

0 comments on commit 76a5469

Please sign in to comment.