Skip to content

Commit

Permalink
Add tests for commandComplete message being out of order
Browse files Browse the repository at this point in the history
  • Loading branch information
brianc committed Aug 12, 2024
1 parent 54dc7af commit 2d67ef6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
7 changes: 6 additions & 1 deletion packages/pg/lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,13 +377,18 @@ class Client extends EventEmitter {
}

_handleCommandComplete(msg) {
if (this.activeQuery == null) {
const error = new Error('Received unexpected commandComplete message from backend.')

Check failure on line 381 in packages/pg/lib/client.js

View workflow job for this annotation

GitHub Actions / lint

Delete `·`

Check failure on line 381 in packages/pg/lib/client.js

View workflow job for this annotation

GitHub Actions / lint

Delete `·`
this._handleErrorEvent(error)
return
}
// delegate commandComplete to active query
this.activeQuery.handleCommandComplete(msg, this.connection)
}

_handleParseComplete() {
if (this.activeQuery == null) {
const error =new Error('Received parseComplete when not in parsing state')
const error = new Error('Received unexpected parseComplete message from backend.')

Check failure on line 391 in packages/pg/lib/client.js

View workflow job for this annotation

GitHub Actions / lint

Delete `·`

Check failure on line 391 in packages/pg/lib/client.js

View workflow job for this annotation

GitHub Actions / lint

Delete `·`
this._handleErrorEvent(error)
return
}
Expand Down
25 changes: 16 additions & 9 deletions packages/pg/test/integration/gh-issues/3174-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const options = {
database: 'existing',
}

const startMockServer = (port, timeout, callback) => {
const startMockServer = (port, badBuffer, callback) => {
const sockets = new Set()

const server = net.createServer((socket) => {
Expand Down Expand Up @@ -48,7 +48,7 @@ const startMockServer = (port, timeout, callback) => {
socket.write(buffers.readyForQuery())
// this message is invalid, but sometimes sent out of order when using proxies or pg-bouncer
setImmediate(() => {
socket.write(buffers.parseComplete())
socket.write(badBuffer)
})
break
case 'Q':
Expand All @@ -58,7 +58,7 @@ const startMockServer = (port, timeout, callback) => {
socket.write(buffers.readyForQuery())
// this message is invalid, but sometimes sent out of order when using proxies or pg-bouncer
setImmediate(() => {
socket.write(buffers.parseComplete())
socket.write(badBuffer)
})
default:
// console.log('got code', code)
Expand All @@ -83,9 +83,11 @@ const delay = (ms) =>
setTimeout(resolve, ms)
})

suite.testAsync('Out of order parseComplete on simple query is catchable', async () => {
const testErrorBuffer = (bufferName, errorBuffer) => {

Check failure on line 87 in packages/pg/test/integration/gh-issues/3174-tests.js

View workflow job for this annotation

GitHub Actions / lint

Replace `⏎` with `··`

Check failure on line 87 in packages/pg/test/integration/gh-issues/3174-tests.js

View workflow job for this annotation

GitHub Actions / lint

Replace `⏎` with `··`
suite.testAsync(`Out of order ${bufferName} on simple query is catchable`, async () => {
const closeServer = await new Promise((resolve, reject) => {

Check failure on line 89 in packages/pg/test/integration/gh-issues/3174-tests.js

View workflow job for this annotation

GitHub Actions / lint

Insert `··`

Check failure on line 89 in packages/pg/test/integration/gh-issues/3174-tests.js

View workflow job for this annotation

GitHub Actions / lint

Insert `··`
return startMockServer(options.port, 0, (closeServer) => resolve(closeServer))
return startMockServer(options.port, errorBuffer, (closeServer) => resolve(closeServer))

Check failure on line 90 in packages/pg/test/integration/gh-issues/3174-tests.js

View workflow job for this annotation

GitHub Actions / lint

Insert `··`

Check failure on line 90 in packages/pg/test/integration/gh-issues/3174-tests.js

View workflow job for this annotation

GitHub Actions / lint

Insert `··`
})

Check failure on line 91 in packages/pg/test/integration/gh-issues/3174-tests.js

View workflow job for this annotation

GitHub Actions / lint

Insert `··`

Check failure on line 91 in packages/pg/test/integration/gh-issues/3174-tests.js

View workflow job for this annotation

GitHub Actions / lint

Insert `··`
const client = new helper.Client(options)

Check failure on line 92 in packages/pg/test/integration/gh-issues/3174-tests.js

View workflow job for this annotation

GitHub Actions / lint

Insert `··`

Check failure on line 92 in packages/pg/test/integration/gh-issues/3174-tests.js

View workflow job for this annotation

GitHub Actions / lint

Insert `··`
await client.connect()

Check failure on line 93 in packages/pg/test/integration/gh-issues/3174-tests.js

View workflow job for this annotation

GitHub Actions / lint

Insert `··`

Check failure on line 93 in packages/pg/test/integration/gh-issues/3174-tests.js

View workflow job for this annotation

GitHub Actions / lint

Insert `··`
Expand All @@ -105,9 +107,9 @@ suite.testAsync('Out of order parseComplete on simple query is catchable', async
await closeServer()
})

suite.testAsync('Out of order parseComplete on extended query is catchable', async () => {
suite.testAsync(`Out of order ${bufferName} on extended query is catchable`, async () => {
const closeServer = await new Promise((resolve, reject) => {
return startMockServer(options.port, 0, (closeServer) => resolve(closeServer))
return startMockServer(options.port, errorBuffer, (closeServer) => resolve(closeServer))
})
const client = new helper.Client(options)
await client.connect()
Expand All @@ -129,9 +131,9 @@ suite.testAsync('Out of order parseComplete on extended query is catchable', asy
await closeServer()
})

suite.testAsync('Out of order parseComplete on pool is catchable', async () => {
suite.testAsync(`Out of order ${bufferName} on pool is catchable`, async () => {
const closeServer = await new Promise((resolve, reject) => {
return startMockServer(options.port, 0, (closeServer) => resolve(closeServer))
return startMockServer(options.port, errorBuffer, (closeServer) => resolve(closeServer))
})
const pool = new helper.pg.Pool(options)

Expand All @@ -150,3 +152,8 @@ suite.testAsync('Out of order parseComplete on pool is catchable', async () => {
await pool.end()
await closeServer()
})

}

testErrorBuffer('parseComplete', buffers.parseComplete())
testErrorBuffer('commandComplete', buffers.commandComplete('f'))

0 comments on commit 2d67ef6

Please sign in to comment.