UNPKG

3.22 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('maxUses', () => {
11 it(
12 'can create a single client and use it once',
13 co.wrap(function* () {
14 const pool = new Pool({ maxUses: 2 })
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 'getting a connection a second time returns the same connection and releasing it also closes it',
26 co.wrap(function* () {
27 const pool = new Pool({ maxUses: 2 })
28 expect(pool.waitingCount).to.equal(0)
29 const client = yield pool.connect()
30 client.release()
31 const client2 = yield pool.connect()
32 expect(client).to.equal(client2)
33 expect(client2._ending).to.equal(false)
34 client2.release()
35 expect(client2._ending).to.equal(true)
36 return yield pool.end()
37 })
38 )
39
40 it(
41 'getting a connection a third time returns a new connection',
42 co.wrap(function* () {
43 const pool = new Pool({ maxUses: 2 })
44 expect(pool.waitingCount).to.equal(0)
45 const client = yield pool.connect()
46 client.release()
47 const client2 = yield pool.connect()
48 expect(client).to.equal(client2)
49 client2.release()
50 const client3 = yield pool.connect()
51 expect(client3).not.to.equal(client2)
52 client3.release()
53 return yield pool.end()
54 })
55 )
56
57 it(
58 'getting a connection from a pending request gets a fresh client when the released candidate is expended',
59 co.wrap(function* () {
60 const pool = new Pool({ max: 1, maxUses: 2 })
61 expect(pool.waitingCount).to.equal(0)
62 const client1 = yield pool.connect()
63 pool.connect().then((client2) => {
64 expect(client2).to.equal(client1)
65 expect(pool.waitingCount).to.equal(1)
66 // Releasing the client this time should also expend it since maxUses is 2, causing client3 to be a fresh client
67 client2.release()
68 })
69 const client3Promise = pool.connect().then((client3) => {
70 // client3 should be a fresh client since client2's release caused the first client to be expended
71 expect(pool.waitingCount).to.equal(0)
72 expect(client3).not.to.equal(client1)
73 return client3.release()
74 })
75 // There should be two pending requests since we have 3 connect requests but a max size of 1
76 expect(pool.waitingCount).to.equal(2)
77 // Releasing the client should not yet expend it since maxUses is 2
78 client1.release()
79 yield client3Promise
80 return yield pool.end()
81 })
82 )
83
84 it(
85 'logs when removing an expended client',
86 co.wrap(function* () {
87 const messages = []
88 const log = function (msg) {
89 messages.push(msg)
90 }
91 const pool = new Pool({ maxUses: 1, log })
92 const client = yield pool.connect()
93 client.release()
94 expect(messages).to.contain('remove expended client')
95 return yield pool.end()
96 })
97 )
98})