diff --git a/lib/index.js b/lib/index.js index 3d7aa5807..b78e4caa9 100644 --- a/lib/index.js +++ b/lib/index.js @@ -19,15 +19,23 @@ 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(); + pool.destroyAllNow(function() { + count--; + if(count === 0) { + self.emit('end'); + } + }); }); }); }; + PG.prototype.connect = function(config, callback) { if(typeof config == "function") { callback = config; diff --git a/test/integration/connection-pool/ending-pool-tests.js b/test/integration/connection-pool/ending-pool-tests.js index e227baac3..83f4b1bc2 100644 --- a/test/integration/connection-pool/ending-pool-tests.js +++ b/test/integration/connection-pool/ending-pool-tests.js @@ -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('end', function() { + eventSink.add(); + }); + //this should exit the process, killing each connection pool helper.pg.end(); });