releasing-clients.js 1.6 KB

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