@@ -5,8 +5,11 @@ const assert = require('assert')
55
66const suite = new helper . Suite ( )
77suite . test ( 'connecting to invalid port' , ( cb ) => {
8- const pool = new gaussdb . Pool ( { port : 13801 } )
9- pool . connect ( ) . catch ( ( e ) => cb ( ) )
8+ const pool = new gaussdb . Pool ( { port : 13801 , connectionTimeoutMillis : 2000 } )
9+ pool . connect ( ) . catch ( ( ) => {
10+ pool . end ( )
11+ cb ( )
12+ } )
1013} )
1114
1215suite . test ( 'errors emitted on checked-out clients' , ( cb ) => {
@@ -18,38 +21,19 @@ suite.test('errors emitted on checked-out clients', (cb) => {
1821 client . query ( 'SELECT NOW()' , function ( ) {
1922 pool . connect (
2023 assert . success ( function ( client2 , done2 ) {
21- helper . versionGTE (
22- client2 ,
23- 90200 ,
24- assert . success ( function ( isGreater ) {
25- let killIdleQuery =
26- 'SELECT pid, (SELECT pg_terminate_backend(pid)) AS killed FROM pg_stat_activity WHERE state = $1'
27- let params = [ 'idle' ]
28- if ( ! isGreater ) {
29- killIdleQuery =
30- 'SELECT procpid, (SELECT pg_terminate_backend(procpid)) AS killed FROM pg_stat_activity WHERE current_query LIKE $1'
31- params = [ '%IDLE%' ]
32- }
24+ client . once ( 'error' , ( err ) => {
25+ client . on ( 'error' , ( err ) => { } )
26+ done ( err )
27+ cb ( )
28+ } )
3329
34- client . once ( 'error' , ( err ) => {
35- client . on ( 'error' , ( err ) => { } )
36- done ( err )
37- cb ( )
38- } )
30+ // Simulate a network error by destroying the underlying socket
31+ // pg_terminate_backend need superuser privilege
32+ client . connection . stream . destroy ( new Error ( 'Simulated connection error' ) )
3933
40- // kill the connection from client
41- client2 . query (
42- killIdleQuery ,
43- params ,
44- assert . success ( function ( res ) {
45- // check to make sure client connection actually was killed
46- // return client2 to the pool
47- done2 ( )
48- pool . end ( )
49- } )
50- )
51- } )
52- )
34+ // Return client2 to the pool
35+ done2 ( )
36+ pool . end ( )
5337 } )
5438 )
5539 } )
@@ -64,7 +48,16 @@ suite.test('connection-level errors cause queued queries to fail', (cb) => {
6448 client . query (
6549 'SELECT pg_terminate_backend(pg_backend_pid())' ,
6650 assert . calls ( ( err ) => {
67- assert . equal ( err . code , '57P01' )
51+ const dbType = process . env . DB_TYPE || process . env . GAUSS_TYPE || 'gaussdb'
52+ if ( dbType . toLowerCase ( ) === 'opengauss' ) {
53+ // In OpenGauss, pg_terminate_backend returns a proper DatabaseError
54+ assert . equal ( err . code , '57P01' )
55+ } else {
56+ // Note: In GaussDB, pg_terminate_backend() drops the TCP connection immediately
57+ // rather than returning a proper PostgreSQL DatabaseError with code '57P01'.
58+ assert . equal ( err . message , 'Connection terminated unexpectedly' )
59+ assert . equal ( err . code , undefined )
60+ }
6861 } )
6962 )
7063
@@ -96,7 +89,16 @@ suite.test('connection-level errors cause future queries to fail', (cb) => {
9689 client . query (
9790 'SELECT pg_terminate_backend(pg_backend_pid())' ,
9891 assert . calls ( ( err ) => {
99- assert . equal ( err . code , '57P01' )
92+ const dbType = process . env . DB_TYPE || process . env . GAUSS_TYPE || 'gaussdb'
93+ if ( dbType . toLowerCase ( ) === 'opengauss' ) {
94+ // In OpenGauss, pg_terminate_backend returns a proper DatabaseError
95+ assert . equal ( err . code , '57P01' )
96+ } else {
97+ // Note: In GaussDB, pg_terminate_backend() drops the TCP connection immediately
98+ // rather than returning a proper PostgreSQL DatabaseError with code '57P01'.
99+ assert . equal ( err . message , 'Connection terminated unexpectedly' )
100+ assert . equal ( err . code , undefined )
101+ }
100102 } )
101103 )
102104
@@ -132,5 +134,5 @@ suite.test('handles socket error during pool.query and destroys it immediately',
132134 const stream = pool . _clients [ 0 ] . connection . stream
133135 setTimeout ( ( ) => {
134136 stream . emit ( 'error' , new Error ( 'network issue' ) )
135- } , 100 )
137+ } , 1000 )
136138} )
0 commit comments