Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Emit fail when a worker fails.

  • Loading branch information...
commit c3b39a40a5408b355f94a2cac83729158c9d1eb7 1 parent f3e8cfa
@jaredhanson authored
Showing with 62 additions and 0 deletions.
  1. +1 −0  lib/functionpool/pool.js
  2. +61 −0 test/pool-test.js
View
1  lib/functionpool/pool.js
@@ -139,6 +139,7 @@ Pool.prototype._dispatch = function() {
worker.once('done', function(err, res) {
task.callback && task.callback(err, res);
self._working--;
+ if (err) { self.emit('fail'); };
self.emit('done');
});
View
61 test/pool-test.js
@@ -110,6 +110,67 @@ vows.describe('Pool').addBatch({
},
},
+ 'pool with multiple workers that fail': {
+ topic: function() {
+ var self = this;
+ var count = 0;
+ var failCount = 0;
+ var pool = new Pool({ size: 3 }, function(done) {
+ count++;
+ setTimeout(function() { done(new Error('something went wrong')); }, 100);
+ });
+ pool.on('fail', function() {
+ failCount++;
+ });
+ pool.on('idle', function() {
+ self.callback(null, pool, count, failCount);
+ });
+ pool.task();
+ pool.task();
+ pool.task();
+ pool.task();
+ pool.task();
+ },
+
+ 'should work 5 times': function (err, pool, count) {
+ assert.equal(count, 5);
+ },
+ 'should fail 5 times': function (err, pool, count, failCount) {
+ assert.equal(failCount, 5);
+ },
+ },
+
+ 'pool that clears queue after a worker fails': {
+ topic: function() {
+ var self = this;
+ var count = 0;
+ var failCount = 0;
+ var pool = new Pool({ size: 3 }, function(done) {
+ count++;
+ setTimeout(function() { done(new Error('something went wrong')); }, 100);
+ });
+ pool.on('fail', function() {
+ failCount++;
+ pool.clearQueue();
+ });
+ pool.on('idle', function() {
+ self.callback(null, pool, count, failCount);
+ });
+ pool.task();
+ pool.task();
+ pool.task();
+ pool.task();
+ pool.task();
+ },
+
+ 'should work 3 times': function (err, pool, count) {
+ assert.equal(count, 3);
+ },
+ 'should fail 3 times': function (err, pool, count, failCount) {
+ assert.equal(failCount, 3);
+ },
+ },
+
'pool should pass arguments to worker': {
topic: function() {
var self = this;
Please sign in to comment.
Something went wrong with that request. Please try again.