any-db-pool
Advanced tools
Comparing version 2.2.0 to 2.3.0
112
index.js
@@ -1,5 +0,5 @@ | ||
var inherits = require('util').inherits | ||
var inherits = require('util').inherits | ||
var EventEmitter = require('events').EventEmitter | ||
var GenericPool = require('generic-pool').Pool | ||
var chain = require('./lib/chain') | ||
var GenericPool = require('generic-pool').Pool | ||
var chain = require('./lib/chain') | ||
@@ -16,19 +16,21 @@ module.exports = ConnectionPool | ||
options = options || {} | ||
options = options || {} | ||
connParams = connParams || {} | ||
if (options.create) { | ||
console.warn("PoolConfig.create ignored, use PoolConfig.onConnect instead") | ||
console.warn('PoolConfig.create ignored, use PoolConfig.onConnect instead') | ||
} | ||
if (options.destroy) { | ||
console.warn("PoolConfig.destroy ignored") | ||
console.warn('PoolConfig.destroy ignored') | ||
} | ||
if (adapter.name == 'sqlite3' | ||
&& /:memory:$/i.test(connParams.database) | ||
&& (options.min > 1 || options.max > 1)) | ||
{ | ||
if ( | ||
adapter.name == 'sqlite3' && | ||
/:memory:$/i.test(connParams.database) && | ||
(options.min > 1 || options.max > 1) | ||
) { | ||
console.warn( | ||
"Pools of more than 1 connection do not work for in-memory SQLite3 databases\n" + | ||
"The specified minimum (%d) and maximum (%d) connections have been overridden", | ||
options.min, options.max | ||
'Pools of more than 1 connection do not work for in-memory SQLite3 databases\n' + | ||
'The specified minimum (%d) and maximum (%d) connections have been overridden', | ||
options.min, | ||
options.max, | ||
) | ||
@@ -38,14 +40,18 @@ if (options.min) options.min = 1 | ||
} | ||
var onConnect = options.onConnect || function (c, done) { done(null, c) } | ||
var onConnect = | ||
options.onConnect || | ||
function(c, done) { | ||
done(null, c) | ||
} | ||
var poolOpts = { | ||
min: options.min || 0, | ||
max: options.max || 10, | ||
create: function (ready) { | ||
adapter.createConnection(connParams, function (err, conn) { | ||
if (err) return ready(err); | ||
create: function(ready) { | ||
adapter.createConnection(connParams, function(err, conn) { | ||
if (err) return ready(err) | ||
onConnect(conn, function (err, connection) { | ||
if (err) return ready(err); | ||
onConnect(conn, function(err, connection) { | ||
if (err) return ready(err) | ||
conn.on('error', self._handleIdleError) | ||
@@ -57,5 +63,5 @@ ready(null, connection) | ||
destroy: function (connection) { | ||
destroy: function(connection) { | ||
connection.removeAllListeners() | ||
connection.on('error', function () {}) | ||
connection.on('error', function() {}) | ||
connection.end() | ||
@@ -83,3 +89,3 @@ }, | ||
this._shouldDestroyConnection = function (err) { | ||
this._shouldDestroyConnection = function(err) { | ||
if (err instanceof CancelledQueryError) { | ||
@@ -92,3 +98,3 @@ return false | ||
var self = this | ||
self._handleIdleError = function (err) { | ||
self._handleIdleError = function(err) { | ||
var connection = this | ||
@@ -100,10 +106,10 @@ self._maybeDestroy(connection, err) | ||
ConnectionPool.prototype.query = function (statement, params, callback) { | ||
var self = this | ||
, query = this.adapter.createQuery(statement, params, callback) | ||
, connection = null | ||
ConnectionPool.prototype.query = function(statement, params, callback) { | ||
var self = this, | ||
query = this.adapter.createQuery(statement, params, callback), | ||
connection = null | ||
if (query.callback) { | ||
callback = query.callback | ||
query.callback = function (err, result) { | ||
query.callback = function(err, result) { | ||
self._maybeDestroy(connection, err) | ||
@@ -114,3 +120,3 @@ callback(err, result) | ||
var finished = false | ||
query.once('end', function () { | ||
query.once('end', function() { | ||
if (!finished) { | ||
@@ -121,3 +127,3 @@ finished = true | ||
}) | ||
query.once('error', function (err) { | ||
query.once('error', function(err) { | ||
if (!finished) { | ||
@@ -138,3 +144,3 @@ finished = true | ||
*/ | ||
function handleConnectionError (error) { | ||
function handleConnectionError(error) { | ||
self._maybeDestroy(connection, error) | ||
@@ -144,3 +150,3 @@ forwardError(error) | ||
function forwardError (error) { | ||
function forwardError(error) { | ||
if (query.callback) { | ||
@@ -153,3 +159,3 @@ query.callback(error) | ||
this.acquire(function (err, connection_) { | ||
this.acquire(function(err, connection_) { | ||
if (err) { | ||
@@ -168,7 +174,7 @@ return handleConnectionError(err) | ||
connection.on('error', handleConnectionError) | ||
query.once('end', function () { | ||
query.once('end', function() { | ||
connection.removeListener('error', handleConnectionError) | ||
}) | ||
connection.query(query); | ||
connection.query(query) | ||
self.emit('query', query) | ||
@@ -180,22 +186,22 @@ }) | ||
ConnectionPool.prototype.cancel = function (query) { | ||
ConnectionPool.prototype.cancel = function(query) { | ||
this._cancelledQueries.push(query) | ||
} | ||
ConnectionPool.prototype.acquire = function (callback) { | ||
ConnectionPool.prototype.acquire = function(callback) { | ||
var self = this | ||
self._pool.acquire(function (err, connection) { | ||
if (err) return callback(err); | ||
self._pool.acquire(function(err, connection) { | ||
if (err) return callback(err) | ||
connection.removeListener('error', self._handleIdleError) | ||
self.emit('acquire', connection) | ||
callback(null, connection) | ||
}); | ||
}) | ||
} | ||
ConnectionPool.prototype.release = function (connection) { | ||
ConnectionPool.prototype.release = function(connection) { | ||
var self = this | ||
self.emit('release', connection) | ||
connection.removeAllListeners() | ||
self._reset(connection, function (err) { | ||
if (err) return self.destroy(connection); | ||
self._reset(connection, function(err) { | ||
if (err) return self.destroy(connection) | ||
connection.on('error', self._handleIdleError) | ||
@@ -206,10 +212,10 @@ self._pool.release(connection) | ||
ConnectionPool.prototype.destroy = function (connection) { | ||
ConnectionPool.prototype.destroy = function(connection) { | ||
this._pool.destroy(connection) | ||
} | ||
ConnectionPool.prototype.close = function (callback) { | ||
ConnectionPool.prototype.close = function(callback) { | ||
var self = this | ||
this._pool.drain(function () { | ||
self._pool.destroyAllNow(function () { | ||
this._pool.drain(function() { | ||
self._pool.destroyAllNow(function() { | ||
self.emit('close') | ||
@@ -221,3 +227,3 @@ if (callback) callback() | ||
ConnectionPool.prototype._maybeDestroy = function (connection, error) { | ||
ConnectionPool.prototype._maybeDestroy = function(connection, error) { | ||
if (connection) { | ||
@@ -232,3 +238,3 @@ if (error && this._shouldDestroyConnection(error)) { | ||
ConnectionPool.prototype._checkCancellation = function (query) { | ||
ConnectionPool.prototype._checkCancellation = function(query) { | ||
var cancelIndex = this._cancelledQueries.indexOf(query) | ||
@@ -244,4 +250,4 @@ if (cancelIndex >= 0) { | ||
Error.call(this) | ||
this.message = "Query was cancelled before connection was acquired" | ||
this.name = "CancelledQueryError" | ||
} | ||
this.message = 'Query was cancelled before connection was acquired' | ||
this.name = 'CancelledQueryError' | ||
} |
@@ -1,21 +0,22 @@ | ||
module.exports = makeChain; | ||
module.exports = makeChain | ||
function makeChain(steps) { | ||
return chain; | ||
return chain | ||
function chain(it, callback) { | ||
var i = 0, currentStep; | ||
(function next(err) { | ||
if (err) return callback(err); | ||
var i = 0, | ||
currentStep | ||
;(function next(err) { | ||
if (err) return callback(err) | ||
if ((currentStep = steps[i++])) { | ||
try { | ||
currentStep(it, next); | ||
currentStep(it, next) | ||
} catch (err) { | ||
callback(err); | ||
callback(err) | ||
} | ||
} else { | ||
callback(null, it); | ||
callback(null, it) | ||
} | ||
})(); | ||
})() | ||
} | ||
} |
{ | ||
"name": "any-db-pool", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"description": "Any-DB connection pool", | ||
@@ -23,7 +23,7 @@ "main": "index.js", | ||
"devDependencies": { | ||
"tape": "~2.3.2", | ||
"any-db-fake": "~0.0.3", | ||
"any-db-fake": "2.3.0", | ||
"covert": "~0.1.1", | ||
"extend": "~1.2.1" | ||
"extend": "~1.2.1", | ||
"tape": "~2.3.2" | ||
} | ||
} |
@@ -11,14 +11,15 @@ # any-db-pool - database agnostic connection pool | ||
var pool = anyDB.createPool('postgres://user:pass@localhost/dbname', { | ||
min: 5, max: 15, | ||
reset: function (conn, done) { | ||
min: 5, | ||
max: 15, | ||
reset: function(conn, done) { | ||
conn.query('ROLLBACK', done) | ||
} | ||
}, | ||
}) | ||
// Proxies to mysql's connection.query | ||
var q = pool.query('SELECT 1', function (err, res) { }) | ||
var q = pool.query('SELECT 1', function(err, res) {}) | ||
``` | ||
*Note:* As shown above, [ConnectionPool](#api) instances are usually created | ||
with [anyDB.createPool][createPool]. The [any-db][] package will be installed | ||
_Note:_ As shown above, [ConnectionPool](#api) instances are usually created | ||
with [anyDB.createPool][createpool]. The [any-db][] package will be installed | ||
alongside any adapters (e.g. [any-db-postgres][]), so most users should depend | ||
@@ -30,3 +31,3 @@ on their adapter and **not** on `any-db` or `any-db-pool`. | ||
This package contains a database connection pool that can be used with any | ||
driver, but it requires an [any-db compliant adapter][Adapter]. If you are | ||
driver, but it requires an [any-db compliant adapter][adapter]. If you are | ||
writing a library that needs to support multiple database backends (e.g. | ||
@@ -66,13 +67,13 @@ SQLite3 or Postgres or MySQL) then it's strongly recommended that you add | ||
- `min` (default `0`) The minimum number of connections to keep open in the pool. | ||
- `max` (default `10`) The maximum number of connections to keep open in the pool. When this limit is reached further requests for connections will queue waiting for an existing connection to be released back into the pool. | ||
- `refreshIdle` (default `true`) When this is true, the pool will reap connections that have been idle for more than `idleTimeout` milliseconds. | ||
- `idleTimeout` (default `30000`) The maximum amount of time a connection can sit idle in the pool before being reaped. | ||
- `reapInterval` (default `1000`) How frequently the pool should check for connections that are old enough to be reaped. | ||
- `onConnect` Called immediately after a connection is first established. Use this to do one-time setup of new connections. The supplied `Connection` will not be added to the pool until you pass it to the `done` continuation. | ||
- `reset` Called each time a connection is returned to the pool. Use this to restore a connection to it's original state (e.g. rollback transactions, set the database session vars). If `reset` fails to call the `done` continuation the connection will be lost in limbo. | ||
- `shouldDestroyConnection` (default `function (err) { return true }`) - Called | ||
when an error is encountered by `pool.query` or emitted by an idle | ||
connection. If `shouldDestroyConnection(error)` is truthy the connection will | ||
be destroyed, otherwise it will be reset. | ||
- `min` (default `0`) The minimum number of connections to keep open in the pool. | ||
- `max` (default `10`) The maximum number of connections to keep open in the pool. When this limit is reached further requests for connections will queue waiting for an existing connection to be released back into the pool. | ||
- `refreshIdle` (default `true`) When this is true, the pool will reap connections that have been idle for more than `idleTimeout` milliseconds. | ||
- `idleTimeout` (default `30000`) The maximum amount of time a connection can sit idle in the pool before being reaped. | ||
- `reapInterval` (default `1000`) How frequently the pool should check for connections that are old enough to be reaped. | ||
- `onConnect` Called immediately after a connection is first established. Use this to do one-time setup of new connections. The supplied `Connection` will not be added to the pool until you pass it to the `done` continuation. | ||
- `reset` Called each time a connection is returned to the pool. Use this to restore a connection to it's original state (e.g. rollback transactions, set the database session vars). If `reset` fails to call the `done` continuation the connection will be lost in limbo. | ||
- `shouldDestroyConnection` (default `function (err) { return true }`) - Called | ||
when an error is encountered by `pool.query` or emitted by an idle | ||
connection. If `shouldDestroyConnection(error)` is truthy the connection will | ||
be destroyed, otherwise it will be reset. | ||
@@ -142,4 +143,5 @@ ### ConnectionPool.query | ||
One argument is passed to event listeners: | ||
* `query` - a [Query][] object. | ||
- `query` - a [Query][] object. | ||
#### Close event | ||
@@ -155,10 +157,10 @@ | ||
[generic-pool][gpool] is awesome, but it's *very* generic. This is a Good | ||
[generic-pool][gpool] is awesome, but it's _very_ generic. This is a Good | ||
Thing for a library with "generic" in the name, but not so good for the very | ||
common but slightly more specialized case of pooling stateful SQL database | ||
connections. This library uses `generic-pool` and simply augments it with some | ||
connections. This library uses `generic-pool` and simply augments it with some | ||
added niceties: | ||
* Hooks for initializing and/or resetting connection state when connections are added or returned to the pool. | ||
* A `query` method that allows queries to be performed without the user needing a reference to a connection object (and potentially leaking that reference). | ||
- Hooks for initializing and/or resetting connection state when connections are added or returned to the pool. | ||
- A `query` method that allows queries to be performed without the user needing a reference to a connection object (and potentially leaking that reference). | ||
@@ -175,6 +177,7 @@ ## Stop telling me not to use this directly | ||
var poolParams = { | ||
min: 5, max: 15, | ||
reset: function (conn, done) { | ||
min: 5, | ||
max: 15, | ||
reset: function(conn, done) { | ||
conn.query('ROLLBACK', done) | ||
} | ||
}, | ||
} | ||
@@ -194,5 +197,5 @@ var pool = new ConnectionPool(adapter, connectionParams, poolParams) | ||
[any-db-postgres]: https://github.com/grncdr/node-any-db-postgres | ||
[Adapter]: https://github.com/grncdr/node-any-db-adapter-spec#adapter | ||
[createPool]: https://github.com/grncdr/node-any-db#exportscreatepool | ||
[Queryable.query]: https://github.com/grncdr/node-any-db-adapter-spec#queryablequery | ||
[Query]: https://github.com/grncdr/node-any-db-adapter-spec#query | ||
[adapter]: https://github.com/grncdr/node-any-db-adapter-spec#adapter | ||
[createpool]: https://github.com/grncdr/node-any-db#exportscreatepool | ||
[queryable.query]: https://github.com/grncdr/node-any-db-adapter-spec#queryablequery | ||
[query]: https://github.com/grncdr/node-any-db-adapter-spec#query |
@@ -5,11 +5,13 @@ var test = require('tape') | ||
test('adapter.reset hook', function (t) { | ||
test('adapter.reset hook', function(t) { | ||
t.plan(1) | ||
var pool = ConnectionPool(mockAdapter({ | ||
reset: function (conn, done) { | ||
t.pass("adapter.reset was called") | ||
done() | ||
} | ||
})) | ||
var pool = ConnectionPool( | ||
mockAdapter({ | ||
reset: function(conn, done) { | ||
t.pass('adapter.reset was called') | ||
done() | ||
}, | ||
}), | ||
) | ||
@@ -20,15 +22,17 @@ pool.query('select 1') | ||
test('adapter.reset hook errors', function (t) { | ||
test('adapter.reset hook errors', function(t) { | ||
t.plan(2) | ||
var pool = ConnectionPool(mockAdapter({ | ||
reset: function (conn, done) { | ||
t.pass("adapter.reset was called") | ||
done(new Error("ruh-roh!")) | ||
} | ||
})) | ||
var pool = ConnectionPool( | ||
mockAdapter({ | ||
reset: function(conn, done) { | ||
t.pass('adapter.reset was called') | ||
done(new Error('ruh-roh!')) | ||
}, | ||
}), | ||
) | ||
var destroy = pool.destroy | ||
pool.destroy = function (connection) { | ||
pool.destroy = function(connection) { | ||
t.pass('connection was destroyed') | ||
@@ -35,0 +39,0 @@ destroy.call(this, connection) |
@@ -1,33 +0,45 @@ | ||
var test = require('tape') | ||
var makeChain = require('../lib/chain') | ||
test('lib/chain', function (t) { | ||
test('lib/chain', function(t) { | ||
makeChain([ | ||
function (thing, next) { thing.prop = 1; next(); }, | ||
function (thing, next) { thing.prop2 = 2; next(); } | ||
])({}, function (err, thing) { | ||
t.equal(thing.prop, 1, "step 1 called"); | ||
t.equal(thing.prop2, 2, "step 2 called"); | ||
}); | ||
function(thing, next) { | ||
thing.prop = 1 | ||
next() | ||
}, | ||
function(thing, next) { | ||
thing.prop2 = 2 | ||
next() | ||
}, | ||
])({}, function(err, thing) { | ||
t.equal(thing.prop, 1, 'step 1 called') | ||
t.equal(thing.prop2, 2, 'step 2 called') | ||
}) | ||
makeChain([ | ||
function (thing, next) { thing.prop = 1; next('error'); }, | ||
function (thing, next) { /* never executed */ } | ||
])({}, function (err, thing) { | ||
t.equal(err, 'error'); | ||
t.ok(!thing, "errors short circuit"); | ||
}); | ||
function(thing, next) { | ||
thing.prop = 1 | ||
next('error') | ||
}, | ||
function(thing, next) { | ||
/* never executed */ | ||
}, | ||
])({}, function(err, thing) { | ||
t.equal(err, 'error') | ||
t.ok(!thing, 'errors short circuit') | ||
}) | ||
makeChain([])('passthru', function (err, thing) { | ||
t.equal(thing, 'passthru', "empty chain is a pass-through"); | ||
}); | ||
makeChain([])('passthru', function(err, thing) { | ||
t.equal(thing, 'passthru', 'empty chain is a pass-through') | ||
}) | ||
makeChain([ | ||
function (thing, next) { throw "error"; } | ||
])('passthru', function (err, thing) { | ||
t.equal(err, 'error', "thrown errors are caught and forwarded"); | ||
}); | ||
function(thing, next) { | ||
throw 'error' | ||
}, | ||
])('passthru', function(err, thing) { | ||
t.equal(err, 'error', 'thrown errors are caught and forwarded') | ||
}) | ||
t.end() | ||
}) |
@@ -8,3 +8,3 @@ // https://github.com/grncdr/node-any-db-pool/issues/3 | ||
test('pool close callback', {keepOpen: true}, function (t) { | ||
test('pool close callback', { keepOpen: true }, function(t) { | ||
t.plan(3) | ||
@@ -14,12 +14,14 @@ | ||
connection: { | ||
end: function () { t.pass("Connection.end was called") } | ||
} | ||
end: function() { | ||
t.pass('Connection.end was called') | ||
}, | ||
}, | ||
}) | ||
var pool = new ConnectionPool(adapter) | ||
pool.on('close', function () { | ||
pool.on('close', function() { | ||
t.pass('"close" was emitted') | ||
}) | ||
pool.acquire(function (err, conn) { | ||
pool.acquire(function(err, conn) { | ||
if (err) throw err | ||
@@ -29,3 +31,3 @@ pool.release(conn) | ||
pool.close(function (err) { | ||
pool.close(function(err) { | ||
if (err) throw err | ||
@@ -32,0 +34,0 @@ t.pass('Pool close callback was called') |
@@ -5,9 +5,13 @@ var test = require('tape') | ||
test('Connection error forwarding', function (t) { | ||
test('Connection error forwarding', function(t) { | ||
// A stub adapter errors on connect | ||
var pool = ConnectionPool(mockAdapter({ | ||
createConnection: function (_, callback) { | ||
process.nextTick(function () { callback(new Error("Blammo")) }) | ||
} | ||
})) | ||
var pool = ConnectionPool( | ||
mockAdapter({ | ||
createConnection: function(_, callback) { | ||
process.nextTick(function() { | ||
callback(new Error('Blammo')) | ||
}) | ||
}, | ||
}), | ||
) | ||
@@ -17,29 +21,28 @@ t.plan(2) | ||
t.test('Connection errors in pool.query', function (t) { | ||
t.plan(6); | ||
t.test('Connection errors in pool.query', function(t) { | ||
t.plan(6) | ||
pool.query('This is not valid SQL', function(err) { | ||
t.assert(err, "Error should be passed to callback when there are no params") | ||
t.equal('Blammo', err.message, "Got expected error") | ||
}); | ||
t.assert(err, 'Error should be passed to callback when there are no params') | ||
t.equal('Blammo', err.message, 'Got expected error') | ||
}) | ||
pool.query('This is not valid SQL', [], function(err) { | ||
t.assert(err, "Error should be passed to callback when there are params") | ||
t.equal('Blammo', err.message, "Got expected error") | ||
}); | ||
t.assert(err, 'Error should be passed to callback when there are params') | ||
t.equal('Blammo', err.message, 'Got expected error') | ||
}) | ||
pool.query('Still invalid SQL').on('error', function (err) { | ||
t.assert(err, "Error should be emitted when there is no callback") | ||
t.equal('Blammo', err.message, "Got expected error") | ||
pool.query('Still invalid SQL').on('error', function(err) { | ||
t.assert(err, 'Error should be emitted when there is no callback') | ||
t.equal('Blammo', err.message, 'Got expected error') | ||
}) | ||
}) | ||
}); | ||
t.test('Connection errors in pool.acquire', function (t) { | ||
t.test('Connection errors in pool.acquire', function(t) { | ||
t.plan(2) | ||
pool.acquire(function (err, conn) { | ||
t.assert(err, "Error is forwarded to callback") | ||
t.equal('Blammo', err.message, "Got expected error") | ||
pool.acquire(function(err, conn) { | ||
t.assert(err, 'Error is forwarded to callback') | ||
t.equal('Blammo', err.message, 'Got expected error') | ||
}) | ||
}) | ||
}) |
@@ -8,3 +8,3 @@ var EventEmitter = require('events').EventEmitter | ||
test("Pool.query queues when no connections available", function (t) { | ||
test('Pool.query queues when no connections available', function(t) { | ||
t.plan(8) | ||
@@ -19,11 +19,13 @@ | ||
// if queries are executed by the connection we expect. | ||
createConnection: function (_, callback) { | ||
var connection = extend(new EventEmitter, { | ||
createConnection: function(_, callback) { | ||
var connection = extend(new EventEmitter(), { | ||
id: ++connectionCount, | ||
query: function (q) { | ||
t.equal(++receivedQueries, q.id, "query " + q.id + " executed in order") | ||
t.equal(q.expectedConnectionId, this.id, "query " + q.id + " used connection " + this.id) | ||
process.nextTick(function () { q.emit('end') }) | ||
query: function(q) { | ||
t.equal(++receivedQueries, q.id, 'query ' + q.id + ' executed in order') | ||
t.equal(q.expectedConnectionId, this.id, 'query ' + q.id + ' used connection ' + this.id) | ||
process.nextTick(function() { | ||
q.emit('end') | ||
}) | ||
}, | ||
end: function () {} | ||
end: function() {}, | ||
}) | ||
@@ -34,11 +36,11 @@ callback(null, connection) | ||
// We abuse createQuery to pass in our expected connection ID for each query | ||
createQuery: function (connectionId) { | ||
var q = new EventEmitter | ||
createQuery: function(connectionId) { | ||
var q = new EventEmitter() | ||
q.id = ++queryCount | ||
q.expectedConnectionId = connectionId | ||
return q | ||
} | ||
}, | ||
} | ||
var pool = new ConnectionPool(adapter, "", {max: 2}) | ||
var pool = new ConnectionPool(adapter, '', { max: 2 }) | ||
@@ -45,0 +47,0 @@ // Instead of a query, we pass the expected connection id |
@@ -1,2 +0,1 @@ | ||
var test = require('tape') | ||
@@ -6,3 +5,3 @@ var ConnectionPool = require('../') | ||
test('adapter.reset hook', function (t) { | ||
test('adapter.reset hook', function(t) { | ||
t.plan(1) | ||
@@ -12,6 +11,6 @@ | ||
pool.acquire(function (err, conn) { | ||
pool.acquire(function(err, conn) { | ||
pool.release(conn) | ||
setTimeout(function () { | ||
setTimeout(function() { | ||
conn.emit('error', new Error('expected message')) | ||
@@ -21,3 +20,3 @@ }, 10) | ||
pool.on('error', function (err) { | ||
pool.on('error', function(err) { | ||
t.equal(err.message, 'expected message') | ||
@@ -24,0 +23,0 @@ pool.close() |
var test = require('tape') | ||
var ConnectionPool = require('../') | ||
test('Illegal pool options', function (t) { | ||
test('Illegal pool options', function(t) { | ||
t.plan(2) | ||
var warn = console.warn | ||
console.warn = t.pass.bind(t, "printed warning") | ||
ConnectionPool({}, {}, {create: function () {}}) | ||
ConnectionPool({}, {}, {destroy: function () {}}) | ||
console.warn = t.pass.bind(t, 'printed warning') | ||
ConnectionPool({}, {}, { create: function() {} }) | ||
ConnectionPool({}, {}, { destroy: function() {} }) | ||
console.warn = warn | ||
}) |
@@ -5,22 +5,22 @@ var test = require('tape') | ||
test('ConnectionPool onConnect/reset hooks', function (t) { | ||
test('ConnectionPool onConnect/reset hooks', function(t) { | ||
// Create a pool with 2 connections maximum. | ||
// each connection will be initialized once and reset twice | ||
t.plan(6) | ||
var connectCount = 2, resetCount = 4 | ||
var connectCount = 2, | ||
resetCount = 4 | ||
var pool = ConnectionPool(mockAdapter(), "", { | ||
var pool = ConnectionPool(mockAdapter(), '', { | ||
max: 2, | ||
onConnect: function (conn, ready) { | ||
onConnect: function(conn, ready) { | ||
t.ok(connectCount-- > 0, 'onConnect called') | ||
ready(null, conn) | ||
}, | ||
reset: function (conn, ready) { | ||
reset: function(conn, ready) { | ||
t.ok(resetCount-- > 0, 'reset called') | ||
ready() | ||
} | ||
}, | ||
}) | ||
;[1, 2, 3, 4].forEach(function () { | ||
pool.acquire(function (err, conn) { | ||
;[1, 2, 3, 4].forEach(function() { | ||
pool.acquire(function(err, conn) { | ||
process.nextTick(pool.release.bind(pool, conn)) | ||
@@ -27,0 +27,0 @@ }) |
@@ -1,2 +0,1 @@ | ||
var test = require('tape') | ||
@@ -6,18 +5,17 @@ var ConnectionPool = require('../') | ||
test('ConnectionPool onConnect/reset hooks', function (t) { | ||
test('ConnectionPool onConnect/reset hooks', function(t) { | ||
// Create a pool with 2 connections maximum. | ||
// each connection will be initialized once and reset twice | ||
t.plan(2) | ||
var pool = new ConnectionPool(mockAdapter(), "", { | ||
onConnect: function (conn, ready) { | ||
var pool = new ConnectionPool(mockAdapter(), '', { | ||
onConnect: function(conn, ready) { | ||
ready(new Error('expected message')) | ||
} | ||
}, | ||
}) | ||
pool.acquire(function (err, connection) { | ||
t.equal(err.message, "expected message") | ||
t.ok(!connection, "No connection") | ||
pool.acquire(function(err, connection) { | ||
t.equal(err.message, 'expected message') | ||
t.ok(!connection, 'No connection') | ||
pool.close() | ||
}) | ||
}) |
@@ -5,5 +5,5 @@ var test = require('tape') | ||
test('ConnectionPool.prototype.cancel', function (t) { | ||
var pool = ConnectionPool(mockAdapter(), "") | ||
var query = pool.query("SELECT 1 FROM some_table", callback) | ||
test('ConnectionPool.prototype.cancel', function(t) { | ||
var pool = ConnectionPool(mockAdapter(), '') | ||
var query = pool.query('SELECT 1 FROM some_table', callback) | ||
@@ -14,6 +14,6 @@ // cancel the query synchronously | ||
function callback(err, result) { | ||
t.ok(err, "Expected CancelledQueryError, but no error received") | ||
t.equal(''+err, "CancelledQueryError: Query was cancelled before connection was acquired") | ||
t.ok(err, 'Expected CancelledQueryError, but no error received') | ||
t.equal('' + err, 'CancelledQueryError: Query was cancelled before connection was acquired') | ||
t.end() | ||
} | ||
}) |
@@ -6,19 +6,23 @@ var test = require('tape') | ||
test('Unhandled query errors are emitted by pool', function (t) { | ||
var pool = ConnectionPool(mockAdapter({ | ||
connection: { | ||
query: function (q) { | ||
process.nextTick(function () { q.emit('error', new Error('dang')) }) | ||
return q | ||
} | ||
} | ||
})) | ||
test('Unhandled query errors are emitted by pool', function(t) { | ||
var pool = ConnectionPool( | ||
mockAdapter({ | ||
connection: { | ||
query: function(q) { | ||
process.nextTick(function() { | ||
q.emit('error', new Error('dang')) | ||
}) | ||
return q | ||
}, | ||
}, | ||
}), | ||
) | ||
t.plan(2) | ||
pool.query("whatever") | ||
pool.on('error', function (err) { | ||
pool.query('whatever') | ||
pool.on('error', function(err) { | ||
t.pass("pool emitted 'error' event") | ||
t.equal(err.message, 'dang', "got expected error") | ||
t.equal(err.message, 'dang', 'got expected error') | ||
}) | ||
pool.close() | ||
}) |
@@ -6,11 +6,11 @@ var test = require('tape') | ||
test("Pool query events", function (t) { | ||
var expected = 'SELECT 1'; | ||
test('Pool query events', function(t) { | ||
var expected = 'SELECT 1' | ||
t.plan(1) | ||
var pool = new ConnectionPool(mockAdapter(), "") | ||
var pool = new ConnectionPool(mockAdapter(), '') | ||
pool.on('query', function onQuery(query) { | ||
t.equal(query.text, expected, "emitted query") | ||
t.equal(query.text, expected, 'emitted query') | ||
}) | ||
@@ -17,0 +17,0 @@ |
@@ -5,19 +5,23 @@ var test = require('tape') | ||
test('options.shouldDestroyConnection', function (t) { | ||
test('options.shouldDestroyConnection', function(t) { | ||
t.plan(1) | ||
var pool = new ConnectionPool(mockAdapter({ | ||
connection: { | ||
query: function (query) { | ||
query.callback(new Error("the expected error")) | ||
} | ||
var pool = new ConnectionPool( | ||
mockAdapter({ | ||
connection: { | ||
query: function(query) { | ||
query.callback(new Error('the expected error')) | ||
}, | ||
}, | ||
}), | ||
{}, | ||
{ | ||
shouldDestroyConnection: function(err) { | ||
return false | ||
}, | ||
}, | ||
}), {}, { | ||
shouldDestroyConnection: function (err) { | ||
return false | ||
} | ||
}) | ||
) | ||
pool.query('select 1', function (err, result) { | ||
t.equal(err.message, "the expected error") | ||
pool.query('select 1', function(err, result) { | ||
t.equal(err.message, 'the expected error') | ||
}) | ||
@@ -24,0 +28,0 @@ |
@@ -5,3 +5,3 @@ var test = require('tape') | ||
test('options.refreshIdle', function (t) { | ||
test('options.refreshIdle', function(t) { | ||
t.plan(2) | ||
@@ -11,20 +11,24 @@ | ||
connection: { | ||
end: function () { | ||
end: function() { | ||
t.pass('Connection.end was called') | ||
} | ||
} | ||
}, | ||
}, | ||
}) | ||
var pool = new ConnectionPool(adapter, {}, { | ||
min: 1, | ||
max: 2, | ||
refreshIdle: true, | ||
// these are extremely short intervals, it's not recommended to use a | ||
// reapInterval or idleTimeout of less than a second in practice. | ||
reapInterval: 5, | ||
idleTimeout: 10 | ||
}) | ||
var pool = new ConnectionPool( | ||
adapter, | ||
{}, | ||
{ | ||
min: 1, | ||
max: 2, | ||
refreshIdle: true, | ||
// these are extremely short intervals, it's not recommended to use a | ||
// reapInterval or idleTimeout of less than a second in practice. | ||
reapInterval: 5, | ||
idleTimeout: 10, | ||
}, | ||
) | ||
setTimeout(function () { | ||
setTimeout(function() { | ||
pool.close() | ||
}, 15) | ||
}) |
@@ -5,16 +5,15 @@ var test = require('tape') | ||
test('A sqlite3://:memory: pool can only have one connection', function (t) { | ||
test('A sqlite3://:memory: pool can only have one connection', function(t) { | ||
t.plan(2) | ||
var warn = console.warn | ||
console.warn = t.pass.bind(t, "printed a warning") | ||
console.warn = t.pass.bind(t, 'printed a warning') | ||
var adapter = mockAdapter({ name: 'sqlite3' }) | ||
var connParams = { database: ':memory:' } | ||
var adapter = mockAdapter({name: 'sqlite3'}) | ||
var connParams = {database: ':memory:'} | ||
ConnectionPool(adapter, connParams, { min: 3 }).close() | ||
ConnectionPool(adapter, connParams, { max: 3 }).close() | ||
ConnectionPool(adapter, connParams, {min: 3}).close() | ||
ConnectionPool(adapter, connParams, {max: 3}).close() | ||
console.warn = warn | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
594
195
25146
20