Skip to content

Commit 9af987f

Browse files
committed
Pass all tests
1 parent 31b2b1d commit 9af987f

File tree

3 files changed

+61
-24
lines changed

3 files changed

+61
-24
lines changed

index.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ Cursor.prototype.handlePortalSuspended = function() {
6565
}
6666

6767
Cursor.prototype.handleReadyForQuery = function() {
68-
6968
}
7069

7170
Cursor.prototype.handleError = function(msg) {
@@ -79,10 +78,11 @@ Cursor.prototype.handleError = function(msg) {
7978
for(var i = 0; i < this._queue.length; i++) {
8079
this._queue.pop()[1](msg)
8180
}
81+
//call sync to keep this connection from hanging
82+
this.connection.sync()
8283
}
8384

8485
Cursor.prototype._getRows = function(rows, cb) {
85-
console.log('get', rows)
8686
this.state = 'busy'
8787
this._cb = cb
8888
this._rows = []
@@ -103,7 +103,6 @@ Cursor.prototype.end = function(cb) {
103103
}
104104

105105
Cursor.prototype.read = function(rows, cb) {
106-
console.log('read', rows, this.state)
107106
var self = this
108107
if(this.state == 'idle') {
109108
return this._getRows(rows, cb)

test/error-handling.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
var assert = require('assert')
2+
var Cursor = require('../')
3+
var pg = require('pg.js')
4+
5+
var text = 'SELECT generate_series as num FROM generate_series(0, 4)'
6+
7+
describe('error handling', function() {
8+
it('can continue after error', function(done) {
9+
var client = new pg.Client()
10+
client.connect()
11+
var cursor = client.query(new Cursor('asdfdffsdf'))
12+
cursor.read(1, function(err) {
13+
assert(err)
14+
client.query('SELECT NOW()', function(err, res) {
15+
assert.ifError(err)
16+
client.end()
17+
done()
18+
})
19+
})
20+
})
21+
})
22+
23+
describe('proper cleanup', function() {
24+
it('can issue multiple cursors on one client', function(done) {
25+
var client = new pg.Client()
26+
client.connect()
27+
var cursor1 = client.query(new Cursor(text))
28+
cursor1.read(8, function(err, rows) {
29+
assert.ifError(err)
30+
assert.equal(rows.length, 5)
31+
cursor2 = client.query(new Cursor(text))
32+
cursor2.read(8, function(err, rows) {
33+
assert.ifError(err)
34+
assert.equal(rows.length, 5)
35+
client.end()
36+
done()
37+
})
38+
})
39+
})
40+
})

test/index.js

+19-21
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,23 @@ var text = 'SELECT generate_series as num FROM generate_series(0, 5)'
66

77
describe('cursor', function() {
88

9-
var client;
9+
beforeEach(function(done) {
10+
var client = this.client = new pg.Client()
11+
client.connect(done)
1012

11-
var pgCursor = function(text, values) {
12-
client.connect()
13-
client.on('drain', client.end.bind(client))
14-
return client.query(new Cursor(text, values || []))
15-
}
16-
17-
before(function() {
18-
client = new pg.Client()
13+
this.pgCursor = function(text, values) {
14+
client.on('drain', client.end.bind(client))
15+
return client.query(new Cursor(text, values || []))
16+
}
1917
})
2018

2119

22-
after(function() {
23-
client.end()
20+
afterEach(function() {
21+
this.client.end()
2422
})
2523

2624
it('fetch 6 when asking for 10', function(done) {
27-
var cursor = pgCursor(text)
25+
var cursor = this.pgCursor(text)
2826
cursor.read(10, function(err, res) {
2927
assert.ifError(err)
3028
assert.equal(res.length, 6)
@@ -33,7 +31,7 @@ describe('cursor', function() {
3331
})
3432

3533
it('end before reading to end', function(done) {
36-
var cursor = pgCursor(text)
34+
var cursor = this.pgCursor(text)
3735
cursor.read(3, function(err, res) {
3836
assert.ifError(err)
3937
assert.equal(res.length, 3)
@@ -42,7 +40,7 @@ describe('cursor', function() {
4240
})
4341

4442
it('callback with error', function(done) {
45-
var cursor = pgCursor('select asdfasdf')
43+
var cursor = this.pgCursor('select asdfasdf')
4644
cursor.read(1, function(err) {
4745
assert(err)
4846
done()
@@ -51,7 +49,7 @@ describe('cursor', function() {
5149

5250

5351
it('read a partial chunk of data', function(done) {
54-
var cursor = pgCursor(text)
52+
var cursor = this.pgCursor(text)
5553
cursor.read(2, function(err, res) {
5654
assert.ifError(err)
5755
assert.equal(res.length, 2)
@@ -70,7 +68,7 @@ describe('cursor', function() {
7068
})
7169

7270
it('read return length 0 past the end', function(done) {
73-
var cursor = pgCursor(text)
71+
var cursor = this.pgCursor(text)
7472
cursor.read(2, function(err, res) {
7573
cursor.read(100, function(err, res) {
7674
assert.equal(res.length, 4)
@@ -84,19 +82,19 @@ describe('cursor', function() {
8482

8583
it('read huge result', function(done) {
8684
this.timeout(10000)
87-
var text = 'SELECT generate_series as num FROM generate_series(0, 1000000)'
85+
var text = 'SELECT generate_series as num FROM generate_series(0, 100000)'
8886
var values = []
89-
cursor = pgCursor(text, values);
87+
cursor = this.pgCursor(text, values);
9088
var count = 0;
9189
var read = function() {
92-
cursor.read(1000, function(err, rows) {
90+
cursor.read(100, function(err, rows) {
9391
if(err) return done(err);
9492
if(!rows.length) {
95-
assert.equal(count, 1000001)
93+
assert.equal(count, 100001)
9694
return done()
9795
}
9896
count += rows.length;
99-
if(count%100000 == 0) {
97+
if(count%10000 == 0) {
10098
//console.log(count)
10199
}
102100
setImmediate(read)

0 commit comments

Comments
 (0)