-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cancellable Query with AbortSignal option
- Loading branch information
Showing
4 changed files
with
209 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
packages/pg/test/integration/cancel/cancel-query-with-abort-signal-tests.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
var helper = require('./../test-helper') | ||
|
||
var pg = helper.pg | ||
const Client = pg.Client | ||
const DatabaseError = pg.DatabaseError | ||
|
||
if (!global.AbortController) { | ||
// skip these tests on node < 15 | ||
return | ||
} | ||
|
||
const suite = new helper.Suite('query cancellation with abort signal') | ||
|
||
suite.test('query with signal succeeds if not aborted', function (done) { | ||
const client = new Client() | ||
const { signal } = new AbortController() | ||
|
||
client.connect( | ||
assert.success(() => { | ||
client.query( | ||
new pg.Query({ text: 'select pg_sleep(0.1)', signal }), | ||
assert.success((result) => { | ||
assert.equal(result.rows[0].pg_sleep, '') | ||
client.end(done) | ||
}) | ||
) | ||
}) | ||
) | ||
}) | ||
|
||
suite.test('query with signal is not submitted if the signal is already aborted', function (done) { | ||
const client = new Client() | ||
const signal = AbortSignal.abort() | ||
|
||
let counter = 0 | ||
|
||
client.query( | ||
new pg.Query({ text: 'INVALID SQL...' }), | ||
assert.calls((err) => { | ||
assert(err instanceof DatabaseError) | ||
counter++ | ||
}) | ||
) | ||
|
||
client.query( | ||
new pg.Query({ text: 'begin' }), | ||
assert.success(() => { | ||
counter++ | ||
}) | ||
) | ||
|
||
client.query( | ||
new pg.Query({ text: 'INVALID SQL...', signal }), | ||
assert.calls((err) => { | ||
assert.equal(err.name, 'AbortError') | ||
counter++ | ||
}) | ||
) | ||
|
||
client.query( | ||
new pg.Query({ text: 'select 1' }), | ||
assert.success(() => { | ||
counter++ | ||
assert.equal(counter, 4) | ||
client.end(done) | ||
}) | ||
) | ||
|
||
client.connect(assert.success(() => {})) | ||
}) | ||
|
||
suite.test('query can be canceled with abort signal', function (done) { | ||
const client = new Client() | ||
const ac = new AbortController() | ||
const { signal } = ac | ||
|
||
client.query( | ||
new pg.Query({ text: 'SELECT pg_sleep(0.5)', signal }), | ||
assert.calls((err) => { | ||
assert(err instanceof DatabaseError) | ||
assert(err.code === '57014') | ||
client.end(done) | ||
}) | ||
) | ||
|
||
client.connect( | ||
assert.success(() => { | ||
setTimeout(() => { | ||
ac.abort() | ||
}, 50) | ||
}) | ||
) | ||
}) | ||
|
||
suite.test('long abort signal timeout does not keep the query / connection going', function (done) { | ||
const client = new Client() | ||
const signal = AbortSignal.timeout(10_000) | ||
|
||
client.query( | ||
new pg.Query({ text: 'SELECT pg_sleep(0.1)', signal }), | ||
assert.success((result) => { | ||
assert.equal(result.rows[0].pg_sleep, '') | ||
client.end(done) | ||
}) | ||
) | ||
|
||
client.connect(assert.success(() => {})) | ||
}) |