@@ -5,6 +5,7 @@ const cp = require('child_process')
5
5
const path = require ( 'path' )
6
6
7
7
const postgres = require ( '../lib' )
8
+ const delay = ms => new Promise ( r => setTimeout ( r , ms ) )
8
9
9
10
const login = {
10
11
user : 'postgres_js_test'
@@ -30,7 +31,7 @@ const options = {
30
31
user : login . user ,
31
32
pass : login . pass ,
32
33
idle_timeout : 0.2 ,
33
- debug : true ,
34
+ debug : false ,
34
35
max : 1
35
36
}
36
37
@@ -385,7 +386,7 @@ t('sql file throws', async() =>
385
386
386
387
t ( 'sql file cached' , async ( ) => {
387
388
await sql . file ( path . join ( __dirname , 'select.sql' ) )
388
- await new Promise ( r => setTimeout ( r , 20 ) )
389
+ await delay ( 20 )
389
390
390
391
return [ 1 , ( await sql . file ( path . join ( __dirname , 'select.sql' ) ) ) [ 0 ] . x ]
391
392
} )
@@ -416,7 +417,6 @@ t('Connection ended timeout', async() => {
416
417
417
418
t ( 'Connection ended error' , async ( ) => {
418
419
const sql = postgres ( options )
419
-
420
420
sql . end ( )
421
421
return [ 'CONNECTION_ENDED' , ( await sql `` . catch ( x => x . code ) ) ]
422
422
} )
@@ -549,6 +549,21 @@ t('listen and notify with weird name', async() => {
549
549
) ]
550
550
} )
551
551
552
+ t ( 'listen reconnects' , async ( ) => {
553
+ const listener = postgres ( options )
554
+ , xs = [ ]
555
+
556
+ const { state : { pid } } = await listener . listen ( 'test' , x => xs . push ( x ) )
557
+ await sql . notify ( 'test' , 'a' )
558
+ await sql `select pg_terminate_backend(${ pid } ::int)`
559
+ await delay ( 50 )
560
+ await sql . notify ( 'test' , 'b' )
561
+ await delay ( 50 )
562
+ listener . end ( )
563
+
564
+ return [ 'ab' , xs . join ( '' ) ]
565
+ } )
566
+
552
567
t ( 'responds with server parameters (application_name)' , async ( ) =>
553
568
[ 'postgres.js' , await new Promise ( ( resolve , reject ) => postgres ( {
554
569
...options ,
@@ -788,7 +803,7 @@ t('Cursor works', async() => {
788
803
const order = [ ]
789
804
await sql `select 1 as x union select 2 as x` . cursor ( async ( x ) => {
790
805
order . push ( x . x + 'a' )
791
- await new Promise ( r => setTimeout ( r , 100 ) )
806
+ await delay ( 100 )
792
807
order . push ( x . x + 'b' )
793
808
} )
794
809
return [ '1a1b2a2b' , order . join ( '' ) ]
@@ -815,7 +830,7 @@ t('Cursor throw works', async() => {
815
830
const order = [ ]
816
831
await sql `select 1 as x union select 2 as x` . cursor ( async ( x ) => {
817
832
order . push ( x . x + 'a' )
818
- await new Promise ( r => setTimeout ( r , 100 ) )
833
+ await delay ( 100 )
819
834
throw new Error ( 'watty' )
820
835
} ) . catch ( ( ) => order . push ( 'err' ) )
821
836
return [ '1aerr' , order . join ( '' ) ]
0 commit comments