-
Notifications
You must be signed in to change notification settings - Fork 0
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
Use connection pause/resume to prevent kill race condition #7
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1051,6 +1051,68 @@ module.exports = function (knex) { | |
}); | ||
}); | ||
|
||
it('.timeout(ms, {cancel: true}) should not cancel subsequent queries in a transaction if timeouted query completes while being cancelled', async function () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test will fail without The current behaviour without Fixed behaviour without the race condition with Btw, note that query B has no timeout, to make sure it won't timeout while waiting in the queue. |
||
if (!isMysql(knex)) { | ||
return this.skip(); | ||
} | ||
|
||
const getProcessesForDriver = { | ||
pg: async () => { | ||
const results = await knex.raw('SELECT * from pg_stat_activity'); | ||
return _.map(_.filter(results.rows, {state: 'active'}), 'query'); | ||
}, | ||
mysql: async () => { | ||
const results = await knex.raw('SHOW PROCESSLIST'); | ||
return _.map(results[0], 'Info'); | ||
}, | ||
mysql2: async () => { | ||
const results = await knex.raw('SHOW PROCESSLIST'); | ||
return _.map(results[0], 'Info'); | ||
}, | ||
}; | ||
|
||
const driverName = knex.client.driverName; | ||
if (!Object.prototype.hasOwnProperty.call(getProcessesForDriver, driverName)) { | ||
throw new Error('Missing test query for driverName: ' + driverName); | ||
} | ||
|
||
const getProcesses = getProcessesForDriver[driverName]; | ||
|
||
return knex.transaction(async (trx) => { | ||
const queryA = trx.raw('SELECT SLEEP(1), "A"'); | ||
const queryB = trx.raw('SELECT SLEEP(1), "B"'); | ||
|
||
const promise = Promise.all([ | ||
queryA.timeout(500, { cancel: true }), | ||
queryB, | ||
]) | ||
|
||
await delay(100); | ||
const processesBeforeTimeout = await getProcesses(); | ||
expect(processesBeforeTimeout).to.include(queryA.toString()); | ||
expect(processesBeforeTimeout).to.not.include(queryB.toString()); | ||
|
||
const origAcquireRawConnection = trx.client.acquireRawConnection.bind(trx.client); | ||
trx.client.acquireRawConnection = async () => { | ||
await delay(600); | ||
return origAcquireRawConnection(); | ||
} | ||
|
||
await expect(promise).to.eventually.be.rejected.and.to.deep.include({ | ||
timeout: 500, | ||
name: 'KnexTimeoutError', | ||
message: 'Defined query timeout of 500ms exceeded when running query.', | ||
}); | ||
|
||
trx.client.acquireRawConnection = origAcquireRawConnection; | ||
|
||
await delay(100); | ||
const processesAfterTimeout = await getProcesses(); | ||
expect(processesAfterTimeout).to.not.include(queryA.toString()); | ||
expect(processesAfterTimeout).to.include(queryB.toString()); | ||
}); | ||
}); | ||
|
||
it('.timeout(ms, {cancel: true}) should cancel slow query even if connection pool is exhausted', async function () { | ||
// Only mysql/postgres query cancelling supported for now | ||
if (!isMysql(knex) && !isPostgreSQL(knex)) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor tweak: this could be run before connection destroy, so working connection will continue to work earlier
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved in f1a3d8a