add websocket server to this dir. fix stuff for client
This commit is contained in:
42
websocket_server/node_modules/pg-pool/test/bring-your-own-promise.js
generated
vendored
Normal file
42
websocket_server/node_modules/pg-pool/test/bring-your-own-promise.js
generated
vendored
Normal file
@ -0,0 +1,42 @@
|
||||
'use strict'
|
||||
const co = require('co')
|
||||
const expect = require('expect.js')
|
||||
|
||||
const describe = require('mocha').describe
|
||||
const it = require('mocha').it
|
||||
const BluebirdPromise = require('bluebird')
|
||||
|
||||
const Pool = require('../')
|
||||
|
||||
const checkType = (promise) => {
|
||||
expect(promise).to.be.a(BluebirdPromise)
|
||||
return promise.catch((e) => undefined)
|
||||
}
|
||||
|
||||
describe('Bring your own promise', function () {
|
||||
it(
|
||||
'uses supplied promise for operations',
|
||||
co.wrap(function* () {
|
||||
const pool = new Pool({ Promise: BluebirdPromise })
|
||||
const client1 = yield checkType(pool.connect())
|
||||
client1.release()
|
||||
yield checkType(pool.query('SELECT NOW()'))
|
||||
const client2 = yield checkType(pool.connect())
|
||||
// TODO - make sure pg supports BYOP as well
|
||||
client2.release()
|
||||
yield checkType(pool.end())
|
||||
})
|
||||
)
|
||||
|
||||
it(
|
||||
'uses promises in errors',
|
||||
co.wrap(function* () {
|
||||
const pool = new Pool({ Promise: BluebirdPromise, port: 48484 })
|
||||
yield checkType(pool.connect())
|
||||
yield checkType(pool.end())
|
||||
yield checkType(pool.connect())
|
||||
yield checkType(pool.query())
|
||||
yield checkType(pool.end())
|
||||
})
|
||||
)
|
||||
})
|
29
websocket_server/node_modules/pg-pool/test/connection-strings.js
generated
vendored
Normal file
29
websocket_server/node_modules/pg-pool/test/connection-strings.js
generated
vendored
Normal file
@ -0,0 +1,29 @@
|
||||
const expect = require('expect.js')
|
||||
const describe = require('mocha').describe
|
||||
const it = require('mocha').it
|
||||
const Pool = require('../')
|
||||
|
||||
describe('Connection strings', function () {
|
||||
it('pool delegates connectionString property to client', function (done) {
|
||||
const connectionString = 'postgres://foo:bar@baz:1234/xur'
|
||||
|
||||
const pool = new Pool({
|
||||
// use a fake client so we can check we're passed the connectionString
|
||||
Client: function (args) {
|
||||
expect(args.connectionString).to.equal(connectionString)
|
||||
return {
|
||||
connect: function (cb) {
|
||||
cb(new Error('testing'))
|
||||
},
|
||||
on: function () {},
|
||||
}
|
||||
},
|
||||
connectionString: connectionString,
|
||||
})
|
||||
|
||||
pool.connect(function (err, client) {
|
||||
expect(err).to.not.be(undefined)
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
229
websocket_server/node_modules/pg-pool/test/connection-timeout.js
generated
vendored
Normal file
229
websocket_server/node_modules/pg-pool/test/connection-timeout.js
generated
vendored
Normal file
@ -0,0 +1,229 @@
|
||||
'use strict'
|
||||
const net = require('net')
|
||||
const co = require('co')
|
||||
const expect = require('expect.js')
|
||||
|
||||
const describe = require('mocha').describe
|
||||
const it = require('mocha').it
|
||||
const before = require('mocha').before
|
||||
const after = require('mocha').after
|
||||
|
||||
const Pool = require('../')
|
||||
|
||||
describe('connection timeout', () => {
|
||||
const connectionFailure = new Error('Temporary connection failure')
|
||||
|
||||
before((done) => {
|
||||
this.server = net.createServer((socket) => {
|
||||
socket.on('data', () => {
|
||||
// discard any buffered data or the server wont terminate
|
||||
})
|
||||
})
|
||||
|
||||
this.server.listen(() => {
|
||||
this.port = this.server.address().port
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
after((done) => {
|
||||
this.server.close(done)
|
||||
})
|
||||
|
||||
it('should callback with an error if timeout is passed', (done) => {
|
||||
const pool = new Pool({ connectionTimeoutMillis: 10, port: this.port, host: 'localhost' })
|
||||
pool.connect((err, client, release) => {
|
||||
expect(err).to.be.an(Error)
|
||||
expect(err.message).to.contain('timeout')
|
||||
expect(client).to.equal(undefined)
|
||||
expect(pool.idleCount).to.equal(0)
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should reject promise with an error if timeout is passed', (done) => {
|
||||
const pool = new Pool({ connectionTimeoutMillis: 10, port: this.port, host: 'localhost' })
|
||||
pool.connect().catch((err) => {
|
||||
expect(err).to.be.an(Error)
|
||||
expect(err.message).to.contain('timeout')
|
||||
expect(pool.idleCount).to.equal(0)
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it(
|
||||
'should handle multiple timeouts',
|
||||
co.wrap(
|
||||
function* () {
|
||||
const errors = []
|
||||
const pool = new Pool({ connectionTimeoutMillis: 1, port: this.port, host: 'localhost' })
|
||||
for (var i = 0; i < 15; i++) {
|
||||
try {
|
||||
yield pool.connect()
|
||||
} catch (e) {
|
||||
errors.push(e)
|
||||
}
|
||||
}
|
||||
expect(errors).to.have.length(15)
|
||||
}.bind(this)
|
||||
)
|
||||
)
|
||||
|
||||
it('should timeout on checkout of used connection', (done) => {
|
||||
const pool = new Pool({ connectionTimeoutMillis: 100, max: 1 })
|
||||
pool.connect((err, client, release) => {
|
||||
expect(err).to.be(undefined)
|
||||
expect(client).to.not.be(undefined)
|
||||
pool.connect((err, client) => {
|
||||
expect(err).to.be.an(Error)
|
||||
expect(client).to.be(undefined)
|
||||
release()
|
||||
pool.end(done)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('should not break further pending checkouts on a timeout', (done) => {
|
||||
const pool = new Pool({ connectionTimeoutMillis: 200, max: 1 })
|
||||
pool.connect((err, client, releaseOuter) => {
|
||||
expect(err).to.be(undefined)
|
||||
|
||||
pool.connect((err, client) => {
|
||||
expect(err).to.be.an(Error)
|
||||
expect(client).to.be(undefined)
|
||||
releaseOuter()
|
||||
})
|
||||
|
||||
setTimeout(() => {
|
||||
pool.connect((err, client, releaseInner) => {
|
||||
expect(err).to.be(undefined)
|
||||
expect(client).to.not.be(undefined)
|
||||
releaseInner()
|
||||
pool.end(done)
|
||||
})
|
||||
}, 100)
|
||||
})
|
||||
})
|
||||
|
||||
it('should timeout on query if all clients are busy', (done) => {
|
||||
const pool = new Pool({ connectionTimeoutMillis: 100, max: 1 })
|
||||
pool.connect((err, client, release) => {
|
||||
expect(err).to.be(undefined)
|
||||
expect(client).to.not.be(undefined)
|
||||
pool.query('select now()', (err, result) => {
|
||||
expect(err).to.be.an(Error)
|
||||
expect(result).to.be(undefined)
|
||||
release()
|
||||
pool.end(done)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('should recover from timeout errors', (done) => {
|
||||
const pool = new Pool({ connectionTimeoutMillis: 100, max: 1 })
|
||||
pool.connect((err, client, release) => {
|
||||
expect(err).to.be(undefined)
|
||||
expect(client).to.not.be(undefined)
|
||||
pool.query('select now()', (err, result) => {
|
||||
expect(err).to.be.an(Error)
|
||||
expect(result).to.be(undefined)
|
||||
release()
|
||||
pool.query('select $1::text as name', ['brianc'], (err, res) => {
|
||||
expect(err).to.be(undefined)
|
||||
expect(res.rows).to.have.length(1)
|
||||
pool.end(done)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('continues processing after a connection failure', (done) => {
|
||||
const Client = require('pg').Client
|
||||
const orgConnect = Client.prototype.connect
|
||||
let called = false
|
||||
|
||||
Client.prototype.connect = function (cb) {
|
||||
// Simulate a failure on first call
|
||||
if (!called) {
|
||||
called = true
|
||||
|
||||
return setTimeout(() => {
|
||||
cb(connectionFailure)
|
||||
}, 100)
|
||||
}
|
||||
// And pass-through the second call
|
||||
orgConnect.call(this, cb)
|
||||
}
|
||||
|
||||
const pool = new Pool({
|
||||
Client: Client,
|
||||
connectionTimeoutMillis: 1000,
|
||||
max: 1,
|
||||
})
|
||||
|
||||
pool.connect((err, client, release) => {
|
||||
expect(err).to.be(connectionFailure)
|
||||
|
||||
pool.query('select $1::text as name', ['brianc'], (err, res) => {
|
||||
expect(err).to.be(undefined)
|
||||
expect(res.rows).to.have.length(1)
|
||||
pool.end(done)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('releases newly connected clients if the queued already timed out', (done) => {
|
||||
const Client = require('pg').Client
|
||||
|
||||
const orgConnect = Client.prototype.connect
|
||||
|
||||
let connection = 0
|
||||
|
||||
Client.prototype.connect = function (cb) {
|
||||
// Simulate a failure on first call
|
||||
if (connection === 0) {
|
||||
connection++
|
||||
|
||||
return setTimeout(() => {
|
||||
cb(connectionFailure)
|
||||
}, 300)
|
||||
}
|
||||
|
||||
// And second connect taking > connection timeout
|
||||
if (connection === 1) {
|
||||
connection++
|
||||
|
||||
return setTimeout(() => {
|
||||
orgConnect.call(this, cb)
|
||||
}, 1000)
|
||||
}
|
||||
|
||||
orgConnect.call(this, cb)
|
||||
}
|
||||
|
||||
const pool = new Pool({
|
||||
Client: Client,
|
||||
connectionTimeoutMillis: 1000,
|
||||
max: 1,
|
||||
})
|
||||
|
||||
// Direct connect
|
||||
pool.connect((err, client, release) => {
|
||||
expect(err).to.be(connectionFailure)
|
||||
})
|
||||
|
||||
// Queued
|
||||
let called = 0
|
||||
pool.connect((err, client, release) => {
|
||||
// Verify the callback is only called once
|
||||
expect(called++).to.be(0)
|
||||
expect(err).to.be.an(Error)
|
||||
|
||||
pool.query('select $1::text as name', ['brianc'], (err, res) => {
|
||||
expect(err).to.be(undefined)
|
||||
expect(res.rows).to.have.length(1)
|
||||
pool.end(done)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
40
websocket_server/node_modules/pg-pool/test/ending.js
generated
vendored
Normal file
40
websocket_server/node_modules/pg-pool/test/ending.js
generated
vendored
Normal file
@ -0,0 +1,40 @@
|
||||
'use strict'
|
||||
const co = require('co')
|
||||
const expect = require('expect.js')
|
||||
|
||||
const describe = require('mocha').describe
|
||||
const it = require('mocha').it
|
||||
|
||||
const Pool = require('../')
|
||||
|
||||
describe('pool ending', () => {
|
||||
it('ends without being used', (done) => {
|
||||
const pool = new Pool()
|
||||
pool.end(done)
|
||||
})
|
||||
|
||||
it('ends with a promise', () => {
|
||||
return new Pool().end()
|
||||
})
|
||||
|
||||
it(
|
||||
'ends with clients',
|
||||
co.wrap(function* () {
|
||||
const pool = new Pool()
|
||||
const res = yield pool.query('SELECT $1::text as name', ['brianc'])
|
||||
expect(res.rows[0].name).to.equal('brianc')
|
||||
return pool.end()
|
||||
})
|
||||
)
|
||||
|
||||
it(
|
||||
'allows client to finish',
|
||||
co.wrap(function* () {
|
||||
const pool = new Pool()
|
||||
const query = pool.query('SELECT $1::text as name', ['brianc'])
|
||||
yield pool.end()
|
||||
const res = yield query
|
||||
expect(res.rows[0].name).to.equal('brianc')
|
||||
})
|
||||
)
|
||||
})
|
260
websocket_server/node_modules/pg-pool/test/error-handling.js
generated
vendored
Normal file
260
websocket_server/node_modules/pg-pool/test/error-handling.js
generated
vendored
Normal file
@ -0,0 +1,260 @@
|
||||
'use strict'
|
||||
const net = require('net')
|
||||
const co = require('co')
|
||||
const expect = require('expect.js')
|
||||
|
||||
const describe = require('mocha').describe
|
||||
const it = require('mocha').it
|
||||
|
||||
const Pool = require('../')
|
||||
|
||||
describe('pool error handling', function () {
|
||||
it('Should complete these queries without dying', function (done) {
|
||||
const pool = new Pool()
|
||||
let errors = 0
|
||||
let shouldGet = 0
|
||||
function runErrorQuery() {
|
||||
shouldGet++
|
||||
return new Promise(function (resolve, reject) {
|
||||
pool
|
||||
.query("SELECT 'asd'+1 ")
|
||||
.then(function (res) {
|
||||
reject(res) // this should always error
|
||||
})
|
||||
.catch(function (err) {
|
||||
errors++
|
||||
resolve(err)
|
||||
})
|
||||
})
|
||||
}
|
||||
const ps = []
|
||||
for (let i = 0; i < 5; i++) {
|
||||
ps.push(runErrorQuery())
|
||||
}
|
||||
Promise.all(ps).then(function () {
|
||||
expect(shouldGet).to.eql(errors)
|
||||
pool.end(done)
|
||||
})
|
||||
})
|
||||
|
||||
it('Catches errors in client.query', async function () {
|
||||
let caught = false
|
||||
const pool = new Pool()
|
||||
try {
|
||||
await pool.query(null)
|
||||
} catch (e) {
|
||||
caught = true
|
||||
}
|
||||
pool.end()
|
||||
expect(caught).to.be(true)
|
||||
})
|
||||
|
||||
describe('calling release more than once', () => {
|
||||
it(
|
||||
'should throw each time',
|
||||
co.wrap(function* () {
|
||||
const pool = new Pool()
|
||||
const client = yield pool.connect()
|
||||
client.release()
|
||||
expect(() => client.release()).to.throwError()
|
||||
expect(() => client.release()).to.throwError()
|
||||
return yield pool.end()
|
||||
})
|
||||
)
|
||||
|
||||
it('should throw each time with callbacks', function (done) {
|
||||
const pool = new Pool()
|
||||
|
||||
pool.connect(function (err, client, clientDone) {
|
||||
expect(err).not.to.be.an(Error)
|
||||
clientDone()
|
||||
|
||||
expect(() => clientDone()).to.throwError()
|
||||
expect(() => clientDone()).to.throwError()
|
||||
|
||||
pool.end(done)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('using an ended pool', () => {
|
||||
it('rejects all additional promises', (done) => {
|
||||
const pool = new Pool()
|
||||
const promises = []
|
||||
pool.end().then(() => {
|
||||
const squash = (promise) => promise.catch((e) => 'okay!')
|
||||
promises.push(squash(pool.connect()))
|
||||
promises.push(squash(pool.query('SELECT NOW()')))
|
||||
promises.push(squash(pool.end()))
|
||||
Promise.all(promises).then((res) => {
|
||||
expect(res).to.eql(['okay!', 'okay!', 'okay!'])
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('returns an error on all additional callbacks', (done) => {
|
||||
const pool = new Pool()
|
||||
pool.end(() => {
|
||||
pool.query('SELECT *', (err) => {
|
||||
expect(err).to.be.an(Error)
|
||||
pool.connect((err) => {
|
||||
expect(err).to.be.an(Error)
|
||||
pool.end((err) => {
|
||||
expect(err).to.be.an(Error)
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('error from idle client', () => {
|
||||
it(
|
||||
'removes client from pool',
|
||||
co.wrap(function* () {
|
||||
const pool = new Pool()
|
||||
const client = yield pool.connect()
|
||||
expect(pool.totalCount).to.equal(1)
|
||||
expect(pool.waitingCount).to.equal(0)
|
||||
expect(pool.idleCount).to.equal(0)
|
||||
client.release()
|
||||
yield new Promise((resolve, reject) => {
|
||||
process.nextTick(() => {
|
||||
let poolError
|
||||
pool.once('error', (err) => {
|
||||
poolError = err
|
||||
})
|
||||
|
||||
let clientError
|
||||
client.once('error', (err) => {
|
||||
clientError = err
|
||||
})
|
||||
|
||||
client.emit('error', new Error('expected'))
|
||||
|
||||
expect(clientError.message).to.equal('expected')
|
||||
expect(poolError.message).to.equal('expected')
|
||||
expect(pool.idleCount).to.equal(0)
|
||||
expect(pool.totalCount).to.equal(0)
|
||||
pool.end().then(resolve, reject)
|
||||
})
|
||||
})
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
describe('error from in-use client', () => {
|
||||
it(
|
||||
'keeps the client in the pool',
|
||||
co.wrap(function* () {
|
||||
const pool = new Pool()
|
||||
const client = yield pool.connect()
|
||||
expect(pool.totalCount).to.equal(1)
|
||||
expect(pool.waitingCount).to.equal(0)
|
||||
expect(pool.idleCount).to.equal(0)
|
||||
|
||||
yield new Promise((resolve, reject) => {
|
||||
process.nextTick(() => {
|
||||
let poolError
|
||||
pool.once('error', (err) => {
|
||||
poolError = err
|
||||
})
|
||||
|
||||
let clientError
|
||||
client.once('error', (err) => {
|
||||
clientError = err
|
||||
})
|
||||
|
||||
client.emit('error', new Error('expected'))
|
||||
|
||||
expect(clientError.message).to.equal('expected')
|
||||
expect(poolError).not.to.be.ok()
|
||||
expect(pool.idleCount).to.equal(0)
|
||||
expect(pool.totalCount).to.equal(1)
|
||||
client.release()
|
||||
pool.end().then(resolve, reject)
|
||||
})
|
||||
})
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
describe('passing a function to pool.query', () => {
|
||||
it('calls back with error', (done) => {
|
||||
const pool = new Pool()
|
||||
console.log('passing fn to query')
|
||||
pool.query((err) => {
|
||||
expect(err).to.be.an(Error)
|
||||
pool.end(done)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('pool with lots of errors', () => {
|
||||
it(
|
||||
'continues to work and provide new clients',
|
||||
co.wrap(function* () {
|
||||
const pool = new Pool({ max: 1 })
|
||||
const errors = []
|
||||
for (var i = 0; i < 20; i++) {
|
||||
try {
|
||||
yield pool.query('invalid sql')
|
||||
} catch (err) {
|
||||
errors.push(err)
|
||||
}
|
||||
}
|
||||
expect(errors).to.have.length(20)
|
||||
expect(pool.idleCount).to.equal(0)
|
||||
expect(pool.query).to.be.a(Function)
|
||||
const res = yield pool.query('SELECT $1::text as name', ['brianc'])
|
||||
expect(res.rows).to.have.length(1)
|
||||
expect(res.rows[0].name).to.equal('brianc')
|
||||
return pool.end()
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it('should continue with queued items after a connection failure', (done) => {
|
||||
const closeServer = net
|
||||
.createServer((socket) => {
|
||||
socket.destroy()
|
||||
})
|
||||
.unref()
|
||||
|
||||
closeServer.listen(() => {
|
||||
const pool = new Pool({ max: 1, port: closeServer.address().port, host: 'localhost' })
|
||||
pool.connect((err) => {
|
||||
expect(err).to.be.an(Error)
|
||||
if (err.code) {
|
||||
expect(err.code).to.be('ECONNRESET')
|
||||
}
|
||||
})
|
||||
pool.connect((err) => {
|
||||
expect(err).to.be.an(Error)
|
||||
if (err.code) {
|
||||
expect(err.code).to.be('ECONNRESET')
|
||||
}
|
||||
closeServer.close(() => {
|
||||
pool.end(done)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('handles post-checkout client failures in pool.query', (done) => {
|
||||
const pool = new Pool({ max: 1 })
|
||||
pool.on('error', () => {
|
||||
// We double close the connection in this test, prevent exception caused by that
|
||||
})
|
||||
pool.query('SELECT pg_sleep(5)', [], (err) => {
|
||||
expect(err).to.be.an(Error)
|
||||
done()
|
||||
})
|
||||
|
||||
setTimeout(() => {
|
||||
pool._clients[0].end()
|
||||
}, 1000)
|
||||
})
|
||||
})
|
124
websocket_server/node_modules/pg-pool/test/events.js
generated
vendored
Normal file
124
websocket_server/node_modules/pg-pool/test/events.js
generated
vendored
Normal file
@ -0,0 +1,124 @@
|
||||
'use strict'
|
||||
|
||||
const expect = require('expect.js')
|
||||
const EventEmitter = require('events').EventEmitter
|
||||
const describe = require('mocha').describe
|
||||
const it = require('mocha').it
|
||||
const Pool = require('../')
|
||||
|
||||
describe('events', function () {
|
||||
it('emits connect before callback', function (done) {
|
||||
const pool = new Pool()
|
||||
let emittedClient = false
|
||||
pool.on('connect', function (client) {
|
||||
emittedClient = client
|
||||
})
|
||||
|
||||
pool.connect(function (err, client, release) {
|
||||
if (err) return done(err)
|
||||
release()
|
||||
pool.end()
|
||||
expect(client).to.be(emittedClient)
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('emits "connect" only with a successful connection', function () {
|
||||
const pool = new Pool({
|
||||
// This client will always fail to connect
|
||||
Client: mockClient({
|
||||
connect: function (cb) {
|
||||
process.nextTick(() => {
|
||||
cb(new Error('bad news'))
|
||||
})
|
||||
},
|
||||
}),
|
||||
})
|
||||
pool.on('connect', function () {
|
||||
throw new Error('should never get here')
|
||||
})
|
||||
return pool.connect().catch((e) => expect(e.message).to.equal('bad news'))
|
||||
})
|
||||
|
||||
it('emits acquire every time a client is acquired', function (done) {
|
||||
const pool = new Pool()
|
||||
let acquireCount = 0
|
||||
pool.on('acquire', function (client) {
|
||||
expect(client).to.be.ok()
|
||||
acquireCount++
|
||||
})
|
||||
for (let i = 0; i < 10; i++) {
|
||||
pool.connect(function (err, client, release) {
|
||||
if (err) return done(err)
|
||||
release()
|
||||
})
|
||||
pool.query('SELECT now()')
|
||||
}
|
||||
setTimeout(function () {
|
||||
expect(acquireCount).to.be(20)
|
||||
pool.end(done)
|
||||
}, 100)
|
||||
})
|
||||
|
||||
it('emits release every time a client is released', function (done) {
|
||||
const pool = new Pool()
|
||||
let releaseCount = 0
|
||||
pool.on('release', function (err, client) {
|
||||
expect(err instanceof Error).not.to.be(true)
|
||||
expect(client).to.be.ok()
|
||||
releaseCount++
|
||||
})
|
||||
const promises = []
|
||||
for (let i = 0; i < 10; i++) {
|
||||
pool.connect(function (err, client, release) {
|
||||
if (err) return done(err)
|
||||
release()
|
||||
})
|
||||
promises.push(pool.query('SELECT now()'))
|
||||
}
|
||||
Promise.all(promises).then(() => {
|
||||
pool.end(() => {
|
||||
expect(releaseCount).to.be(20)
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('emits release with an error if client is released due to an error', function (done) {
|
||||
const pool = new Pool()
|
||||
pool.connect(function (err, client, release) {
|
||||
expect(err).to.equal(undefined)
|
||||
const releaseError = new Error('problem')
|
||||
pool.once('release', function (err, errClient) {
|
||||
expect(err).to.equal(releaseError)
|
||||
expect(errClient).to.equal(client)
|
||||
pool.end(done)
|
||||
})
|
||||
release(releaseError)
|
||||
})
|
||||
})
|
||||
|
||||
it('emits error and client if an idle client in the pool hits an error', function (done) {
|
||||
const pool = new Pool()
|
||||
pool.connect(function (err, client) {
|
||||
expect(err).to.equal(undefined)
|
||||
client.release()
|
||||
setImmediate(function () {
|
||||
client.emit('error', new Error('problem'))
|
||||
})
|
||||
pool.once('error', function (err, errClient) {
|
||||
expect(err.message).to.equal('problem')
|
||||
expect(errClient).to.equal(client)
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
function mockClient(methods) {
|
||||
return function () {
|
||||
const client = new EventEmitter()
|
||||
Object.assign(client, methods)
|
||||
return client
|
||||
}
|
||||
}
|
20
websocket_server/node_modules/pg-pool/test/idle-timeout-exit.js
generated
vendored
Normal file
20
websocket_server/node_modules/pg-pool/test/idle-timeout-exit.js
generated
vendored
Normal file
@ -0,0 +1,20 @@
|
||||
// This test is meant to be spawned from idle-timeout.js
|
||||
if (module === require.main) {
|
||||
const allowExitOnIdle = process.env.ALLOW_EXIT_ON_IDLE === '1'
|
||||
const Pool = require('../index')
|
||||
|
||||
const pool = new Pool({
|
||||
maxLifetimeSeconds: 2,
|
||||
idleTimeoutMillis: 200,
|
||||
...(allowExitOnIdle ? { allowExitOnIdle: true } : {}),
|
||||
})
|
||||
pool.query('SELECT NOW()', (err, res) => console.log('completed first'))
|
||||
pool.on('remove', () => {
|
||||
console.log('removed')
|
||||
done()
|
||||
})
|
||||
|
||||
setTimeout(() => {
|
||||
pool.query('SELECT * from generate_series(0, 1000)', (err, res) => console.log('completed second'))
|
||||
}, 50)
|
||||
}
|
118
websocket_server/node_modules/pg-pool/test/idle-timeout.js
generated
vendored
Normal file
118
websocket_server/node_modules/pg-pool/test/idle-timeout.js
generated
vendored
Normal file
@ -0,0 +1,118 @@
|
||||
'use strict'
|
||||
const co = require('co')
|
||||
const expect = require('expect.js')
|
||||
|
||||
const describe = require('mocha').describe
|
||||
const it = require('mocha').it
|
||||
const { fork } = require('child_process')
|
||||
const path = require('path')
|
||||
|
||||
const Pool = require('../')
|
||||
|
||||
const wait = (time) => new Promise((resolve) => setTimeout(resolve, time))
|
||||
|
||||
describe('idle timeout', () => {
|
||||
it('should timeout and remove the client', (done) => {
|
||||
const pool = new Pool({ idleTimeoutMillis: 10 })
|
||||
pool.query('SELECT NOW()')
|
||||
pool.on('remove', () => {
|
||||
expect(pool.idleCount).to.equal(0)
|
||||
expect(pool.totalCount).to.equal(0)
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it(
|
||||
'times out and removes clients when others are also removed',
|
||||
co.wrap(function* () {
|
||||
const pool = new Pool({ idleTimeoutMillis: 10 })
|
||||
const clientA = yield pool.connect()
|
||||
const clientB = yield pool.connect()
|
||||
clientA.release()
|
||||
clientB.release(new Error())
|
||||
|
||||
const removal = new Promise((resolve) => {
|
||||
pool.on('remove', () => {
|
||||
expect(pool.idleCount).to.equal(0)
|
||||
expect(pool.totalCount).to.equal(0)
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
|
||||
const timeout = wait(100).then(() => Promise.reject(new Error('Idle timeout failed to occur')))
|
||||
|
||||
try {
|
||||
yield Promise.race([removal, timeout])
|
||||
} finally {
|
||||
pool.end()
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
it(
|
||||
'can remove idle clients and recreate them',
|
||||
co.wrap(function* () {
|
||||
const pool = new Pool({ idleTimeoutMillis: 1 })
|
||||
const results = []
|
||||
for (var i = 0; i < 20; i++) {
|
||||
let query = pool.query('SELECT NOW()')
|
||||
expect(pool.idleCount).to.equal(0)
|
||||
expect(pool.totalCount).to.equal(1)
|
||||
results.push(yield query)
|
||||
yield wait(2)
|
||||
expect(pool.idleCount).to.equal(0)
|
||||
expect(pool.totalCount).to.equal(0)
|
||||
}
|
||||
expect(results).to.have.length(20)
|
||||
})
|
||||
)
|
||||
|
||||
it(
|
||||
'does not time out clients which are used',
|
||||
co.wrap(function* () {
|
||||
const pool = new Pool({ idleTimeoutMillis: 1 })
|
||||
const results = []
|
||||
for (var i = 0; i < 20; i++) {
|
||||
let client = yield pool.connect()
|
||||
expect(pool.totalCount).to.equal(1)
|
||||
expect(pool.idleCount).to.equal(0)
|
||||
yield wait(10)
|
||||
results.push(yield client.query('SELECT NOW()'))
|
||||
client.release()
|
||||
expect(pool.idleCount).to.equal(1)
|
||||
expect(pool.totalCount).to.equal(1)
|
||||
}
|
||||
expect(results).to.have.length(20)
|
||||
return pool.end()
|
||||
})
|
||||
)
|
||||
|
||||
it('unrefs the connections and timeouts so the program can exit when idle when the allowExitOnIdle option is set', function (done) {
|
||||
const child = fork(path.join(__dirname, 'idle-timeout-exit.js'), [], {
|
||||
silent: true,
|
||||
env: { ...process.env, ALLOW_EXIT_ON_IDLE: '1' },
|
||||
})
|
||||
let result = ''
|
||||
child.stdout.setEncoding('utf8')
|
||||
child.stdout.on('data', (chunk) => (result += chunk))
|
||||
child.on('error', (err) => done(err))
|
||||
child.on('close', () => {
|
||||
expect(result).to.equal('completed first\ncompleted second\n')
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('keeps old behavior when allowExitOnIdle option is not set', function (done) {
|
||||
const child = fork(path.join(__dirname, 'idle-timeout-exit.js'), [], {
|
||||
silent: true,
|
||||
})
|
||||
let result = ''
|
||||
child.stdout.setEncoding('utf8')
|
||||
child.stdout.on('data', (chunk) => (result += chunk))
|
||||
child.on('error', (err) => done(err))
|
||||
child.on('close', () => {
|
||||
expect(result).to.equal('completed first\ncompleted second\nremoved\n')
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
226
websocket_server/node_modules/pg-pool/test/index.js
generated
vendored
Normal file
226
websocket_server/node_modules/pg-pool/test/index.js
generated
vendored
Normal file
@ -0,0 +1,226 @@
|
||||
'use strict'
|
||||
const expect = require('expect.js')
|
||||
const _ = require('lodash')
|
||||
|
||||
const describe = require('mocha').describe
|
||||
const it = require('mocha').it
|
||||
|
||||
const Pool = require('../')
|
||||
|
||||
describe('pool', function () {
|
||||
describe('with callbacks', function () {
|
||||
it('works totally unconfigured', function (done) {
|
||||
const pool = new Pool()
|
||||
pool.connect(function (err, client, release) {
|
||||
if (err) return done(err)
|
||||
client.query('SELECT NOW()', function (err, res) {
|
||||
release()
|
||||
if (err) return done(err)
|
||||
expect(res.rows).to.have.length(1)
|
||||
pool.end(done)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('passes props to clients', function (done) {
|
||||
const pool = new Pool({ binary: true })
|
||||
pool.connect(function (err, client, release) {
|
||||
release()
|
||||
if (err) return done(err)
|
||||
expect(client.binary).to.eql(true)
|
||||
pool.end(done)
|
||||
})
|
||||
})
|
||||
|
||||
it('can run a query with a callback without parameters', function (done) {
|
||||
const pool = new Pool()
|
||||
pool.query('SELECT 1 as num', function (err, res) {
|
||||
expect(res.rows[0]).to.eql({ num: 1 })
|
||||
pool.end(function () {
|
||||
done(err)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('can run a query with a callback', function (done) {
|
||||
const pool = new Pool()
|
||||
pool.query('SELECT $1::text as name', ['brianc'], function (err, res) {
|
||||
expect(res.rows[0]).to.eql({ name: 'brianc' })
|
||||
pool.end(function () {
|
||||
done(err)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('passes connection errors to callback', function (done) {
|
||||
const pool = new Pool({ port: 53922 })
|
||||
pool.query('SELECT $1::text as name', ['brianc'], function (err, res) {
|
||||
expect(res).to.be(undefined)
|
||||
expect(err).to.be.an(Error)
|
||||
// a connection error should not polute the pool with a dead client
|
||||
expect(pool.totalCount).to.equal(0)
|
||||
pool.end(function (err) {
|
||||
done(err)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('does not pass client to error callback', function (done) {
|
||||
const pool = new Pool({ port: 58242 })
|
||||
pool.connect(function (err, client, release) {
|
||||
expect(err).to.be.an(Error)
|
||||
expect(client).to.be(undefined)
|
||||
expect(release).to.be.a(Function)
|
||||
pool.end(done)
|
||||
})
|
||||
})
|
||||
|
||||
it('removes client if it errors in background', function (done) {
|
||||
const pool = new Pool()
|
||||
pool.connect(function (err, client, release) {
|
||||
release()
|
||||
if (err) return done(err)
|
||||
client.testString = 'foo'
|
||||
setTimeout(function () {
|
||||
client.emit('error', new Error('on purpose'))
|
||||
}, 10)
|
||||
})
|
||||
pool.on('error', function (err) {
|
||||
expect(err.message).to.be('on purpose')
|
||||
expect(err.client).to.not.be(undefined)
|
||||
expect(err.client.testString).to.be('foo')
|
||||
err.client.connection.stream.on('end', function () {
|
||||
pool.end(done)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('should not change given options', function (done) {
|
||||
const options = { max: 10 }
|
||||
const pool = new Pool(options)
|
||||
pool.connect(function (err, client, release) {
|
||||
release()
|
||||
if (err) return done(err)
|
||||
expect(options).to.eql({ max: 10 })
|
||||
pool.end(done)
|
||||
})
|
||||
})
|
||||
|
||||
it('does not create promises when connecting', function (done) {
|
||||
const pool = new Pool()
|
||||
const returnValue = pool.connect(function (err, client, release) {
|
||||
release()
|
||||
if (err) return done(err)
|
||||
pool.end(done)
|
||||
})
|
||||
expect(returnValue).to.be(undefined)
|
||||
})
|
||||
|
||||
it('does not create promises when querying', function (done) {
|
||||
const pool = new Pool()
|
||||
const returnValue = pool.query('SELECT 1 as num', function (err) {
|
||||
pool.end(function () {
|
||||
done(err)
|
||||
})
|
||||
})
|
||||
expect(returnValue).to.be(undefined)
|
||||
})
|
||||
|
||||
it('does not create promises when ending', function (done) {
|
||||
const pool = new Pool()
|
||||
const returnValue = pool.end(done)
|
||||
expect(returnValue).to.be(undefined)
|
||||
})
|
||||
|
||||
it('never calls callback syncronously', function (done) {
|
||||
const pool = new Pool()
|
||||
pool.connect((err, client) => {
|
||||
if (err) throw err
|
||||
client.release()
|
||||
setImmediate(() => {
|
||||
let called = false
|
||||
pool.connect((err, client) => {
|
||||
if (err) throw err
|
||||
called = true
|
||||
client.release()
|
||||
setImmediate(() => {
|
||||
pool.end(done)
|
||||
})
|
||||
})
|
||||
expect(called).to.equal(false)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('with promises', function () {
|
||||
it('connects, queries, and disconnects', function () {
|
||||
const pool = new Pool()
|
||||
return pool.connect().then(function (client) {
|
||||
return client.query('select $1::text as name', ['hi']).then(function (res) {
|
||||
expect(res.rows).to.eql([{ name: 'hi' }])
|
||||
client.release()
|
||||
return pool.end()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('executes a query directly', () => {
|
||||
const pool = new Pool()
|
||||
return pool.query('SELECT $1::text as name', ['hi']).then((res) => {
|
||||
expect(res.rows).to.have.length(1)
|
||||
expect(res.rows[0].name).to.equal('hi')
|
||||
return pool.end()
|
||||
})
|
||||
})
|
||||
|
||||
it('properly pools clients', function () {
|
||||
const pool = new Pool({ poolSize: 9 })
|
||||
const promises = _.times(30, function () {
|
||||
return pool.connect().then(function (client) {
|
||||
return client.query('select $1::text as name', ['hi']).then(function (res) {
|
||||
client.release()
|
||||
return res
|
||||
})
|
||||
})
|
||||
})
|
||||
return Promise.all(promises).then(function (res) {
|
||||
expect(res).to.have.length(30)
|
||||
expect(pool.totalCount).to.be(9)
|
||||
return pool.end()
|
||||
})
|
||||
})
|
||||
|
||||
it('supports just running queries', function () {
|
||||
const pool = new Pool({ poolSize: 9 })
|
||||
const text = 'select $1::text as name'
|
||||
const values = ['hi']
|
||||
const query = { text: text, values: values }
|
||||
const promises = _.times(30, () => pool.query(query))
|
||||
return Promise.all(promises).then(function (queries) {
|
||||
expect(queries).to.have.length(30)
|
||||
return pool.end()
|
||||
})
|
||||
})
|
||||
|
||||
it('recovers from query errors', function () {
|
||||
const pool = new Pool()
|
||||
|
||||
const errors = []
|
||||
const promises = _.times(30, () => {
|
||||
return pool.query('SELECT asldkfjasldkf').catch(function (e) {
|
||||
errors.push(e)
|
||||
})
|
||||
})
|
||||
return Promise.all(promises).then(() => {
|
||||
expect(errors).to.have.length(30)
|
||||
expect(pool.totalCount).to.equal(0)
|
||||
expect(pool.idleCount).to.equal(0)
|
||||
return pool.query('SELECT $1::text as name', ['hi']).then(function (res) {
|
||||
expect(res.rows).to.eql([{ name: 'hi' }])
|
||||
return pool.end()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
47
websocket_server/node_modules/pg-pool/test/lifetime-timeout.js
generated
vendored
Normal file
47
websocket_server/node_modules/pg-pool/test/lifetime-timeout.js
generated
vendored
Normal file
@ -0,0 +1,47 @@
|
||||
'use strict'
|
||||
const co = require('co')
|
||||
const expect = require('expect.js')
|
||||
|
||||
const describe = require('mocha').describe
|
||||
const it = require('mocha').it
|
||||
|
||||
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.4)')
|
||||
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.4)')
|
||||
expect(pool.expiredCount).to.equal(0)
|
||||
expect(pool.totalCount).to.equal(1)
|
||||
yield query
|
||||
yield new Promise((resolve) => setTimeout(resolve, 100))
|
||||
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)
|
||||
})
|
||||
)
|
||||
})
|
20
websocket_server/node_modules/pg-pool/test/logging.js
generated
vendored
Normal file
20
websocket_server/node_modules/pg-pool/test/logging.js
generated
vendored
Normal file
@ -0,0 +1,20 @@
|
||||
const expect = require('expect.js')
|
||||
|
||||
const describe = require('mocha').describe
|
||||
const it = require('mocha').it
|
||||
|
||||
const Pool = require('../')
|
||||
|
||||
describe('logging', function () {
|
||||
it('logs to supplied log function if given', function () {
|
||||
const messages = []
|
||||
const log = function (msg) {
|
||||
messages.push(msg)
|
||||
}
|
||||
const pool = new Pool({ log: log })
|
||||
return pool.query('SELECT NOW()').then(function () {
|
||||
expect(messages.length).to.be.greaterThan(0)
|
||||
return pool.end()
|
||||
})
|
||||
})
|
||||
})
|
97
websocket_server/node_modules/pg-pool/test/max-uses.js
generated
vendored
Normal file
97
websocket_server/node_modules/pg-pool/test/max-uses.js
generated
vendored
Normal file
@ -0,0 +1,97 @@
|
||||
const expect = require('expect.js')
|
||||
const co = require('co')
|
||||
|
||||
const describe = require('mocha').describe
|
||||
const it = require('mocha').it
|
||||
|
||||
const Pool = require('../')
|
||||
|
||||
describe('maxUses', () => {
|
||||
it(
|
||||
'can create a single client and use it once',
|
||||
co.wrap(function* () {
|
||||
const pool = new Pool({ maxUses: 2 })
|
||||
expect(pool.waitingCount).to.equal(0)
|
||||
const client = yield pool.connect()
|
||||
const res = yield client.query('SELECT $1::text as name', ['hi'])
|
||||
expect(res.rows[0].name).to.equal('hi')
|
||||
client.release()
|
||||
pool.end()
|
||||
})
|
||||
)
|
||||
|
||||
it(
|
||||
'getting a connection a second time returns the same connection and releasing it also closes it',
|
||||
co.wrap(function* () {
|
||||
const pool = new Pool({ maxUses: 2 })
|
||||
expect(pool.waitingCount).to.equal(0)
|
||||
const client = yield pool.connect()
|
||||
client.release()
|
||||
const client2 = yield pool.connect()
|
||||
expect(client).to.equal(client2)
|
||||
expect(client2._ending).to.equal(false)
|
||||
client2.release()
|
||||
expect(client2._ending).to.equal(true)
|
||||
return yield pool.end()
|
||||
})
|
||||
)
|
||||
|
||||
it(
|
||||
'getting a connection a third time returns a new connection',
|
||||
co.wrap(function* () {
|
||||
const pool = new Pool({ maxUses: 2 })
|
||||
expect(pool.waitingCount).to.equal(0)
|
||||
const client = yield pool.connect()
|
||||
client.release()
|
||||
const client2 = yield pool.connect()
|
||||
expect(client).to.equal(client2)
|
||||
client2.release()
|
||||
const client3 = yield pool.connect()
|
||||
expect(client3).not.to.equal(client2)
|
||||
client3.release()
|
||||
return yield pool.end()
|
||||
})
|
||||
)
|
||||
|
||||
it(
|
||||
'getting a connection from a pending request gets a fresh client when the released candidate is expended',
|
||||
co.wrap(function* () {
|
||||
const pool = new Pool({ max: 1, maxUses: 2 })
|
||||
expect(pool.waitingCount).to.equal(0)
|
||||
const client1 = yield pool.connect()
|
||||
pool.connect().then((client2) => {
|
||||
expect(client2).to.equal(client1)
|
||||
expect(pool.waitingCount).to.equal(1)
|
||||
// Releasing the client this time should also expend it since maxUses is 2, causing client3 to be a fresh client
|
||||
client2.release()
|
||||
})
|
||||
const client3Promise = pool.connect().then((client3) => {
|
||||
// client3 should be a fresh client since client2's release caused the first client to be expended
|
||||
expect(pool.waitingCount).to.equal(0)
|
||||
expect(client3).not.to.equal(client1)
|
||||
return client3.release()
|
||||
})
|
||||
// There should be two pending requests since we have 3 connect requests but a max size of 1
|
||||
expect(pool.waitingCount).to.equal(2)
|
||||
// Releasing the client should not yet expend it since maxUses is 2
|
||||
client1.release()
|
||||
yield client3Promise
|
||||
return yield pool.end()
|
||||
})
|
||||
)
|
||||
|
||||
it(
|
||||
'logs when removing an expended client',
|
||||
co.wrap(function* () {
|
||||
const messages = []
|
||||
const log = function (msg) {
|
||||
messages.push(msg)
|
||||
}
|
||||
const pool = new Pool({ maxUses: 1, log })
|
||||
const client = yield pool.connect()
|
||||
client.release()
|
||||
expect(messages).to.contain('remove expended client')
|
||||
return yield pool.end()
|
||||
})
|
||||
)
|
||||
})
|
53
websocket_server/node_modules/pg-pool/test/releasing-clients.js
generated
vendored
Normal file
53
websocket_server/node_modules/pg-pool/test/releasing-clients.js
generated
vendored
Normal file
@ -0,0 +1,53 @@
|
||||
const Pool = require('../')
|
||||
|
||||
const expect = require('expect.js')
|
||||
|
||||
describe('releasing clients', () => {
|
||||
it('removes a client which cannot be queried', async () => {
|
||||
// make a pool w/ only 1 client
|
||||
const pool = new Pool({ max: 1 })
|
||||
expect(pool.totalCount).to.eql(0)
|
||||
const client = await pool.connect()
|
||||
expect(pool.totalCount).to.eql(1)
|
||||
expect(pool.idleCount).to.eql(0)
|
||||
// reach into the client and sever its connection
|
||||
client.connection.end()
|
||||
|
||||
// wait for the client to error out
|
||||
const err = await new Promise((resolve) => client.once('error', resolve))
|
||||
expect(err).to.be.ok()
|
||||
expect(pool.totalCount).to.eql(1)
|
||||
expect(pool.idleCount).to.eql(0)
|
||||
|
||||
// try to return it to the pool - this removes it because its broken
|
||||
client.release()
|
||||
expect(pool.totalCount).to.eql(0)
|
||||
expect(pool.idleCount).to.eql(0)
|
||||
|
||||
// make sure pool still works
|
||||
const { rows } = await pool.query('SELECT NOW()')
|
||||
expect(rows).to.have.length(1)
|
||||
await pool.end()
|
||||
})
|
||||
|
||||
it('removes a client which is ending', async () => {
|
||||
// make a pool w/ only 1 client
|
||||
const pool = new Pool({ max: 1 })
|
||||
expect(pool.totalCount).to.eql(0)
|
||||
const client = await pool.connect()
|
||||
expect(pool.totalCount).to.eql(1)
|
||||
expect(pool.idleCount).to.eql(0)
|
||||
// end the client gracefully (but you shouldn't do this with pooled clients)
|
||||
client.end()
|
||||
|
||||
// try to return it to the pool
|
||||
client.release()
|
||||
expect(pool.totalCount).to.eql(0)
|
||||
expect(pool.idleCount).to.eql(0)
|
||||
|
||||
// make sure pool still works
|
||||
const { rows } = await pool.query('SELECT NOW()')
|
||||
expect(rows).to.have.length(1)
|
||||
await pool.end()
|
||||
})
|
||||
})
|
10
websocket_server/node_modules/pg-pool/test/setup.js
generated
vendored
Normal file
10
websocket_server/node_modules/pg-pool/test/setup.js
generated
vendored
Normal file
@ -0,0 +1,10 @@
|
||||
const crash = (reason) => {
|
||||
process.on(reason, (err) => {
|
||||
console.error(reason, err.stack)
|
||||
process.exit(-1)
|
||||
})
|
||||
}
|
||||
|
||||
crash('unhandledRejection')
|
||||
crash('uncaughtError')
|
||||
crash('warning')
|
58
websocket_server/node_modules/pg-pool/test/sizing.js
generated
vendored
Normal file
58
websocket_server/node_modules/pg-pool/test/sizing.js
generated
vendored
Normal file
@ -0,0 +1,58 @@
|
||||
const expect = require('expect.js')
|
||||
const co = require('co')
|
||||
const _ = require('lodash')
|
||||
|
||||
const describe = require('mocha').describe
|
||||
const it = require('mocha').it
|
||||
|
||||
const Pool = require('../')
|
||||
|
||||
describe('pool size of 1', () => {
|
||||
it(
|
||||
'can create a single client and use it once',
|
||||
co.wrap(function* () {
|
||||
const pool = new Pool({ max: 1 })
|
||||
expect(pool.waitingCount).to.equal(0)
|
||||
const client = yield pool.connect()
|
||||
const res = yield client.query('SELECT $1::text as name', ['hi'])
|
||||
expect(res.rows[0].name).to.equal('hi')
|
||||
client.release()
|
||||
pool.end()
|
||||
})
|
||||
)
|
||||
|
||||
it(
|
||||
'can create a single client and use it multiple times',
|
||||
co.wrap(function* () {
|
||||
const pool = new Pool({ max: 1 })
|
||||
expect(pool.waitingCount).to.equal(0)
|
||||
const client = yield pool.connect()
|
||||
const wait = pool.connect()
|
||||
expect(pool.waitingCount).to.equal(1)
|
||||
client.release()
|
||||
const client2 = yield wait
|
||||
expect(client).to.equal(client2)
|
||||
client2.release()
|
||||
return yield pool.end()
|
||||
})
|
||||
)
|
||||
|
||||
it(
|
||||
'can only send 1 query at a time',
|
||||
co.wrap(function* () {
|
||||
const pool = new Pool({ max: 1 })
|
||||
|
||||
// the query text column name changed in PostgreSQL 9.2
|
||||
const versionResult = yield pool.query('SHOW server_version_num')
|
||||
const version = parseInt(versionResult.rows[0].server_version_num, 10)
|
||||
const queryColumn = version < 90200 ? 'current_query' : 'query'
|
||||
|
||||
const queryText = 'SELECT COUNT(*) as counts FROM pg_stat_activity WHERE ' + queryColumn + ' = $1'
|
||||
const queries = _.times(20, () => pool.query(queryText, [queryText]))
|
||||
const results = yield Promise.all(queries)
|
||||
const counts = results.map((res) => parseInt(res.rows[0].counts, 10))
|
||||
expect(counts).to.eql(_.times(20, (i) => 1))
|
||||
return yield pool.end()
|
||||
})
|
||||
)
|
||||
})
|
19
websocket_server/node_modules/pg-pool/test/submittable.js
generated
vendored
Normal file
19
websocket_server/node_modules/pg-pool/test/submittable.js
generated
vendored
Normal file
@ -0,0 +1,19 @@
|
||||
'use strict'
|
||||
const Cursor = require('pg-cursor')
|
||||
const expect = require('expect.js')
|
||||
const describe = require('mocha').describe
|
||||
const it = require('mocha').it
|
||||
|
||||
const Pool = require('../')
|
||||
|
||||
describe('submittle', () => {
|
||||
it('is returned from the query method', false, (done) => {
|
||||
const pool = new Pool()
|
||||
const cursor = pool.query(new Cursor('SELECT * from generate_series(0, 1000)'))
|
||||
cursor.read((err, rows) => {
|
||||
expect(err).to.be(undefined)
|
||||
expect(!!rows).to.be.ok()
|
||||
cursor.close(done)
|
||||
})
|
||||
})
|
||||
})
|
0
websocket_server/node_modules/pg-pool/test/timeout.js
generated
vendored
Normal file
0
websocket_server/node_modules/pg-pool/test/timeout.js
generated
vendored
Normal file
24
websocket_server/node_modules/pg-pool/test/verify.js
generated
vendored
Normal file
24
websocket_server/node_modules/pg-pool/test/verify.js
generated
vendored
Normal file
@ -0,0 +1,24 @@
|
||||
'use strict'
|
||||
const expect = require('expect.js')
|
||||
|
||||
const describe = require('mocha').describe
|
||||
const it = require('mocha').it
|
||||
|
||||
const Pool = require('../')
|
||||
|
||||
describe('verify', () => {
|
||||
it('verifies a client with a callback', (done) => {
|
||||
const pool = new Pool({
|
||||
verify: (client, cb) => {
|
||||
cb(new Error('nope'))
|
||||
},
|
||||
})
|
||||
|
||||
pool.connect((err, client) => {
|
||||
expect(err).to.be.an(Error)
|
||||
expect(err.message).to.be('nope')
|
||||
pool.end()
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
Reference in New Issue
Block a user