Skip to content
Source
Sign inSign up
samwillis/neon-durable-streams
Open History to see the last commit

sql-latency.mjs

19 lines831 Bytes

1// Opt-in qualification fixture, never imported by shipped code. It delays SQL
2// sends to reproduce a distant coordinator while retaining real Neon services.
3import pg from 'pg'
4const delay = Number(process.env.DS_TEST_SQL_DELAY_MS)
5if (!Number.isSafeInteger(delay) || delay < 1 || delay > 1000) throw new RangeError('Set DS_TEST_SQL_DELAY_MS between 1 and 1000')
6const original = pg.Client.prototype.query
7pg.Client.prototype.query = function (...args) {
8 const callback = typeof args.at(-1) === 'function' ? args.at(-1) : undefined
9 if (callback) {
10 setTimeout(() => original.apply(this, args), delay)
11 return
12 }
13 return new Promise((resolve, reject) => {
14 setTimeout(() => {
15 try { Promise.resolve(original.apply(this, args)).then(resolve, reject) }
16 catch (error) { reject(error) }
17 }, delay)
18 })