11import  *  as  os  from  'os' ; 
2- import  *  as  http  from  'http' ; 
32import  { exec }  from  'child_process' ; 
43import  { expect }  from  'chai' ; 
54
@@ -13,19 +12,16 @@ import {settings} from './settings';
1312
1413const  isWin  =  os . type ( ) . search ( 'Windows' )  >  - 1 ; 
1514
16- let  database ,  pool ,  pgDataDir ; 
15+ let  database ,  pool ; 
16+ const  pgServiceName  =  process . env . PG_SERVICE_NAME ; 
1717
1818describe . only ( 'Database;' ,  function  ( )  { 
19-     this . timeout ( 15000 ) ; 
19+     this . timeout ( 45000 ) ; 
2020
2121    before ( async  done  =>  { 
2222        try  { 
2323            database  =  new  Database ( settings ) ; 
2424            pool  =  database . pool ; 
25- 
26-             if  ( isWin )  { 
27-                 pgDataDir  =  await  getPgDataDir ( database ) ; 
28-             } 
2925            done ( ) ; 
3026        }  catch  ( err )  { 
3127            done ( err ) ; 
@@ -34,9 +30,6 @@ describe.only('Database;', function () {
3430
3531    after ( async  done  =>  { 
3632        try  { 
37-             if  ( pgDataDir )  { 
38-                 await  startPostgresql ( ) ; 
39-             } 
4033            await  database . close ( ) ; 
4134            done ( ) ; 
4235        }  catch  ( err )  { 
@@ -95,7 +88,7 @@ describe.only('Database;', function () {
9588            database . on ( ERROR_EVENT ,  function  dbErrorHandler ( err )  { 
9689                try  { 
9790                    expect ( err ) . to . be . an . instanceof ( Error ) ; 
98-                     expect ( err . message ) . to . include ( 'terminating connection' ) ; 
91+                     expect ( err . message ) . to . include ( isWin  ?  ''  :  'terminating connection' ) ; 
9992
10093                    checkPoolState ( 0 ,  0 ,  PoolState . active ) ; 
10194
@@ -131,7 +124,7 @@ describe.only('Database;', function () {
131124                try  { 
132125                    expect ( session ) . to . be . undefined ; 
133126                    expect ( err ) . to . be . an . instanceof ( Error ) ; 
134-                     expect ( err . message ) . to . include ( 'ECONNREFUSED' ) ; 
127+                     expect ( err . message ) . to . include ( isWin  ?  ''  :  'ECONNREFUSED' ) ; 
135128
136129                    checkPoolState ( 0 ,  0 ,  PoolState . active ) ; 
137130
@@ -142,6 +135,13 @@ describe.only('Database;', function () {
142135            } 
143136        } ) ; 
144137
138+         if  ( isWin )  { 
139+             it ( 'waiting of pg service' ,  async  done  =>  { 
140+                 await  wait ( 20000 ) ; 
141+                 done ( ) ; 
142+             } ) ; 
143+         } 
144+ 
145145        it ( 'should return result without an error' ,  async  done  =>  { 
146146            try  { 
147147                await  startPostgresql ( ) ; 
@@ -202,9 +202,6 @@ async function getUsers(session: Session, userId: number): Promise<any> {
202202function  execCommand ( command : string ) : Promise < void >  { 
203203    return  new  Promise ( ( resolve ,  reject )  =>  { 
204204        exec ( command ,  ( err ,  stdout ,  stderr )  =>  { 
205-             console . log ( err ) 
206-             console . log ( stderr ) 
207-             console . log ( stdout ) 
208205            const  error  =  err  ||  stderr ; 
209206
210207            error  ? reject ( error )  : resolve ( ) ; 
@@ -214,34 +211,18 @@ function execCommand(command: string): Promise<void> {
214211
215212async  function  startPostgresql ( ) : Promise < void >  { 
216213    const  command  =  isWin 
217-         ? `pg_ctl -D  "${ pgDataDir } " start ` 
214+         ? `NET START  "${ pgServiceName } " ` 
218215        : 'brew services start postgresql' ; 
219216
220217    await  execCommand ( command ) ; 
221-     await  wait ( 1000 ) ; 
218+     await  wait ( 2000 ) ; 
222219} 
223220
224221async  function  stopPostgresql ( ) : Promise < void >  { 
225222    const  command  =  isWin 
226-         ? `pg_ctl -D " ${ pgDataDir } " stop ` 
223+         ? `NET STOP  ${ pgServiceName }  
227224        : 'brew services stop postgresql' ; 
228225
229226    await  execCommand ( command ) ; 
230-     await  wait ( 1000 ) ; 
231- } 
232- 
233- async  function  getPgDataDir ( db : Database ) : Promise < string >  { 
234-     const  session  =  await  connectToDatabase ( db ) ; 
235- 
236-     const  query  =  { 
237-         text : `SHOW data_directory` , 
238-         mask : 'single' 
239-     } ; 
240- 
241-     const  result  =  await  session . execute ( query ) ; 
242- 
243-     await  session . close ( ) ; 
244-     await  wait ( settings . pool . idleTimeout  +  500 ) ; 
245- 
246-     return  result . data_directory ; 
227+     await  wait ( 2000 ) ; 
247228} 
0 commit comments