Skip to content

Commit

Permalink
Merge pull request #571 from letsface/ended-event-on-pool-drained
Browse files Browse the repository at this point in the history
emit event 'ended' on pool drained
  • Loading branch information
brianc committed Apr 22, 2014
2 parents 65d177e + 0882c8d commit 1047aeb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/index.js
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions test/integration/connection-pool/ending-pool-tests.js
Expand Up @@ -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();
});
Expand Down

0 comments on commit 1047aeb

Please sign in to comment.