max-uses.js 3.1 KB

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