From a720dc774b33b8eabc70acf8c8d1d1894c16bf10 Mon Sep 17 00:00:00 2001 From: Brian Carlson Date: Sat, 5 Aug 2017 16:59:20 -0500 Subject: [PATCH] Some cleanup --- .travis.yml | 4 +++- index.js | 13 +++++------ package.json | 6 ++--- test/error-handling.js | 50 ++++++++++++++++++++++++++++++++++++++---- test/index.js | 1 + test/mocha.opts | 1 - 6 files changed, 59 insertions(+), 16 deletions(-) diff --git a/.travis.yml b/.travis.yml index 267b07b5..54d66546 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,9 @@ language: node_js dist: trusty sudo: false node_js: + - "4.2" - "6" + - "8" env: - PGUSER=postgres services: @@ -10,4 +12,4 @@ services: addons: postgresql: "9.6" before_script: - - psql -c 'create database travis;' -U postgres | true \ No newline at end of file + - psql -c 'create database travis;' -U postgres | true diff --git a/index.js b/index.js index 84236fc6..6b810381 100644 --- a/index.js +++ b/index.js @@ -98,10 +98,8 @@ Cursor.prototype.handleReadyForQuery = function() { this.state = 'done' } -Cursor.prototype.handleEmptyQuery = function(con) { - if (con.sync) { - con.sync() - } +Cursor.prototype.handleEmptyQuery = function() { + this.connection.sync() }; Cursor.prototype.handleError = function(msg) { @@ -140,8 +138,9 @@ Cursor.prototype.end = function(cb) { if(this.state != 'initialized') { this.connection.sync() } - this.connection.end() this.connection.stream.once('end', cb) + console.log('calling end on connection') + this.connection.end() } Cursor.prototype.close = function(cb) { @@ -167,10 +166,10 @@ Cursor.prototype.read = function(rows, cb) { return this._queue.push([rows, cb]) } if(this.state == 'error') { - return cb(this._error) + return setImmediate(() => cb(this._error)) } if(this.state == 'done') { - return cb(null, []) + return setImmediate(() => cb(null, [])) } else { throw new Error("Unknown state: " + this.state) diff --git a/package.json b/package.json index f37edf9d..bfd060b9 100644 --- a/package.json +++ b/package.json @@ -7,13 +7,13 @@ "test": "test" }, "scripts": { - "test": "mocha test/" + "test": "mocha test" }, "author": "Brian M. Carlson", "license": "MIT", "devDependencies": { - "pg": "~6.0.0", - "mocha": "~1.17.1" + "mocha": "^3.5.0", + "pg": "~6.0.0" }, "dependencies": {} } diff --git a/test/error-handling.js b/test/error-handling.js index fedee4b1..45c7f363 100644 --- a/test/error-handling.js +++ b/test/error-handling.js @@ -20,16 +20,58 @@ describe('error handling', function() { }) }) -describe('proper cleanup', function() { - it('can issue multiple cursors on one client', function(done) { +describe('read callback does not fire sync', () => { + it('does not fire error callback sync', (done) => { + var client = new pg.Client() + client.connect() + var cursor = client.query(new Cursor('asdfdffsdf')) + let after = false + cursor.read(1, function(err) { + assert(err, 'error should be returned') + assert.equal(after, true, 'should not call read sync') + after = false + cursor.read(1, function (err) { + assert(err, 'error should be returned') + assert.equal(after, true, 'should not call read sync') + client.end() + done() + }) + after = true + }) + after = true + }) + + it('does not fire result sync after finished', (done) => { + var client = new pg.Client() + client.connect() + var cursor = client.query(new Cursor('SELECT NOW()')) + let after = false + cursor.read(1, function(err) { + assert.equal(after, true, 'should not call read sync') + cursor.read(1, function (err) { + after = false + cursor.read(1, function (err) { + assert.equal(after, true, 'should not call read sync') + client.end() + done() + }) + after = true + }) + }) + after = true + }) +}) + +describe('proper cleanup', function () { + it('can issue multiple cursors on one client', function (done) { var client = new pg.Client() client.connect() var cursor1 = client.query(new Cursor(text)) - cursor1.read(8, function(err, rows) { + cursor1.read(8, function (err, rows) { assert.ifError(err) assert.equal(rows.length, 5) cursor2 = client.query(new Cursor(text)) - cursor2.read(8, function(err, rows) { + cursor2.read(8, function (err, rows) { assert.ifError(err) assert.equal(rows.length, 5) client.end() diff --git a/test/index.js b/test/index.js index cc97960e..fd8c73d2 100644 --- a/test/index.js +++ b/test/index.js @@ -30,6 +30,7 @@ describe('cursor', function() { }) }) + for (var i = 0; i < 100; i++) it('end before reading to end', function(done) { var cursor = this.pgCursor(text) cursor.read(3, function(err, res) { diff --git a/test/mocha.opts b/test/mocha.opts index 8fd0f04e..46e8e69d 100644 --- a/test/mocha.opts +++ b/test/mocha.opts @@ -1,3 +1,2 @@ --no-exit --bail ---reporter=spec