11import  { expect }  from  'chai' ; 
22
33import  { ConnectionError }  from  '../lib/errors' ; 
4- import  { createNewPool }  from  './helpers' ; 
4+ import  { createNewPool ,  pgProxyServer ,  PROXY_SERVER_PORT }  from  './helpers' ; 
5+ 
6+ const  connectionOptions  =  { port : PROXY_SERVER_PORT } ; 
7+ 
8+ let  server ; 
59
610describe ( 'Pool connection timeout;' ,  ( )  =>  { 
11+     before ( done  =>  { 
12+         server  =  pgProxyServer ( 250 ,  done ) ; 
13+     } ) ; 
14+ 
15+     after ( done  =>  { 
16+         server . close ( done ) ; 
17+     } ) ; 
718
819    it ( 'should callback with an error if timeout is passed' ,  done  =>  { 
9-         const  pool  =  createNewPool ( { connectionTimeout : 2 } ) ; 
20+         const  pool  =  createNewPool ( { connectionTimeout : 150 } ,   connectionOptions ) ; 
1021
1122        pool . acquire ( ( err ,  client )  =>  { 
12-             expect ( err ) . to . be . an . instanceof ( ConnectionError ) ; 
13-             expect ( err . message ) . to . contain ( 'Connection request has timed out' ) ; 
14-             expect ( pool . idleCount ) . to . equal ( 0 ) ; 
15-             expect ( pool . totalCount ) . to . equal ( 1 ) ; 
16-             expect ( client ) . to . be . undefined ; 
23+             try  { 
24+                 expect ( err ) . to . be . an . instanceof ( ConnectionError ) ; 
25+                 expect ( err . message ) . to . contain ( 'Connection request has timed out' ) ; 
26+                 expect ( pool . idleCount ) . to . equal ( 0 ) ; 
27+                 expect ( pool . totalCount ) . to . equal ( 1 ) ; 
28+                 expect ( client ) . to . be . undefined ; 
1729
18-             pool . shutdown ( done ) ; 
30+                 pool . shutdown ( done ) ; 
31+             }  catch  ( err )  { 
32+                 pool . shutdown ( ( )  =>  done ( err ) ) ; 
33+             } 
1934        } ) ; 
2035    } ) ; 
2136
2237    it ( 'should handle multiple timeouts' ,  async  done  =>  { 
2338        const  iterations  =  15 ; 
24-         const  pool  =  createNewPool ( { connectionTimeout : 2 ,  maxSize : iterations } ) ; 
39+         const  pool  =  createNewPool ( { connectionTimeout : 150 ,  maxSize : iterations } ,   connectionOptions ) ; 
2540        const  errors  =  [ ] ; 
2641
2742        try  { 
@@ -44,33 +59,36 @@ describe('Pool connection timeout;', () => {
4459            expect ( pool . totalCount ) . to . equal ( 15 ) ; 
4560
4661            pool . shutdown ( done ) ; 
47-         }  catch  ( e )  { 
48-             done ( e ) ; 
62+         }  catch  ( err )  { 
63+             pool . shutdown ( ( )   =>   done ( err ) ) ; 
4964        } 
5065    } ) ; 
5166
5267    it ( 'should timeout on checkout of used connection' ,  done  =>  { 
53-         const  pool  =  createNewPool ( { connectionTimeout : 100 ,  maxSize : 1 } ) ; 
54- 
55-         pool . acquire ( ( err ,  client )  =>  { 
56-             expect ( err ) . to . be . undefined ; 
57-             expect ( client ) . to . not . be . undefined ; 
58-             expect ( pool . totalCount ) . to . equal ( 1 ) ; 
59- 
68+         const  pool  =  createNewPool ( { connectionTimeout : 400 ,  maxSize : 1 } ,  connectionOptions ) ; 
69+         try  { 
6070            pool . acquire ( ( err ,  client )  =>  { 
61-                 expect ( err ) . to . be . an . instanceof ( ConnectionError ) ; 
62-                 expect ( err . message ) . to . contain ( 'Connection request has timed out' ) ; 
63-                 expect ( client ) . to . be . undefined ; 
71+                 expect ( err ) . to . be . undefined ; 
72+                 expect ( client ) . to . not . be . undefined ; 
6473                expect ( pool . totalCount ) . to . equal ( 1 ) ; 
6574
66-                 ( pool  as  any ) . clients . entries ( ) . next ( ) . value [ 0 ] . release ( ) ; 
67-                 pool . shutdown ( done ) ; 
75+                 pool . acquire ( ( err ,  client )  =>  { 
76+                     expect ( err ) . to . be . an . instanceof ( ConnectionError ) ; 
77+                     expect ( err . message ) . to . contain ( 'Connection request has timed out' ) ; 
78+                     expect ( client ) . to . be . undefined ; 
79+                     expect ( pool . totalCount ) . to . equal ( 1 ) ; 
80+ 
81+                     ( pool  as  any ) . clients . entries ( ) . next ( ) . value [ 0 ] . release ( ) ; 
82+                     pool . shutdown ( done ) ; 
83+                 } ) ; 
6884            } ) ; 
69-         } ) ; 
85+         }  catch  ( err )  { 
86+             pool . shutdown ( ( )  =>  done ( err ) ) ; 
87+         } 
7088    } ) ; 
7189
7290    it ( 'should timeout on query if all clients are busy' ,  done  =>  { 
73-         const  pool  =  createNewPool ( { connectionTimeout : 100 ,  maxSize : 1 } ) ; 
91+         const  pool  =  createNewPool ( { connectionTimeout : 400 ,  maxSize : 1 } ,   connectionOptions ) ; 
7492
7593        pool . acquire ( ( err ,  client )  =>  { 
7694            expect ( err ) . to . be . undefined ; 
@@ -91,7 +109,7 @@ describe('Pool connection timeout;', () => {
91109    } ) ; 
92110
93111    it ( 'should recover from timeout errors' ,  done  =>  { 
94-         const  pool  =  createNewPool ( { connectionTimeout : 100 ,  maxSize : 1 } ) ; 
112+         const  pool  =  createNewPool ( { connectionTimeout : 400 ,  maxSize : 1 } ,   connectionOptions ) ; 
95113
96114        pool . acquire ( ( err ,  client )  =>  { 
97115            expect ( err ) . to . be . undefined ; 
0 commit comments