From 2ac9db93305fc7374b56cc121d184ec26f069aa2 Mon Sep 17 00:00:00 2001 From: Roy Binux Date: Wed, 28 Dec 2016 16:06:16 +0000 Subject: [PATCH] pool.destroy didn't re-dispense resources (#173) * pool.destroy didn't trigger dispensing resources --- lib/Pool.js | 2 ++ test/generic-pool-test.js | 66 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/lib/Pool.js b/lib/Pool.js index 42b59322..6b1a9961 100644 --- a/lib/Pool.js +++ b/lib/Pool.js @@ -455,6 +455,8 @@ class Pool extends EventEmitter { pooledResource.deallocate() this._destroy(pooledResource) + + this._dispense() return this._Promise.resolve() } diff --git a/test/generic-pool-test.js b/test/generic-pool-test.js index 49f327aa..02dbcb00 100644 --- a/test/generic-pool-test.js +++ b/test/generic-pool-test.js @@ -598,3 +598,69 @@ tap.test('validate acquires object from the pool', function (t) { }) .catch(t.threw) }) + +tap.test('release to pool should work', 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) + t.equal(pool.pending, 1) + pool.release(obj) + }) + .catch(t.threw) + + pool.acquire() + .then(function (obj) { + t.equal(pool.available, 0) + t.equal(pool.borrowed, 1) + t.equal(pool.pending, 0) + pool.release(obj) + utils.stopPool(pool) + t.end() + }) + .catch(t.threw) +}) + +tap.test('destroy should redispense', 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) + t.equal(pool.pending, 1) + pool.destroy(obj) + }) + .catch(t.threw) + + pool.acquire() + .then(function (obj) { + t.equal(pool.available, 0) + t.equal(pool.borrowed, 1) + t.equal(pool.pending, 0) + pool.release(obj) + utils.stopPool(pool) + t.end() + }) + .catch(t.threw) +})