diff --git a/lib/native/client.js b/lib/native/client.js index e57a9419..639484ec 100644 --- a/lib/native/client.js +++ b/lib/native/client.js @@ -63,6 +63,16 @@ Client.prototype.connect = function(cb) { return self.emit('error', err); }; + var result + if (!cb) { + var resolve, reject + cb = (err) => err ? reject(err) : resolve() + result = new global.Promise(function(res, rej) { + resolve = res + reject = rej + }) + } + this.connectionParameters.getLibpqConnectionString(function(err, conString) { if(err) return onError(err); self.native.connect(conString, function(err) { @@ -95,6 +105,8 @@ Client.prototype.connect = function(cb) { if(cb) cb(); }); }); + + return result }; //send a query to the server @@ -135,6 +147,15 @@ Client.prototype.end = function(cb) { if(!this._connected) { this.once('connect', this.end.bind(this, cb)); } + var result; + if (!cb) { + var resolve, reject + cb = (err) => err ? reject(err) : resolve() + result = new global.Promise(function(res, rej) { + resolve = res + reject = rej + }) + } this.native.end(function() { //send an error to the active query if(self._hasActiveQuery()) { @@ -145,6 +166,7 @@ Client.prototype.end = function(cb) { self.emit('end'); if(cb) cb(); }); + return result }; Client.prototype._hasActiveQuery = function() { diff --git a/test/integration/client/error-handling-tests.js b/test/integration/client/error-handling-tests.js index 9aff27fb..cc60e2c9 100644 --- a/test/integration/client/error-handling-tests.js +++ b/test/integration/client/error-handling-tests.js @@ -16,6 +16,24 @@ var createErorrClient = function() { const suite = new helper.Suite('error handling') +suite.test('query receives error on client shutdown', false, function(done) { + var client = new Client(); + client.connect(assert.success(function() { + const config = { + text: 'select pg_sleep(5)', + name: 'foobar' + } + client.query(new pg.Query(config), assert.calls(function(err, res) { + assert(err instanceof Error) + done() + })); + setTimeout(() => { + client.end() + assert.emits(client, 'end'); + }, 50) + })); +}); + ;(function () { var client = createErorrClient(); @@ -83,7 +101,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: 'asldkfjasdf!!#1308140.com' + host: '!#%!@#%' }); client.connect(function(error, client) { assert(error instanceof Error); @@ -109,24 +127,6 @@ suite.test('non-query error', function(done) { }) }); - -suite.test('query receives error on client shutdown', false, function(done) { - var client = new Client(); - client.connect(function(err) { - if (err) { - return done(err) - } - client.query('SELECT pg_sleep(5)', assert.calls(function(err, res) { - assert(err instanceof Error) - done() - })); - setTimeout(() => { - client.end() - assert.emits(client, 'end'); - }, 50) - }); -}); - suite.test('within a simple query', (done) => { var client = createErorrClient(); diff --git a/test/integration/client/promise-api-tests.js b/test/integration/client/promise-api-tests.js index 17094c9d..ee088f94 100644 --- a/test/integration/client/promise-api-tests.js +++ b/test/integration/client/promise-api-tests.js @@ -40,10 +40,11 @@ suite.test('connected client does not reject promise after connection', (done) = setTimeout(() => { client.on('error', (e) => { assert(e instanceof Error) + client.end() done() }) // manually kill the connection - client.connection.stream.end() + client.emit('error', new Error('something bad happened...but not really')) }, 50) }) }) diff --git a/test/integration/client/query-error-handling-prepared-statement-tests.js b/test/integration/client/query-error-handling-prepared-statement-tests.js index a250f78b..7aa68f59 100644 --- a/test/integration/client/query-error-handling-prepared-statement-tests.js +++ b/test/integration/client/query-error-handling-prepared-statement-tests.js @@ -5,7 +5,7 @@ var util = require('util'); var suite = new helper.Suite(); suite.test('client end during query execution of prepared statement', function(done) { - var client = new Client(helper.args); + var client = new Client(); client.connect(assert.success(function() { var sleepQuery = 'select pg_sleep($1)'; @@ -18,11 +18,11 @@ suite.test('client end during query execution of prepared statement', function(d var queryInstance = new Query(queryConfig, assert.calls(function (err, result) { assert.equal(err.message, 'Connection terminated'); + done(); })) var query1 = client.query(queryInstance); - query1.on('error', function (err) { assert.fail('Prepared statement should not emit error'); }); @@ -35,7 +35,7 @@ suite.test('client end during query execution of prepared statement', function(d assert.fail('Prepared statement when executed should not return before being killed'); }); - client.end(done); + client.end(); })); }); diff --git a/test/native/connection-tests.js b/test/native/connection-tests.js deleted file mode 100644 index be84be6e..00000000 --- a/test/native/connection-tests.js +++ /dev/null @@ -1,36 +0,0 @@ -var helper = require(__dirname + "/../test-helper"); -var Client = require(__dirname + "/../../lib/native"); -var domain = require('domain'); - -test('connecting with wrong parameters', function() { - var con = new Client("user=asldfkj hostaddr=127.0.0.1 port=5432 dbname=asldkfj"); - assert.emits(con, 'error', function(error) { - assert.ok(error != null, "error should not be null"); - con.end(); - }); - - con.connect(); -}); - -test('connects', function() { - var con = new Client(helper.config); - con.connect(); - assert.emits(con, 'connect', function() { - test('disconnects', function() { - con.end(); - }) - }) -}) - -test('preserves domain', function() { - var dom = domain.create(); - - dom.run(function() { - var con = new Client(helper.config); - assert.ok(dom === require('domain').active, 'domain is active'); - con.connect(function() { - assert.ok(dom === require('domain').active, 'domain is still active'); - con.end(); - }); - }); -}) diff --git a/test/suite.js b/test/suite.js index 1de1b69c..7d248b36 100644 --- a/test/suite.js +++ b/test/suite.js @@ -45,7 +45,7 @@ class Suite { run(test, cb) { process.stdout.write(' ' + test.name + ' ') if (!test.action) { - process.stdout.write('? - SKIPPED') + process.stdout.write('? - SKIPPED\n') return cb() }