|
| 1 | +const Pool = require('../') |
| 2 | + |
| 3 | +const expect = require('expect.js') |
| 4 | +const net = require('net') |
| 5 | + |
| 6 | +describe('releasing clients', () => { |
| 7 | + it('removes a client which cannot be queried', async () => { |
| 8 | + // make a pool w/ only 1 client |
| 9 | + const pool = new Pool({ max: 1 }) |
| 10 | + expect(pool.totalCount).to.eql(0) |
| 11 | + const client = await pool.connect() |
| 12 | + expect(pool.totalCount).to.eql(1) |
| 13 | + expect(pool.idleCount).to.eql(0) |
| 14 | + // reach into the client and sever its connection |
| 15 | + client.connection.end() |
| 16 | + |
| 17 | + // wait for the client to error out |
| 18 | + const err = await new Promise((resolve) => client.once('error', resolve)) |
| 19 | + expect(err).to.be.ok() |
| 20 | + expect(pool.totalCount).to.eql(1) |
| 21 | + expect(pool.idleCount).to.eql(0) |
| 22 | + |
| 23 | + // try to return it to the pool - this removes it because its broken |
| 24 | + client.release() |
| 25 | + expect(pool.totalCount).to.eql(0) |
| 26 | + expect(pool.idleCount).to.eql(0) |
| 27 | + |
| 28 | + // make sure pool still works |
| 29 | + const { rows } = await pool.query('SELECT NOW()') |
| 30 | + expect(rows).to.have.length(1) |
| 31 | + await pool.end() |
| 32 | + }) |
| 33 | + |
| 34 | + it('removes a client which is ending', async () => { |
| 35 | + // make a pool w/ only 1 client |
| 36 | + const pool = new Pool({ max: 1 }) |
| 37 | + expect(pool.totalCount).to.eql(0) |
| 38 | + const client = await pool.connect() |
| 39 | + expect(pool.totalCount).to.eql(1) |
| 40 | + expect(pool.idleCount).to.eql(0) |
| 41 | + // end the client gracefully (but you shouldn't do this with pooled clients) |
| 42 | + client.end() |
| 43 | + |
| 44 | + // try to return it to the pool |
| 45 | + client.release() |
| 46 | + expect(pool.totalCount).to.eql(0) |
| 47 | + expect(pool.idleCount).to.eql(0) |
| 48 | + |
| 49 | + // make sure pool still works |
| 50 | + const { rows } = await pool.query('SELECT NOW()') |
| 51 | + expect(rows).to.have.length(1) |
| 52 | + await pool.end() |
| 53 | + }) |
| 54 | +}) |
0 commit comments