Skip to content
This repository was archived by the owner on Dec 30, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ Pool.prototype._create = function (cb) {
} else {
this.log('client connected')
this.emit('connect', client)
cb(null, client)

if (this.options.connectionSetup !== undefined) {
this.options.connectionSetup(client, cb)
} else {
cb(null, client)
}
}
}.bind(this))
}
Expand Down
33 changes: 33 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ describe('pool', function () {
})
})

it('calls the connectionSetup function when successfully connected', function (done) {
var called = false
var pool = new Pool({
connectionSetup: function (client, cb) {
called = true
cb(null, client)
}
})
pool.connect(function (err, client, release) {
if (err) return done(err)
expect(called).to.be(true)
release()
pool.end(done)
})
})

it('passes props to clients', function (done) {
var pool = new Pool({ binary: true })
pool.connect(function (err, client, release) {
Expand Down Expand Up @@ -73,6 +89,23 @@ describe('pool', function () {
})
})

it('does not call setupConnection when an error occurs', function (done) {
var called = false
var pool = new Pool({
host: 'no-postgres-server-here.com',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe start a net.Server on a random port and use it as the host instead, to make the test fully local.

connectionSetup: function () {
called = true
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to call the callback to fail properly.

}
})
pool.query('SELECT $1::text as name', ['brianc'], function (err, res) {
expect(err).to.be.an(Error)
expect(called).to.be(false)
pool.end(function (err) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pool.end(done)

done(err)
})
})
})

it('removes client if it errors in background', function (done) {
var pool = new Pool()
pool.connect(function (err, client, release) {
Expand Down