Skip to content

pg-cursor: Fix errors only being sent to half the queue #2831

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/pg-cursor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,10 @@ class Cursor extends EventEmitter {
}
// dispatch error to all waiting callbacks
for (let i = 0; i < this._queue.length; i++) {
this._queue.pop()[1](msg)
const queuedCallback = this._queue[i][1]
queuedCallback.call(this, msg)
}
this._queue.length = 0

if (this.listenerCount('error') > 0) {
// only dispatch error events if we have a listener
Expand Down
17 changes: 17 additions & 0 deletions packages/pg-cursor/test/error-handling.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,23 @@ describe('error handling', function () {
})
})
})

it('errors queued reads', async () => {
const client = new pg.Client()
await client.connect()

const cursor = client.query(new Cursor('asdfdffsdf'))

const immediateRead = cursor.read(1)
const queuedRead1 = cursor.read(1)
const queuedRead2 = cursor.read(1)

assert(await immediateRead.then(() => null, (err) => err))
assert(await queuedRead1.then(() => null, (err) => err))
assert(await queuedRead2.then(() => null, (err) => err))

client.end()
})
})

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