|
| 1 | +const expect = require('expect.js') |
| 2 | +const co = require('co') |
| 3 | +const _ = require('lodash') |
| 4 | + |
| 5 | +const describe = require('mocha').describe |
| 6 | +const it = require('mocha').it |
| 7 | + |
| 8 | +const Pool = require('../') |
| 9 | + |
| 10 | +describe('maxUses', () => { |
| 11 | + it('can create a single client and use it once', co.wrap(function * () { |
| 12 | + const pool = new Pool({ maxUses: 2 }) |
| 13 | + expect(pool.waitingCount).to.equal(0) |
| 14 | + const client = yield pool.connect() |
| 15 | + const res = yield client.query('SELECT $1::text as name', ['hi']) |
| 16 | + expect(res.rows[0].name).to.equal('hi') |
| 17 | + client.release() |
| 18 | + pool.end() |
| 19 | + })) |
| 20 | + |
| 21 | + it('getting a connection a second time returns the same connection and releasing it also closes it', co.wrap(function * () { |
| 22 | + const pool = new Pool({ maxUses: 2 }) |
| 23 | + expect(pool.waitingCount).to.equal(0) |
| 24 | + const client = yield pool.connect() |
| 25 | + client.release() |
| 26 | + const client2 = yield pool.connect() |
| 27 | + expect(client).to.equal(client2) |
| 28 | + expect(client2._ending).to.equal(false) |
| 29 | + client2.release() |
| 30 | + expect(client2._ending).to.equal(true) |
| 31 | + return yield pool.end() |
| 32 | + })) |
| 33 | + |
| 34 | + it('getting a connection a third time returns a new connection', co.wrap(function * () { |
| 35 | + const pool = new Pool({ maxUses: 2 }) |
| 36 | + expect(pool.waitingCount).to.equal(0) |
| 37 | + const client = yield pool.connect() |
| 38 | + client.release() |
| 39 | + const client2 = yield pool.connect() |
| 40 | + expect(client).to.equal(client2) |
| 41 | + client2.release() |
| 42 | + const client3 = yield pool.connect() |
| 43 | + expect(client3).not.to.equal(client2) |
| 44 | + client3.release() |
| 45 | + return yield pool.end() |
| 46 | + })) |
| 47 | + |
| 48 | + it('getting a connection from a pending request gets a fresh client when the released candidate is expended', co.wrap(function * () { |
| 49 | + const pool = new Pool({ max: 1, maxUses: 2 }) |
| 50 | + expect(pool.waitingCount).to.equal(0) |
| 51 | + const client1 = yield pool.connect() |
| 52 | + pool.connect() |
| 53 | + .then(client2 => { |
| 54 | + expect(client2).to.equal(client1) |
| 55 | + expect(pool.waitingCount).to.equal(1) |
| 56 | + // Releasing the client this time should also expend it since maxUses is 2, causing client3 to be a fresh client |
| 57 | + client2.release() |
| 58 | + }) |
| 59 | + const client3Promise = pool.connect() |
| 60 | + .then(client3 => { |
| 61 | + // client3 should be a fresh client since client2's release caused the first client to be expended |
| 62 | + expect(pool.waitingCount).to.equal(0) |
| 63 | + expect(client3).not.to.equal(client1) |
| 64 | + return client3.release() |
| 65 | + }) |
| 66 | + // There should be two pending requests since we have 3 connect requests but a max size of 1 |
| 67 | + expect(pool.waitingCount).to.equal(2) |
| 68 | + // Releasing the client should not yet expend it since maxUses is 2 |
| 69 | + client1.release() |
| 70 | + yield client3Promise |
| 71 | + return yield pool.end() |
| 72 | + })) |
| 73 | + |
| 74 | + it('logs when removing an expended client', co.wrap(function * () { |
| 75 | + const messages = [] |
| 76 | + const log = function (msg) { |
| 77 | + messages.push(msg) |
| 78 | + } |
| 79 | + const pool = new Pool({ maxUses: 1, log }) |
| 80 | + const client = yield pool.connect() |
| 81 | + client.release() |
| 82 | + expect(messages).to.contain('remove expended client') |
| 83 | + return yield pool.end() |
| 84 | + })) |
| 85 | +}) |
0 commit comments