11import assert from 'assert'
22import helper from './helper'
33import QueryStream from '../src'
4+ import { Pool , Client } from 'pg'
45
56helper ( 'error' , function ( client ) {
67 it ( 'receives error on stream' , function ( done ) {
@@ -21,3 +22,71 @@ helper('error', function (client) {
2122 client . query ( 'SELECT NOW()' , done )
2223 } )
2324} )
25+
26+ describe ( 'error recovery' , ( ) => {
27+ // created from https://github.com/chrisdickinson/pg-test-case
28+ it ( 'recovers from a streaming error in a transaction' , async ( ) => {
29+ const pool = new Pool ( )
30+ const client = await pool . connect ( )
31+ await client . query ( `CREATE TEMP TABLE frobnicators (
32+ id serial primary key,
33+ updated timestamp
34+ )` )
35+ await client . query ( `BEGIN;` )
36+ const query = new QueryStream ( `INSERT INTO frobnicators ("updated") VALUES ($1) RETURNING "id"` , [ Date . now ( ) ] )
37+ let error : Error | undefined = undefined
38+ query . on ( 'data' , console . log ) . on ( 'error' , ( e ) => {
39+ error = e
40+ } )
41+ client . query ( query ) // useless callback necessitated by an older version of honeycomb-beeline
42+
43+ await client . query ( `ROLLBACK` )
44+ assert ( error , 'Error should not be undefined' )
45+ const { rows } = await client . query ( 'SELECT NOW()' )
46+ assert . strictEqual ( rows . length , 1 )
47+ client . release ( )
48+ const client2 = await pool . connect ( )
49+ await client2 . query ( `BEGIN` )
50+ client2 . release ( )
51+ pool . end ( )
52+ } )
53+
54+ // created from https://github.com/brianc/node-postgres/pull/2333
55+ it ( 'handles an error on a stream after a plain text non-stream error' , async ( ) => {
56+ const client = new Client ( )
57+ const stmt = 'SELECT * FROM goose;'
58+ await client . connect ( )
59+ return new Promise ( ( resolve , reject ) => {
60+ client . query ( stmt ) . catch ( ( e ) => {
61+ assert ( e , 'Query should have rejected with an error' )
62+ const stream = new QueryStream ( 'SELECT * FROM duck' )
63+ client . query ( stream )
64+ stream . on ( 'data' , ( ) => { } )
65+ stream . on ( 'error' , ( ) => {
66+ client . end ( ( err ) => {
67+ err ? reject ( err ) : resolve ( )
68+ } )
69+ } )
70+ } )
71+ } )
72+ } )
73+
74+ it ( 'does not crash when closing a connection with a queued stream' , async ( ) => {
75+ const client = new Client ( )
76+ const stmt = 'SELECT * FROM goose;'
77+ await client . connect ( )
78+ return new Promise ( async ( resolve ) => {
79+ let queryError : Error | undefined
80+ client . query ( stmt ) . catch ( ( e ) => {
81+ queryError = e
82+ } )
83+ const stream = client . query ( new QueryStream ( stmt ) )
84+ stream . on ( 'data' , ( ) => { } )
85+ stream . on ( 'error' , ( ) => {
86+ assert ( queryError , 'query should have errored due to client ending' )
87+ resolve ( )
88+ } )
89+ await client . end ( )
90+ } )
91+ } )
92+ } )
0 commit comments