Skip to content

Commit 382d6d6

Browse files
author
Ricky Ng-Adam
committed
emit event when all pool are actually destroyed
1 parent fb9876c commit 382d6d6

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

lib/index.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,22 @@ util.inherits(PG, EventEmitter);
1919

2020
PG.prototype.end = function() {
2121
var self = this;
22-
Object.keys(self.pools.all).forEach(function(key) {
22+
var keys = Object.keys(self.pools.all);
23+
var count = keys.length;
24+
keys.forEach(function(key) {
2325
var pool = self.pools.all[key];
2426
delete self.pools.all[key];
2527
pool.drain(function() {
2628
pool.destroyAllNow();
29+
count--;
30+
if(count === 0) {
31+
self.emit('ended');
32+
}
2733
});
2834
});
2935
};
3036

37+
3138
PG.prototype.connect = function(config, callback) {
3239
if(typeof config == "function") {
3340
callback = config;

test/integration/connection-pool/ending-pool-tests.js

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ var called = false;
44
test('disconnects', function() {
55
var sink = new helper.Sink(4, function() {
66
called = true;
7+
var eventSink = new helper.Sink(1, function() {});
8+
helper.pg.on('ended', function() {
9+
eventSink.add();
10+
});
11+
712
//this should exit the process, killing each connection pool
813
helper.pg.end();
914
});

0 commit comments

Comments
 (0)