Skip to content

Commit 2c3f55e

Browse files
committed
Make all tests pass
1 parent c961c90 commit 2c3f55e

File tree

5 files changed

+35
-24
lines changed

5 files changed

+35
-24
lines changed

lib/client.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,10 @@ Client.prototype.connect = function(callback) {
173173
var activeQuery = self.activeQuery;
174174
self.activeQuery = null;
175175
self.readyForQuery = true;
176-
self._pulseQueryQueue();
177176
if(activeQuery) {
178177
activeQuery.handleReadyForQuery(con);
179178
}
179+
self._pulseQueryQueue();
180180
});
181181

182182
con.on('error', function(error) {
@@ -390,6 +390,12 @@ Client.prototype.query = function(config, values, callback) {
390390

391391
Client.prototype.end = function(cb) {
392392
this._ending = true;
393+
if (this.activeQuery) {
394+
// if we have an active query we need to force a disconnect
395+
// on the socket - otherwise a hung query could block end forever
396+
this.connection.stream.destroy(new Error('Connection terminated by user'))
397+
return;
398+
}
393399
if (cb) {
394400
this.connection.end();
395401
this.connection.once('end', cb);

test/integration/client/error-handling-tests.js

+18-14
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,26 @@ var createErorrClient = function() {
1616

1717
const suite = new helper.Suite('error handling')
1818

19-
suite.test('query receives error on client shutdown', false, function(done) {
19+
suite.test('query receives error on client shutdown', function(done) {
2020
var client = new Client();
2121
client.connect(assert.success(function() {
2222
const config = {
2323
text: 'select pg_sleep(5)',
2424
name: 'foobar'
2525
}
26+
let queryError;
2627
client.query(new pg.Query(config), assert.calls(function(err, res) {
2728
assert(err instanceof Error)
28-
done()
29+
queryError = err
2930
}));
30-
setTimeout(() => {
31-
client.end()
32-
assert.emits(client, 'end');
33-
}, 50)
31+
setTimeout(() => client.end(), 50)
32+
client.once('end', () => {
33+
assert(queryError instanceof Error)
34+
done()
35+
})
3436
}));
3537
});
3638

37-
;(function () {
38-
var client = createErorrClient();
39-
40-
var q = client.query({ text: "CREATE TEMP TABLE boom(age integer); INSERT INTO boom (age) VALUES (28);", binary: false });
41-
4239
var ensureFuture = function (testClient, done) {
4340
var goodQuery = testClient.query(new pg.Query("select age from boom"));
4441
assert.emits(goodQuery, 'row', function (row) {
@@ -48,6 +45,10 @@ suite.test('query receives error on client shutdown', false, function(done) {
4845
};
4946

5047
suite.test("when query is parsing", (done) => {
48+
var client = createErorrClient();
49+
50+
var q = client.query({ text: "CREATE TEMP TABLE boom(age integer); INSERT INTO boom (age) VALUES (28);" });
51+
5152

5253
//this query wont parse since there isn't a table named bang
5354
var query = client.query(new pg.Query({
@@ -61,6 +62,10 @@ suite.test('query receives error on client shutdown', false, function(done) {
6162
});
6263

6364
suite.test("when a query is binding", function (done) {
65+
var client = createErorrClient();
66+
67+
var q = client.query({ text: "CREATE TEMP TABLE boom(age integer); INSERT INTO boom (age) VALUES (28);" });
68+
6469

6570
var query = client.query(new pg.Query({
6671
text: 'select * from boom where age = $1',
@@ -72,7 +77,6 @@ suite.test('query receives error on client shutdown', false, function(done) {
7277
ensureFuture(client, done);
7378
});
7479
});
75-
})();
7680

7781
suite.test('non-query error with callback', function(done) {
7882
var client = new Client({
@@ -101,7 +105,7 @@ suite.test('non-error calls supplied callback', function(done) {
101105

102106
suite.test('when connecting to an invalid host with callback', function (done) {
103107
var client = new Client({
104-
host: '!#%!@#%'
108+
user: 'very invalid username',
105109
});
106110
client.connect(function(error, client) {
107111
assert(error instanceof Error);
@@ -111,7 +115,7 @@ suite.test('when connecting to an invalid host with callback', function (done) {
111115

112116
suite.test('when connecting to invalid host with promise', function(done) {
113117
var client = new Client({
114-
host: 'asdlfkjasldkfjlaskdfj'
118+
user: 'very invalid username'
115119
});
116120
client.connect().catch((e) => done());
117121
});

test/integration/client/query-as-promise-tests.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@ pg.connect(helper.config, assert.success(function(client, done) {
1616
client.query('ALKJSDF')
1717
.catch(function(e) {
1818
assert(e instanceof Error)
19+
client.query('SELECT 1 as num')
20+
.then(function (result) {
21+
assert.equal(result.rows[0].num, 1)
22+
done()
23+
pg.end()
24+
})
1925
})
2026
})
21-
22-
client.query('SELECT 1 as num')
23-
.then(function(result) {
24-
assert.equal(result.rows[0].num, 1)
25-
done()
26-
pg.end()
27-
})
2827
}))

test/integration/client/query-error-handling-tests.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var Query = helper.pg.Query;
55
test('error during query execution', function() {
66
var client = new Client(helper.args);
77
client.connect(assert.success(function() {
8-
var queryText = 'select pg_sleep(5)'
8+
var queryText = 'select pg_sleep(10)'
99
var sleepQuery = new Query(queryText);
1010
var pidColName = 'procpid'
1111
var queryColName = 'current_query';

test/integration/gh-issues/699-tests.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ helper.pg.connect(helper.config, function (err, client, done) {
1515
var stream = client.query(copyFrom("COPY employee FROM STDIN"));
1616
stream.on('end', function () {
1717
done();
18-
helper.pg.end();
18+
setTimeout(() => {
19+
helper.pg.end();
20+
}, 50)
1921
});
2022

2123
for (var i = 1; i <= 5; i++) {

0 commit comments

Comments
 (0)