Skip to content

Commit c7dc621

Browse files
authored
pg-cursor: Fix errors only being sent to half the queue (brianc#2831)
* pg-cursor: Add failing test for errors on queued reads * pg-cursor: Fix errors being sent to only half the queue
1 parent c7133eb commit c7dc621

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

packages/pg-cursor/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,10 @@ class Cursor extends EventEmitter {
171171
}
172172
// dispatch error to all waiting callbacks
173173
for (let i = 0; i < this._queue.length; i++) {
174-
this._queue.pop()[1](msg)
174+
const queuedCallback = this._queue[i][1]
175+
queuedCallback.call(this, msg)
175176
}
177+
this._queue.length = 0
176178

177179
if (this.listenerCount('error') > 0) {
178180
// only dispatch error events if we have a listener

packages/pg-cursor/test/error-handling.js

+17
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,23 @@ describe('error handling', function () {
1919
})
2020
})
2121
})
22+
23+
it('errors queued reads', async () => {
24+
const client = new pg.Client()
25+
await client.connect()
26+
27+
const cursor = client.query(new Cursor('asdfdffsdf'))
28+
29+
const immediateRead = cursor.read(1)
30+
const queuedRead1 = cursor.read(1)
31+
const queuedRead2 = cursor.read(1)
32+
33+
assert(await immediateRead.then(() => null, (err) => err))
34+
assert(await queuedRead1.then(() => null, (err) => err))
35+
assert(await queuedRead2.then(() => null, (err) => err))
36+
37+
client.end()
38+
})
2239
})
2340

2441
describe('read callback does not fire sync', () => {

0 commit comments

Comments
 (0)