[Pkg-javascript-commits] [node-generic-pool] 04/11: New upstream version 3.1.1

Jérémy Lal kapouer at moszumanska.debian.org
Sat Nov 12 11:43:50 UTC 2016


This is an automated email from the git hooks/post-receive script.

kapouer pushed a commit to branch master
in repository node-generic-pool.

commit ac103bb963cee3948d6f31114140244e098e8a46
Author: Jérémy Lal <kapouer at melix.org>
Date:   Sat Nov 12 12:31:32 2016 +0100

    New upstream version 3.1.1
---
 .eslintrc.js                             |   6 +
 .travis.yml                              |  18 +-
 CHANGELOG.md                             | 148 ++++++++
 Makefile                                 |  14 +
 README.md                                | 548 ++++++++++++++-------------
 fabfile.py                               |  27 --
 index.js                                 |  14 +
 lib/DefaultEvictor.js                    |  19 +
 lib/Deferred.js                          |  50 +++
 lib/Deque.js                             | 106 ++++++
 lib/DequeIterator.js                     |  20 +
 lib/DoublyLinkedList.js                  |  94 +++++
 lib/DoublyLinkedListIterator.js          |  93 +++++
 lib/Pool.js                              | 578 +++++++++++++++++++++++++++++
 lib/PoolDefaults.js                      |  33 ++
 lib/PoolOptions.js                       |  79 ++++
 lib/PooledResource.js                    |  49 +++
 lib/PooledResourceStateEnum.js           |  11 +
 lib/PriorityQueue.js                     |  68 ++++
 lib/Queue.js                             |  35 ++
 lib/ResourceLoan.js                      |  29 ++
 lib/ResourceRequest.js                   |  72 ++++
 lib/errors.js                            |  26 ++
 lib/factoryValidator.js                  |  14 +
 lib/generic-pool.js                      | 467 -----------------------
 package.json                             |  80 +++-
 test/doubly-linked-list-iterator-test.js | 139 +++++++
 test/doubly-linked-list-test.js          |  28 ++
 test/generic-pool-acquiretimeout-test.js |  72 ++++
 test/generic-pool-test.js                | 600 ++++++++++++++++++++++++++++++
 test/generic-pool.test.js                | 619 -------------------------------
 test/resource-request-test.js            |  60 +++
 test/utils.js                            |  38 ++
 33 files changed, 2872 insertions(+), 1382 deletions(-)

diff --git a/.eslintrc.js b/.eslintrc.js
new file mode 100644
index 0000000..614c344
--- /dev/null
+++ b/.eslintrc.js
@@ -0,0 +1,6 @@
+module.exports = {
+    "extends": "standard",
+    "plugins": [
+        "standard"
+    ]
+};
\ No newline at end of file
diff --git a/.travis.yml b/.travis.yml
index 8111245..5ed63c5 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,4 +1,18 @@
 language: node_js
 node_js:
-  - 0.6
-  - 0.8
\ No newline at end of file
+  - "4"
+  - "5"
+  - "6"
+  - "7"
+
+install:
+  - make install
+
+script:
+  - make lint
+  - make test
+
+sudo: false
+
+matrix:
+  fast_finish: true
diff --git a/CHANGELOG.md b/CHANGELOG.md
new file mode 100644
index 0000000..f0f16cc
--- /dev/null
+++ b/CHANGELOG.md
@@ -0,0 +1,148 @@
+# Change Log
+
+## [3.1.0] - November 6 2016
+- Inject dependencies into Pool to allow easier user extension
+
+## [3.0.1] - November 1 2016
+- Passthrough Pool's promise impl to deferreds so they are used internally and exposed correctly on pool.acquire (@eide)
+
+## [3.0.0] - October 30 2016
+- This is pretty big and the migration guide in the README has more detailed set of changes!
+- switch support to nodejs v4 and above
+- change external interfaces to use promises instead of callbacks
+- remove logging
+- decouple create/destroy operations from acquire/release operations
+- pool should now be created via `poolCreate` factory method instead of constructor.
+- huge internal rewrite and flow control changes
+- Pool is now an eventEmitter
+
+## [2.4.3] - October 15 2016
+- Use domain.bind to preserve domain context (@LewisJEllis)
+
+## [2.4.2] - March 26 2016
+- Travis now runs and fails lint checks (@kevinburke)
+- fixed bug #128 where using async validation incorrectly tracked resource state (@johnjdooley and @robfyfe)
+- fixed broken readme example that had aged badly
+
+## [2.4.1] - February 20 2016
+- Documented previously created/fixed bug #122 (thanks @jasonrhodes)
+- Improved Makefile and test runner docs thanks (@kevinburke)
+- fixed bug documented in #121 where pool could make incorrect decisions about which resources were eligible for removal. (thanks @mikemorris)
+
+## [2.4.0] - January 18 2016
+- Merged #118 - closes #110 - optional eslinting for test and lib using "standard" ruleset
+- Merged #114 - closes #113 - "classes" now used internally instead of object literals and exports support being called as a constructor (along with old factory behaviour) (contributed by @felixfbecker)
+- Move history from README.md to CHANGELOG.md and reformat
+- Closes #122 - fixes trapped connection bug when destroying a connection while others are in use
+
+## [2.3.1] - January 7 2016
+- Documentation fixes and widened number of nodejs versions tested on travis
+
+## [2.3.0] - January 1 2016
+- Merged #105 - allow asynchronous validate functions (contributed by @felipou)
+
+## [2.2.2] - December 13 2015
+- Merged #106 - fix condition where non "resource pool" created objects could be returned to the pool. (contributed by @devzer01)
+
+## [2.2.1] - October 30 2015
+- Merged #104 - fix #103 - condition where pool can create > specified max number of connections (contributed by @devzer01)
+
+## [2.2.0] - March 26 2015
+- Merged #92 - add getMaxPoolSize function (contributed by platypusMaximus)
+
+## [2.1.1] - July 5 2015
+- fix README error about priority queueing (spotted by @kmdm)
+
+## [2.1.0] - June 19 2014
+- Merged #72 - Add optional returnToHead flag, if true, resources are returned to head of queue (stack like behaviour) upon release (contributed by calibr), also see #68 for further discussion.
+
+## [2.0.4] - July 27 2013
+- Merged #64 - Fix for not removing idle objects (contributed by PiotrWpl)
+
+## [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
+
+=======
+[unreleased]: https://github.com/coopernurse/node-pool/compare/v3.1.0...HEAD
+[3.1.0]: https://github.com/coopernurse/node-pool/compare/v3.0.1...v3.1.0
+[3.0.1]: https://github.com/coopernurse/node-pool/compare/v3.0.0...v3.0.1
+[3.0.0]: https://github.com/coopernurse/node-pool/compare/v2.4.3...v3.0.0
+[2.4.3]: https://github.com/coopernurse/node-pool/compare/v2.4.2...v2.4.3
+[2.4.2]: https://github.com/coopernurse/node-pool/compare/v2.4.1...v2.4.2
+[2.4.1]: https://github.com/coopernurse/node-pool/compare/v2.4.0...v2.4.1
+[2.4.0]: https://github.com/coopernurse/node-pool/compare/v2.3.1...v2.4.0
+[2.3.1]: https://github.com/coopernurse/node-pool/compare/v2.3.0...v2.3.1
+[2.3.0]: https://github.com/coopernurse/node-pool/compare/v2.2.2...v2.3.0
+[2.2.2]: https://github.com/coopernurse/node-pool/compare/v2.2.1...v2.2.2
+[2.2.1]: https://github.com/coopernurse/node-pool/compare/v2.2.0...v2.2.1
+[2.2.0]: https://github.com/coopernurse/node-pool/compare/v2.1.1...v2.2.0
+[2.1.1]: https://github.com/coopernurse/node-pool/compare/v2.1.0...v2.1.1
+[2.1.0]: https://github.com/coopernurse/node-pool/compare/v2.0.4...v2.1.0
+[2.0.4]: https://github.com/coopernurse/node-pool/compare/v2.0.3...v2.0.4
+[2.0.3]: https://github.com/coopernurse/node-pool/compare/v2.0.2...v2.0.3
+[2.0.2]: https://github.com/coopernurse/node-pool/compare/v2.0.1...v2.0.2
+[2.0.1]: https://github.com/coopernurse/node-pool/compare/v2.0.0...v2.0.1
+[2.0.0]: https://github.com/coopernurse/node-pool/compare/v1.0.12...v2.0.0
diff --git a/Makefile b/Makefile
index 669888d..405a7a4 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,18 @@
+.PHONY: all clean install check test lint
+
 all:
 
+clean:
+	rm -rf node_modules
+
+install:
+	npm install
+
 check:
 	npm test
