Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add connection lifetime limit option and tests (#2698)
Co-authored-by: ChrisG0x20 <position0x45@hotmail.com>
- Loading branch information
1 parent
5508c0e
commit 8392918
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
'use strict' | ||
const co = require('co') | ||
const expect = require('expect.js') | ||
|
||
const describe = require('mocha').describe | ||
const it = require('mocha').it | ||
const path = require('path') | ||
|
||
const Pool = require('../') | ||
|
||
describe('lifetime timeout', () => { | ||
it('connection lifetime should expire and remove the client', (done) => { | ||
const pool = new Pool({ maxLifetimeSeconds: 1 }) | ||
pool.query('SELECT NOW()') | ||
pool.on('remove', () => { | ||
console.log('expired while idle - on-remove event') | ||
expect(pool.expiredCount).to.equal(0) | ||
expect(pool.totalCount).to.equal(0) | ||
done() | ||
}) | ||
}) | ||
it('connection lifetime should expire and remove the client after the client is done working', (done) => { | ||
const pool = new Pool({ maxLifetimeSeconds: 1 }) | ||
pool.query('SELECT pg_sleep(1.01)') | ||
pool.on('remove', () => { | ||
console.log('expired while busy - on-remove event') | ||
expect(pool.expiredCount).to.equal(0) | ||
expect(pool.totalCount).to.equal(0) | ||
done() | ||
}) | ||
}) | ||
it('can remove expired clients and recreate them', | ||
co.wrap(function* () { | ||
const pool = new Pool({ maxLifetimeSeconds: 1 }) | ||
let query = pool.query('SELECT pg_sleep(1)') | ||
expect(pool.expiredCount).to.equal(0) | ||
expect(pool.totalCount).to.equal(1) | ||
yield query | ||
expect(pool.expiredCount).to.equal(0) | ||
expect(pool.totalCount).to.equal(0) | ||
yield pool.query('SELECT NOW()') | ||
expect(pool.expiredCount).to.equal(0) | ||
expect(pool.totalCount).to.equal(1) | ||
}) | ||
) | ||
}) |