Skip to content
Open
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
6 changes: 4 additions & 2 deletions src/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ export class Query extends Promise {
if (!signal || this.settled)
return this

this.aborts && this.aborts()

if (signal.aborted)
return (this.cancel().catch(noop), this)

Expand Down Expand Up @@ -185,8 +187,8 @@ export class Query extends Promise {
resolve({ value, done: false })
return new Promise(r => prev = r)
}
this.resolve = x => (this.active = false, this.settled = true, this.trace && traced(this, null, x), resolve({ done: true }))
this.reject = x => (this.active = false, this.settled = true, this.trace && traced(this, x), reject(x))
this.resolve = x => (this.timer && untime(this), this.aborts && this.aborts(), this.active = false, this.settled = true, this.trace && traced(this, null, x), resolve({ done: true }))
this.reject = x => (this.timer && untime(this), this.timedOut && x && timedOut(x) && (x = late(this, x)), this.aborts && this.aborts(), this.active = false, this.settled = true, this.trace && traced(this, x), reject(x))
})
this.execute()
return promise
Expand Down
49 changes: 49 additions & 0 deletions tests/unit/cancel.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,52 @@ t('A canceled sql.batch leaves no cancel_timeout timer behind', async() => {
await clock.run(300)
return ['57014,0', code + ',' + clock.pending]
})

t('Calling signal() again removes the previous abort listener', async() => {
const clock = Clock()
, server = Server({ clock, random: Random(1), delay: 0, split: 0 })
, sql = postgres({
host: 'sim', port: 5432, user: 'sim', database: 'sim', max: 1, fetch_types: false,
max_lifetime: 0, idle_timeout: 0, timers: clock.timers,
socket: () => server.socket()
})
, ac1 = new AbortController()
, ac2 = new AbortController()

const q = sql`select 1 as id, 0 as scope sleep(100)`.signal(ac1.signal).signal(ac2.signal)
let outcome
q.then(rows => outcome = 'rows:' + rows.length, e => outcome = 'err:' + e.code)
await clock.run(20)
ac1.abort()
await clock.run(200)
return ['rows:1', outcome]
})

t('Cursor async iterator clears query_timeout timer and abort listener on completion', async() => {
const clock = Clock()
, server = Server({ clock, random: Random(1), delay: 0, split: 0 })
, sql = postgres({
host: 'sim', port: 5432, user: 'sim', database: 'sim', max: 1, fetch_types: false,
max_lifetime: 0, idle_timeout: 0, query_timeout: 5, timers: clock.timers,
socket: () => server.socket()
})
, ac = new AbortController()
, q = sql`select 1 as id, 0 as scope`.signal(ac.signal)

let removed = false
const origRemove = ac.signal.removeEventListener.bind(ac.signal)
ac.signal.removeEventListener = (...args) => (removed = true, origRemove(...args))

const rows = []
const iter = q.cursor(1)[Symbol.asyncIterator]()
const done = (async() => {
for (;;) {
const step = await iter.next()
if (step.done) break
rows.push(...step.value)
}
})()
await clock.run(200)
await done
return ['1,true,0', rows.length + ',' + removed + ',' + clock.pending]
})
Loading