max-uses.js 3.1 KB

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