+
+test:
+	npm test
+
+lint:
+	npm run lint
diff --git a/README.md b/README.md
index 15d1a40..153ec1c 100644
--- a/README.md
+++ b/README.md
@@ -1,309 +1,347 @@
 [![build status](https://secure.travis-ci.org/coopernurse/node-pool.png)](http://travis-ci.org/coopernurse/node-pool)
 
-# About
+# Generic Pool
 
-  Generic resource pool.  Can be used to reuse or throttle expensive resources such as
-  database connections.
-  
-## 2.0 Release Warning
+## About
 
-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)
+  Generic resource pool with Promise based API. Can be used to reuse or throttle usage of expensive resources such as database connections.
 
-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
-    
+**V3 upgrade warning**
+
+Version 3 contains many breaking changes. The differences are mostly minor and I hope easy to accomodate. There is a very rough and basic [upgrade guide](https://gist.github.com/sandfox/5ca20648b60a0cb959638c0cd6fcd02d) I've written, improvements and other attempts most welcome.
+
+If you are after the older version 2 of this library you should look at the [current github branch](https://github.com/coopernurse/node-pool/tree/v2.4) for it.
+
+
 ## 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
+The history has been moved to the [CHANGELOG](CHANGELOG.md)
+
+
+## Installation
+
+```sh
+$ npm install generic-pool [--save]
+```
+
 
 ## 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)
-    
+Here is an example using a fictional generic database driver that doesn't implement any pooling whatsoever itself.
+
+```js
+var genericPool = require('generic-pool');
+var DbDriver = require('some-db-driver');
+
+/**
+ * Step 1 - Create pool using a factory object
+ */
+const factory = {
+    create: function(){
+		 return new Promise(function(resolve, reject{
+	        var client = DbDriver.createClient()
+	        client.on('connected', function(){
+	            resolve(client)
+	        })
+	    })
+    }
+    destroy: function(client){
+        return new Promise(function(resolve){
+          client.on('end', function(){
+            resolve()
+          })
+          client.disconnect()
+        })
+    }
+}
+
+var opts = {
+    max: 10, // maximum size of the pool
+    min: 2 // minimum size of the pool
+}
+
+var myPool = genericPool.createPool(factory, opts)
+
+/**
+ * Step 2 - Use pool in your code to acquire/release resources
+ */
+
+// acquire connection - Promise is resolved
+// once a resource becomes available
+const resourcePromise = myPool.acquire()
+
+resourcePromise.then(function(client) {
+	client.query("select * from foo", [], function() {
+	    // return object back to pool
+	    pool.release(client);
+	});
+})
+.catch(function(err){
+   // handle error - this is generally a timeout or maxWaitingClients 
+   // error
+});
+
+/**
+ * Step 3 - Drain pool during shutdown (optional)
+ */
+// Only call this once in your application -- at the point you want
+// to shutdown and stop using this pool.
+pool.drain(function() {
+    pool.clear();
+});
+
+```
+
+
+## Documentation
+
+
+
+### Creating a pool
+
+Whilst it is possible to directly instantiate the Pool class directly, it is recommended to use the `createPool` function exported by module as the constructor method signature may change in the future.
+
+### createPool
+
+The createPool function takes two arguments:
+
+- `factory` :  an object containing functions to create/destroy/test resources for the `Pool`
+- `opts` : an optional object/dictonary to allow configuring/altering behaviour the of the `Pool`
+
+```js
+const genericPool = require('generic-pool')
+const pool = genericPool.createPool(factory, opts)
+```
+
+**factory**
+
+Can be any object/instance but must have the following properties:
+
+- `create` : a function that the pool will call when it wants a new resource. It should return a Promise that either resolves to a `resource` or rejects to an `Error` if it is unable to create a resourse for whatever.
+- `destroy`: a function that the pool will call when it wants to destroy a resource. It should accept one argument `resource` where `resource` is whatever `factory.create` made. The `destroy` function should return a `Promise` that resolves once it has destroyed the resource.
+
+
+optionally it can also have the following property:
+
+- `validate`: a function that the pool will call if it wants to validate a resource. It should accept one argument `resource` where `resource` is whatever `factory.create` made. Should return a `Promise` that resolves a `boolean` where `true` indicates the resource is still valid or `false` if the resource is invalid. 
+
+_Note: The values returned from `create`, `destroy`, and `validate` are all wrapped in a `Promise.resolve` by the pool before being used internally._
+
+**opts**
+
+An optional object/dictionary with the any of the following properties: 
+
+- `max`: maximum number of resources to create at any given time. (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 equal `max`. (default=0)
+- `maxWaitingClients`: maximum number of queued requests allowed, additional `acquire` calls will be callback with an `err` in a future cycle of the event loop.
+- `testOnBorrow`: `boolean`: should the pool validate resources before giving them to clients. Requires that either `factory.validate` or `factory.validateAsync` to be specified
+- `idleTimeoutMillis`: max milliseconds a resource can stay unused in the pool without being borrowed before it should be destroyed (default 30000)
+- `reapIntervalMillis`: interval to check for idle resources (default 1000). (remove me!)
+- `acquireTimeoutMillis`: max milliseconds an `acquire` call will wait for a resource before timing out. (default no limit), if supplied should non-zero positive integer.
+- `fifo` : if true the oldest resources will be first to be allocated. If false the most recently released resources will be the first to be allocated. This in effect turns the pool's behaviour from a queue into a stack. `boolean`, (default true)
+- `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)
+- `autostart`: boolean, should the pool start creating resources etc once the constructor is called, (default true)
+- `evictionRunIntervalMillis`: How often to run eviction checks. Default: 0 (does not run).
+- `numTestsPerRun`: Number of resources to check each eviction run.  Default: 3.
+- `softIdleTimeoutMillis`: amount of time an object may sit idle in the pool before it is eligible for eviction by the idle object evictor (if any), with the extra condition that at least "min idle" object instances remain in the pool. Default -1 (nothing can get evicted)
+- `idleTimeoutMillis`: the minimum amount of time that an object may sit idle in the pool before it is eligible for eviction due to idle time. Supercedes `softIdleTimeoutMillis` Default: 30000
+- `Promise`: Promise lib, a Promises/A+ implementation that the pool should use. Defaults to whatever `global.Promise` is (usually native promises).
+
+### pool.acquire
+
+```js
+const onfulfilled = function(resource){
+	resource.doStuff()
+	// release/destroy/etc
+}
+
+pool.acquire().then(onfulfilled)
+//or
+const priority = 2
+pool.acquire(priority).then(onfulfilled)
+```
+
+This function is for when you want to "borrow" a resource from the pool.
+
+`acquire` takes one optional argument:
+
+- `priority`: optional, number, see **Priority Queueing** below.
+
+and returns a `Promise`
+Once a resource in the pool is available, the promise will be resolved with a `resource` (whatever `factory.create` makes for you). If the Pool is unable to give a resource (e.g timeout) then the promise will be rejected with an `Error`
+
+### pool.release
+
+```js
+pool.release(resource)
+```
+
+This function is for when you want to return a resource to the pool.
+
+`release` takes one required argument:
+
+- `resource`: a previously borrowed resource
+
+and returns a `Promise`. This promise will resolve once the `resource` is accepted by the pool, or reject if the pool is unable to accept the `resource` for any reason (e.g `resource` is not a resource or object that came from the pool). If you do not care the outcome it is safe to ignore this promise.
+
+### pool.destroy
+
+This function is for when you want to return a resource to the pool but want it destroyed rather than being made available to other resources. E.g you may know the resource has timed out or crashed.
+
+`destroy` takes one required argument:
+
+- `resource`: a previously borrowed resource
+
+and returns a `Promise`. This promise will resolve once the `resource` is accepted by the pool, or reject if the pool is unable to accept the `resource` for any reason (e.g `resource` is not a resource or object that came from the pool). If you do not care the outcome it is safe to ignore this promise.
+
+### pool.on
+
+The pool is an event emitter. Below are the events it emits and any args for those events
+
+- `factoryCreateError` : emitted when a promise returned by `factory.create` is rejected. If this event has no listeners then the `error` will be silently discarded
+  - `error`: whatever `reason` the promise was rejected with. 
+
+- `factoryDestroyError` : emitted when a promise returned by `factory.destroy` is rejected. If this event has no listeners then the `error` will be silently discarded
+  - `error`: whatever `reason` the promise was rejected with.
+
+## Idle Object Eviction
+
+The pool has an evictor (off by default) which will inspect idle items in the pool and `destroy` them if they are too old.
+
+By default the evictor does not run, to enable it you must set the `evictionRunIntervalMillis` option to a non-zero value. Once enable the evictor will check at most `numTestsPerEvictionRun` each time, this is to stop it blocking your application if you have lots of resources in the pool.
+
+## Draining
+
 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 
+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.  
+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.  
+never end.
 
 In these cases, use the pool.drain() function.  This sets the pool
-into a "draining" state which will gracefully wait until all 
+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)
+If you do this, your node process will exit gracefully.
 
 ## 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..
+The pool 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. Each priority slot has it's own internal queue created for it. When a resource is available for borrowing, the first request in the highest priority queue will be given it.
+
+Specifying a `priority` to `acquire` that is outside the `priorityRange` set at `Pool` creation time will result in the `priority` being converted the lowest possible `priority` 
+
+```js
+// create pool with priorityRange of 3
+// borrowers can specify a priority 0 to 2
+var opts = {
+  priorityRange : 3
+}
+var pool = genericPool.createPool(someFactory,opts);
+
+// acquire connection - no priority specified - will go onto lowest priority queue
+pool.acquire().thenfunction(client) {
+    pool.release(client);
+});
+
+// acquire connection - high priority - will go into highest priority queue
+pool.acquire(0).then(function(client) {
+    pool.release(client);
+});
+
+// acquire connection - medium priority - will go into 'mid' priority queue
+pool.acquire(1).then(function(client) {
+    pool.release(client);
+});
+
+// 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()`:
+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:
+
+If you do this, your node process will exit gracefully.
+
+If you know you would like to terminate all the available resources in your pool before any timeouts they might have are reached, you can use `clear()` in conjunction with `drain()`:
 
-    pool.drain(function() {
-	    pool.destroyAllNow();
-    });
+```js
+const p = pool.drain()
+.then(function() {
+    return pool.clear();
+});
+```
+The `promise` returned will resolve once all waiting clients have acquired and return resources, and any available resources have been destroyed
 
 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();
-        });
-    });
+This has now been extracted out it's own module [generic-pool-decorator](https://github.com/sandfox/generic-pool-decorator)
 
 ## Pool info
 
-The following functions will let you get information about the pool:
+The following properties will let you get information about the pool:
+
+```js
 
-    // 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.size
 
-    // 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.available
 
-    // returns number of unused resources in the pool
-    pool.availableObjectsCount()
+// returns number of callers waiting to acquire a resource
+pool.pending
 
-    // returns number of callers waiting to acquire a resource
-    pool.waitingClientsCount()
+// returns number of maxixmum number of resources allowed by ppol
+pool.max
 
+// returns number of minimum number of resources allowed by ppol
+pool.min
+
+```
 
 ## Run Tests
 
