Some cleanup

This commit is contained in:
Brian Carlson 2017-08-05 16:59:20 -05:00
parent 5a0af8cdd1
commit a720dc774b
6 changed files with 59 additions and 16 deletions

View File

@ -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
- psql -c 'create database travis;' -U postgres | true

View File

@ -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)

View File

@ -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": {}
}

View File

@ -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()

View File

@ -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) {

View File

@ -1,3 +1,2 @@
--no-exit
--bail
--reporter=spec