Skip to content

Commit

Permalink
fix: rare case when non-last task errors and finishes last
Browse files Browse the repository at this point in the history
  • Loading branch information
fent committed Mar 25, 2018
1 parent a1ebcf1 commit 7e3bff1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ module.exports = class Q extends EventEmitter {
} else {
self.emit('error', err);
}
self._next2();
self._next1();
if (self.active === 0) { self.emit('drain'); }
return;
Expand Down Expand Up @@ -195,7 +196,6 @@ module.exports = class Q extends EventEmitter {
if (this.active === 0) { this.emit('drain'); }
});
this.worker2.apply(null, task.args);
this._next2();
}


Expand Down
27 changes: 27 additions & 0 deletions test/error-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,33 @@ describe('Worker 1 calls callback with error', () => {
q.push(1);
});
});

describe('Later task finishes before errored task', () => {
it('All task finish', (done) => {
var otherCallback, errored;
var q = new Q((a, callback) => setTimeout(() => {
console.log('worker1', a);
if (a === 1) {
otherCallback = callback;
} else if (a === 2) {
callback(null, a);
otherCallback(new Error('thing'), a);
}
}), (a, callback) => {
console.log('worker2', a, q.active);
setTimeout(callback);
});
q.on('error', () => {
errored = true;
});
q.on('drain', () => {
assert.ok(errored);
done();
});
q.push(1);
q.push(2);
});
});
});

describe('Worker 2 calls callback with error', () => {
Expand Down

0 comments on commit 7e3bff1

Please sign in to comment.