Skip to content
This repository was archived by the owner on Dec 30, 2019. It is now read-only.
Merged
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
54 changes: 35 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,32 @@ var Pool = module.exports = function (options, Client) {

util.inherits(Pool, EventEmitter)

Pool.prototype._promise = function (cb, executor) {
if (!cb) {
return new this.Promise(executor)
}

function resolved (value) {
process.nextTick(function () {
cb(null, value)
})
}

function rejected (error) {
process.nextTick(function () {
cb(error)
})
}

executor(resolved, rejected)
}

Pool.prototype._promiseNoCallback = function (callback, executor) {
return callback
? executor()
: new this.Promise(executor)
}

Pool.prototype._destroy = function (client) {
if (client._destroying) return
client._destroying = true
Expand All @@ -43,7 +69,6 @@ Pool.prototype._create = function (cb) {
if (err) {
this.log('client connection error:', err)
cb(err)
this.emit('error', err)
} else {
this.log('client connected')
this.emit('connect', client)
Expand All @@ -53,15 +78,17 @@ Pool.prototype._create = function (cb) {
}

Pool.prototype.connect = function (cb) {
return new this.Promise(function (resolve, reject) {
return this._promiseNoCallback(cb, function (resolve, reject) {
this.log('acquire client begin')
this.pool.acquire(function (err, client) {
if (err) {
this.log('acquire client. error:', err)
if (cb) {
cb(err, null, function () {})
} else {
reject(err)
}
return reject(err)
return
}

this.log('acquire client')
Expand All @@ -80,9 +107,9 @@ Pool.prototype.connect = function (cb) {

if (cb) {
cb(null, client, client.release)
} else {
resolve(client)
}

return resolve(client)
}.bind(this))
}.bind(this))
}
Expand All @@ -95,36 +122,25 @@ Pool.prototype.query = function (text, values, cb) {
values = undefined
}

return new this.Promise(function (resolve, reject) {
return this._promise(cb, function (resolve, reject) {
this.connect(function (err, client, done) {
if (err) {
if (cb) {
cb(err)
}
return reject(err)
}
client.query(text, values, function (err, res) {
done(err)
err ? reject(err) : resolve(res)
if (cb) {
cb(err, res)
}
})
})
}.bind(this))
}

Pool.prototype.end = function (cb) {
this.log('draining pool')
return new this.Promise(function (resolve, reject) {
return this._promise(cb, function (resolve, reject) {
this.pool.drain(function () {
this.log('pool drained, calling destroy all now')
this.pool.destroyAllNow(function () {
if (cb) {
cb()
}
resolve()
})
this.pool.destroyAllNow(resolve)
}.bind(this))
}.bind(this))
}
26 changes: 2 additions & 24 deletions test/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('events', function () {
})
})

it('emits "error" with a failed connection', function (done) {
it('emits "connect" only with a successful connection', function (done) {
var pool = new Pool({
// This client will always fail to connect
Client: mockClient({
Expand All @@ -34,29 +34,7 @@ describe('events', function () {
pool.on('connect', function () {
throw new Error('should never get here')
})
pool.on('error', function (err) {
if (err) done()
else done(new Error('expected failure'))
})
pool.connect()
})

it('callback err with a failed connection', function (done) {
var pool = new Pool({
// This client will always fail to connect
Client: mockClient({
connect: function (cb) {
process.nextTick(function () { cb(new Error('bad news')) })
}
})
})
pool.on('connect', function () {
throw new Error('should never get here')
})
pool.on('error', function (err) {
if (!err) done(new Error('expected failure'))
})
pool.connect(function (err) {
pool._create(function (err) {
if (err) done()
else done(new Error('expected failure'))
})
Expand Down
26 changes: 26 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,32 @@ describe('pool', function () {
pool.end(done)
})
})

it('does not create promises when connecting', function (done) {
var pool = new Pool()
var returnValue = pool.connect(function (err, client, release) {
release()
if (err) return done(err)
pool.end(done)
})
expect(returnValue).to.be(undefined)
})

it('does not create promises when querying', function (done) {
var pool = new Pool()
var returnValue = pool.query('SELECT 1 as num', function (err) {
pool.end(function () {
done(err)
})
})
expect(returnValue).to.be(undefined)
})

it('does not create promises when ending', function (done) {
var pool = new Pool()
var returnValue = pool.end(done)
expect(returnValue).to.be(undefined)
})
})

describe('with promises', function () {
Expand Down