pax_global_header00006660000000000000000000000064120756650430014522gustar00rootroot0000000000000052 comment=be063d9e620d084807fdbf40d978dece04f2d44d node-pool-2.0.3/000077500000000000000000000000001207566504300134205ustar00rootroot00000000000000node-pool-2.0.3/.gitignore000066400000000000000000000000701207566504300154050ustar00rootroot00000000000000fabfile.pyc node-pool.iml node-pool.tmproj node_modules node-pool-2.0.3/.travis.yml000066400000000000000000000000521207566504300155260ustar00rootroot00000000000000language: node_js node_js: - 0.6 - 0.8node-pool-2.0.3/Makefile000066400000000000000000000000271207566504300150570ustar00rootroot00000000000000all: check: npm test node-pool-2.0.3/README.md000066400000000000000000000312711207566504300147030ustar00rootroot00000000000000[![build status](https://secure.travis-ci.org/coopernurse/node-pool.png)](http://travis-ci.org/coopernurse/node-pool) # About Generic resource pool. Can be used to reuse or throttle expensive resources such as database connections. ## 2.0 Release Warning The 2.0.0 release removed support for variable argument callbacks. When you acquire a resource from the pool, your callback *must* accept two arguments: (err, obj) Previously this library attempted to determine the arity of the callback, but this resulted in a variety of issues. This change eliminates these issues, and makes the acquire callback parameter order consistent with the factory.create callback. ## Installation $ npm install generic-pool ## History 2.0.3 - January 16 2013 - Merged #56/#57 - Add optional refreshIdle flag. If false, idle resources at the pool minimum will not be destroyed/re-created. (contributed by wshaver) - Merged #54 - Factory can be asked to validate pooled objects (contributed by tikonen) 2.0.2 - October 22 2012 - Fix #51, #48 - createResource() should check for null clientCb in err case (contributed by pooyasencha) - Merged #52 - fix bug of infinite wait when create object aync error (contributed by windyrobin) - Merged #53 - change the position of dispense and callback to ensure the time order (contributed by windyrobin) 2.0.1 - August 29 2012 - Fix #44 - leak of 'err' and 'obj' in createResource() - Add devDependencies block to package.json - Add travis-ci.org integration 2.0.0 - July 31 2012 - Non-backwards compatible change: remove adjustCallback - acquire() callback must accept two params: (err, obj) - Add optional 'min' param to factory object that specifies minimum number of resources to keep in pool - Merged #38 (package.json/Makefile changes - contributed by strk) 1.0.12 - June 27 2012 - Merged #37 (Clear remove idle timer after destroyAllNow - contributed by dougwilson) 1.0.11 - June 17 2012 - Merged #36 ("pooled" method to perform function decoration for pooled methods - contributed by cosbynator) 1.0.10 - May 3 2012 - Merged #35 (Remove client from availbleObjects on destroy(client) - contributed by blax) 1.0.9 - Dec 18 2011 - Merged #25 (add getName() - contributed by BryanDonovan) - Merged #27 (remove sys import - contributed by botker) - Merged #26 (log levels - contributed by JoeZ99) 1.0.8 - Nov 16 2011 - Merged #21 (add getter methods to see pool size, etc. - contributed by BryanDonovan) 1.0.7 - Oct 17 2011 - Merged #19 (prevent release on the same obj twice - contributed by tkrynski) - Merged #20 (acquire() returns boolean indicating whether pool is full - contributed by tilgovi) 1.0.6 - May 23 2011 - Merged #13 (support error variable in acquire callback - contributed by tmcw) - Note: This change is backwards compatible. But new code should use the two parameter callback format in pool.create() functions from now on. - Merged #15 (variable scope issue in dispense() - contributed by eevans) 1.0.5 - Apr 20 2011 - Merged #12 (ability to drain pool - contributed by gdusbabek) 1.0.4 - Jan 25 2011 - Fixed #6 (objects reaped with undefined timeouts) - Fixed #7 (objectTimeout issue) 1.0.3 - Dec 9 2010 - Added priority queueing (thanks to sylvinus) - Contributions from Poetro - Name changes to match conventions described here: http://en.wikipedia.org/wiki/Object_pool_pattern - borrow() renamed to acquire() - returnToPool() renamed to release() - destroy() removed from public interface - added JsDoc comments - Priority queueing enhancements 1.0.2 - Nov 9 2010 - First NPM release ## Example ### Step 1 - Create pool using a factory object // Create a MySQL connection pool with // a max of 10 connections, a min of 2, and a 30 second max idle time var poolModule = require('generic-pool'); var pool = poolModule.Pool({ name : 'mysql', create : function(callback) { var Client = require('mysql').Client; var c = new Client(); c.user = 'scott'; c.password = 'tiger'; c.database = 'mydb'; c.connect(); // parameter order: err, resource // new in 1.0.6 callback(null, c); }, destroy : function(client) { client.end(); }, max : 10, // optional. if you set this, make sure to drain() (see step 3) min : 2, // specifies how long a resource can stay idle in pool before being removed idleTimeoutMillis : 30000, // if true, logs via console.log - can also be a function log : true }); ### Step 2 - Use pool in your code to acquire/release resources // acquire connection - callback function is called // once a resource becomes available pool.acquire(function(err, client) { if (err) { // handle error - this is generally the err from your // factory.create function } else { client.query("select * from foo", [], function() { // return object back to pool pool.release(client); }); } }); ### Step 3 - Drain pool during shutdown (optional) If you are shutting down a long-lived process, you may notice that node fails to exit for 30 seconds or so. This is a side effect of the idleTimeoutMillis behavior -- the pool has a setTimeout() call registered that is in the event loop queue, so node won't terminate until all resources have timed out, and the pool stops trying to manage them. This behavior will be more problematic when you set factory.min > 0, as the pool will never become empty, and the setTimeout calls will never end. In these cases, use the pool.drain() function. This sets the pool into a "draining" state which will gracefully wait until all idle resources have timed out. For example, you can call: // Only call this once in your application -- at the point you want // to shutdown and stop using this pool. pool.drain(function() { pool.destroyAllNow(); }); If you do this, your node process will exit gracefully. ## Documentation Pool() accepts an object with these slots: name : name of pool (string, optional) create : function that returns a new resource should call callback() with the created resource destroy : function that accepts a resource and destroys it max : maximum number of resources to create at any given time optional (default=1) min : minimum number of resources to keep in pool at any given time if this is set > max, the pool will silently set the min to factory.max - 1 optional (default=0) refreshIdle : boolean that specifies whether idle resources at or below the min threshold should be destroyed/re-created. optional (default=true) idleTimeoutMillis : max milliseconds a resource can go unused before it should be destroyed (default 30000) reapIntervalMillis : frequency to check for idle resources (default 1000), priorityRange : int between 1 and x - if set, borrowers can specify their relative priority in the queue if no resources are available. see example. (default 1) validate : function that accepts a pooled resource and returns true if the resource is OK to use, or false if the object is invalid. Invalid objects will be destroyed. This function is called in acquire() before returning a resource from the pool. Optional. Default function always returns true. log : true/false or function - If a log is a function, it will be called with two parameters: - log string - log level ('verbose', 'info', 'warn', 'error') Else if log is true, verbose log info will be sent to console.log() Else internal log messages be ignored (this is the default) ## Priority Queueing The pool now supports optional priority queueing. This becomes relevant when no resources are available and the caller has to wait. `acquire()` accepts an optional priority int which specifies the caller's relative position in the queue. // create pool with priorityRange of 3 // borrowers can specify a priority 0 to 2 var pool = poolModule.Pool({ name : 'mysql', create : function(callback) { // do something }, destroy : function(client) { // cleanup. omitted for this example }, max : 10, idleTimeoutMillis : 30000, priorityRange : 3 }); // acquire connection - no priority - will go at end of line pool.acquire(function(err, client) { pool.release(client); }); // acquire connection - high priority - will go into front slot pool.acquire(function(err, client) { pool.release(client); }, 0); // acquire connection - medium priority - will go into middle slot pool.acquire(function(err, client) { pool.release(client); }, 1); // etc.. ## Draining If you know would like to terminate all the resources in your pool before their timeouts have been reached, you can use `destroyAllNow()` in conjunction with `drain()`: pool.drain(function() { pool.destroyAllNow(); }); One side-effect of calling `drain()` is that subsequent calls to `acquire()` will throw an Error. ## Pooled function decoration To transparently handle object acquisition for a function, one can use `pooled()`: var privateFn, publicFn; publicFn = pool.pooled(privateFn = function(client, arg, cb) { // Do something with the client and arg. Client is auto-released when cb is called cb(null, arg); }); Keeping both private and public versions of each function allows for pooled functions to call other pooled functions with the same member. This is a handy pattern for database transactions: var privateTop, privateBottom, publicTop, publicBottom; publicBottom = pool.pooled(privateBottom = function(client, arg, cb) { //Use client, assumed auto-release }); publicTop = pool.pooled(privateTop = function(client, cb) { // e.g., open a database transaction privateBottom(client, "arg", function(err, retVal) { if(err) { return cb(err); } // e.g., close a transaction cb(); }); }); ## Pool info The following functions will let you get information about the pool: // returns factory.name for this pool pool.getName() // returns number of resources in the pool regardless of // whether they are free or in use pool.getPoolSize() // returns number of unused resources in the pool pool.availableObjectsCount() // returns number of callers waiting to acquire a resource pool.waitingClientsCount() ## Run Tests $ npm install expresso $ expresso -I lib test/*.js ## License (The MIT License) Copyright (c) 2010-2013 James Cooper <james@bitmechanic.com> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. node-pool-2.0.3/fabfile.py000066400000000000000000000014441207566504300153650ustar00rootroot00000000000000# # dependencies: # fabric (apt-get install fabric) # node-jslint (http://github.com/reid/node-jslint) # expresso (or replace with whatever node.js test tool you're using) # from fabric.api import local import os, os.path def test(): local('expresso -I lib test/*', capture=False) def jslint(): ignore = [ "/lib-cov/" ] for root, subFolders, files in os.walk("."): for file in files: if file.endswith(".js"): filename = os.path.join(root,file) processFile = True for i in ignore: if filename.find(i) != -1: processFile = False if processFile: print filename local('jslint %s' % filename, capture=False) node-pool-2.0.3/lib/000077500000000000000000000000001207566504300141665ustar00rootroot00000000000000node-pool-2.0.3/lib/generic-pool.js000066400000000000000000000337421207566504300171200ustar00rootroot00000000000000var PriorityQueue = function(size) { var me = {}, slots, i, total = null; // initialize arrays to hold queue elements size = Math.max(+size | 0, 1); slots = []; for (i = 0; i < size; i += 1) { slots.push([]); } // Public methods me.size = function () { var i; if (total === null) { total = 0; for (i = 0; i < size; i += 1) { total += slots[i].length; } } return total; }; me.enqueue = function (obj, priority) { var priorityOrig; // Convert to integer with a default value of 0. priority = priority && + priority | 0 || 0; // Clear cache for total. total = null; if (priority) { priorityOrig = priority; if (priority < 0 || priority >= size) { priority = (size - 1); // put obj at the end of the line console.error("invalid priority: " + priorityOrig + " must be between 0 and " + priority); } } slots[priority].push(obj); }; me.dequeue = function (callback) { var obj = null, i, sl = slots.length; // Clear cache for total. total = null; for (i = 0; i < sl; i += 1) { if (slots[i].length) { obj = slots[i].shift(); break; } } return obj; }; return me; }; /** * Generate an Object pool with a specified `factory`. * * @param {Object} factory * Factory to be used for generating and destorying the items. * @param {String} factory.name * Name of the factory. Serves only logging purposes. * @param {Function} factory.create * Should create the item to be acquired, * and call it's first callback argument with the generated item as it's argument. * @param {Function} factory.destroy * Should gently close any resources that the item is using. * Called before the items is destroyed. * @param {Function} factory.validate * Should return true if connection is still valid and false * If it should be removed from pool. Called before item is * acquired from pool. * @param {Number} factory.max * Maximum number of items that can exist at the same time. Default: 1. * Any further acquire requests will be pushed to the waiting list. * @param {Number} factory.min * Minimum number of items in pool (including in-use). Default: 0. * When the pool is created, or a resource destroyed, this minimum will * be checked. If the pool resource count is below the minimum, a new * resource will be created and added to the pool. * @param {Number} factory.idleTimeoutMillis * Delay in milliseconds after the idle items in the pool will be destroyed. * And idle item is that is not acquired yet. Waiting items doesn't count here. * @param {Number} factory.reapIntervalMillis * Cleanup is scheduled in every `factory.reapIntervalMillis` milliseconds. * @param {Boolean|Function} factory.log * Whether the pool should log activity. If function is specified, * that will be used instead. The function expects the arguments msg, loglevel * @param {Number} factory.priorityRange * The range from 1 to be treated as a valid priority * @param {RefreshIdle} factory.refreshIdle * Should idle resources be destroyed and recreated every idleTimeoutMillis? Default: true. * @returns {Object} An Object pool that works with the supplied `factory`. */ exports.Pool = function (factory) { var me = {}, idleTimeoutMillis = factory.idleTimeoutMillis || 30000, reapInterval = factory.reapIntervalMillis || 1000, refreshIdle = ('refreshIdle' in factory) ? factory.refreshIdle : true, availableObjects = [], waitingClients = new PriorityQueue(factory.priorityRange || 1), count = 0, removeIdleScheduled = false, removeIdleTimer = null, draining = false, // Prepare a logger function. log = factory.log ? (function (str, level) { if (typeof factory.log === 'function') { factory.log(str, level); } else { console.log(level.toUpperCase() + " pool " + factory.name + " - " + str); } } ) : function () {}; factory.validate = factory.validate || function() { return true; }; factory.max = parseInt(factory.max, 10); factory.min = parseInt(factory.min, 10); factory.max = Math.max(isNaN(factory.max) ? 1 : factory.max, 1); factory.min = Math.min(isNaN(factory.min) ? 0 : factory.min, factory.max-1); /////////////// /** * Request the client to be destroyed. The factory's destroy handler * will also be called. * * This should be called within an acquire() block as an alternative to release(). * * @param {Object} obj * The acquired item to be destoyed. */ me.destroy = function(obj) { count -= 1; availableObjects = availableObjects.filter(function(objWithTimeout) { return (objWithTimeout.obj !== obj); }); factory.destroy(obj); ensureMinimum(); }; /** * Checks and removes the available (idle) clients that have timed out. */ function removeIdle() { var toRemove = [], now = new Date().getTime(), i, al, tr, timeout; removeIdleScheduled = false; // Go through the available (idle) items, // check if they have timed out for (i = 0, al = availableObjects.length; i < al && (refreshIdle || (count - factory.min)) > toRemove.length ; i += 1) { timeout = availableObjects[i].timeout; if (now >= timeout) { // Client timed out, so destroy it. log("removeIdle() destroying obj - now:" + now + " timeout:" + timeout, 'verbose'); toRemove.push(availableObjects[i].obj); } } for (i = 0, tr = toRemove.length; i < tr; i += 1) { me.destroy(toRemove[i]); } // Replace the available items with the ones to keep. al = availableObjects.length; if (al > 0) { log("availableObjects.length=" + al, 'verbose'); scheduleRemoveIdle(); } else { log("removeIdle() all objects removed", 'verbose'); } } /** * Schedule removal of idle items in the pool. * * More schedules cannot run concurrently. */ function scheduleRemoveIdle() { if (!removeIdleScheduled) { removeIdleScheduled = true; removeIdleTimer = setTimeout(removeIdle, reapInterval); } } /** * 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), * and call its callback. * - If there are no waiting clients, try to create one if it won't exceed * the maximum number of clients. * - If creating a new client would exceed the maximum, add the client to * the wait list. */ function dispense() { var obj = null, objWithTimeout = null, err = null, clientCb = null, waitingCount = waitingClients.size(); log("dispense() clients=" + waitingCount + " available=" + availableObjects.length, 'info'); if (waitingCount > 0) { while (availableObjects.length > 0) { log("dispense() - reusing obj", 'verbose'); objWithTimeout = availableObjects[0]; if (!factory.validate(objWithTimeout.obj)) { me.destroy(objWithTimeout.obj); continue; } availableObjects.shift(); clientCb = waitingClients.dequeue(); return clientCb(err, objWithTimeout.obj); } if (count < factory.max) { createResource(); } } } function createResource() { count += 1; log("createResource() - creating obj - count=" + count + " min=" + factory.min + " max=" + factory.max, 'verbose'); factory.create(function () { var err, obj; var clientCb = waitingClients.dequeue(); 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; if (clientCb) { clientCb(err, obj); } process.nextTick(function(){ dispense(); }); } else { if (clientCb) { clientCb(err, obj); } else { me.release(obj); } } }); } function ensureMinimum() { var i, diff; if (!draining && (count < factory.min)) { diff = factory.min - count; for (i = 0; i < diff; i++) { createResource(); } } } /** * Request a new client. The callback will be called, * when a new client will be availabe, passing the client to it. * * @param {Function} callback * Callback function to be called after the acquire is successful. * The function will receive the acquired item as the first parameter. * * @param {Number} priority * Optional. Integer between 0 and (priorityRange - 1). Specifies the priority * of the caller if there are no available resources. Lower numbers mean higher * priority. * * @returns {Object} `true` if the pool is not fully utilized, `false` otherwise. */ me.acquire = function (callback, priority) { if (draining) { throw new Error("pool is draining and cannot accept work"); } waitingClients.enqueue(callback, priority); dispense(); return (count < factory.max); }; me.borrow = function (callback, priority) { log("borrow() is deprecated. use acquire() instead", 'warn'); me.acquire(callback, priority); }; /** * Return the client to the pool, in case it is no longer required. * * @param {Object} obj * The acquired object to be put back to the pool. */ me.release = function (obj) { // check to see if this object has already been released (i.e., is back in the pool of availableObjects) if (availableObjects.some(function(objWithTimeout) { return (objWithTimeout.obj === obj); })) { log("release called twice for the same resource: " + (new Error().stack), 'error'); return; } //log("return to pool"); var objWithTimeout = { obj: obj, timeout: (new Date().getTime() + idleTimeoutMillis) }; availableObjects.push(objWithTimeout); log("timeout: " + objWithTimeout.timeout, 'verbose'); dispense(); scheduleRemoveIdle(); }; me.returnToPool = function (obj) { log("returnToPool() is deprecated. use release() instead", 'warn'); me.release(obj); }; /** * Disallow any new requests and let the request backlog dissapate. * * @param {Function} callback * Optional. Callback invoked when all work is done and all clients have been * released. */ me.drain = function(callback) { log("draining", 'info'); // disable the ability to put more work on the queue. draining = true; var check = function() { if (waitingClients.size() > 0) { // wait until all client requests have been satisfied. setTimeout(check, 100); } else if (availableObjects.length != count) { // wait until all objects have been released. setTimeout(check, 100); } else { if (callback) { callback(); } } }; check(); }; /** * Forcibly destroys all clients regardless of timeout. Intended to be * invoked as part of a drain. Does not prevent the creation of new * clients as a result of subsequent calls to acquire. * * Note that if factory.min > 0, the pool will destroy all idle resources * in the pool, but replace them with newly created resources up to the * specified factory.min value. If this is not desired, set factory.min * to zero before calling destroyAllNow() * * @param {Function} callback * Optional. Callback invoked after all existing clients are destroyed. */ me.destroyAllNow = function(callback) { log("force destroying all objects", 'info'); var willDie = availableObjects; availableObjects = []; var obj = willDie.shift(); while (obj !== null && obj !== undefined) { me.destroy(obj.obj); obj = willDie.shift(); } removeIdleScheduled = false; clearTimeout(removeIdleTimer); if (callback) { callback(); } }; /** * Decorates a function to use a acquired client from the object pool when called. * * @param {Function} decorated * The decorated function, accepting a client as the first argument and * (optionally) a callback as the final argument. * * @param {Number} priority * Optional. Integer between 0 and (priorityRange - 1). Specifies the priority * of the caller if there are no available resources. Lower numbers mean higher * priority. */ me.pooled = function(decorated, priority) { return function() { var callerArgs = arguments; var callerCallback = callerArgs[callerArgs.length - 1]; var callerHasCallback = typeof callerCallback === 'function'; me.acquire(function(err, client) { if(err) { if(callerHasCallback) { callerCallback(err); } return; } var args = [client].concat(Array.prototype.slice.call(callerArgs, 0, callerHasCallback ? -1 : undefined)); args.push(function() { me.release(client); if(callerHasCallback) { callerCallback.apply(null, arguments); } }); decorated.apply(null, args); }, priority); }; }; me.getPoolSize = function() { return count; }; me.getName = function() { return factory.name; }; me.availableObjectsCount = function() { return availableObjects.length; }; me.waitingClientsCount = function() { return waitingClients.size(); }; // create initial resources (if factory.min > 0) ensureMinimum(); return me; }; node-pool-2.0.3/package.json000066400000000000000000000016021207566504300157050ustar00rootroot00000000000000{ "name": "generic-pool", "description": "Generic resource pooling for Node.JS", "version": "2.0.3", "author": "James Cooper ", "contributors": [ { "name": "James Cooper", "email": "james@bitmechanic.com" }, { "name": "Peter Galiba", "email": "poetro@poetro.hu", "url": "http://poetro.hu/" }, { "name": "Gary Dusbabek" }, { "name": "Tom MacWright", "url" : "http://www.developmentseed.org/" }, { "name": "Douglas Christopher Wilson", "email": "doug@somethingdoug.com", "url" : "http://somethingdoug.com/" } ], "keywords": ["pool", "pooling", "throttle"], "main": "lib/generic-pool.js", "repository": { "type": "git", "url": "http://github.com/coopernurse/node-pool.git" }, "devDependencies": { "expresso": ">0.0.0" }, "engines": { "node": ">= 0.2.0" }, "scripts": { "test": "expresso -I lib test/*.js" } } node-pool-2.0.3/test/000077500000000000000000000000001207566504300143775ustar00rootroot00000000000000node-pool-2.0.3/test/generic-pool.test.js000066400000000000000000000467451207566504300203160ustar00rootroot00000000000000var assert = require('assert'); var poolModule = require('..'); module.exports = { 'expands to max limit' : function (beforeExit) { var createCount = 0; var destroyCount = 0; var borrowCount = 0; var factory = { name : 'test1', create : function(callback) { callback(null, { count: ++createCount }); }, destroy : function(client) { destroyCount++; }, max : 2, idleTimeoutMillis : 100 }; var pool = poolModule.Pool(factory); for (var i = 0; i < 10; i++) { var full = !pool.acquire(function(err, obj) { return function(err, obj) { assert.equal(typeof obj.count, 'number'); setTimeout(function() { borrowCount++; pool.release(obj); }, 100); }; }()); assert.ok((i < 1) ^ full); } beforeExit(function() { assert.equal(0, factory.min); assert.equal(2, createCount); assert.equal(2, destroyCount); assert.equal(10, borrowCount); }); }, 'respects min limit' : function (beforeExit) { var createCount = 0; var destroyCount = 0; var borrowCount = 0; var pool = poolModule.Pool({ name : 'test-min', create : function(callback) { callback(null, { count: ++createCount }); }, destroy : function(client) { destroyCount++; }, min : 1, max : 2, idleTimeoutMillis : 100 }); pool.drain(); beforeExit(function() { assert.equal(0, pool.availableObjectsCount()); assert.equal(1, createCount); assert.equal(1, destroyCount); }); }, 'min and max limit defaults' : function (beforeExit) { var factory = { name : "test-limit-defaults", create : function(callback) { callback(null, {}); }, destroy : function(client) { }, idleTimeoutMillis: 100 }; var pool = poolModule.Pool(factory); beforeExit(function() { assert.equal(1, factory.max); assert.equal(0, factory.min); }); }, 'malformed min and max limits are ignored' : function (beforeExit) { var factory = { name : "test-limit-defaults2", create : function(callback) { callback(null, {}); }, destroy : function(client) { }, idleTimeoutMillis: 100, min : "asf", max : [ ] }; var pool = poolModule.Pool(factory); beforeExit(function() { assert.equal(1, factory.max); assert.equal(0, factory.min); }); }, 'min greater than max sets to max minus one' : function (beforeExit) { var factory = { name : "test-limit-defaults3", create : function(callback) { callback(null, {}); }, destroy : function(client) { }, idleTimeoutMillis: 100, min : 5, max : 3 }; var pool = poolModule.Pool(factory); pool.drain(); beforeExit(function() { assert.equal(3, factory.max); assert.equal(2, factory.min); }); }, 'supports priority on borrow' : function(beforeExit) { var borrowTimeLow = 0; var borrowTimeHigh = 0; var borrowCount = 0; var i; var pool = poolModule.Pool({ name : 'test2', create : function(callback) { callback(); }, destroy : function(client) { }, max : 1, idleTimeoutMillis : 100, priorityRange : 2 }); for (i = 0; i < 10; i++) { pool.acquire(function(err, obj) { return function() { setTimeout(function() { var t = new Date().getTime(); if (t > borrowTimeLow) { borrowTimeLow = t; } borrowCount++; pool.release(obj); }, 50); }; }(), 1); } for (i = 0; i < 10; i++) { pool.acquire(function(obj) { return function() { setTimeout(function() { var t = new Date().getTime(); if (t > borrowTimeHigh) { borrowTimeHigh = t; } borrowCount++; pool.release(obj); }, 50); }; }(), 0); } beforeExit(function() { assert.equal(20, borrowCount); assert.equal(true, borrowTimeLow > borrowTimeHigh); }); }, 'removes correct object on reap' : function (beforeExit) { var destroyed = []; var clientCount = 0; var pool = poolModule.Pool({ name : 'test3', create : function(callback) { callback(null, { id : ++clientCount }); }, destroy : function(client) { destroyed.push(client.id); }, max : 2, idleTimeoutMillis : 100 }); pool.acquire(function(err, client) { assert.equal(typeof client.id, 'number'); // should be removed second setTimeout(function() { pool.release(client); }, 5); }); pool.acquire(function(err, client) { assert.equal(typeof client.id, 'number'); // should be removed first pool.release(client); }); setTimeout(function() { }, 102); beforeExit(function() { assert.equal(2, destroyed[0]); assert.equal(1, destroyed[1]); }); }, 'tests drain' : function (beforeExit) { var created = 0; var destroyed = 0; var count = 5; var acquired = 0; var pool = poolModule.Pool({ name : 'test4', create : function(callback) { callback(null, {id: ++created}); }, destroy : function(client) { destroyed += 1; }, max : 2, idletimeoutMillis : 300000 }); for (var i = 0; i < count; i++) { pool.acquire(function(err, client) { acquired += 1; assert.equal(typeof client.id, 'number'); setTimeout(function() { pool.release(client); }, 250); }); } assert.notEqual(count, acquired); pool.drain(function() { assert.equal(count, acquired); // short circuit the absurdly long timeouts above. pool.destroyAllNow(); beforeExit(function() {}); }); // subsequent calls to acquire should return an error. assert.throws(function() { pool.acquire(function(client) {}); }, Error); }, '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); }); } var called = false; pool.acquire(function(err, client) { assert.ok(err === null); assert.equal(typeof client.id, 'number'); called = true; }); beforeExit(function() { assert.ok(called); assert.equal(pool.waitingClientsCount(), 0); }); }, 'handle creation errors for delayed creates' : function (beforeExit) { var created = 0; var pool = poolModule.Pool({ name : 'test6', create : function(callback) { if (created < 5) { setTimeout(function() { callback(new Error('Error occurred.')); }, 0); } else { setTimeout(function() { callback({ id : created }); }, 0); } 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); }); } var called = false; pool.acquire(function(err, client) { assert.ok(err === null); assert.equal(typeof client.id, 'number'); called = true; }); beforeExit(function() { assert.ok(called); assert.equal(pool.waitingClientsCount(), 0); }); }, 'pooled decorator should acquire and release' : function (beforeExit) { var assertion_count = 0; var destroyed_count = 0; var pool = poolModule.Pool({ name : 'test1', create : function(callback) { callback({id: Math.floor(Math.random()*1000)}); }, destroy : function(client) { destroyed_count += 1; }, max : 1, idleTimeoutMillis : 100 }); var pooledFn = pool.pooled(function(client, cb) { assert.equal(typeof client.id, 'number'); assert.equal(pool.getPoolSize(), 1); assertion_count += 2; cb(); }); assert.equal(pool.getPoolSize(), 0); assertion_count += 1; pooledFn(function(err) { if (err) { throw err; } assert.ok(true); assertion_count += 1; }); beforeExit(function() { assert.equal(assertion_count, 4); assert.equal(destroyed_count, 1); }); }, 'pooled decorator should pass arguments and return values' : function(beforeExit) { var assertion_count = 0; var pool = poolModule.Pool({ name : 'test1', create : function(callback) { callback({id: Math.floor(Math.random()*1000)}); }, destroy : function(client) { }, max : 1, idleTimeoutMillis : 100 }); var pooledFn = pool.pooled(function(client, arg1, arg2, cb) { assert.equal(arg1, "First argument"); assert.equal(arg2, "Second argument"); assertion_count += 2; cb(null, "First return", "Second return"); }); pooledFn("First argument", "Second argument", function(err, retVal1, retVal2) { if(err) { throw err; } assert.equal(retVal1, "First return"); assert.equal(retVal2, "Second return"); assertion_count += 2; }); beforeExit(function() { assert.equal(assertion_count, 4); }); }, 'pooled decorator should allow undefined callback' : function(beforeExit) { var assertion_count = 0; var pool = poolModule.Pool({ name : 'test1', create : function(callback) { callback({id: Math.floor(Math.random()*1000)}); }, destroy : function(client) { }, max : 1, idleTimeoutMillis : 100 }); var pooledFn = pool.pooled(function(client, arg, cb) { assert.equal(arg, "Arg!"); assertion_count += 1; cb(); }); pooledFn("Arg!"); beforeExit(function() { assert.equal(pool.getPoolSize(), 0); assert.equal(assertion_count, 1); }); }, 'pooled decorator should forward pool errors' : function(beforeExit) { var assertion_count = 0; var pool = poolModule.Pool({ name : 'test1', create : function(callback) { callback(new Error('Pool error')); }, destroy : function(client) { }, max : 1, idleTimeoutMillis : 100 }); var pooledFn = pool.pooled(function(cb) { assert.ok(false, "Pooled function shouldn't be called due to a pool error"); }); pooledFn(function(err, obj) { assert.equal(err.message, 'Pool error'); assertion_count += 1; }); beforeExit(function() { assert.equal(assertion_count, 1); }); }, 'getPoolSize' : function (beforeExit) { var assertion_count = 0; var pool = poolModule.Pool({ name : 'test1', create : function(callback) { callback({id: Math.floor(Math.random()*1000)}); }, destroy : function(client) { }, max : 2, idleTimeoutMillis : 100 }); assert.equal(pool.getPoolSize(), 0); assertion_count += 1; pool.acquire(function(err, obj1) { if (err) { throw err; } assert.equal(pool.getPoolSize(), 1); assertion_count += 1; pool.acquire(function(err, obj2) { if (err) { throw err; } assert.equal(pool.getPoolSize(), 2); assertion_count += 1; pool.release(obj1); pool.release(obj2); pool.acquire(function(err, obj3) { if (err) { throw err; } // should still be 2 assert.equal(pool.getPoolSize(), 2); assertion_count += 1; pool.release(obj3); }); }); }); beforeExit(function() { assert.equal(assertion_count, 4); }); }, 'availableObjectsCount' : function (beforeExit) { var assertion_count = 0; var pool = poolModule.Pool({ name : 'test1', create : function(callback) { callback({id: Math.floor(Math.random()*1000)}); }, destroy : function(client) { }, max : 2, idleTimeoutMillis : 100 }); assert.equal(pool.availableObjectsCount(), 0); assertion_count += 1; pool.acquire(function(err, obj1) { if (err) { throw err; } assert.equal(pool.availableObjectsCount(), 0); assertion_count += 1; pool.acquire(function(err, obj2) { if (err) { throw err; } assert.equal(pool.availableObjectsCount(), 0); assertion_count += 1; pool.release(obj1); assert.equal(pool.availableObjectsCount(), 1); assertion_count += 1; pool.release(obj2); assert.equal(pool.availableObjectsCount(), 2); assertion_count += 1; pool.acquire(function(err, obj3) { if (err) { throw err; } assert.equal(pool.availableObjectsCount(), 1); assertion_count += 1; pool.release(obj3); assert.equal(pool.availableObjectsCount(), 2); assertion_count += 1; }); }); }); beforeExit(function() { assert.equal(assertion_count, 7); }); }, 'logPassesLogLevel': function(beforeExit){ var loglevels = {'verbose':0, 'info':1, 'warn':2, 'error':3}; var logmessages = {verbose:[], info:[], warn:[], error:[]}; var factory = { name : 'test1', create : function(callback) {callback(null, {id:Math.floor(Math.random()*1000)}); }, destroy : function(client) {}, max : 2, idleTimeoutMillis: 100, log : function(msg, level) {testlog(msg, level);} }; var testlog = function(msg, level){ assert.ok(level in loglevels); logmessages[level].push(msg); }; var pool = poolModule.Pool(factory); var pool2 = poolModule.Pool({ name : 'testNoLog', create : function(callback) {callback(null, {id:Math.floor(Math.random()*1000)}); }, destroy : function(client) {}, max : 2, idleTimeoutMillis: 100 }); assert.equal(pool2.getName(), 'testNoLog'); pool.acquire(function(err, obj){ if (err) {throw err;} assert.equal(logmessages.verbose[0], 'createResource() - creating obj - count=1 min=0 max=2'); assert.equal(logmessages.info[0], 'dispense() clients=1 available=0'); logmessages.info = []; logmessages.verbose = []; pool2.borrow(function(err, obj){ assert.equal(logmessages.info.length, 0); assert.equal(logmessages.verbose.length, 0); assert.equal(logmessages.warn.length, 0); }); }); }, 'removes from available objects on destroy': function(beforeExit){ var destroyCalled = false; var factory = { name: 'test', create: function(callback) {callback(null, {}); }, destroy: function(client) {destroyCalled = true; }, max: 2, idleTimeoutMillis: 100 }; var pool = poolModule.Pool(factory); pool.acquire(function(err, obj){ pool.destroy(obj); }); assert.equal(destroyCalled, true); assert.equal(pool.availableObjectsCount(), 0); }, 'removes from available objects on validation failure': function(beforeExit){ var destroyCalled = false, validateCalled = false, count = 0; var factory = { name: 'test', create: function(callback) {callback(null, {count: count++}); }, destroy: function(client) {destroyCalled = client.count; }, validate: function(client) {validateCalled = true; return client.count != 0;}, max: 2, idleTimeoutMillis: 100 }; var pool = poolModule.Pool(factory); pool.acquire(function(err, obj){ pool.release(obj); assert.equal(obj.count, 0); pool.acquire(function(err, obj){ pool.release(obj); assert.equal(obj.count, 1); }); }); assert.equal(validateCalled, true); assert.equal(destroyCalled, 0); assert.equal(pool.availableObjectsCount(), 1); }, 'do schedule again if error occured when creating new Objects async': function(beforeExit){ var factory = { name: 'test', create: function(callback) { process.nextTick(function(){ var err = new Error('Create Error'); callback(err); }) }, destroy: function(client) {}, max: 1, idleTimeoutMillis: 100 }; var getFlag = 0; var pool = poolModule.Pool(factory); pool.acquire(function(){}); pool.acquire(function(err, obj){ getFlag = 1; assert(err); assert.equal(pool.availableObjectsCount(), 0); }); beforeExit(function() { assert.equal(getFlag, 1); }); } };