From 2c3f55e5bdd1f6075fe168cfecdb8f375d5fef26 Mon Sep 17 00:00:00 2001 From: "Brian M. Carlson" Date: Tue, 13 Jun 2017 19:24:39 -0500 Subject: [PATCH] Make all tests pass --- lib/client.js | 8 ++++- .../client/error-handling-tests.js | 32 +++++++++++-------- .../client/query-as-promise-tests.js | 13 ++++---- .../client/query-error-handling-tests.js | 2 +- test/integration/gh-issues/699-tests.js | 4 ++- 5 files changed, 35 insertions(+), 24 deletions(-) diff --git a/lib/client.js b/lib/client.js index c1cef452..0b67ba31 100644 --- a/lib/client.js +++ b/lib/client.js @@ -173,10 +173,10 @@ Client.prototype.connect = function(callback) { var activeQuery = self.activeQuery; self.activeQuery = null; self.readyForQuery = true; - self._pulseQueryQueue(); if(activeQuery) { activeQuery.handleReadyForQuery(con); } + self._pulseQueryQueue(); }); con.on('error', function(error) { @@ -390,6 +390,12 @@ Client.prototype.query = function(config, values, callback) { Client.prototype.end = function(cb) { this._ending = true; + if (this.activeQuery) { + // if we have an active query we need to force a disconnect + // on the socket - otherwise a hung query could block end forever + this.connection.stream.destroy(new Error('Connection terminated by user')) + return; + } if (cb) { this.connection.end(); this.connection.once('end', cb); diff --git a/test/integration/client/error-handling-tests.js b/test/integration/client/error-handling-tests.js index cc60e2c9..69d43085 100644 --- a/test/integration/client/error-handling-tests.js +++ b/test/integration/client/error-handling-tests.js @@ -16,29 +16,26 @@ var createErorrClient = function() { const suite = new helper.Suite('error handling') -suite.test('query receives error on client shutdown', false, function(done) { +suite.test('query receives error on client shutdown', function(done) { var client = new Client(); client.connect(assert.success(function() { const config = { text: 'select pg_sleep(5)', name: 'foobar' } + let queryError; client.query(new pg.Query(config), assert.calls(function(err, res) { assert(err instanceof Error) - done() + queryError = err })); - setTimeout(() => { - client.end() - assert.emits(client, 'end'); - }, 50) + setTimeout(() => client.end(), 50) + client.once('end', () => { + assert(queryError instanceof Error) + done() + }) })); }); -;(function () { - var client = createErorrClient(); - - var q = client.query({ text: "CREATE TEMP TABLE boom(age integer); INSERT INTO boom (age) VALUES (28);", binary: false }); - var ensureFuture = function (testClient, done) { var goodQuery = testClient.query(new pg.Query("select age from boom")); assert.emits(goodQuery, 'row', function (row) { @@ -48,6 +45,10 @@ suite.test('query receives error on client shutdown', false, function(done) { }; suite.test("when query is parsing", (done) => { + var client = createErorrClient(); + + var q = client.query({ text: "CREATE TEMP TABLE boom(age integer); INSERT INTO boom (age) VALUES (28);" }); + //this query wont parse since there isn't a table named bang var query = client.query(new pg.Query({ @@ -61,6 +62,10 @@ suite.test('query receives error on client shutdown', false, function(done) { }); suite.test("when a query is binding", function (done) { + var client = createErorrClient(); + + var q = client.query({ text: "CREATE TEMP TABLE boom(age integer); INSERT INTO boom (age) VALUES (28);" }); + var query = client.query(new pg.Query({ text: 'select * from boom where age = $1', @@ -72,7 +77,6 @@ suite.test('query receives error on client shutdown', false, function(done) { ensureFuture(client, done); }); }); -})(); suite.test('non-query error with callback', function(done) { var client = new Client({ @@ -101,7 +105,7 @@ suite.test('non-error calls supplied callback', function(done) { suite.test('when connecting to an invalid host with callback', function (done) { var client = new Client({ - host: '!#%!@#%' + user: 'very invalid username', }); client.connect(function(error, client) { assert(error instanceof Error); @@ -111,7 +115,7 @@ suite.test('when connecting to an invalid host with callback', function (done) { suite.test('when connecting to invalid host with promise', function(done) { var client = new Client({ - host: 'asdlfkjasldkfjlaskdfj' + user: 'very invalid username' }); client.connect().catch((e) => done()); }); diff --git a/test/integration/client/query-as-promise-tests.js b/test/integration/client/query-as-promise-tests.js index ac958729..92eedcc8 100644 --- a/test/integration/client/query-as-promise-tests.js +++ b/test/integration/client/query-as-promise-tests.js @@ -16,13 +16,12 @@ pg.connect(helper.config, assert.success(function(client, done) { client.query('ALKJSDF') .catch(function(e) { assert(e instanceof Error) + client.query('SELECT 1 as num') + .then(function (result) { + assert.equal(result.rows[0].num, 1) + done() + pg.end() + }) }) }) - - client.query('SELECT 1 as num') - .then(function(result) { - assert.equal(result.rows[0].num, 1) - done() - pg.end() - }) })) diff --git a/test/integration/client/query-error-handling-tests.js b/test/integration/client/query-error-handling-tests.js index 2e28737d..f2dd294e 100644 --- a/test/integration/client/query-error-handling-tests.js +++ b/test/integration/client/query-error-handling-tests.js @@ -5,7 +5,7 @@ var Query = helper.pg.Query; test('error during query execution', function() { var client = new Client(helper.args); client.connect(assert.success(function() { - var queryText = 'select pg_sleep(5)' + var queryText = 'select pg_sleep(10)' var sleepQuery = new Query(queryText); var pidColName = 'procpid' var queryColName = 'current_query'; diff --git a/test/integration/gh-issues/699-tests.js b/test/integration/gh-issues/699-tests.js index 2918c9ae..f9cb876d 100644 --- a/test/integration/gh-issues/699-tests.js +++ b/test/integration/gh-issues/699-tests.js @@ -15,7 +15,9 @@ helper.pg.connect(helper.config, function (err, client, done) { var stream = client.query(copyFrom("COPY employee FROM STDIN")); stream.on('end', function () { done(); - helper.pg.end(); + setTimeout(() => { + helper.pg.end(); + }, 50) }); for (var i = 1; i <= 5; i++) {