@@ -317,6 +317,9 @@ function runBinary_(self, retries, fnCallback) {
317317 self . isProcessRunning = true ;
318318 isCallback = true ;
319319 self . getTunnelName ( function ( tunnelName ) {
320+ if ( tunnelName === null ) {
321+ return fnCallback ( true , false ) ;
322+ }
320323 console . log ( `Tunnel successfully initiated. You can start testing now` ) ;
321324 return fnCallback ( null , true ) ;
322325 } ) ;
@@ -841,7 +844,6 @@ function getBinaryPath_(that, options, fnCallback) {
841844 */
842845function retryTunnelName_ ( self , infoAPIPort , retries , fnCallback ) {
843846 try {
844- var timeoutId = null ;
845847 // Check whether max retries is reached ?.
846848 if ( retries >= 0 ) {
847849 // local Server path for getting tunnelName
@@ -856,14 +858,11 @@ function retryTunnelName_(self, infoAPIPort, retries, fnCallback) {
856858 response . on ( 'end' , ( ) => {
857859 // After successfully getting name clear the timeout and return name
858860 if ( response . statusCode === 200 ) {
859- clearTimeout_ ( timeoutId ) ;
860861 if ( typeof json === 'string' ) json = JSON . parse ( json ) ;
861862 return fnCallback ( ( json . data && json . data . tunnelName ) || null ) ;
862863 } else {
863- clearTimeout_ ( timeoutId ) ;
864- timeoutId = setTimeout ( function ( ) {
865- retryTunnelName_ ( self , infoAPIPort , retries - 1 , fnCallback ) ;
866- } , 500 ) ;
864+ sleep ( 500 ) ;
865+ retryTunnelName_ ( self , infoAPIPort , retries - 1 , fnCallback ) ;
867866 }
868867 } ) ;
869868 } )
@@ -876,21 +875,20 @@ function retryTunnelName_(self, infoAPIPort, retries, fnCallback) {
876875 'Getting error while trying to get the tunnel name from running local server. Error : ' +
877876 e
878877 ) ;
879- clearTimeout_ ( timeoutId ) ;
880878 // wait .5s for next retries
881- timeoutId = setTimeout ( function ( ) {
882- retryTunnelName_ ( self , infoAPIPort , retries - 1 , fnCallback ) ;
883- } , 500 ) ;
879+ sleep ( 500 ) ;
880+ retryTunnelName_ ( self , infoAPIPort , retries - 1 , fnCallback ) ;
884881 } ) ;
885882 } else {
886- clearTimeout_ ( timeoutId ) ;
883+ sleep ( 500 ) ;
887884 logger . log (
888885 self . options [ 'user' ] ,
889886 self . options [ 'key' ] ,
890887 { filename : __filename } ,
891888 self . options ,
892889 'Number of retries to to get tunnel name exceeded.'
893890 ) ;
891+ console . log ( 'Number of retries to to get tunnel name exceeded.' ) ;
894892 return fnCallback ( null ) ;
895893 }
896894 } catch ( e ) {
@@ -924,12 +922,11 @@ function clearTimeout_(timeoutId) {
924922function killProcess_ ( that , fnCallback ) {
925923 that . proc . on ( 'exit' , function ( ) {
926924 if ( that . proc . pid ) {
927- setTimeout ( function ( ) {
928- try {
929- process . kill ( that . proc . pid ) ;
930- } catch ( err ) { }
931- return fnCallback ( ) ;
932- } , 500 ) ;
925+ sleep ( 500 ) ;
926+ try {
927+ process . kill ( that . proc . pid ) ;
928+ } catch ( err ) { }
929+ return fnCallback ( ) ;
933930 }
934931 return fnCallback ( ) ;
935932 } ) ;
@@ -940,5 +937,9 @@ function killProcess_(that, fnCallback) {
940937 that . proc . kill ( 'SIGINT' ) ;
941938 }
942939}
940+ function sleep ( sleepDuration ) {
941+ var now = new Date ( ) . getTime ( ) ;
942+ while ( new Date ( ) . getTime ( ) < now + sleepDuration ) { /* do nothing */ }
943+ }
943944module . exports = Tunnel ;
944945module . exports . Tunnel = Tunnel ;
0 commit comments