Skip to content

Commit 79a4bf6

Browse files
committed
Merge pull request #589 from marekventur/master
Make sure 'end' is emitted even if no connection has ever happened
2 parents 3898f5d + 47b0aaf commit 79a4bf6

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

lib/index.js

+14-10
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,22 @@ PG.prototype.end = function() {
2121
var self = this;
2222
var keys = Object.keys(self.pools.all);
2323
var count = keys.length;
24-
keys.forEach(function(key) {
25-
var pool = self.pools.all[key];
26-
delete self.pools.all[key];
27-
pool.drain(function() {
28-
pool.destroyAllNow(function() {
29-
count--;
30-
if(count === 0) {
31-
self.emit('end');
32-
}
24+
if(count === 0) {
25+
self.emit('end');
26+
} else {
27+
keys.forEach(function(key) {
28+
var pool = self.pools.all[key];
29+
delete self.pools.all[key];
30+
pool.drain(function() {
31+
pool.destroyAllNow(function() {
32+
count--;
33+
if(count === 0) {
34+
self.emit('end');
35+
}
36+
});
3337
});
3438
});
35-
});
39+
}
3640
};
3741

3842

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var helper = require(__dirname + '/test-helper')
2+
3+
var called = false;
4+
test('disconnects', function() {
5+
called = true;
6+
var eventSink = new helper.Sink(1, function() {});
7+
helper.pg.on('end', function() {
8+
eventSink.add();
9+
});
10+
11+
//this should exit the process
12+
helper.pg.end();
13+
})
14+
15+

0 commit comments

Comments
 (0)