|
| 1 | +var expect = require('expect.js') |
| 2 | +var Client = require('pg').Client |
| 3 | +var co = require('co') |
| 4 | +var Promise = require('bluebird') |
| 5 | +var _ = require('lodash') |
| 6 | + |
| 7 | +var Pool = require('../') |
| 8 | + |
| 9 | +describe('pool', function() { |
| 10 | + |
| 11 | + describe('with callbacks', function() { |
| 12 | + it('works totally unconfigured', function(done) { |
| 13 | + const pool = new Pool() |
| 14 | + pool.connect(function(err, client, release) { |
| 15 | + if (err) return done(err) |
| 16 | + client.query('SELECT NOW()', function(err, res) { |
| 17 | + release() |
| 18 | + if (err) return done(err) |
| 19 | + expect(res.rows).to.have.length(1) |
| 20 | + pool.end(done) |
| 21 | + }) |
| 22 | + }) |
| 23 | + }) |
| 24 | + |
| 25 | + it('passes props to clients', function(done) { |
| 26 | + const pool = new Pool({ binary: true }) |
| 27 | + pool.connect(function(err, client, release) { |
| 28 | + release() |
| 29 | + expect(client.binary).to.eql(true) |
| 30 | + pool.end(done) |
| 31 | + }) |
| 32 | + }) |
| 33 | + |
| 34 | + it('removes client if it errors in background', function(done) { |
| 35 | + const pool = new Pool() |
| 36 | + pool.connect(function(err, client, release) { |
| 37 | + release() |
| 38 | + client.testString = 'foo' |
| 39 | + setTimeout(function() { |
| 40 | + client.emit('error', new Error('on purpose')) |
| 41 | + }, 10) |
| 42 | + }) |
| 43 | + pool.on('error', function(err) { |
| 44 | + expect(err.message).to.be('on purpose') |
| 45 | + expect(err.client).to.not.be(undefined) |
| 46 | + expect(err.client.testString).to.be('foo') |
| 47 | + err.client.connection.stream.on('end', function() { |
| 48 | + pool.end(done) |
| 49 | + }) |
| 50 | + }) |
| 51 | + }) |
| 52 | + }) |
| 53 | + |
| 54 | + describe('with promises', function() { |
| 55 | + it('connects and disconnects', co.wrap(function*() { |
| 56 | + var pool = new Pool() |
| 57 | + var client = yield pool.connect() |
| 58 | + expect(pool.pool.availableObjectsCount()).to.be(0) |
| 59 | + var res = yield client.queryAsync('select $1::text as name', ['hi']) |
| 60 | + expect(res.rows).to.eql([{ name: 'hi' }]) |
| 61 | + client.release() |
| 62 | + expect(pool.pool.getPoolSize()).to.be(1) |
| 63 | + expect(pool.pool.availableObjectsCount()).to.be(1) |
| 64 | + return yield pool.end() |
| 65 | + })) |
| 66 | + |
| 67 | + it('properly pools clients', co.wrap(function*() { |
| 68 | + var pool = new Pool({ poolSize: 9 }) |
| 69 | + var count = 0 |
| 70 | + while (count < 30) { |
| 71 | + count++ |
| 72 | + pool.connect().then(function(client) { |
| 73 | + client.queryAsync('select $1::text as name', ['hi']).then(function(res) { |
| 74 | + client.release() |
| 75 | + }) |
| 76 | + }) |
| 77 | + } |
| 78 | + yield Promise.delay(100) |
| 79 | + expect(pool.pool.getPoolSize()).to.be(9) |
| 80 | + return yield pool.end() |
| 81 | + })) |
| 82 | + |
| 83 | + it('supports just running queries', co.wrap(function*() { |
| 84 | + var pool = new Pool({ poolSize: 9 }) |
| 85 | + var count = 0 |
| 86 | + var queries = _.times(30).map(function() { |
| 87 | + return pool.query('SELECT $1::text as name', ['hi']) |
| 88 | + }) |
| 89 | + console.log('executing') |
| 90 | + yield queries |
| 91 | + expect(pool.pool.getPoolSize()).to.be(9) |
| 92 | + expect(pool.pool.availableObjectsCount()).to.be(9) |
| 93 | + return yield pool.end() |
| 94 | + })) |
| 95 | + |
| 96 | + it('recovers from all errors', co.wrap(function*() { |
| 97 | + var pool = new Pool({ poolSize: 9 }) |
| 98 | + var count = 0 |
| 99 | + |
| 100 | + while(count++ < 30) { |
| 101 | + try { |
| 102 | + yield pool.query('SELECT lksjdfd') |
| 103 | + } catch(e) { |
| 104 | + } |
| 105 | + } |
| 106 | + var res = yield pool.query('SELECT $1::text as name', ['hi']) |
| 107 | + expect(res.rows).to.eql([{ name: 'hi' }]) |
| 108 | + return yield pool.end() |
| 109 | + })) |
| 110 | + }) |
| 111 | +}) |
0 commit comments