Make sure 'end' is emitted even if no connection has ever happened

This commit is contained in:
Marek 2014-05-13 12:57:01 +01:00
parent 3898f5d8b2
commit 47b0aafa6d
2 changed files with 29 additions and 10 deletions

View File

@ -21,18 +21,22 @@ PG.prototype.end = function() {
var self = this; var self = this;
var keys = Object.keys(self.pools.all); var keys = Object.keys(self.pools.all);
var count = keys.length; var count = keys.length;
keys.forEach(function(key) { if(count === 0) {
var pool = self.pools.all[key]; self.emit('end');
delete self.pools.all[key]; } else {
pool.drain(function() { keys.forEach(function(key) {
pool.destroyAllNow(function() { var pool = self.pools.all[key];
count--; delete self.pools.all[key];
if(count === 0) { pool.drain(function() {
self.emit('end'); pool.destroyAllNow(function() {
} count--;
if(count === 0) {
self.emit('end');
}
});
}); });
}); });
}); }
}; };

View File

@ -0,0 +1,15 @@
var helper = require(__dirname + '/test-helper')
var called = false;
test('disconnects', function() {
called = true;
var eventSink = new helper.Sink(1, function() {});
helper.pg.on('end', function() {
eventSink.add();
});
//this should exit the process
helper.pg.end();
})