From 382d6d66f962a223c1d3873d94547a6b23ea6d81 Mon Sep 17 00:00:00 2001 From: Ricky Ng-Adam Date: Thu, 17 Apr 2014 16:18:49 +0800 Subject: [PATCH 1/4] 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 3d7aa5807..02c016364 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 e227baac3..f6026a5c0 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(); }); From 85c28e9088f4a00e9bf5a61c7ebf516ce08af2a4 Mon Sep 17 00:00:00 2001 From: Ricky Ng-Adam Date: Thu, 17 Apr 2014 16:25:59 +0800 Subject: [PATCH 2/4] destroyAllNow also has a callback --- lib/index.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/index.js b/lib/index.js index 02c016364..5a395ede2 100644 --- a/lib/index.js +++ b/lib/index.js @@ -25,11 +25,12 @@ PG.prototype.end = function() { var pool = self.pools.all[key]; delete self.pools.all[key]; pool.drain(function() { - pool.destroyAllNow(); - count--; - if(count === 0) { - self.emit('ended'); - } + pool.destroyAllNow(function() { + count--; + if(count === 0) { + self.emit('ended'); + } + }); }); }); }; From 9ab6ed76d1859dd61b5c8e38ff2e465df369e5fd Mon Sep 17 00:00:00 2001 From: Ricky Ng-Adam Date: Thu, 17 Apr 2014 17:04:46 +0800 Subject: [PATCH 3/4] remove extra whitespace --- lib/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 5a395ede2..dd6e693b5 100644 --- a/lib/index.js +++ b/lib/index.js @@ -29,7 +29,7 @@ PG.prototype.end = function() { count--; if(count === 0) { self.emit('ended'); - } + } }); }); }); From 0882c8da021001487757159e4b7df07b16e0c100 Mon Sep 17 00:00:00 2001 From: Ricky Ng-Adam Date: Fri, 18 Apr 2014 23:30:45 +0800 Subject: [PATCH 4/4] from ended to end --- lib/index.js | 2 +- test/integration/connection-pool/ending-pool-tests.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index dd6e693b5..b78e4caa9 100644 --- a/lib/index.js +++ b/lib/index.js @@ -28,7 +28,7 @@ PG.prototype.end = function() { pool.destroyAllNow(function() { count--; if(count === 0) { - self.emit('ended'); + self.emit('end'); } }); }); diff --git a/test/integration/connection-pool/ending-pool-tests.js b/test/integration/connection-pool/ending-pool-tests.js index f6026a5c0..83f4b1bc2 100644 --- a/test/integration/connection-pool/ending-pool-tests.js +++ b/test/integration/connection-pool/ending-pool-tests.js @@ -5,7 +5,7 @@ test('disconnects', function() { var sink = new helper.Sink(4, function() { called = true; var eventSink = new helper.Sink(1, function() {}); - helper.pg.on('ended', function() { + helper.pg.on('end', function() { eventSink.add(); });