Skip to content

Commit 48ed190

Browse files
committed
Write tests for change
1 parent b9f67a0 commit 48ed190

File tree

3 files changed

+58
-58
lines changed

3 files changed

+58
-58
lines changed

packages/pg-pool/index.js

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,10 @@ class PendingItem {
2525
}
2626
}
2727

28-
function throwOnRelease () {
28+
function throwOnDoubleRelease () {
2929
throw new Error('Release called on client which has already been released to the pool.')
3030
}
3131

32-
function release (client, err) {
33-
client.release = throwOnRelease
34-
if (err || this.ending || !client._queryable || client._ending) {
35-
this._remove(client)
36-
this._pulseQueue()
37-
return
38-
}
39-
40-
// idle timeout
41-
let tid
42-
if (this.options.idleTimeoutMillis) {
43-
tid = setTimeout(() => {
44-
this.log('remove idle client')
45-
this._remove(client)
46-
}, this.options.idleTimeoutMillis)
47-
}
48-
49-
this._idle.push(new IdleItem(client, tid))
50-
this._pulseQueue()
51-
}
52-
5332
function promisify (Promise, callback) {
5433
if (callback) {
5534
return { callback: callback, result: undefined }
@@ -281,7 +260,7 @@ class Pool extends EventEmitter {
281260

282261
client.release = (err) => {
283262
if (released) {
284-
throw new Error('Release called on client which has already been released to the pool.')
263+
throwOnDoubleRelease()
285264
}
286265

287266
released = true
@@ -317,7 +296,8 @@ class Pool extends EventEmitter {
317296
_release (client, idleListener, err) {
318297
client.on('error', idleListener)
319298

320-
if (err || this.ending) {
299+
// TODO(bmc): expose a proper, public interface _queryable and _ending
300+
if (err || this.ending || !client._queryable || client._ending) {
321301
this._remove(client)
322302
this._pulseQueue()
323303
return

packages/pg-pool/test/error-handling.js

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -181,40 +181,6 @@ describe('pool error handling', function () {
181181
})
182182
})
183183

184-
describe('releasing a not queryable client', () => {
185-
it('removes the client from the pool', (done) => {
186-
const pool = new Pool({ max: 1 })
187-
const connectionError = new Error('connection failed')
188-
189-
pool.once('error', () => {
190-
// Ignore error on pool
191-
})
192-
193-
pool.connect((err, client) => {
194-
expect(err).to.be(undefined)
195-
196-
client.once('error', (err) => {
197-
expect(err).to.eql(connectionError)
198-
199-
// Releasing the client should remove it from the pool,
200-
// whether called with an error or not
201-
client.release()
202-
203-
// Verify that the pool is still usuable and new client has been
204-
// created
205-
pool.query('SELECT $1::text as name', ['brianc'], (err, res) => {
206-
expect(err).to.be(undefined)
207-
expect(res.rows).to.eql([{ name: 'brianc' }])
208-
209-
pool.end(done)
210-
})
211-
})
212-
213-
client.emit('error', connectionError)
214-
})
215-
})
216-
})
217-
218184
describe('pool with lots of errors', () => {
219185
it('continues to work and provide new clients', co.wrap(function* () {
220186
const pool = new Pool({ max: 1 })
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const Pool = require('../')
2+
3+
const expect = require('expect.js')
4+
const net = require('net')
5+
6+
describe('releasing clients', () => {
7+
it('removes a client which cannot be queried', async () => {
8+
// make a pool w/ only 1 client
9+
const pool = new Pool({ max: 1 })
10+
expect(pool.totalCount).to.eql(0)
11+
const client = await pool.connect()
12+
expect(pool.totalCount).to.eql(1)
13+
expect(pool.idleCount).to.eql(0)
14+
// reach into the client and sever its connection
15+
client.connection.end()
16+
17+
// wait for the client to error out
18+
const err = await new Promise((resolve) => client.once('error', resolve))
19+
expect(err).to.be.ok()
20+
expect(pool.totalCount).to.eql(1)
21+
expect(pool.idleCount).to.eql(0)
22+
23+
// try to return it to the pool - this removes it because its broken
24+
client.release()
25+
expect(pool.totalCount).to.eql(0)
26+
expect(pool.idleCount).to.eql(0)
27+
28+
// make sure pool still works
29+
const { rows } = await pool.query('SELECT NOW()')
30+
expect(rows).to.have.length(1)
31+
await pool.end()
32+
})
33+
34+
it('removes a client which is ending', async () => {
35+
// make a pool w/ only 1 client
36+
const pool = new Pool({ max: 1 })
37+
expect(pool.totalCount).to.eql(0)
38+
const client = await pool.connect()
39+
expect(pool.totalCount).to.eql(1)
40+
expect(pool.idleCount).to.eql(0)
41+
// end the client gracefully (but you shouldn't do this with pooled clients)
42+
client.end()
43+
44+
// try to return it to the bool
45+
client.release()
46+
expect(pool.totalCount).to.eql(0)
47+
expect(pool.idleCount).to.eql(0)
48+
49+
// make sure pool still works
50+
const { rows } = await pool.query('SELECT NOW()')
51+
expect(rows).to.have.length(1)
52+
await pool.end()
53+
})
54+
})

0 commit comments

Comments
 (0)