Skip to content
Closed
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
3 changes: 2 additions & 1 deletion packages/pg-query-stream/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ class QueryStream extends Readable implements Submittable {
}

public _destroy(_err: Error, cb: Function) {
this.cursor.close((err?: Error) => {
this.cursor.close(async (err?: Error) => {
if(process.env.WAIT_AFTER_CLOSE === '2') await new Promise(resolve => setTimeout(resolve, 100));
cb(err || _err)
})
}
Expand Down
30 changes: 30 additions & 0 deletions packages/pg-query-stream/test/close.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import assert from 'assert'
import concat from 'concat-stream'
import pg from 'pg'
import QueryStream from '../src'
import helper from './helper'

Expand Down Expand Up @@ -90,4 +91,33 @@ if (process.version.startsWith('v8.')) {
stream.on('close', done)
})
})

describe('use after error', () => {
it.only('should work if used after error', async () => {
const pool = new pg.Pool({ max: 1, connectionTimeoutMillis: 400, statement_timeout: 400 });

const res1 = await pool.query('SELECT TRUE');
assert.deepStrictEqual(res1.rows, [ { bool:true } ]);

const query = new QueryStream('SELECT TRUE');
const client = await pool.connect();
const stream = await client.query(query);

await assert.rejects(() => pool.query('SELECT TRUE'), { message: 'timeout exceeded when trying to connect' });

await stream.destroy();
if(process.env.WAIT_AFTER_CLOSE === '1') await new Promise(resolve => setTimeout(resolve, 100));

if(process.env.WAIT_AFTER_CLOSE === '2') {
await new Promise(resolve => stream.once('close', resolve));
}

await client.release();

const res2 = await pool.query('SELECT TRUE');
assert.deepStrictEqual(res2.rows, [ { bool:true } ]);

await pool.end();
})
})
}