From 4cd56cc4f857a723aa8eb42ee04b312b86bb0f1e Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Wed, 24 May 2017 16:04:50 +0200 Subject: [PATCH] Make pool name consistent on missing config params (#1279) * Going red: using a config object creates two pools when missing some params It should only create a pool in a consistent way, even if some params are not provided in the first place. * Delay the pool name generation to make it consistent between calls * Don't fallback to empty object as config is already defined --- lib/index.js | 2 +- .../single-pool-on-object-config-tests.js | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 test/integration/connection-pool/single-pool-on-object-config-tests.js diff --git a/lib/index.js b/lib/index.js index 65e7be3..a2c2792 100644 --- a/lib/index.js +++ b/lib/index.js @@ -54,7 +54,6 @@ PG.prototype.connect = function(config, callback) { callback = config; config = null; } - var poolName = JSON.stringify(config || {}); if (typeof config == 'string') { config = new ConnectionParameters(config); } @@ -66,6 +65,7 @@ PG.prototype.connect = function(config, callback) { config.idleTimeoutMillis = config.idleTimeoutMillis || config.poolIdleTimeout || defaults.poolIdleTimeout; config.log = config.log || config.poolLog || defaults.poolLog; + var poolName = JSON.stringify(config); this._pools[poolName] = this._pools[poolName] || new this.Pool(config); var pool = this._pools[poolName]; if(!pool.listeners('error').length) { diff --git a/test/integration/connection-pool/single-pool-on-object-config-tests.js b/test/integration/connection-pool/single-pool-on-object-config-tests.js new file mode 100644 index 0000000..a28cbf5 --- /dev/null +++ b/test/integration/connection-pool/single-pool-on-object-config-tests.js @@ -0,0 +1,13 @@ +var helper = require(__dirname + "/../test-helper"); +var pg = require(__dirname + "/../../../lib"); + +pg.connect(helper.config, assert.success(function(client, done) { + assert.equal(Object.keys(pg._pools).length, 1); + pg.connect(helper.config, assert.success(function(client2, done2) { + assert.equal(Object.keys(pg._pools).length, 1); + + done(); + done2(); + pg.end(); + })); +}));