emit event when all pool are actually destroyed

This commit is contained in:
Ricky Ng-Adam 2014-04-17 16:18:49 +08:00
parent fb9876c896
commit 382d6d66f9
2 changed files with 13 additions and 1 deletions

View File

@ -19,15 +19,22 @@ util.inherits(PG, EventEmitter);
PG.prototype.end = function() {
var self = this;
Object.keys(self.pools.all).forEach(function(key) {
var keys = Object.keys(self.pools.all);
var count = keys.length;
keys.forEach(function(key) {
var pool = self.pools.all[key];
delete self.pools.all[key];
pool.drain(function() {
pool.destroyAllNow();
count--;
if(count === 0) {
self.emit('ended');
}
});
});
};
PG.prototype.connect = function(config, callback) {
if(typeof config == "function") {
callback = config;

View File

@ -4,6 +4,11 @@ var called = false;
test('disconnects', function() {
var sink = new helper.Sink(4, function() {
called = true;
var eventSink = new helper.Sink(1, function() {});
helper.pg.on('ended', function() {
eventSink.add();
});
//this should exit the process, killing each connection pool
helper.pg.end();
});