-    $ npm install expresso
-    $ expresso -I lib test/*.js
+    $ npm install
+    $ npm test
+
+The tests are run/written using Tap. Most are ports from the old espresso tests and are not in great condition. Most cases are inside `test/generic-pool-test.js` with newer cases in their own files (legacy reasons).
+
+## Linting
+
+We use eslint and the `standard` ruleset.
+
 
-## License 
+## License
 
 (The MIT License)
 
-Copyright (c) 2010-2013 James Cooper <james at bitmechanic.com>
+Copyright (c) 2010-2016 James Cooper <james at bitmechanic.com>
 
 Permission is hereby granted, free of charge, to any person obtaining
 a copy of this software and associated documentation files (the
diff --git a/fabfile.py b/fabfile.py
deleted file mode 100644
index becfc46..0000000
--- a/fabfile.py
+++ /dev/null
@@ -1,27 +0,0 @@
-#
-#   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)
-
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..fbe6a51
--- /dev/null
+++ b/index.js
@@ -0,0 +1,14 @@
+const Pool = require('./lib/Pool')
+const Deque = require('./lib/Deque')
+const PriorityQueue = require('./lib/PriorityQueue')
+const DefaultEvictor = require('./lib/DefaultEvictor')
+module.exports = {
+  Pool: Pool, 
+  Deque: Deque,
+  PriorityQueue: PriorityQueue,
+  DefaultEvictor: DefaultEvictor,
+  createPool: function(factory, config){
+    return new Pool(DefaultEvictor, Deque, PriorityQueue, factory, config)
+  }
+}
+
diff --git a/lib/DefaultEvictor.js b/lib/DefaultEvictor.js
new file mode 100644
index 0000000..0580160
--- /dev/null
+++ b/lib/DefaultEvictor.js
@@ -0,0 +1,19 @@
+'use strict'
+
+class DefaultEvictor {
+  evict (config, pooledResource, availableObjectsCount) {
+    const idleTime = Date.now() - pooledResource.lastIdleTime
+
+    if (config.softIdleTimeoutMillis < idleTime && config.min < availableObjectsCount) {
+      return true
+    }
+
+    if (config.idleTimeoutMillis < idleTime) {
+      return true
+    }
+
+    return false
+  }
+}
+
+module.exports = DefaultEvictor
diff --git a/lib/Deferred.js b/lib/Deferred.js
new file mode 100644
index 0000000..0ba6550
--- /dev/null
+++ b/lib/Deferred.js
@@ -0,0 +1,50 @@
+'use strict'
+
+/**
+ * This is apparently a bit like a Jquery deferred, hence the name
+ */
+
+class Deferred {
+
+  constructor (Promise) {
+    this._state = Deferred.PENDING
+    this._resolve = undefined
+    this._reject = undefined
+
+    this._promise = new Promise((resolve, reject) => {
+      this._resolve = resolve
+      this._reject = reject
+    })
+  }
+
+  get state () {
+    return this._state
+  }
+
+  get promise () {
+    return this._promise
+  }
+
+  reject (reason) {
+    if (this._state !== Deferred.PENDING) {
+      return
+    }
+    this._state = Deferred.REJECTED
+    this._reject(reason)
+  }
+
+  resolve (value) {
+    if (this._state !== Deferred.PENDING) {
+      return
+    }
+    this._state = Deferred.FULFILLED
+    this._resolve(value)
+  }
+}
+
+// TODO: should these really live here? or be a seperate 'state' enum
+Deferred.PENDING = 'PENDING'
+Deferred.FULFILLED = 'FULFILLED'
+Deferred.REJECTED = 'REJECTED'
+
+module.exports = Deferred
diff --git a/lib/Deque.js b/lib/Deque.js
new file mode 100644
index 0000000..c63b6ed
--- /dev/null
+++ b/lib/Deque.js
@@ -0,0 +1,106 @@
+'use strict'
+
+const DoublyLinkedList = require('./DoublyLinkedList')
+const DequeIterator = require('./DequeIterator')
+/**
+ * DoublyLinkedList backed double ended queue
+ * implements just enough to keep the Pool
+ */
+class Deque {
+  constructor () {
+    this._list = new DoublyLinkedList()
+  }
+
+  /**
+   * removes and returns the first element from the queue
+   * @return {[type]} [description]
+   */
+  shift () {
+    if (this._length === 0) {
+      return undefined
+    }
+
+    const node = this._list.head
+    this._list.remove(node)
+
+    return node.data
+  }
+
+  /**
+   * adds one elemts to the beginning of the queue
+   * @param  {[type]} element [description]
+   * @return {[type]}         [description]
+   */
+  unshift (element) {
+    const node = DoublyLinkedList.createNode(element)
+
+    this._list.insertBeginning(node)
+  }
+
+  /**
+   * adds one to the end of the queue
+   * @param  {[type]} element [description]
+   * @return {[type]}         [description]
+   */
+  push (element) {
+    const node = DoublyLinkedList.createNode(element)
+
+    this._list.insertEnd(node)
+  }
+
+  /**
+   * removes and returns the last element from the queue
+   */
+  pop () {
+    if (this._length === 0) {
+      return undefined
+    }
+
+    const node = this._list.tail
+    this._list.remove(node)
+
+    return node.data
+  }
+
+  [Symbol.iterator] () {
+    return new DequeIterator(this._list)
+  }
+
+  iterator () {
+    return new DequeIterator(this._list)
+  }
+
+  reverseIterator () {
+    return new DequeIterator(this._list, true)
+  }
+
+  /**
+   * get a reference to the item at the head of the queue
+   * @return {element} [description]
+   */
+  get head () {
+    if (this._list.length === 0) {
+      return undefined
+    }
+    const node = this._list.head
+    return node.data
+  }
+
+  /**
+   * get a reference to the item at the tail of the queue
+   * @return {element} [description]
+   */
+  get tail () {
+    if (this._list.length === 0) {
+      return undefined
+    }
+    const node = this._list.tail
+    return node.data
+  }
+
+  get length () {
+    return this._list.length
+  }
+}
+
+module.exports = Deque
diff --git a/lib/DequeIterator.js b/lib/DequeIterator.js
new file mode 100644
index 0000000..fad2d0a
--- /dev/null
+++ b/lib/DequeIterator.js
@@ -0,0 +1,20 @@
+'use strict'
+
+const DoublyLinkedListIterator = require('./DoublyLinkedListIterator')
+/**
+ * Thin wrapper around an underlying DDL iterator
+ */
+class DequeIterator extends DoublyLinkedListIterator {
+  next () {
+    const result = super.next()
+
+    // unwrap the node...
+    if (result.value) {
+      result.value = result.value.data
+    }
+
+    return result
+  }
+}
+
+module.exports = DequeIterator
diff --git a/lib/DoublyLinkedList.js b/lib/DoublyLinkedList.js
new file mode 100644
index 0000000..1aaaa89
--- /dev/null
+++ b/lib/DoublyLinkedList.js
@@ -0,0 +1,94 @@
+'use strict'
+
+/**
+ * A Doubly Linked List, because there aren't enough in the world...
+ * this is pretty much a direct JS port of the one wikipedia
+ * https://en.wikipedia.org/wiki/Doubly_linked_list
+ *
+ * For most usage 'insertBeginning' and 'insertEnd' should be enough
+ *
+ * nodes are expected to something like a POJSO like
+ * {
+ *   prev: null,
+ *   next: null,
+ *   something: 'whatever you like'
+ * }
+ */
+class DoublyLinkedList {
+  constructor () {
+    this.head = null
+    this.tail = null
+    this.length = 0
+  }
+
+  insertBeginning (node) {
+    if (this.head === null) {
+      this.head = node
+      this.tail = node
+      node.prev = null
+      node.next = null
+      this.length++
+    } else {
+      this.insertBefore(this.head, node)
+    }
+  }
+
+  insertEnd (node) {
+    if (this.tail === null) {
+      this.insertBeginning(node)
+    } else {
+      this.insertAfter(this.tail, node)
+    }
+  }
+
+  insertAfter (node, newNode) {
+    newNode.prev = node
+    newNode.next = node.next
+    if (node.next === null) {
+      this.tail = newNode
+    } else {
+      node.next.prev = newNode
+    }
+    node.next = newNode
+    this.length++
+  }
+
+  insertBefore (node, newNode) {
+    newNode.prev = node.prev
+    newNode.next = node
+    if (node.prev === null) {
+      this.head = newNode
+    } else {
+      node.prev.next = newNode
+    }
+    node.prev = newNode
+    this.length++
+  }
+
+  remove (node) {
+    if (node.prev === null) {
+      this.head = node.next
+    } else {
+      node.prev.next = node.next
+    }
+    if (node.next === null) {
+      this.tail = node.prev
+    } else {
+      node.next.prev = node.prev
+    }
+    node.prev = null
+    node.next = null
+    this.length--
+  }
+
+  // FIXME: this should not live here and has become a dumping ground...
+  static createNode (data) {
+    return {
+      prev: null,
+      next: null,
+      data: data
+    }
+  }
+}
+
+module.exports = DoublyLinkedList
diff --git a/lib/DoublyLinkedListIterator.js b/lib/DoublyLinkedListIterator.js
new file mode 100644
index 0000000..21c506e
--- /dev/null
+++ b/lib/DoublyLinkedListIterator.js
@@ -0,0 +1,93 @@
+'use strict'
+
+/**
+ * Creates an interator for a DoublyLinkedList starting at the given node
+ * It's internal cursor will remains relative to the last "iterated" node as that
+ * node moves through the list until it either iterates to the end of the list,
+ * or the the node it's tracking is removed from the list. Until the first 'next'
+ * call it tracks the head/tail of the linked list. This means that one can create
+ * an iterator on an empty list, then add nodes, and then the iterator will follow
+ * those nodes. Because the DoublyLinkedList nodes don't track their owning "list" and
+ * it's highly inefficient to walk the list for every iteration, the iterator won't know
+ * if the node has been detached from one List and added to another list, or if the iterator
+ *
+ * The created object is an es6 compatible iterator
+ */
+class DoublyLinkedListIterator {
+
+  /**
+   * @param  {Object} doublyLinkedListNode a node that is part of a doublyLinkedList
+   * @param  {Boolean} reverse             is this a reverse iterator? default: false
+   */
+  constructor (doublyLinkedList, reverse) {
+    this._list = doublyLinkedList
+    // NOTE: these key names are tied to the DoublyLinkedListIterator
+    this._direction = reverse === true ? 'prev' : 'next'
+    this._startPosition = reverse === true ? 'tail' : 'head'
+    this._started = false
+    this._cursor = null
+    this._done = false
+  }
+
+  _start () {
+    this._cursor = this._list[this._startPosition]
+    this._started = true
+  }
+
+  _advanceCursor () {
+    if (this._started === false) {
+      this._started = true
+      this._cursor = this._list[this._startPosition]
+      return
+    }
+    this._cursor = this._cursor[this._direction]
+  }
+
+  reset () {
+    this._done = false
+    this._started = false
+    this._cursor = null
+  }
+
+  remove () {
+    if (this._started === false || this._done === true || this._isCursorDetached()) {
+      return false
+    }
+    this._list.remove(this._cursor)
+  }
+
+  next () {
+    if (this._done === true) {
+      return { done: true }
+    }
+
+    this._advanceCursor()
+
+    // if there is no node at the cursor or the node at the cursor is no longer part of
+    // a doubly linked list then we are done/finished/kaput
+    if (this._cursor === null || this._isCursorDetached()) {
+      this._done = true
+      return { done: true }
+    }
+
+    return {
+      value: this._cursor,
+      done: false
+    }
+  }
+
+  /**
+   * Is the node detached from a list?
+   * NOTE: you can trick/bypass/confuse this check by removing a node from one DoublyLinkedList
+   * and adding it to another.
+   * TODO: We can make this smarter by checking the direction of travel and only checking
+   * the required next/prev/head/tail rather than all of them
+   * @param  {[type]}  node [description]
+   * @return {Boolean}      [description]
+   */
+  _isCursorDetached () {
+    return this._cursor.prev === null && this._cursor.next === null && this._list.tail !== this._cursor && this._list.head !== this._cursor
+  }
+}
+
+module.exports = DoublyLinkedListIterator
diff --git a/lib/Pool.js b/lib/Pool.js
new file mode 100644
index 0000000..b7abea5
--- /dev/null
+++ b/lib/Pool.js
@@ -0,0 +1,578 @@
+'use strict'
+
+const EventEmitter = require('events').EventEmitter
+
+const factoryValidator = require('./factoryValidator')
+const PoolOptions = require('./PoolOptions')
+const ResourceRequest = require('./ResourceRequest')
+const ResourceLoan = require('./ResourceLoan')
+const PooledResource = require('./PooledResource')
+
+/**
+ * TODO: move me
+ */
+const FACTORY_CREATE_ERROR = 'factoryCreateError'
+const FACTORY_DESTROY_ERROR = 'factoryDestroyError'
+
+class Pool extends EventEmitter {
+
+  /**
+   * Generate an Object pool with a specified `factory` and `config`.
+   *
+   * @param {Object} factory
+   *   Factory to be used for generating and destroying the items.
+   * @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
+   *   Test if a resource is still valid .Should return a promise that resolves to a boolean, true if resource is still valid and false
+   *   If it should be removed from pool.
+   */
+  constructor (Evictor, Deque, PriorityQueue, factory, options) {
+    super()
+
+    factoryValidator(factory)
+
+    this._config = new PoolOptions(options)
+
+    // TODO: fix up this ugly glue-ing
+    this._Promise = this._config.Promise
+
+    this._factory = factory
+    this._draining = false
+    this._started = false
+    /**
+     * Holds waiting clients
+     * @type {PriorityQueue}
+     */
+    this._waitingClientsQueue = new PriorityQueue(this._config.priorityRange)
+
+    /**
+     * Collection of promises for resource creation calls made by the pool to factory.create
+     * @type {Set}
+     */
+    this._factoryCreateOperations = new Set()
+
+    /**
+     * Collection of promises for resource destruction calls made by the pool to factory.destroy
+     * @type {Set}
+     */
+    this._factoryDestroyOperations = new Set()
+
+    /**
+     * A queue/stack of pooledResources awaiting acquisition
+     * TODO: replace with LinkedList backed array
+     * @type {Array}
+     */
+    this._availableObjects = new Deque()
+
+    /**
+     * Collection of references for any resource that are undergoing validation before being acquired
+     * @type {Set}
+     */
+    this._testOnBorrowResources = new Set()
+
+    /**
+     * Collection of references for any resource that are undergoing validation before being returned
+     * @type {Set}
+     */
+    this._testOnReturnResources = new Set()
+
+    /**
+     * Collection of promises for any validations currently in process
+     * @type {Set}
+     */
+    this._validationOperations = new Set()
+
+    /**
+     * All objects associated with this pool in any state (except destroyed)
+     * @type {PooledResourceCollection}
+     */
+    this._allObjects = new Set()
+
+    /**
+     * Loans keyed by the borrowed resource
+     * @type {Map}
+     */
+    this._resourceLoans = new Map()
+
+    /**
+     * Infinitely looping iterator over available object
+     * @type {DLLArrayIterator}
+     */
+    this._evictionIterator = this._availableObjects.iterator()
+
+    this._evictor = new Evictor()
+
+    /**
+     * handle for setTimeout for next eviction run
+     * @type {[type]}
+     */
+    this._scheduledEviction = null
+
+    // create initial resources (if factory.min > 0)
+    if (this._config.autostart === true) {
+      this.start()
+    }
+  }
+
+  _destroy (pooledResource) {
+    // FIXME: do we need another state for "in destruction"?
+    pooledResource.invalidate()
+    this._allObjects.delete(pooledResource)
+    // NOTE: this maybe very bad promise usage?
+    const destroyPromise = this._factory.destroy(pooledResource.obj)
+    const wrappedDestroyPromise = this._Promise.resolve(destroyPromise)
+    this._factoryDestroyOperations.add(wrappedDestroyPromise)
+
+    wrappedDestroyPromise
+    .catch((reason) => {
+      this.emit(FACTORY_DESTROY_ERROR, reason)
+    })
+    .then(() => {
+      this._factoryDestroyOperations.delete(wrappedDestroyPromise)
+    })
+
+    // TODO: maybe ensuring minimum pool size should live outside here
+    this._ensureMinimum()
+  }
+
+  /**
+   * Attempt to move an available resource into test and then onto a waiting client
+   * @return {Boolean} could we move an available resource into test
+   */
+  _testOnBorrow () {
+    if (this._availableObjects.length < 1) {
+      return false
+    }
+
+    const pooledResource = this._availableObjects.shift()
+    // Mark the resource as in test
+    pooledResource.test()
+    this._testOnBorrowResources.add(pooledResource)
+    const validationPromise = this._factory.validate(pooledResource.obj)
+    const wrappedValidationPromise = this._Promise.resolve(validationPromise)
+    this._validationOperations.add(wrappedValidationPromise)
+
+    wrappedValidationPromise.then((isValid) => {
+      this._validationOperations.delete(wrappedValidationPromise)
+      this._testOnBorrowResources.delete(pooledResource)
+
+      if (isValid === false) {
+        pooledResource.invalidate()
+        this._destroy(pooledResource)
+        this._dispense()
+        return
+      }
+      this._dispatchPooledResourceToNextWaitingClient(pooledResource)
+    })
+
+    return true
+  }
+
+  /**
+   * Attempt to move an available resource to a waiting client
+   * @return {Boolean} [description]
+   */
+  _dispatchResource () {
+    if (this._availableObjects.length < 1) {
+      return false
+    }
+
+    const pooledResource = this._availableObjects.shift()
+    this._dispatchPooledResourceToNextWaitingClient(pooledResource)
+    return
+  }
+
+  /**
+   * Attempt to resolve an outstanding resource request using an available resource from
+   * the pool, or creating new ones
+   *
+   * @private
+   */
+  _dispense () {
+    /**
+     * Local variables for ease of reading/writing
+     * these don't (shouldn't) change across the execution of this fn
+     */
+    const numWaitingClients = this._waitingClientsQueue.length
+
+    // If there aren't any waiting requests then there is nothing to do
+    // so lets short-circuit
+    if (numWaitingClients < 1) {
+      return
+    }
+
+    // If we are doing test-on-borrow see how many more resources need to be moved into test
+    // to help satisfy waitingClients
+    if (this._config.testOnBorrow === true) {
+      // how many available resources do we need to shift into test
+      const desiredNumberOfResourcesToMoveIntoTest = numWaitingClients - this._testOnBorrowResources.size
+      const actualNumberOfResourcesToMoveIntoTest = Math.min(this._availableObjects.length, desiredNumberOfResourcesToMoveIntoTest)
+      for (let i = 0; actualNumberOfResourcesToMoveIntoTest > i; i++) {
+        this._testOnBorrow()
+      }
+    }
+
+    // Do we still have outstanding requests that can't be fulfilled by resources in-test and available
+    const potentiallyAllocableResources = this._availableObjects.length +
+      this._testOnBorrowResources.size +
+      this._testOnReturnResources.size +
+      this._factoryCreateOperations.size
+
+    const resourceShortfall = numWaitingClients - potentiallyAllocableResources
+
+    const spareResourceCapacity = this._config.max - (this._allObjects.size + this._factoryCreateOperations.size)
+    const actualNumberOfResourcesToCreate = Math.min(spareResourceCapacity, resourceShortfall)
+    for (let i = 0; actualNumberOfResourcesToCreate > i; i++) {
+      this._createResource()
+    }
+
+    // if we aren't testing-on-borrow then lets try to allocate what we can
+    if (this._config.testOnBorrow === false) {
+      const actualNumberOfResourcesToDispatch = Math.min(this._availableObjects.length, numWaitingClients)
+      for (let i = 0; actualNumberOfResourcesToDispatch > i; i++) {
+        this._dispatchResource()
+      }
+    }
+  }
+
+  /**
+   * Dispatches a pooledResource to the next waiting client (if any) else
+   * puts the PooledResource back on the available list
+   * @param  {[type]} pooledResource [description]
+   * @return {[type]}                [description]
+   */
+  _dispatchPooledResourceToNextWaitingClient (pooledResource) {
+    const clientResourceRequest = this._waitingClientsQueue.dequeue()
+    if (clientResourceRequest === undefined) {
+      // While we were away either all the waiting clients timed out
+      // or were somehow fulfilled. put our pooledResource back.
+      this._addPooledResourceToAvailableObjects(pooledResource)
+      // TODO: do need to trigger anything before we leave?
+      return false
+    }
+    const loan = new ResourceLoan(pooledResource, this._Promise)
+    this._resourceLoans.set(pooledResource.obj, loan)
+    pooledResource.allocate()
+    clientResourceRequest.resolve(pooledResource.obj)
+    return true
+  }
+
+  /**
+   * @private
+   */
+  _createResource () {
+    // An attempt to create a resource
+    const factoryPromise = this._factory.create()
+    const wrappedFactoryPromise = this._Promise.resolve(factoryPromise)
+    this._factoryCreateOperations.add(wrappedFactoryPromise)
+
+    wrappedFactoryPromise
+    .then((resource) => {
+      this._factoryCreateOperations.delete(wrappedFactoryPromise)
+      this._handleNewResource(resource)
+    })
+    .catch((reason) => {
+      this._factoryCreateOperations.delete(wrappedFactoryPromise)
+      this.emit(FACTORY_CREATE_ERROR, reason)
+      this._dispense()
+    })
+  }
+
+  _handleNewResource (resource) {
+    const pooledResource = new PooledResource(resource)
+    this._allObjects.add(pooledResource)
+    // TODO: check we aren't exceding our maxPoolSize before doing
+    this._dispatchPooledResourceToNextWaitingClient(pooledResource)
+  }
+
+  /**
+   * @private
+   */
+  _ensureMinimum () {
+    if (this._draining === true) {
+      return
+    }
+    if (this._count < this._config.min) {
+      const diff = this._config.min - this._count
+      for (let i = 0; i < diff; i++) {
+        this._createResource()
+      }
+    }
+  }
+
+  _evict () {
+    const testsToRun = Math.min(this._config.numTestsPerEvictionRun, this._availableObjects.length)
+    const evictionConfig = {
+      softIdleTimeoutMillis: this._config.softIdleTimeoutMillis,
+      idleTimeoutMillis: this._config.idleTimeoutMillis,
+      min: this._config.min
+    }
+    for (let testsHaveRun = 0; testsHaveRun < testsToRun;) {
+      const iterationResult = this._evictionIterator.next()
+
+      // Safety check incase we could get stuck in infinite loop because we
+      // somehow emptied the array after chekcing it's length
+      if (iterationResult.done === true && this._availableObjects.length < 1) {
+        this._evictionIterator.reset()
+        return
+      }
+      // if this happens it should just mean we reached the end of the
+      // list and can reset the cursor.
+      if (iterationResult.done === true && this._availableObjects.length > 0) {
+        this._evictionIterator.reset()
+        break
+      }
+
+      const resource = iterationResult.value
+
+      const shouldEvict = this._evictor.evict(evictionConfig, resource, this._availableObjects.length)
+      testsHaveRun++
+
+      if (shouldEvict === true) {
+        // take it out of the _availableObjects list
+        this._evictionIterator.remove()
+        this._destroy(resource)
+      }
+    }
+  }
+
+  _scheduleEvictorRun () {
+    // Start eviction if set
+    if (this._config.evictionRunIntervalMillis > 0) {
+      this._scheduledEviction = setTimeout(() => {
+        this._evict()
+        this._scheduleEvictorRun()
+      }, this._config.evictionRunIntervalMillis)
+    }
+  }
+
+  _descheduleEvictorRun () {
+    clearTimeout(this._scheduledEviction)
+    this._scheduledEviction = null
+  }
+
+  start () {
+    if (this._draining === true) {
+      return
+    }
+    if (this._started === true) {
+      return
+    }
+    this._started = true
+    this._scheduleEvictorRun()
+    this._ensureMinimum()
+  }
+
+  /**
+   * Request a new resource. The callback will be called,
+   * when a new resource is available, passing the resource to the callback.
+   * TODO: should we add a seperate "acquireWithPriority" function
+   *
+   * @param {Function} callback
+   *   Callback function to be called after the acquire is successful.
+   *   If there is an error preventing the acquisition of resource, an error will
+   *   be the first parameter, else it will be null.
+   *   The acquired resource will be the second 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 {Promise}
+   */
+  acquire (priority) {
+    if (this._draining) {
+      return this._Promise.reject(new Error('pool is draining and cannot accept work'))
+    }
+
+    // TODO: should we defer this check till after this event loop incase "the situation" changes in the meantime
+    if (this._config.maxWaitingClients !== undefined && this._waitingClientsQueue.length >= this._config.maxWaitingClients) {
+      return this._Promise.reject(new Error('max waitingClients count exceeded'))
+    }
+
+    const resourceRequest = new ResourceRequest(this._config.acquireTimeoutMillis, this._Promise)
+    this._waitingClientsQueue.enqueue(resourceRequest, priority)
+    this._dispense()
+
+    return resourceRequest.promise
+  }
+
+  /**
+   * Return the resource to the pool when it is no longer required.
+   *
+   * @param {Object} obj
+   *   The acquired object to be put back to the pool.
+   */
+  release (resource) {
+    // check for an outstanding loan
+    const loan = this._resourceLoans.get(resource)
+
+    if (loan === undefined) {
+      return this._Promise.reject(new Error('Resource not currently part of this pool'))
+    }
+
+    this._resourceLoans.delete(resource)
+    loan.resolve()
+    const pooledResource = loan.pooledResource
+
+    pooledResource.deallocate()
+    this._addPooledResourceToAvailableObjects(pooledResource)
+
+    this._dispense()
+    return this._Promise.resolve()
+  }
+
+  /**
+   * Request the resource 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} resource
+   *   The acquired resource to be destoyed.
+   */
+  destroy (resource) {
+    // check for an outstanding loan
+    const loan = this._resourceLoans.get(resource)
+
+    if (loan === undefined) {
+      return this._Promise.reject(new Error('Resource not currently part of this pool'))
+    }
+
+    this._resourceLoans.delete(resource)
+    loan.resolve()
+    const pooledResource = loan.pooledResource
+
+    pooledResource.deallocate()
+    this._destroy(pooledResource)
+    return this._Promise.resolve()
+  }
+
+  _addPooledResourceToAvailableObjects (pooledResource) {
+    pooledResource.idle()
+    if (this._config.fifo === true) {
+      this._availableObjects.push(pooledResource)
+    } else {
+      this._availableObjects.unshift(pooledResource)
+    }
+  }
+
+  /**
+   * Disallow any new acquire calls and let the request backlog dissapate.
+   * Resolves once all resources are returned to pool and available...
+   * @returns {Promise}
+   */
+  drain () {
+    // disable the ability to put more work on the queue.
+    this._draining = true
+    return this.__allResourceRequestsSettled()
+      .then(() => {
+        return this.__allResourcesReturned()
+      })
+  }
+
+  __allResourceRequestsSettled () {
+    if (this._waitingClientsQueue.length > 0) {
+      // wait for last waiting client to be settled
+      // FIXME: what if they can "resolve" out of order....?
+      return this._waitingClientsQueue.tail.promise.then(() => {}, () => {})
+    }
+    return this._Promise.resolve()
+  }
+
+  // FIXME: this is a horrific mess
+  __allResourcesReturned () {
+    const ps = []
+    this._resourceLoans.forEach(function (loan) {
+      ps.push(loan.promise)
+    })
+    return this._Promise.all(ps)
+  }
+
+  /**
+   * Forcibly destroys all available resources regardless of timeout.  Intended to be
+   * invoked as part of a drain.  Does not prevent the creation of new
+   * resources 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 clear()
+   *
+   */
+  clear () {
+    this._descheduleEvictorRun()
+    for (const resource of this._availableObjects) {
+      this._destroy(resource)
+    }
+    // TODO: we should handle factory.destroy rejecting...
+    return this._Promise.all(this._factoryDestroyOperations)
+  }
+
+  /**
+   * The combined count of the currently created objects and those in the
+   * process of being created
+   * Does NOT include resources in the process of being destroyed
+   * sort of legacy...
+   * @return {Number}
+   */
+  get _count () {
+    return this._allObjects.size + this._factoryCreateOperations.size
+  }
+
+  /**
+   * see _count above
+   * @return {Number} [description]
+   */
+  get size () {
+    return this._count
+  }
+
+  /**
+   * number of available resources
+   * @return {Number} [description]
+   */
+  get available () {
+    return this._availableObjects.length
+  }
+
+  /**
+   * number of resources that are currently acquired
+   * @return {[type]} [description]
+   */
+  get borrowed () {
+    return this._resourceLoans.size
+  }
+
+  /**
+   * number of waiting acquire calls
+   * @return {[type]} [description]
+   */
+  get pending () {
+    return this._waitingClientsQueue.length
+  }
+
+  /**
+   * maximum size of the pool
+   * @return {[type]} [description]
+   */
+  get max () {
+    return this._config.max
+  }
+
+  /**
+   * minimum size of the pool
+   * @return {[type]} [description]
+   */
+  get min () {
+    return this._config.min
+  }
+}
+
+module.exports = Pool
diff --git a/lib/PoolDefaults.js b/lib/PoolDefaults.js
new file mode 100644
index 0000000..dd1c60d
--- /dev/null
+++ b/lib/PoolDefaults.js
@@ -0,0 +1,33 @@
+'use strict'
+/**
+ * Create the default settings used by the pool
+ *
+ * @class
+ */
+class PoolDefaults {
+  constructor () {
+    this.fifo = true
+    this.priorityRange = 1
+
+    this.testOnBorrow = false
+    this.testOnReturn = false
+
+    this.autostart = true
+
+    this.evictionRunIntervalMillis = 0
+    this.numTestsPerEvictionRun = 3
+    this.softIdleTimeoutMillis = -1
+    this.idleTimeoutMillis = 30000
+
+    // FIXME: no defaults!
+    this.acquireTimeoutMillis = null
+    this.maxWaitingClients = null
+
+    this.min = null
+    this.max = null
+    // FIXME: this seems odd?
+    this.Promise = Promise
+  }
+}
+
+module.exports = PoolDefaults
diff --git a/lib/PoolOptions.js b/lib/PoolOptions.js
new file mode 100644
index 0000000..88bca97
--- /dev/null
+++ b/lib/PoolOptions.js
@@ -0,0 +1,79 @@
+'use strict'
+
+const PoolDefaults = require('./PoolDefaults')
+
+class PoolOptions {
+  /**
+   * @param {Object} config
+   *   configuration for the pool
+   * @param {Number} config.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} config.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} config.maxWaitingClients
+   *   maximum number of queued requests allowed after which acquire calls will be rejected
+   * @param {Number} config.acquireTimeoutMillis
+   *   Delay in milliseconds after which the an `acquire` call will fail. optional.
+   *   Default: undefined. Should be positive and non-zero
+   * @param {Number} config.priorityRange
+   *   The range from 1 to be treated as a valid priority
+   * @param {Bool} [config.fifo=true]
+   *   Sets whether the pool has LIFO (last in, first out) behaviour with respect to idle objects.
+   *   if false then pool has FIFO behaviour
+   * @param {Bool} [config.autostart=true]
+   *   Should the pool start creating resources etc once the constructor is called
+   * @param {Number} opts.evictionRunIntervalMillis
+   *   How often to run eviction checks.  Default: 0 (does not run).
+   * @param {Number} opts.numTestsPerEvictionRun
+   *   Number of resources to check each eviction run.  Default: 3.
+   * @param {Number} opts.softIdleTimeoutMillis
+   *   amount of time an object may sit idle in the pool before it is eligible
+   *   for eviction by the idle object evictor (if any), with the extra condition
+   *   that at least "min idle" object instances remain in the pool. Default -1 (nothing can get evicted)
+   * @param {Number} opts.idleTimeoutMillis
+   *   the minimum amount of time that an object may sit idle in the pool before it is eligible for eviction
+   *   due to idle time. Supercedes "softIdleTimeoutMillis" Default: 30000
+   * @param {Promise} [config.Promise=Promise]
+   *   What promise implementation should the pool use, defaults to native promises.
+   */
+  constructor (opts) {
+    const poolDefaults = new PoolDefaults()
+
+    opts = opts || {}
+
+    this.fifo = (typeof opts.fifo === 'boolean') ? opts.fifo : poolDefaults.fifo
+    this.priorityRange = opts.priorityRange || poolDefaults.priorityRange
+
+    this.testOnBorrow = (typeof opts.testOnBorrow === 'boolean') ? opts.testOnBorrow : poolDefaults.testOnBorrow
+    this.testOnReturn = (typeof opts.testOnReturn === 'boolean') ? opts.testOnReturn : poolDefaults.testOnReturn
+
+    this.autostart = (typeof opts.autostart === 'boolean') ? opts.autostart : poolDefaults.autostart
+
+    if (opts.acquireTimeoutMillis) {
+      this.acquireTimeoutMillis = parseInt(opts.acquireTimeoutMillis, 10)
+    }
+
+    if (opts.maxWaitingClients) {
+      this.maxWaitingClients = parseInt(opts.maxWaitingClients, 10)
+    }
+
+    this.max = parseInt(opts.max, 10)
+    this.min = parseInt(opts.min, 10)
+
+    this.max = Math.max(isNaN(this.max) ? 1 : this.max, 1)
+    this.min = Math.min(isNaN(this.min) ? 0 : this.min, this.max)
+
+    this.evictionRunIntervalMillis = opts.evictionRunIntervalMillis || poolDefaults.evictionRunIntervalMillis
+    this.numTestsPerEvictionRun = opts.numTestsPerEvictionRun || poolDefaults.numTestsPerEvictionRun
+    this.softIdleTimeoutMillis = opts.softIdleTimeoutMillis || poolDefaults.softIdleTimeoutMillis
+    this.idleTimeoutMillis = opts.idleTimeoutMillis || poolDefaults.idleTimeoutMillis
+
+    this.Promise = (typeof opts.Promise === 'object') ? opts.Promise : poolDefaults.Promise
+  }
+}
+
+module.exports = PoolOptions
diff --git a/lib/PooledResource.js b/lib/PooledResource.js
new file mode 100644
index 0000000..3eeeada
--- /dev/null
+++ b/lib/PooledResource.js
@@ -0,0 +1,49 @@
+'use strict'
+
+const PooledResourceStateEnum = require('./PooledResourceStateEnum')
+
+/**
+ * @class
+ * @private
+ */
+class PooledResource {
+  constructor (resource) {
+    this.creationTime = Date.now()
+    this.lastReturnTime = null
+    this.lastBorrowTime = null
+    this.lastIdleTime = null
+    this.obj = resource
+    this.state = PooledResourceStateEnum.IDLE
+  }
+
+  // mark the resource as "allocated"
+  allocate () {
+    this.lastBorrowTime = Date.now()
+    this.state = PooledResourceStateEnum.ALLOCATED
+  }
+
+  // mark the resource as "deallocated"
+  deallocate () {
+    this.lastReturnTime = Date.now()
+    this.state = PooledResourceStateEnum.IDLE
+  }
+
+  invalidate () {
+    this.state = PooledResourceStateEnum.INVALID
+  }
+
+  test () {
+    this.state = PooledResourceStateEnum.VALIDATION
+  }
+
+  idle () {
+    this.lastIdleTime = Date.now()
+    this.state = PooledResourceStateEnum.IDLE
+  }
+
+  returning () {
+    this.state = PooledResourceStateEnum.RETURNING
+  }
+}
+
+module.exports = PooledResource
diff --git a/lib/PooledResourceStateEnum.js b/lib/PooledResourceStateEnum.js
new file mode 100644
index 0000000..fb94788
--- /dev/null
+++ b/lib/PooledResourceStateEnum.js
@@ -0,0 +1,11 @@
+'use strict'
+
+const PooledResourceStateEnum = {
+  ALLOCATED: 'ALLOCATED', // In use
+  IDLE: 'IDLE', // In the queue, not in use.
+  INVALID: 'INVALID', // Failed validation
+  RETURNING: 'RETURNING', // Resource is in process of returning
+  VALIDATION: 'VALIDATION' // Currently being tested
+}
+
+module.exports = PooledResourceStateEnum
diff --git a/lib/PriorityQueue.js b/lib/PriorityQueue.js
new file mode 100644
index 0000000..17633fb
--- /dev/null
+++ b/lib/PriorityQueue.js
@@ -0,0 +1,68 @@
+'use strict'
+
+const Queue = require('./Queue')
+
+/**
+ * @class
+ * @private
+ */
+class PriorityQueue {
+  constructor (size) {
+    this._size = Math.max(+size | 0, 1)
+    this._slots = []
+    // initialize arrays to hold queue elements
+    for (let i = 0; i < this._size; i++) {
+      this._slots.push(new Queue())
+    }
+  }
+
+  get length () {
+    let _length = 0
+    for (let i = 0, slots = this._slots.length; i < slots; i++) {
+      _length += this._slots[i].length
+    }
+    return _length
+  }
+
+  enqueue (obj, priority) {
+    // Convert to integer with a default value of 0.
+    priority = priority && +priority | 0 || 0
+
+    if (priority) {
+      if (priority < 0 || priority >= this._size) {
+        priority = (this._size - 1)
+        // put obj at the end of the line
+      }
+    }
+    this._slots[priority].push(obj)
+  }
+
+  dequeue () {
+    for (let i = 0, sl = this._slots.length; i < sl; i += 1) {
+      if (this._slots[i].length) {
+        return this._slots[i].shift()
+      }
+    }
+    return
+  }
+
+  get head () {
+    for (let i = 0, sl = this._slots.length; i < sl; i += 1) {
+      if (this._slots[i].length > 0) {
+        return this._slots[i].head
+      }
+    }
+    return
+  }
+
+  get tail () {
+    for (let i = this._slots.length - 1; i >= 0; i--) {
+      if (this._slots[i].length > 0) {
+        return this._slots[i].tail
+      }
+    }
+    return
+  }
+}
+
+module.exports = PriorityQueue
diff --git a/lib/Queue.js b/lib/Queue.js
new file mode 100644
index 0000000..65cd861
--- /dev/null
+++ b/lib/Queue.js
@@ -0,0 +1,35 @@
+'use strict'
+
+const DoublyLinkedList = require('./DoublyLinkedList')
+const Deque = require('./Deque')
+
+/**
+ * Sort of a internal queue for holding the waiting
+ * resource requets for a given "priority".
+ * Also handles managing timeouts rejections on items (is this the best place for this?)
+ * This is the last point where we know which queue a resourceRequest is in
+ *
+ */
+class Queue extends Deque {
+  /**
+   * Adds the obj to the end of the list for this slot
+   * we completely override the parent method because we need access to the
+   * node for our rejection handler
+   * @param {[type]} item [description]
+   */
+  push (resourceRequest) {
+    const node = DoublyLinkedList.createNode(resourceRequest)
+    resourceRequest.promise.catch(this._createTimeoutRejectionHandler(node))
+    this._list.insertEnd(node)
+  }
+
+  _createTimeoutRejectionHandler (node) {
+    return (reason) => {
+      if (reason.name === 'TimeoutError') {
+        this._list.remove(node)
+      }
+    }
+  }
+}
+
+module.exports = Queue
diff --git a/lib/ResourceLoan.js b/lib/ResourceLoan.js
new file mode 100644
index 0000000..a6187b3
--- /dev/null
+++ b/lib/ResourceLoan.js
@@ -0,0 +1,29 @@
+'use strict'
+
+const Deferred = require('./Deferred')
+
+/**
+ * Plan is to maybe add tracking via Error objects
+ * and other fun stuff!
+ */
+
+class ResourceLoan extends Deferred {
+  /**
+   *
+   * @param  {PooledResource} pooledResource the PooledResource this loan belongs to
+   * @return {[type]}                [description]
+   */
+  constructor (pooledResource, Promise) {
+    super(Promise)
+    this._creationTimestamp = Date.now()
+    this.pooledResource = pooledResource
+  }
+
+  reject () {
+    /**
+     * Loans can only be resolved at the moment
+     */
+  }
+}
+
+module.exports = ResourceLoan
diff --git a/lib/ResourceRequest.js b/lib/ResourceRequest.js
new file mode 100644
index 0000000..db53632
--- /dev/null
+++ b/lib/ResourceRequest.js
@@ -0,0 +1,72 @@
+'use strict'
+
+const Deferred = require('./Deferred')
+const errors = require('./errors')
+
+function fbind (fn, ctx) {
+  return function bound () {
+    return fn.apply(ctx, arguments)
+  }
+}
+
+/**
+ * Wraps a users request for a resource
+ * Basically a promise mashed in with a timeout
+ * @private
+ */
+class ResourceRequest extends Deferred {
+
+  /**
+   * [constructor description]
+   * @param  {Number} ttl     timeout
+   */
+  constructor (ttl, Promise) {
+    super(Promise)
+    this._creationTimestamp = Date.now()
+    this._timeout = null
+
+    if (ttl !== undefined) {
+      this.setTimeout(ttl)
+    }
+  }
+
+  setTimeout (delay) {
+    if (this._state !== ResourceRequest.PENDING) {
+      return
+    }
+    const ttl = parseInt(delay, 10)
+
+    if (isNaN(ttl) || ttl <= 0) {
+      throw new Error('delay must be a positive int')
+    }
+
+    const age = Date.now() - this._creationTimestamp
+
+    if (this._timeout) {
+      this.removeTimeout()
+    }
+
+    this._timeout = setTimeout(fbind(this._fireTimeout, this), Math.max(ttl - age, 0))
+  }
+
+  removeTimeout () {
+    clearTimeout(this._timeout)
+    this._timeout = null
+  }
+
+  _fireTimeout () {
+    this.reject(new errors.TimeoutError('ResourceRequest timed out'))
+  }
+
+  reject (reason) {
+    this.removeTimeout()
+    super.reject(reason)
+  }
+
+  resolve (value) {
+    this.removeTimeout()
+    super.resolve(value)
+  }
+}
+
+module.exports = ResourceRequest
diff --git a/lib/errors.js b/lib/errors.js
new file mode 100644
index 0000000..3dc3c79
--- /dev/null
+++ b/lib/errors.js
@@ -0,0 +1,26 @@
+'use strict'
+
+class ExtendableError extends Error {
+  constructor (message) {
+    super(message)
+    this.name = this.constructor.name
+    this.message = message
+    if (typeof Error.captureStackTrace === 'function') {
+      Error.captureStackTrace(this, this.constructor)
+    } else {
+      this.stack = (new Error(message)).stack
+    }
+  }
+}
+
+/* eslint-disable no-useless-constructor */
+class TimeoutError extends ExtendableError {
+  constructor (m) {
+    super(m)
+  }
+}
+/* eslint-enable no-useless-constructor */
+
+module.exports = {
+  TimeoutError: TimeoutError
+}
diff --git a/lib/factoryValidator.js b/lib/factoryValidator.js
new file mode 100644
index 0000000..0e4fd11
--- /dev/null
+++ b/lib/factoryValidator.js
@@ -0,0 +1,14 @@
+
+module.exports = function (factory) {
+  if (typeof factory.create !== 'function') {
+    throw new TypeError('factory.create must be a function')
+  }
+
+  if (typeof factory.destroy !== 'function') {
+    throw new TypeError('factory.destroy must be a function')
+  }
+
+  if (typeof factory.validate !== 'undefined' && typeof factory.validate !== 'function') {
+    throw new TypeError('factory.validate must be a function')
+  }
+}
diff --git a/lib/generic-pool.js b/lib/generic-pool.js
deleted file mode 100644
index 6bd3aa9..0000000
--- a/lib/generic-pool.js
+++ /dev/null
@@ -1,467 +0,0 @@
-var 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;
-};
diff --git a/package.json b/package.json
index f144731..61e5448 100644
--- a/package.json
+++ b/package.json
@@ -1,26 +1,82 @@
 {
   "name": "generic-pool",
   "description": "Generic resource pooling for Node.JS",
-  "version": "2.0.3",
+  "version": "3.1.1",
   "author": "James Cooper <james at bitmechanic.com>",
   "contributors": [
-    { "name": "James Cooper", "email": "james at bitmechanic.com" },
-    { "name": "Peter Galiba", "email": "poetro at poetro.hu", "url": "http://poetro.hu/" },
-    { "name": "Gary Dusbabek" },
-    { "name": "Tom MacWright", "url" : "http://www.developmentseed.org/" },
-    { "name": "Douglas Christopher Wilson", "email": "doug at somethingdoug.com", "url" : "http://somethingdoug.com/" }
+    {
+      "name": "James Cooper",
+      "email": "james at bitmechanic.com"
+    },
+    {
+      "name": "Peter Galiba",
+      "email": "poetro at poetro.hu",
+      "url": "http://poetro.hu/"
+    },
+    {
+      "name": "Gary Dusbabek"
+    },
+    {
+      "name": "Tom MacWright",
+      "url": "http://www.developmentseed.org/"
+    },
+    {
+      "name": "Douglas Christopher Wilson",
+      "email": "doug at somethingdoug.com",
+      "url": "http://somethingdoug.com/"
+    },
+    {
+      "name": "calibr"
+    },
+    {
+      "name": "Justin Robinson",
+      "email": "jrobinson at redventures.com>"
+    },
+    {
+      "name": "Nayana Hettiarachchi",
+      "email": "nayana at corp-gems.com"
+    },
+    {
+      "name": "Felipe Machado",
+      "email": "felipou at gmail.com"
+    },
+    {
+      "name": "Felix Becker",
+      "email": "felix.b at outlook.com"
+    },
+    {
+      "name": "sandfox",
+      "email": "james.butler at sandfox.co.uk"
+    },
+    {
+      "name": "Lewis J Ellis",
+      "email": "me at lewisjellis.com"
+    }
   ],
-  "keywords": ["pool", "pooling", "throttle"],
-  "main": "lib/generic-pool.js",
+  "keywords": [
+    "pool",
+    "pooling",
+    "throttle"
+  ],
+  "main": "index.js",
   "repository": {
     "type": "git",
     "url": "http://github.com/coopernurse/node-pool.git"
   },
   "devDependencies": {
-      "expresso": ">0.0.0"
+    "eslint": "^3.4.0",
+    "eslint-config-standard": "^6.0.0",
+    "eslint-plugin-promise": "^3.3.0",
+    "eslint-plugin-standard": "^2.0.0",
+    "tap": "^8.0.0"
+  },
+  "engines": {
+    "node": ">= 4"
   },
-  "engines": { "node": ">= 0.2.0" },
   "scripts": {
-     "test": "expresso -I lib test/*.js"
-  }
+    "lint": "eslint lib test",
+    "lint-fix": "eslint --fix lib test",
+    "test": "tap test/*-test.js "
+  },
+  "license": "MIT"
 }
