releasing-clients.js 1.7 KB

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