Skip to content

Cursor.handleError(): notify all listeners #2834

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

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 2 additions & 1 deletion packages/pg-cursor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,9 @@ class Cursor extends EventEmitter {
}
// dispatch error to all waiting callbacks
for (let i = 0; i < this._queue.length; i++) {
this._queue.pop()[1](msg)
this._queue[i][1](msg)
}
this._queue.length = 0

if (this.listenerCount('error') > 0) {
// only dispatch error events if we have a listener
Expand Down
33 changes: 33 additions & 0 deletions packages/pg-cursor/test/error-handling.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,39 @@ describe('read callback does not fire sync', () => {
})
after = true
})

it('should fire for every listener', (done) => {
const client = new pg.Client()
client.connect()
const cursor = client.query(new Cursor('SYNTAX ERROR? YOU BET!'))
const callbackCounts = [0, 0, 0, 0, 0];
cursor.read(1, err => {
++callbackCounts[0];
assert.strictEqual(err.message, 'syntax error at or near "SYNTAX"');
})
cursor.read(1, err => {
++callbackCounts[1];
assert.strictEqual(err.message, 'syntax error at or near "SYNTAX"');
})
cursor.read(1, err => {
++callbackCounts[2];
assert.strictEqual(err.message, 'syntax error at or near "SYNTAX"');
})
cursor.read(1, err => {
++callbackCounts[3];
assert.strictEqual(err.message, 'syntax error at or near "SYNTAX"');
})
cursor.read(1, err => {
++callbackCounts[4];
assert.strictEqual(err.message, 'syntax error at or near "SYNTAX"');
})
setTimeout(() => {
assert.deepStrictEqual(callbackCounts, [1, 1, 1, 1, 1], 'all callbacks should be called exactly once')
assert.deepStrictEqual(cursor._queue, [], 'should empty the queue')
client.end()
done()
}, 100);
})
})

describe('proper cleanup', function () {
Expand Down