UNPKG

1.77 kBJavaScriptView Raw
1const expect = require('expect.js')
2const co = require('co')
3const _ = require('lodash')
4
5const describe = require('mocha').describe
6const it = require('mocha').it
7
8const Pool = require('../')
9
10describe('pool size of 1', () => {
11 it('can create a single client and use it once', co.wrap(function * () {
12 const pool = new Pool({ max: 1 })
13 expect(pool.waitingCount).to.equal(0)
14 const client = yield pool.connect()
15 const res = yield client.query('SELECT $1::text as name', ['hi'])
16 expect(res.rows[0].name).to.equal('hi')
17 client.release()
18 pool.end()
19 }))
20
21 it('can create a single client and use it multiple times', co.wrap(function * () {
22 const pool = new Pool({ max: 1 })
23 expect(pool.waitingCount).to.equal(0)
24 const client = yield pool.connect()
25 const wait = pool.connect()
26 expect(pool.waitingCount).to.equal(1)
27 client.release()
28 const client2 = yield wait
29 expect(client).to.equal(client2)
30 client2.release()
31 return yield pool.end()
32 }))
33
34 it('can only send 1 query at a time', co.wrap(function * () {
35 const pool = new Pool({ max: 1 })
36
37 // the query text column name changed in PostgreSQL 9.2
38 const versionResult = yield pool.query('SHOW server_version_num')
39 const version = parseInt(versionResult.rows[0].server_version_num, 10)
40 const queryColumn = version < 90200 ? 'current_query' : 'query'
41
42 const queryText = 'SELECT COUNT(*) as counts FROM pg_stat_activity WHERE ' + queryColumn + ' = $1'
43 const queries = _.times(20, () =>
44 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})