From 382d6d66f962a223c1d3873d94547a6b23ea6d81 Mon Sep 17 00:00:00 2001 From: Ricky Ng-Adam Date: Thu, 17 Apr 2014 16:18:49 +0800 Subject: [PATCH] emit event when all pool are actually destroyed --- lib/index.js | 9 ++++++++- test/integration/connection-pool/ending-pool-tests.js | 5 +++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 3d7aa580..02c01636 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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; diff --git a/test/integration/connection-pool/ending-pool-tests.js b/test/integration/connection-pool/ending-pool-tests.js index e227baac..f6026a5c 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('ended', function() { + eventSink.add(); + }); + //this should exit the process, killing each connection pool helper.pg.end(); });