lifetime-timeout.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. 'use strict'
  2. const co = require('co')
  3. const expect = require('expect.js')
  4. const describe = require('mocha').describe
  5. const it = require('mocha').it
  6. const path = require('path')
  7. const Pool = require('../')
  8. describe('lifetime timeout', () => {
  9. it('connection lifetime should expire and remove the client', (done) => {
  10. const pool = new Pool({ maxLifetimeSeconds: 1 })
  11. pool.query('SELECT NOW()')
  12. pool.on('remove', () => {
  13. console.log('expired while idle - on-remove event')
  14. expect(pool.expiredCount).to.equal(0)
  15. expect(pool.totalCount).to.equal(0)
  16. done()
  17. })
  18. })
  19. it('connection lifetime should expire and remove the client after the client is done working', (done) => {
  20. const pool = new Pool({ maxLifetimeSeconds: 1 })
  21. pool.query('SELECT pg_sleep(1.01)')
  22. pool.on('remove', () => {
  23. console.log('expired while busy - on-remove event')
  24. expect(pool.expiredCount).to.equal(0)
  25. expect(pool.totalCount).to.equal(0)
  26. done()
  27. })
  28. })
  29. it(
  30. 'can remove expired clients and recreate them',
  31. co.wrap(function* () {
  32. const pool = new Pool({ maxLifetimeSeconds: 1 })
  33. let query = pool.query('SELECT pg_sleep(1)')
  34. expect(pool.expiredCount).to.equal(0)
  35. expect(pool.totalCount).to.equal(1)
  36. yield query
  37. expect(pool.expiredCount).to.equal(0)
  38. expect(pool.totalCount).to.equal(0)
  39. yield pool.query('SELECT NOW()')
  40. expect(pool.expiredCount).to.equal(0)
  41. expect(pool.totalCount).to.equal(1)
  42. })
  43. )
  44. })