UNPKG

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