Skip to content

Commit 3a7b226

Browse files
committed
WIP
1 parent 02bcc9d commit 3a7b226

8 files changed

+209
-210
lines changed

lib/client.js

+21-14
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@ Client.prototype.connect = function(callback) {
6767
if(self.ssl) {
6868
con.requestSsl();
6969
} else {
70-
con.startup(self.getStartupConf());
70+
con.startup(self._getStartupConfiguration());
7171
}
7272
});
7373

7474
con.on('sslconnect', function() {
75-
con.startup(self.getStartupConf());
75+
con.startup(self._getStartupConfiguration());
7676
});
7777

7878
function checkPgPass(cb) {
@@ -122,7 +122,7 @@ Client.prototype.connect = function(callback) {
122122
//hook up query handling events to connection
123123
//after the connection initially becomes ready for queries
124124
con.once('readyForQuery', function() {
125-
self._attachEventListeners(con)
125+
self._attachEventListeners(con);
126126

127127
//process possible callback argument to Client#connect
128128
if (callback) {
@@ -134,16 +134,6 @@ Client.prototype.connect = function(callback) {
134134
self.emit('connect');
135135
});
136136

137-
if (!callback) {
138-
return new global.Promise(function (resolve, reject) {
139-
con.once('connect', () => {
140-
con.removeListener('error', reject)
141-
resolve()
142-
})
143-
con.once('error', reject)
144-
})
145-
}
146-
147137
con.on('error', function(error) {
148138
if(this.activeQuery) {
149139
var activeQuery = self.activeQuery;
@@ -197,8 +187,22 @@ Client.prototype.connect = function(callback) {
197187
self.emit('notice', msg);
198188
});
199189

190+
var result;
191+
192+
if (!callback) {
193+
result = new global.Promise(function (resolve, reject) {
194+
con.once('connect', function () {
195+
con.removeListener('error', reject)
196+
resolve()
197+
})
198+
this.once('error', reject)
199+
}.bind(this))
200+
}
201+
202+
return result;
200203
};
201204

205+
202206
// once a connection is established connect listeners
203207
Client.prototype._attachEventListeners = function(con) {
204208
var self = this;
@@ -251,7 +255,7 @@ Client.prototype._attachEventListeners = function(con) {
251255
});
252256
}
253257

254-
Client.prototype.getStartupConf = function() {
258+
Client.prototype._getStartupConfiguration = function() {
255259
var params = this.connectionParameters;
256260

257261
var data = {
@@ -405,6 +409,9 @@ Client.prototype.query = function(config, values, callback) {
405409

406410
Client.prototype.end = function(cb) {
407411
this._ending = true;
412+
if (this.activeQuery) {
413+
return this.connection.stream.end()
414+
}
408415
this.connection.end();
409416
if (cb) {
410417
this.connection.once('end', cb);

lib/connection.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,9 @@ Connection.prototype.password = function(password) {
186186
};
187187

188188
Connection.prototype._send = function(code, more) {
189-
if(!this.stream.writable) { return false; }
189+
if(!this.stream.writable) {
190+
return false;
191+
}
190192
if(more === true) {
191193
this.writer.addHeader(code);
192194
} else {
@@ -308,11 +310,12 @@ Connection.prototype.sync = function() {
308310
this._send(0x53);
309311
};
310312

313+
const END_BUFFER = new Buffer([0x58, 0x00, 0x00, 0x00, 0x04]);
311314
Connection.prototype.end = function() {
312315
//0x58 = 'X'
313316
this.writer.add(emptyBuffer);
314317
this._ending = true;
315-
this._send(0x58);
318+
return this.stream.end(END_BUFFER);
316319
};
317320

318321
Connection.prototype.close = function(msg, more) {

lib/promise.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const util = require('util')
2+
const deprecationMessage = 'Using the promise result as an event emitter is deprecated and will be removed in pg@8.0'
3+
module.exports = function(emitter, callback) {
4+
const promise = new global.Promise(callback)
5+
promise.on = util.deprecate(function () {
6+
emitter.on.apply(emitter, arguments)
7+
}, deprecationMessage);
8+
9+
promise.once = util.deprecate(function () {
10+
emitter.once.apply(emitter, arguments)
11+
}, deprecationMessage)
12+
}
+79-123
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,111 @@
1+
"use strict";
2+
13
var helper = require('./test-helper');
24
var util = require('util');
35

46
var pg = helper.pg
57

6-
78
var createErorrClient = function() {
89
var client = helper.client();
910
client.once('error', function(err) {
10-
//console.log('error', util.inspect(err));
1111
assert.fail('Client shoud not throw error during query execution');
1212
});
1313
client.on('drain', client.end.bind(client));
1414
return client;
1515
};
1616

17-
test('error handling', function() {
18-
test('within a simple query', function() {
19-
var client = createErorrClient();
20-
21-
var query = client.query(new pg.Query("select eeeee from yodas_dsflsd where pixistix = 'zoiks!!!'"));
17+
const suite = new helper.Suite('error handling')
2218

23-
assert.emits(query, 'error', function(error) {
24-
assert.equal(error.severity, "ERROR");
25-
});
19+
suite.test('query receives error on client shutdown', false, function(done) {
20+
var client = new Client();
21+
client.connect(function(err) {
22+
if (err) {
23+
return done(err)
24+
}
25+
client.query('SELECT pg_sleep(5)', assert.calls(function(err, res) {
26+
assert(err instanceof Error)
27+
done()
28+
}));
29+
setTimeout(() => {
30+
client.end()
31+
assert.emits(client, 'end');
32+
}, 50)
2633
});
34+
});
2735

28-
test('within a prepared statement', function() {
29-
30-
var client = createErorrClient();
31-
32-
var q = client.query({text: "CREATE TEMP TABLE boom(age integer); INSERT INTO boom (age) VALUES (28);", binary: false});
36+
suite.test('within a simple query', (done) => {
37+
var client = createErorrClient();
3338

34-
test("when query is parsing", function() {
39+
var query = client.query(new pg.Query("select eeeee from yodas_dsflsd where pixistix = 'zoiks!!!'"));
3540

36-
//this query wont parse since there ain't no table named bang
41+
assert.emits(query, 'error', function(error) {
42+
assert.equal(error.severity, "ERROR");
43+
done();
44+
});
45+
});
3746

38-
var ensureFuture = function(testClient) {
39-
test("client can issue more queries successfully", function() {
40-
var goodQuery = testClient.query(new pg.Query("select age from boom"));
41-
assert.emits(goodQuery, 'row', function(row) {
42-
assert.equal(row.age, 28);
43-
});
44-
});
45-
};
47+
(function () {
48+
var client = createErorrClient();
4649

47-
var query = client.query(new pg.Query({
48-
text: "select * from bang where name = $1",
49-
values: ['0']
50-
}));
50+
var q = client.query({ text: "CREATE TEMP TABLE boom(age integer); INSERT INTO boom (age) VALUES (28);", binary: false });
5151

52-
test("query emits the error", function() {
53-
assert.emits(query, 'error', function(err) {
54-
ensureFuture(client);
55-
});
56-
});
52+
var ensureFuture = function (testClient, done) {
53+
var goodQuery = testClient.query(new pg.Query("select age from boom"));
54+
assert.emits(goodQuery, 'row', function (row) {
55+
assert.equal(row.age, 28);
56+
done();
57+
});
58+
};
5759

58-
test("when a query is binding", function() {
60+
suite.test("when query is parsing", (done) => {
5961

60-
var query = client.query(new pg.Query({
61-
text: 'select * from boom where age = $1',
62-
values: ['asldkfjasdf']
63-
}));
62+
//this query wont parse since there isn't a table named bang
63+
var query = client.query(new pg.Query({
64+
text: "select * from bang where name = $1",
65+
values: ['0']
66+
}));
6467

65-
test("query emits the error", function() {
68+
assert.emits(query, 'error', function (err) {
69+
ensureFuture(client, done);
70+
});
71+
});
6672

67-
assert.emits(query, 'error', function(err) {
68-
test('error has right severity', function() {
69-
assert.equal(err.severity, "ERROR");
70-
})
73+
suite.test("when a query is binding", function (done) {
7174

72-
ensureFuture(client);
73-
});
74-
});
75+
var query = client.query(new pg.Query({
76+
text: 'select * from boom where age = $1',
77+
values: ['asldkfjasdf']
78+
}));
7579

76-
//TODO how to test for errors during execution?
77-
});
80+
assert.emits(query, 'error', function (err) {
81+
assert.equal(err.severity, "ERROR");
82+
ensureFuture(client, done);
7883
});
7984
});
85+
})();
8086

81-
test('non-query error', function() {
82-
var client = new Client({
83-
user:'asldkfjsadlfkj'
84-
});
85-
assert.emits(client, 'error');
86-
client.connect();
87+
suite.test('non-query error', function(done) {
88+
var client = new Client({
89+
user:'asldkfjsadlfkj'
8790
});
88-
89-
test('non-query error with callback', function() {
90-
var client = new Client({
91-
user:'asldkfjsadlfkj'
92-
});
93-
client.connect(assert.calls(function(error, client) {
94-
assert.ok(error);
95-
}));
91+
client.on('error', (err) => {
92+
assert(err instanceof Error)
93+
done()
9694
});
95+
client.connect();
96+
});
9797

98+
suite.test('non-query error with callback', function(done) {
99+
var client = new Client({
100+
user:'asldkfjsadlfkj'
101+
});
102+
client.connect(assert.calls(function(error, client) {
103+
assert(error instanceof Error)
104+
done()
105+
}));
98106
});
99107

100-
test('non-error calls supplied callback', function() {
108+
suite.test('non-error calls supplied callback', function(done) {
101109
var client = new Client({
102110
user: helper.args.user,
103111
password: helper.args.password,
@@ -108,75 +116,23 @@ test('non-error calls supplied callback', function() {
108116

109117
client.connect(assert.calls(function(err) {
110118
assert.ifError(err);
111-
client.end();
119+
client.end(done);
112120
}))
113121
});
114122

115-
test('when connecting to invalid host', function() {
116-
//this test fails about 30% on travis and only on travis...
117-
//I'm not sure what the cause could be
118-
if(process.env.TRAVIS) return false;
119-
123+
suite.test('when connecting to invalid host with promise', function(done) {
120124
var client = new Client({
121-
user: 'aslkdjfsdf',
122-
password: '1234',
123-
host: 'asldkfjasdf!!#1308140.com'
125+
host: 'asdlfkjasldkfjlaskdfj'
124126
});
125-
126-
var delay = 5000;
127-
var tid = setTimeout(function() {
128-
var msg = "When connecting to an invalid host the error event should be emitted but it has been " + delay + " and still no error event."
129-
assert(false, msg);
130-
}, delay);
131-
client.on('error', function() {
132-
clearTimeout(tid);
133-
})
134-
client.connect();
127+
client.connect().catch((e) => done());
135128
});
136129

137-
test('when connecting to invalid host with callback', function() {
130+
suite.test('when connecting to an invalid host with callback', function (done) {
138131
var client = new Client({
139-
user: 'brian',
140-
password: '1234',
141132
host: 'asldkfjasdf!!#1308140.com'
142133
});
143134
client.connect(function(error, client) {
144-
assert(error);
135+
assert(error instanceof Error);
136+
done();
145137
});
146138
});
147-
148-
test('multiple connection errors (gh#31)', function() {
149-
return false;
150-
test('with single client', function() {
151-
//don't run yet...this test fails...need to think of fix
152-
var client = new Client({
153-
user: 'blaksdjf',
154-
password: 'omfsadfas',
155-
host: helper.args.host,
156-
port: helper.args.port,
157-
database: helper.args.database
158-
});
159-
client.connect();
160-
assert.emits(client, 'error', function(e) {
161-
client.connect();
162-
assert.emits(client, 'error');
163-
});
164-
});
165-
166-
test('with callback method', function() {
167-
var badConString = "postgres://aslkdfj:oi14081@"+helper.args.host+":"+helper.args.port+"/"+helper.args.database;
168-
return false;
169-
});
170-
});
171-
172-
test('query receives error on client shutdown', function() {
173-
var client = new Client(helper.config);
174-
client.connect(assert.calls(function() {
175-
client.query('SELECT pg_sleep(5)', assert.calls(function(err, res) {
176-
assert(err);
177-
}));
178-
client.end();
179-
assert.emits(client, 'end');
180-
}));
181-
});
182-

0 commit comments

Comments
 (0)