Pass all tests

This commit is contained in:
Brian M. Carlson 2013-11-20 22:30:52 -06:00
parent 31b2b1da6f
commit 9af987fa63
3 changed files with 61 additions and 24 deletions

View File

@ -65,7 +65,6 @@ Cursor.prototype.handlePortalSuspended = function() {
}
Cursor.prototype.handleReadyForQuery = function() {
}
Cursor.prototype.handleError = function(msg) {
@ -79,10 +78,11 @@ Cursor.prototype.handleError = function(msg) {
for(var i = 0; i < this._queue.length; i++) {
this._queue.pop()[1](msg)
}
//call sync to keep this connection from hanging
this.connection.sync()
}
Cursor.prototype._getRows = function(rows, cb) {
console.log('get', rows)
this.state = 'busy'
this._cb = cb
this._rows = []
@ -103,7 +103,6 @@ Cursor.prototype.end = function(cb) {
}
Cursor.prototype.read = function(rows, cb) {
console.log('read', rows, this.state)
var self = this
if(this.state == 'idle') {
return this._getRows(rows, cb)

40
test/error-handling.js Normal file
View File

@ -0,0 +1,40 @@
var assert = require('assert')
var Cursor = require('../')
var pg = require('pg.js')
var text = 'SELECT generate_series as num FROM generate_series(0, 4)'
describe('error handling', function() {
it('can continue after error', function(done) {
var client = new pg.Client()
client.connect()
var cursor = client.query(new Cursor('asdfdffsdf'))
cursor.read(1, function(err) {
assert(err)
client.query('SELECT NOW()', function(err, res) {
assert.ifError(err)
client.end()
done()
})
})
})
})
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) {
assert.ifError(err)
assert.equal(rows.length, 5)
cursor2 = client.query(new Cursor(text))
cursor2.read(8, function(err, rows) {
assert.ifError(err)
assert.equal(rows.length, 5)
client.end()
done()
})
})
})
})

View File

@ -6,25 +6,23 @@ var text = 'SELECT generate_series as num FROM generate_series(0, 5)'
describe('cursor', function() {
var client;
beforeEach(function(done) {
var client = this.client = new pg.Client()
client.connect(done)
var pgCursor = function(text, values) {
client.connect()
client.on('drain', client.end.bind(client))
return client.query(new Cursor(text, values || []))
}
before(function() {
client = new pg.Client()
this.pgCursor = function(text, values) {
client.on('drain', client.end.bind(client))
return client.query(new Cursor(text, values || []))
}
})
after(function() {
client.end()
afterEach(function() {
this.client.end()
})
it('fetch 6 when asking for 10', function(done) {
var cursor = pgCursor(text)
var cursor = this.pgCursor(text)
cursor.read(10, function(err, res) {
assert.ifError(err)
assert.equal(res.length, 6)
@ -33,7 +31,7 @@ describe('cursor', function() {
})
it('end before reading to end', function(done) {
var cursor = pgCursor(text)
var cursor = this.pgCursor(text)
cursor.read(3, function(err, res) {
assert.ifError(err)
assert.equal(res.length, 3)
@ -42,7 +40,7 @@ describe('cursor', function() {
})
it('callback with error', function(done) {
var cursor = pgCursor('select asdfasdf')
var cursor = this.pgCursor('select asdfasdf')
cursor.read(1, function(err) {
assert(err)
done()
@ -51,7 +49,7 @@ describe('cursor', function() {
it('read a partial chunk of data', function(done) {
var cursor = pgCursor(text)
var cursor = this.pgCursor(text)
cursor.read(2, function(err, res) {
assert.ifError(err)
assert.equal(res.length, 2)
@ -70,7 +68,7 @@ describe('cursor', function() {
})
it('read return length 0 past the end', function(done) {
var cursor = pgCursor(text)
var cursor = this.pgCursor(text)
cursor.read(2, function(err, res) {
cursor.read(100, function(err, res) {
assert.equal(res.length, 4)
@ -84,19 +82,19 @@ describe('cursor', function() {
it('read huge result', function(done) {
this.timeout(10000)
var text = 'SELECT generate_series as num FROM generate_series(0, 1000000)'
var text = 'SELECT generate_series as num FROM generate_series(0, 100000)'
var values = []
cursor = pgCursor(text, values);
cursor = this.pgCursor(text, values);
var count = 0;
var read = function() {
cursor.read(1000, function(err, rows) {
cursor.read(100, function(err, rows) {
if(err) return done(err);
if(!rows.length) {
assert.equal(count, 1000001)
assert.equal(count, 100001)
return done()
}
count += rows.length;
if(count%100000 == 0) {
if(count%10000 == 0) {
//console.log(count)
}
setImmediate(read)