sizing.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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('pool size of 1', () => {
  8. it(
  9. 'can create a single client and use it once',
  10. co.wrap(function* () {
  11. const pool = new Pool({ max: 1 })
  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. 'can create a single client and use it multiple times',
  22. co.wrap(function* () {
  23. const pool = new Pool({ max: 1 })
  24. expect(pool.waitingCount).to.equal(0)
  25. const client = yield pool.connect()
  26. const wait = pool.connect()
  27. expect(pool.waitingCount).to.equal(1)
  28. client.release()
  29. const client2 = yield wait
  30. expect(client).to.equal(client2)
  31. client2.release()
  32. return yield pool.end()
  33. })
  34. )
  35. it(
  36. 'can only send 1 query at a time',
  37. co.wrap(function* () {
  38. const pool = new Pool({ max: 1 })
  39. // the query text column name changed in PostgreSQL 9.2
  40. const versionResult = yield pool.query('SHOW server_version_num')
  41. const version = parseInt(versionResult.rows[0].server_version_num, 10)
  42. const queryColumn = version < 90200 ? 'current_query' : 'query'
  43. const queryText = 'SELECT COUNT(*) as counts FROM pg_stat_activity WHERE ' + queryColumn + ' = $1'
  44. const queries = _.times(20, () => pool.query(queryText, [queryText]))
  45. const results = yield Promise.all(queries)
  46. const counts = results.map((res) => parseInt(res.rows[0].counts, 10))
  47. expect(counts).to.eql(_.times(20, (i) => 1))
  48. return yield pool.end()
  49. })
  50. )
  51. })