diff --git a/test/doubly-linked-list-iterator-test.js b/test/doubly-linked-list-iterator-test.js
new file mode 100644
index 0000000..ff3d30c
--- /dev/null
+++ b/test/doubly-linked-list-iterator-test.js
@@ -0,0 +1,139 @@
+const tap = require('tap')
+const DLL = require('../lib/DoublyLinkedList')
+const Iterator = require('../lib/DoublyLinkedListIterator')
+
+tap.test('iterates forward', function (t) {
+  const dll = new DLL()
+
+  const node1 = DLL.createNode({id: 1})
+  const node2 = DLL.createNode({id: 2})
+  const node3 = DLL.createNode({id: 3})
+  const node4 = DLL.createNode({id: 4})
+
+  dll.insertBeginning(node1)
+  dll.insertBeginning(node2)
+  dll.insertBeginning(node3)
+  dll.insertBeginning(node4)
+
+  const iterator = new Iterator(dll)
+
+  const iterationResult1 = iterator.next()
+  t.notOk(iterationResult1.done)
+  t.same(iterationResult1.value, node4)
+
+  iterator.next()
+  iterator.next()
+
+  const iterationResult4 = iterator.next()
+  t.notOk(iterationResult4.done)
+  t.same(iterationResult4.value, node1)
+
+  const iterationResult5 = iterator.next()
+  t.ok(iterationResult5.done)
+
+  t.end()
+})
+
+tap.test('iterates backwards', function (t) {
+  const dll = new DLL()
+
+  const node1 = DLL.createNode({id: 1})
+  const node2 = DLL.createNode({id: 2})
+  const node3 = DLL.createNode({id: 3})
+  const node4 = DLL.createNode({id: 4})
+
+  dll.insertBeginning(node1)
+  dll.insertBeginning(node2)
+  dll.insertBeginning(node3)
+  dll.insertBeginning(node4)
+
+  const iterator = new Iterator(dll, true)
+
+  const iterationResult1 = iterator.next()
+  t.notOk(iterationResult1.done)
+  t.same(iterationResult1.value, node1)
+
+  iterator.next()
+  iterator.next()
+
+  const iterationResult4 = iterator.next()
+  t.notOk(iterationResult4.done)
+  t.same(iterationResult4.value, node4)
+
+  const iterationResult5 = iterator.next()
+  t.ok(iterationResult5.done)
+
+  t.end()
+})
+
+tap.test('iterates forward when adding nodes after creating iterator', function (t) {
+  const dll = new DLL()
+
+  const node1 = DLL.createNode({id: 1})
+  const node2 = DLL.createNode({id: 2})
+
+  const iterator = new Iterator(dll)
+
+  dll.insertBeginning(node1)
+  dll.insertBeginning(node2)
+
+  const iterationResult1 = iterator.next()
+  t.notOk(iterationResult1.done)
+  t.same(iterationResult1.value, node2)
+
+  const iterationResult2 = iterator.next()
+  t.notOk(iterationResult2.done)
+  t.same(iterationResult2.value, node1)
+
+  const iterationResult3 = iterator.next()
+  t.ok(iterationResult3.done)
+
+  t.end()
+})
+
+tap.test('iterates backwards when adding nodes after creating iterator', function (t) {
+  const dll = new DLL()
+
+  const node1 = DLL.createNode({id: 1})
+  const node2 = DLL.createNode({id: 2})
+
+  const iterator = new Iterator(dll, true)
+
+  dll.insertBeginning(node1)
+  dll.insertBeginning(node2)
+
+  const iterationResult1 = iterator.next()
+  t.notOk(iterationResult1.done)
+  t.same(iterationResult1.value, node1)
+
+  const iterationResult2 = iterator.next()
+  t.notOk(iterationResult2.done)
+  t.same(iterationResult2.value, node2)
+
+  const iterationResult3 = iterator.next()
+  t.ok(iterationResult3.done)
+
+  t.end()
+})
+
+tap.test('stops iterating when node is detached', function (t) {
+  const dll = new DLL()
+  const iterator = new Iterator(dll)
+
+  const node1 = DLL.createNode({id: 1})
+  const node2 = DLL.createNode({id: 2})
+
+  dll.insertBeginning(node1)
+  dll.insertBeginning(node2)
+
+  const iterationResult1 = iterator.next()
+  t.notOk(iterationResult1.done)
+  t.same(iterationResult1.value, node2)
+
+  dll.remove(node1)
+
+  const iterationResult3 = iterator.next()
+  t.ok(iterationResult3.done)
+
+  t.end()
+})
diff --git a/test/doubly-linked-list-test.js b/test/doubly-linked-list-test.js
new file mode 100644
index 0000000..65f12cd
--- /dev/null
+++ b/test/doubly-linked-list-test.js
@@ -0,0 +1,28 @@
+var tap = require('tap')
+var DLL = require('../lib/DoublyLinkedList')
+
+tap.test('operations', function (t) {
+  var dll = new DLL()
+
+  var item1 = {id: 1}
+  var item2 = {id: 2}
+  var item3 = {id: 3}
+  var item4 = {id: 4}
+
+  dll.insertBeginning(DLL.createNode(item1))
+  t.equal(dll.head.data, item1)
+
+  dll.insertEnd(DLL.createNode(item2))
+  t.equal(dll.tail.data, item2)
+
+  dll.insertAfter(dll.tail, DLL.createNode(item3))
+  t.equal(dll.tail.data, item3)
+
+  dll.insertBefore(dll.tail, DLL.createNode(item4))
+  t.equal(dll.tail.data, item3)
+
+  dll.remove(dll.tail)
+  t.equal(dll.tail.data, item4)
+
+  t.end()
+})
diff --git a/test/generic-pool-acquiretimeout-test.js b/test/generic-pool-acquiretimeout-test.js
new file mode 100644
index 0000000..eb9d497
--- /dev/null
+++ b/test/generic-pool-acquiretimeout-test.js
@@ -0,0 +1,72 @@
+'use strict'
+
+const tap = require('tap')
+const createPool = require('../').createPool
+
+tap.test('acquireTimeout handles timed out acquire calls', function (t) {
+  const factory = {
+    create: function () {
+      return new Promise(function (resolve) {
+        setTimeout(function () {
+          resolve({})
+        }, 100)
+      })
+    },
+    destroy: function () { return Promise.resolve() }
+  }
+  const config = {
+    acquireTimeoutMillis: 20,
+    idleTimeoutMillis: 150,
+    log: false
+  }
+
+  const pool = createPool(factory, config)
+
+  pool.acquire()
+  .then(function (resource) {
+    t.fail('wooops')
+  })
+  .catch(function (err) {
+    t.match(err, /ResourceRequest timed out/)
+    return pool.drain()
+  })
+  .then(function () {
+    return pool.clear()
+  })
+  .then(function () {
+  })
+  .then(t.end)
+  .catch(t.error)
+})
+
+tap.test('acquireTimeout handles non timed out acquire calls', function (t) {
+  const myResource = {}
+  const factory = {
+    create: function () {
+      return new Promise(function (resolve) {
+        setTimeout(function () {
+          resolve(myResource)
+        }, 10)
+      })
+    },
+    destroy: function () { return Promise.resolve() }
+  }
+
+  const config = {
+    acquireTimeoutMillis: 400
+  }
+
+  const pool = createPool(factory, config)
+
+  pool.acquire()
+  .then(function (resource) {
+    t.equal(resource, myResource)
+    pool.release(resource)
+    return pool.drain()
+  })
+  .then(function () {
+    return pool.clear()
+  })
+  .then(t.end)
+  .catch(t.error)
+})
diff --git a/test/generic-pool-test.js b/test/generic-pool-test.js
new file mode 100644
index 0000000..49f327a
--- /dev/null
+++ b/test/generic-pool-test.js
@@ -0,0 +1,600 @@
+'use strict'
+
+const tap = require('tap')
+const createPool = require('../').createPool
+const utils = require('./utils')
+const ResourceFactory = utils.ResourceFactory
+
+// tap.test('Pool expands only to max limit', function (t) {
+//   const resourceFactory = new ResourceFactory()
+
+//   const config = {
+//     max: 1
+//   }
+
+//   const pool = createPool(resourceFactory, config)
+
+//     // NOTES:
+//     // - request a resource
+//     // - once we have it, request another and check the pool is fool
+//   pool.acquire(function (err, obj) {
+//     t.error(err)
+//     const poolIsFull = !pool.acquire(function (err, obj) {
+//       t.error(err)
+//       t.equal(1, resourceFactory.created)
+//       pool.release(obj)
+//       utils.stopPool(pool)
+//       t.end()
+//     })
+//     t.ok(poolIsFull)
+//     t.equal(1, resourceFactory.created)
+//     pool.release(obj)
+//   })
+// })
+
+// tap.test('Pool respects min limit', function (t) {
+//   const resourceFactory = new ResourceFactory()
+
+//   const config
+//     min: 1,
+//     max: 2
+//   }
+
+//   const pool = createPool(resourceFactory, config)
+
+//     // FIXME: this logic only works because we know it takes ~1ms to create a resource
+//     // we need better hooks into the pool probably to observe this...
+//   setTimeout(function () {
+//     t.equal(resourceFactory.created, 1)
+//     utils.stopPool(pool)
+//     t.end()
+//   }, 10)
+// })
+
+tap.test('min and max limit defaults', function (t) {
+  const resourceFactory = new ResourceFactory()
+
+  const pool = createPool(resourceFactory)
+
+  t.equal(1, pool.max)
+  t.equal(0, pool.min)
+  utils.stopPool(pool)
+  t.end()
+})
+
+tap.test('malformed min and max limits are ignored', function (t) {
+  const resourceFactory = new ResourceFactory()
+
+  const config = {
+    min: 'asf',
+    max: []
+  }
+  const pool = createPool(resourceFactory, config)
+
+  t.equal(1, pool.max)
+  t.equal(0, pool.min)
+  utils.stopPool(pool)
+  t.end()
+})
+
+tap.test('min greater than max sets to max', function (t) {
+  const resourceFactory = new ResourceFactory()
+
+  const config = {
+    min: 5,
+    max: 3
+  }
+  const pool = createPool(resourceFactory, config)
+
+  t.equal(3, pool.max)
+  t.equal(3, pool.min)
+  utils.stopPool(pool)
+  t.end()
+})
+
+tap.test('supports priority on borrow', function (t) {
+  // NOTE: this test is pretty opaque about what it's really testing/expecting...
+  let borrowTimeLow = 0
+  let borrowTimeHigh = 0
+  let borrowCount = 0
+
+  const resourceFactory = new ResourceFactory()
+
+  const config = {
+    max: 1,
+    priorityRange: 2
+  }
+
+  const pool = createPool(resourceFactory, config)
+
+  function lowPriorityOnFulfilled (obj) {
+    const time = Date.now()
+    if (time > borrowTimeLow) { borrowTimeLow = time }
+    borrowCount++
+    pool.release(obj)
+  }
+
+  function highPriorityOnFulfilled (obj) {
+    const time = Date.now()
+    if (time > borrowTimeHigh) { borrowTimeHigh = time }
+    borrowCount++
+    pool.release(obj)
+  }
+
+  const operations = []
+
+  for (let i = 0; i < 10; i++) {
+    const op = pool.acquire(1).then(lowPriorityOnFulfilled)
+    operations.push(op)
+  }
+
+  for (let i = 0; i < 10; i++) {
+    const op = pool.acquire(0).then(highPriorityOnFulfilled)
+    operations.push(op)
+  }
+
+  Promise.all(operations).then(function () {
+    t.equal(20, borrowCount)
+    t.equal(true, borrowTimeLow >= borrowTimeHigh)
+    utils.stopPool(pool)
+    t.end()
+  })
+  .catch(t.threw)
+})
+
+// FIXME: bad test!
+// pool.destroy makes no obligations to user about when it will destroy the resource
+// we should test that "destroyed" objects are not acquired again instead
+// tap.test('removes correct object on reap', function (t) {
+//   const resourceFactory = new ResourceFactory()
+
+//   const config
+//     max: 2
+//   }
+
+//   const pool = createPool(resourceFactory, config)
+
+//   const op1 = pool.acquire()
+//   .then(function (client) {
+//     return new Promise(function (resolve, reject) {
+//       // should be removed second
+//       setTimeout(function () {
+//         pool.destroy(client)
+//         resolve()
+//       }, 5)
+//     })
+//   })
+
+//   const op2 = pool.acquire()
+//   .then(function (client) {
+//     pool.destroy(client)
+//   })
+
+//   Promise.all([op1, op2]).then(function () {
+//     t.equal(1, resourceFactory.bin[0].id)
+//     t.equal(0, resourceFactory.bin[1].id)
+//     utils.stopPool(pool)
+//     t.end()
+//   })
+//   .catch(t.threw)
+// })
+
+tap.test('evictor removes instances on idletimeout', function (t) {
+  const resourceFactory = new ResourceFactory()
+  const config = {
+    min: 2,
+    max: 2,
+    idleTimeoutMillis: 50,
+    evictionRunIntervalMillis: 10
+  }
+  const pool = createPool(resourceFactory, config)
+
+  setTimeout(function () {
+    pool.acquire()
+    .then((res) => {
+      t.ok(res.id > 1)
+      return pool.release(res)
+    })
+    .then(() => {
+      utils.stopPool(pool)
+      t.end()
+    })
+  }, 120)
+})
+
+tap.test('tests drain', function (t) {
+  const count = 5
+  let acquired = 0
+
+  const resourceFactory = new ResourceFactory()
+  const config = {
+    max: 2,
+    idletimeoutMillis: 300000
+  }
+  const pool = createPool(resourceFactory, config)
+
+  const operations = []
+
+  function onAcquire (client) {
+    acquired += 1
+    t.equal(typeof client.id, 'number')
+    setTimeout(function () {
+      pool.release(client)
+    }, 250)
+  }
+
+    // request 5 resources that release after 250ms
+  for (let i = 0; i < count; i++) {
+    const op = pool.acquire().then(onAcquire)
+    operations.push(op)
+  }
+    // FIXME: what does this assertion prove?
+  t.notEqual(count, acquired)
+
+  Promise.all(operations)
+  .then(function () {
+    return pool.drain()
+  })
+  .then(function () {
+    t.equal(count, acquired)
+    // short circuit the absurdly long timeouts above.
+    pool.clear()
+  })
+  .then(function () {
+    // subsequent calls to acquire should resolve an error.
+    return pool.acquire().then(t.fail,
+      function (e) {
+        t.type(e, Error)
+      })
+  })
+  .then(function () {
+    t.end()
+  })
+})
+
+tap.test('handle creation errors', function (t) {
+  let created = 0
+  const resourceFactory = {
+    create: function () {
+      created++
+      if (created < 5) {
+        return Promise.reject(new Error('Error occurred.'))
+      } else {
+        return Promise.resolve({ id: created })
+      }
+    },
+    destroy: function (client) {}
+  }
+  const config = {
+    max: 1
+  }
+
+  const pool = createPool(resourceFactory, config)
+
+  // FIXME: this section no longer proves anything as factory
+  // errors no longer bubble up through the acquire call
+  // we need to make the Pool an Emitter
+
+  // ensure that creation errors do not populate the pool.
+  // for (const i = 0; i < 5; i++) {
+  //   pool.acquire(function (err, client) {
+  //     t.ok(err instanceof Error)
+  //     t.ok(client === null)
+  //   })
+  // }
+
+  let called = false
+  pool.acquire()
+  .then(function (client) {
+    t.equal(typeof client.id, 'number')
+    called = true
+    pool.release(client)
+  })
+  .then(function () {
+    t.ok(called)
+    t.equal(pool.pending, 0)
+    utils.stopPool(pool)
+    t.end()
+  })
+  .catch(t.threw)
+})
+
+tap.test('handle creation errors for delayed creates', function (t) {
+  let attempts = 0
+
+  const resourceFactory = {
+    create: function () {
+      attempts++
+      if (attempts <= 5) {
+        return Promise.reject(new Error('Error occurred.'))
+      } else {
+        return Promise.resolve({ id: attempts })
+      }
+    },
+    destroy: function (client) { return Promise.resolve() }
+  }
+
+  const config = {
+    max: 1
+  }
+
+  const pool = createPool(resourceFactory, config)
+
+  let errorCount = 0
+  pool.on('factoryCreateError', function (err) {
+    t.ok(err instanceof Error)
+    errorCount++
+  })
+
+  let called = false
+  pool.acquire()
+  .then(function (client) {
+    t.equal(typeof client.id, 'number')
+    called = true
+    pool.release(client)
+  })
+  .then(function () {
+    t.ok(called)
+    t.equal(errorCount, 5)
+    t.equal(pool.pending, 0)
+    utils.stopPool(pool)
+    t.end()
+  })
+  .catch(t.threw)
+})
+
+tap.test('getPoolSize', function (t) {
+  let assertionCount = 0
+  const resourceFactory = new ResourceFactory()
+  const config = {
+    max: 2
+  }
+
+  const pool = createPool(resourceFactory, config)
+
+  const borrowedResources = []
+
+  t.equal(pool.size, 0)
+  assertionCount += 1
+
+  pool.acquire()
+  .then(function (obj) {
+    borrowedResources.push(obj)
+    t.equal(pool.size, 1)
+    assertionCount += 1
+  })
+  .then(function () {
+    return pool.acquire()
+  })
+  .then(function (obj) {
+    borrowedResources.push(obj)
+    t.equal(pool.size, 2)
+    assertionCount += 1
+  })
+  .then(function () {
+    pool.release(borrowedResources.shift())
+    pool.release(borrowedResources.shift())
+  })
+  .then(function () {
+    return pool.acquire()
+  })
+  .then(function (obj) {
+    // should still be 2
+    t.equal(pool.size, 2)
+    assertionCount += 1
+    pool.release(obj)
+  })
+  .then(function () {
+    t.equal(assertionCount, 4)
+    utils.stopPool(pool)
+    t.end()
+  })
+  .catch(t.threw)
+})
+
+tap.test('availableObjectsCount', function (t) {
+  let assertionCount = 0
+  const resourceFactory = new ResourceFactory()
+  const config = {
+    max: 2
+  }
+
+  const pool = createPool(resourceFactory, config)
+
+  const borrowedResources = []
+
+  t.equal(pool.available, 0)
+  assertionCount += 1
+
+  pool.acquire()
+  .then(function (obj) {
+    borrowedResources.push(obj)
+    t.equal(pool.available, 0)
+    assertionCount += 1
+  }).then(function () {
+    return pool.acquire()
+  })
+  .then(function (obj) {
+    borrowedResources.push(obj)
+    t.equal(pool.available, 0)
+    assertionCount += 1
+  })
+  .then(function () {
+    pool.release(borrowedResources.shift())
+    t.equal(pool.available, 1)
+    assertionCount += 1
+
+    pool.release(borrowedResources.shift())
+    t.equal(pool.available, 2)
+    assertionCount += 1
+  })
+  .then(function () {
+    return pool.acquire()
+  })
+  .then(function (obj) {
+    t.equal(pool.available, 1)
+    assertionCount += 1
+    pool.release(obj)
+
+    t.equal(pool.available, 2)
+    assertionCount += 1
+  })
+  .then(function () {
+    t.equal(assertionCount, 7)
+    utils.stopPool(pool)
+    t.end()
+  })
+  .catch(t.threw)
+})
+
+// FIXME: bad test!
+// pool.destroy makes no obligations to user about when it will destroy the resource
+// we should test that "destroyed" objects are not acquired again instead
+// tap.test('removes from available objects on destroy', function (t) {
+//   let destroyCalled = false
+//   const factory = {
+//     create: function () { return Promise.resolve({}) },
+//     destroy: function (client) { destroyCalled = true; return Promise.resolve() }
+//   }
+
+//   const config
+//     max: 2
+//   }
+
+//   const pool = createPool(factory, config)
+
+//   pool.acquire().then(function (obj) {
+//     pool.destroy(obj)
+//   })
+//   .then(function () {
+//     t.equal(destroyCalled, true)
+//     t.equal(pool.available, 0)
+//     utils.stopPool(pool)
+//     t.end()
+//   })
+//   .catch(t.threw)
+// })
+
+// FIXME: bad test!
+// pool.destroy makes no obligations to user about when it will destroy the resource
+// we should test that "destroyed" objects are not acquired again instead
+// tap.test('removes from available objects on validation failure', function (t) {
+//   const destroyCalled = false
+//   const validateCalled = false
+//   const count = 0
+//   const factory = {
+//     create: function () { return Promise.resolve({count: count++}) },
+//     destroy: function (client) { destroyCalled = client.count },
+//     validate: function (client) {
+//       validateCalled = true
+//       return Promise.resolve(client.count > 0)
+//     }
+//   }
+
+//   const config
+//     max: 2,
+//     testOnBorrow: true
+//   }
+
+//   const pool = createPool(factory, config)
+
+//   pool.acquire()
+//   .then(function (obj) {
+//     pool.release(obj)
+//     t.equal(obj.count, 0)
+//   })
+//   .then(function () {
+//     return pool.acquire()
+//   })
+//   .then(function (obj2) {
+//     pool.release(obj2)
+//     t.equal(obj2.count, 1)
+//   })
+//   .then(function () {
+//     t.equal(validateCalled, true)
+//     t.equal(destroyCalled, 0)
+//     t.equal(pool.available, 1)
+//     utils.stopPool(pool)
+//     t.end()
+//   })
+//   .catch(t.threw)
+// })
+
+tap.test('do schedule again if error occured when creating new Objects async', function (t) {
+  // NOTE: we're simulating the first few resource attempts failing
+  let resourceCreationAttempts = 0
+
+  const factory = {
+    create: function () {
+      resourceCreationAttempts++
+      if (resourceCreationAttempts < 2) {
+        return Promise.reject(new Error('Create Error'))
+      }
+      return Promise.resolve({})
+    },
+    destroy: function (client) {}
+  }
+
+  const config = {
+    max: 1
+  }
+
+  const pool = createPool(factory, config)
+  // pool.acquire(function () {})
+  pool.acquire().then(function (obj) {
+    t.equal(pool.available, 0)
+    pool.release(obj)
+    utils.stopPool(pool)
+    t.end()
+  }).catch(t.threw)
+})
+
+tap.test('returns only valid object to the pool', function (t) {
+  const pool = createPool({
+    create: function () {
+      return Promise.resolve({ id: 'validId' })
+    },
+    destroy: function (client) {},
+    max: 1
+  })
+
+  pool.acquire().then(function (obj) {
+    t.equal(pool.available, 0)
+    t.equal(pool.borrowed, 1)
+
+      // Invalid release
+    pool.release({})
+    t.equal(pool.available, 0)
+    t.equal(pool.borrowed, 1)
+
+      // Valid release
+    pool.release(obj)
+    t.equal(pool.available, 1)
+    t.equal(pool.borrowed, 0)
+    utils.stopPool(pool)
+    t.end()
+  }).catch(t.threw)
+})
+
+tap.test('validate acquires object from the pool', function (t) {
+  const pool = createPool({
+    create: function () {
+      return Promise.resolve({ id: 'validId' })
+    },
+    validate: function (resource) {
+      return Promise.resolve(true)
+    },
+    destroy: function (client) {},
+    max: 1
+  })
+
+  pool.acquire()
+  .then(function (obj) {
+    t.equal(pool.available, 0)
+    t.equal(pool.borrowed, 1)
+    pool.release(obj)
+    utils.stopPool(pool)
+    t.end()
+  })
+  .catch(t.threw)
+})
diff --git a/test/generic-pool.test.js b/test/generic-pool.test.js
deleted file mode 100644
index d7ac17a..0000000
--- a/test/generic-pool.test.js
+++ /dev/null
@@ -1,619 +0,0 @@
-var 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);   
-       });
-    }
-
-
-};
diff --git a/test/resource-request-test.js b/test/resource-request-test.js
new file mode 100644
index 0000000..d26fb40
--- /dev/null
+++ b/test/resource-request-test.js
@@ -0,0 +1,60 @@
+var tap = require('tap')
+var ResourceRequest = require('../lib/ResourceRequest')
+
+tap.test('can be created', function (t) {
+  var create = function () {
+    var request = new ResourceRequest(undefined, Promise) // eslint-disable-line no-unused-vars
+  }
+  t.doesNotThrow(create)
+  t.end()
+})
+
+tap.test('times out when created with a ttl', function (t) {
+  var reject = function (err) {
+    t.match(err, /ResourceRequest timed out/)
+    t.end()
+  }
+  var resolve = function (r) {
+    t.fail('should not resolve')
+  }
+  var request = new ResourceRequest(10, Promise) // eslint-disable-line no-unused-vars
+
+  request.promise.then(resolve, reject)
+})
+
+tap.test('calls resolve when resolved', function (t) {
+  var resource = {}
+  var resolve = function (r) {
+    t.equal(r, resource)
+    t.end()
+  }
+  var reject = function (err) {
+    t.error(err)
+  }
+  var request = new ResourceRequest(undefined, Promise)
+  request.promise.then(resolve, reject)
+  request.resolve(resource)
+})
+
+tap.test('removeTimeout removes the timeout', function (t) {
+  var reject = function (err) {
+    t.error(err)
+  }
+  var request = new ResourceRequest(10, Promise)
+  request.promise.then(undefined, reject)
+  request.removeTimeout()
+  setTimeout(function () {
+    t.end()
+  }, 20)
+})
+
+tap.test('does nothing if resolved more than once', function (t) {
+  var request = new ResourceRequest(undefined, Promise)
+  t.doesNotThrow(function () {
+    request.resolve({})
+  })
+  t.doesNotThrow(function () {
+    request.resolve({})
+  })
+  t.end()
+})
diff --git a/test/utils.js b/test/utils.js
new file mode 100644
index 0000000..2bb9640
--- /dev/null
+++ b/test/utils.js
@@ -0,0 +1,38 @@
+/**
+ * Generic class for handling creation of resources
+ * for testing
+ */
+var ResourceFactory = function ResourceFactory () {
+  this.created = 0
+  this.destroyed = 0
+  this.bin = []
+}
+
+ResourceFactory.prototype.create = function () {
+  var id = this.created++
+  var resource = {
+    id: id
+  }
+  return Promise.resolve(resource)
+}
+
+ResourceFactory.prototype.destroy = function (resource) {
+  this.destroyed++
+  this.bin.push(resource)
+  return Promise.resolve()
+}
+
+exports.ResourceFactory = ResourceFactory
+
+/**
+ * drains and terminates the pool
+ *
+ * @param  {[type]} pool [description]
+ * @return {[type]}      [description]
+ */
+exports.stopPool = function (pool) {
+  return pool.drain()
+  .then(function () {
+    return pool.clear()
+  })
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/collab-maint/node-generic-pool.git



More information about the Pkg-javascript-commits mailing list