Permalink
Browse files

Support legacy single-argument callbacks to acquire().

  • Loading branch information...
1 parent 1cb9db9 commit a3b65a371e53599d5439df66566b6c1eff03f80e @yhahn yhahn committed May 18, 2011
Showing with 76 additions and 6 deletions.
  1. +32 −5 lib/generic-pool.js
  2. +44 −1 test/generic-pool.test.js
View
@@ -174,6 +174,20 @@ exports.Pool = function (factory) {
}
/**
+ * Handle callbacks with either the [obj] or [err, obj] arguments in an
+ * adaptive manner. Uses the `cb.length` property to determine the number
+ * of arguments expected by `cb`.
+ */
+ function adjustCallback(cb, err, obj) {
+ if (!cb) return;
+ if (cb.length <= 1) {
+ cb(obj);
+ } else {
+ cb(err, obj);
+ }
+ };
+
+ /**
* Try to get a new client to work, and clean up pool unused (idle) items.
*
* - If there are available clients waiting shift the first one out (LIFO),
@@ -185,23 +199,36 @@ exports.Pool = function (factory) {
*/
function dispense() {
var obj = null,
+ err = null,
waitingCount = waitingClients.size();
log("dispense() clients=" + waitingCount + " available=" + availableObjects.length);
if (waitingCount > 0) {
if (availableObjects.length > 0) {
log("dispense() - reusing obj");
objWithTimeout = availableObjects.shift();
- waitingClients.dequeue()(null, objWithTimeout.obj);
+ adjustCallback(waitingClients.dequeue(), err, objWithTimeout.obj);
}
else if (count < factory.max) {
count += 1;
log("dispense() - creating obj - count=" + count);
- factory.create(function (err, obj) {
+ factory.create(function () {
var cb = waitingClients.dequeue();
- if (cb) {
- cb(err, obj);
+ if (arguments.length > 1) {
+ err = arguments[0];
+ obj = arguments[1];
+ } else {
+ err = (arguments[0] instanceof Error) ? arguments[0] : null;
+ obj = (arguments[0] instanceof Error) ? null : arguments[0];
+ }
+ if (err) {
+ count -= 1;
+ adjustCallback(cb, err, obj);
} else {
- me.release(obj);
+ if (cb) {
+ adjustCallback(cb, err, obj);
+ } else {
+ me.release(obj);
+ }
}
});
}
View
@@ -1,5 +1,5 @@
var assert = require('assert');
-var poolModule = require('generic-pool');
+var poolModule = require('..');
module.exports = {
@@ -149,6 +149,49 @@ module.exports = {
assert.throws(function() {
pool.acquire(function(client) {});
}, Error);
+ },
+
+ 'supports single arg callbacks' : function (beforeExit) {
+ var pool = poolModule.Pool({
+ name : 'test5',
+ create : function(callback) { callback({ id : 1 }); },
+ destroy : function(client) { destroyed.push(client.id); },
+ max : 2,
+ idleTimeoutMillis : 100
+ });
+
+ pool.acquire(function(client) {
+ assert.equal(client.id, 1);
+ });
+ },
+
+ 'handle creation errors' : function (beforeExit) {
+ var created = 0;
+ var pool = poolModule.Pool({
+ name : 'test6',
+ create : function(callback) {
+ if (created < 5) {
+ callback(new Error('Error occurred.'));
+ } else {
+ callback({ id : created });
+ }
+ created++;
+ },
+ destroy : function(client) { },
+ max : 1,
+ idleTimeoutMillis : 1000
+ });
+ // ensure that creation errors do not populate the pool.
+ for (var i = 0; i < 5; i++) {
+ pool.acquire(function(err, client) {
+ assert.ok(err instanceof Error);
+ assert.ok(client === null);
+ });
+ }
+ pool.acquire(function(err, client) {
+ assert.ok(err === null);
+ assert.equal(typeof client.id, 'number');
+ });
}
};

0 comments on commit a3b65a3

Please sign in to comment.