1 | const expect = require('expect.js')
|
2 | const co = require('co')
|
3 | const _ = require('lodash')
|
4 |
|
5 | const describe = require('mocha').describe
|
6 | const it = require('mocha').it
|
7 |
|
8 | const Pool = require('../')
|
9 |
|
10 | describe('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 |
|
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 | })
|