@@ -903,65 +903,149 @@ describe('API routes', () => {
903903            futureDate . setHours ( futureDate . getHours ( )  +  5 ) ; 
904904
905905            function  setupZkTestState ( cb )  { 
906-                 const  {  connectionString }  =  config . zookeeper ; 
907-                 zkClient  =  zookeeper . createClient ( connectionString ) ; 
908-                 zkClient . connect ( ) ; 
909-                 zkClient . once ( 'connected' ,  ( )  =>  { 
910-                     async . series ( [ 
911-                         next  =>  zkClient . mkdirp ( svc . baseZkPath ,  err  =>  { 
912-                             if  ( err  &&  err . name  !==  'NODE_EXISTS' )  { 
913-                                 return  next ( err ) ; 
914-                             } 
915-                             return  next ( ) ; 
916-                         } ) , 
917-                         next  =>  { 
918-                             // emulate first site to be active (not paused) 
919-                             const  path  =  `${ svc . baseZkPath } ${ firstSite }  ; 
920-                             const  data  =  Buffer . from ( 
921-                                 JSON . stringify ( {  paused : false  } ) ) ; 
922-                             zkClient . create ( path ,  data ,  EPHEMERAL_NODE ,  next ) ; 
923-                         } , 
924-                         next  =>  { 
925-                             // emulate second site to be paused 
926-                             const  path  =  `${ svc . baseZkPath } ${ secondSite }  ; 
927-                             const  data  =  Buffer . from ( JSON . stringify ( { 
928-                                 paused : true , 
929-                                 scheduledResume : futureDate . toString ( ) , 
930-                             } ) ) ; 
931-                             zkClient . create ( path ,  data ,  EPHEMERAL_NODE ,  next ) ; 
932-                         } , 
933-                     ] ,  err  =>  { 
934-                         if  ( err )  { 
935-                             process . stdout . write ( 'error occurred in zookeeper'  + 
936-                             ' setup for pause/resume' ) ; 
937-                             return  cb ( err ) ; 
906+                 async . series ( [ 
907+                     // Ensure the base path exists 
908+                     next  =>  zkClient . mkdirp ( svc . baseZkPath ,  err  =>  { 
909+                         if  ( err  &&  err . name  !==  'NODE_EXISTS' )  { 
910+                             return  next ( err ) ; 
938911                        } 
939-                         return  cb ( ) ; 
940-                     } ) ; 
912+                         return  next ( ) ; 
913+                     } ) , 
914+                     
915+                     // Remove existing nodes with proper error handling 
916+                     next  =>  zkClient . remove ( `${ svc . baseZkPath } ${ firstSite }  ,  - 1 ,  err  =>  { 
917+                         if  ( err  &&  err . name  !==  'NO_NODE' )  { 
918+                             return  next ( err ) ; 
919+                         } 
920+                         return  next ( ) ; 
921+                     } ) , 
922+                     next  =>  zkClient . remove ( `${ svc . baseZkPath } ${ secondSite }  ,  - 1 ,  err  =>  { 
923+                         if  ( err  &&  err . name  !==  'NO_NODE' )  { 
924+                             return  next ( err ) ; 
925+                         } 
926+                         return  next ( ) ; 
927+                     } ) , 
928+                     // Add a small delay to ensure nodes are fully removed 
929+                     next  =>  setTimeout ( next ,  100 ) ,                     
930+                     next  =>  { 
931+                         const  path  =  `${ svc . baseZkPath } ${ firstSite }  ; 
932+                         const  data  =  Buffer . from ( JSON . stringify ( {  
933+                             paused : false , 
934+                             scheduledResume : null  // explicitly set to null 
935+                         } ) ) ; 
936+                         zkClient . create ( path ,  data ,  EPHEMERAL_NODE ,  next ) ; 
937+                     } ,                     
938+                     next  =>  { 
939+                         const  path  =  `${ svc . baseZkPath } ${ secondSite }  ; 
940+                         const  data  =  Buffer . from ( JSON . stringify ( { 
941+                             paused : true , 
942+                             scheduledResume : futureDate . toString ( ) , 
943+                         } ) ) ; 
944+                         zkClient . create ( path ,  data ,  EPHEMERAL_NODE ,  next ) ; 
945+                     } , 
946+                 ] ,  err  =>  { 
947+                     if  ( err )  { 
948+                         //eslint-disable-next-line no-console 
949+                         console . error ( 'Error in setupZkTestState:' ,  err ) ; 
950+                         return  cb ( err ) ; 
951+                     } 
952+                     return  cb ( ) ; 
941953                } ) ; 
942-             } 
954+             }              
943955
944-             before ( done  =>  { 
956+             before ( ( )  =>  { 
945957                redis1  =  new  Redis ( ) ; 
946958                redis2  =  new  Redis ( ) ; 
947959
948960                channel1  =  `${ topic } ${ firstSite }  ; 
949-                 redis1 . subscribe ( channel1 ,  err  =>  assert . ifError ( err ) ) ; 
950-                 redis1 . on ( 'message' ,  ( channel ,  message )  =>  { 
951-                     cache1 . push ( {  channel,  message } ) ; 
952-                 } ) ; 
953- 
954961                channel2  =  `${ topic } ${ secondSite }  ; 
955-                 redis2 . subscribe ( channel2 ,  err  =>  assert . ifError ( err ) ) ; 
956-                 redis2 . on ( 'message' ,  ( channel ,  message )  =>  { 
957-                     cache2 . push ( {  channel,  message } ) ; 
958-                 } ) ; 
959-                 setupZkTestState ( done ) ; 
962+ 
963+                 const  zkConnectionString  =  config . zookeeper . connectionString ; 
964+                 zkClient  =  zookeeper . createClient ( zkConnectionString ) ; 
965+ 
966+                 async . series ( [ 
967+                     next  =>  { 
968+                         zkClient . once ( 'connected' ,  ( )  =>  { 
969+                             next ( ) ; 
970+                         } ) ; 
971+                         zkClient . once ( 'error' ,  err  =>  { 
972+                             next ( err ) ; 
973+                         } ) ; 
974+                         zkClient . connect ( ) ; 
975+                     } , 
976+                     next  =>  redis1 . subscribe ( channel1 ,  err  =>  { 
977+                         if  ( err )  { 
978+                             return  next ( err ) ; 
979+                         } 
980+                         redis1 . on ( 'message' ,  ( channel ,  message )  =>  { 
981+                             cache1 . push ( {  channel,  message } ) ; 
982+                         } ) ; 
983+                         next ( ) ; 
984+                     } ) , 
985+                     next  =>  redis2 . subscribe ( channel2 ,  err  =>  { 
986+                         if  ( err )  { 
987+                             return  next ( err ) ; 
988+                         } 
989+                         redis2 . on ( 'message' ,  ( channel ,  message )  =>  { 
990+                             cache2 . push ( {  channel,  message } ) ; 
991+                         } ) ; 
992+                         next ( ) ; 
993+                     } ) , 
994+                 ] ) ; 
960995            } ) ; 
961996
962-             afterEach ( ( )  =>  { 
997+             function  waitForSiteStatus ( site ,  expectedStatus ,  timeout ,  callback )  { 
998+                 const  startTime  =  Date . now ( ) ; 
999+                 const  statusPath  =  `/_/${ svc . name } ${ site }  ; 
1000+ 
1001+                 function  checkStatus ( )  { 
1002+                     getRequest ( statusPath ,  ( err ,  res )  =>  { 
1003+                         if  ( err )  { 
1004+                             return  callback ( err ) ; 
1005+                         }  else  if  ( res  &&  res [ site ]  ===  expectedStatus )  { 
1006+                             return  callback ( ) ; 
1007+                         } 
1008+ 
1009+                         if  ( Date . now ( )  -  startTime  >  timeout )  { 
1010+                             const  currentStatus  =  res  &&  res [ site ]  ? res [ site ]  : 'unknown (error or no response)' ; 
1011+                             return  callback ( new  Error ( 
1012+                                 `[${ svc . name } ${ site }  
1013+                                 '${ expectedStatus } ${ currentStatus }  
1014+                             ) ) ; 
1015+                         } 
1016+                         setTimeout ( checkStatus ,  200 ) ; 
1017+                     } ) ; 
1018+                 } 
1019+                 return  checkStatus ( ) ; 
1020+             } 
1021+ 
1022+             beforeEach ( done  =>  { 
9631023                cache1  =  [ ] ; 
9641024                cache2  =  [ ] ; 
1025+                 setupZkTestState ( err  =>  { 
1026+                     if  ( err )  { 
1027+                         return  done ( err ) ; 
1028+                     } 
1029+                     return  waitForSiteStatus ( firstSite ,  'enabled' ,  5000 ,  done ) ; 
1030+                 } ) ; 
1031+             } ) ; 
1032+ 
1033+             afterEach ( done  =>  { 
1034+                 async . series ( [ 
1035+                     next  =>  zkClient . remove ( `${ svc . baseZkPath } ${ firstSite }  ,  - 1 ,  err  =>  { 
1036+                         if  ( err  &&  err . name  !==  'NO_NODE' )  { 
1037+                             return  next ( err ) ; 
1038+                         } 
1039+                         return  next ( ) ; 
1040+                     } ) , 
1041+                     next  =>  zkClient . remove ( `${ svc . baseZkPath } ${ secondSite }  ,  - 1 ,  err  =>  { 
1042+                         if  ( err  &&  err . name  !==  'NO_NODE' )  { 
1043+                             return  next ( err ) ; 
1044+                         } 
1045+                         return  next ( ) ; 
1046+                     } ) , 
1047+                     next  =>  setTimeout ( next ,  100 ) , 
1048+                 ] ,  done ) ; 
9651049            } ) ; 
9661050
9671051            after ( ( )  =>  { 
@@ -1035,7 +1119,7 @@ describe('API routes', () => {
10351119                } ) ; 
10361120            } ) ; 
10371121
1038-             it ( 'should receive a pause request on all  site channels  from '  + 
1122+             it ( 'should receive a pause request on only first  site channel  from '  + 
10391123            `route /_/${ svc . name }  ,  done  =>  { 
10401124                const  options  =  Object . assign ( { } ,  defaultOptions ,  { 
10411125                    method : 'POST' , 
@@ -1046,18 +1130,15 @@ describe('API routes', () => {
10461130
10471131                    setTimeout ( ( )  =>  { 
10481132                        assert . strictEqual ( cache1 . length ,  1 ) ; 
1049-                         assert . strictEqual ( cache2 . length ,  1 ) ; 
1133+                         assert . strictEqual ( cache2 . length ,  0 ) ; 
10501134
10511135                        assert . deepStrictEqual ( cache1 [ 0 ] . channel ,  channel1 ) ; 
1052-                         assert . deepStrictEqual ( cache2 [ 0 ] . channel ,  channel2 ) ; 
10531136
10541137                        const  message1  =  JSON . parse ( cache1 [ 0 ] . message ) ; 
1055-                         const  message2  =  JSON . parse ( cache2 [ 0 ] . message ) ; 
10561138                        const  expected  =  {  action : 'pauseService'  } ; 
10571139                        assert . deepStrictEqual ( message1 ,  expected ) ; 
1058-                         assert . deepStrictEqual ( message2 ,  expected ) ; 
10591140                        done ( ) ; 
1060-                     } ,  1000 ) ; 
1141+                     } ,  2000 ) ; 
10611142                } ) ; 
10621143            } ) ; 
10631144
@@ -1079,11 +1160,11 @@ describe('API routes', () => {
10791160                        const  expected  =  {  action : 'pauseService'  } ; 
10801161                        assert . deepStrictEqual ( message ,  expected ) ; 
10811162                        done ( ) ; 
1082-                     } ,  1000 ) ; 
1163+                     } ,  2000 ) ; 
10831164                } ) ; 
10841165            } ) ; 
10851166
1086-             it ( 'should receive a resume request on all  site channels  from '  + 
1167+             it ( 'should receive a resume request on second  site channel  from '  + 
10871168            `route /_/${ svc . name }  ,  done  =>  { 
10881169                const  options  =  Object . assign ( { } ,  defaultOptions ,  { 
10891170                    method : 'POST' , 
@@ -1093,22 +1174,19 @@ describe('API routes', () => {
10931174                    assert . ifError ( err ) ; 
10941175
10951176                    setTimeout ( ( )  =>  { 
1096-                         assert . strictEqual ( cache1 . length ,  1 ) ; 
1177+                         assert . strictEqual ( cache1 . length ,  0 ) ; 
10971178                        assert . strictEqual ( cache2 . length ,  1 ) ; 
1098-                         assert . deepStrictEqual ( cache1 [ 0 ] . channel ,  channel1 ) ; 
10991179                        assert . deepStrictEqual ( cache2 [ 0 ] . channel ,  channel2 ) ; 
11001180
1101-                         const  message1  =  JSON . parse ( cache1 [ 0 ] . message ) ; 
11021181                        const  message2  =  JSON . parse ( cache2 [ 0 ] . message ) ; 
11031182                        const  expected  =  {  action : 'resumeService'  } ; 
1104-                         assert . deepStrictEqual ( message1 ,  expected ) ; 
11051183                        assert . deepStrictEqual ( message2 ,  expected ) ; 
11061184                        done ( ) ; 
1107-                     } ,  1000 ) ; 
1185+                     } ,  2000 ) ; 
11081186                } ) ; 
11091187            } ) ; 
11101188
1111-             it ( 'should get scheduled resume jobs for all sites  using route '  + 
1189+             it ( 'should get scheduled resume jobs for only second site  using route '  + 
11121190            `/_/${ svc . name }  ,  done  =>  { 
11131191                getRequest ( `/_/${ svc . name }  ,  ( err ,  res )  =>  { 
11141192                    assert . ifError ( err ) ; 
@@ -1122,11 +1200,11 @@ describe('API routes', () => {
11221200            } ) ; 
11231201
11241202            it ( 'should receive a scheduled resume request with specified '  + 
1125-             `hours from route /_/${ svc . name } ${ firstSite }  , 
1203+             `hours from route /_/${ svc . name } ${ secondSite }  , 
11261204            done  =>  { 
11271205                const  options  =  Object . assign ( { } ,  defaultOptions ,  { 
11281206                    method : 'POST' , 
1129-                     path : `/_/${ svc . name } ${ firstSite }  , 
1207+                     path : `/_/${ svc . name } ${ secondSite }  , 
11301208                } ) ; 
11311209                const  body  =  JSON . stringify ( {  hours : 1  } ) ; 
11321210                makeRequest ( options ,  body ,  ( err ,  res )  =>  { 
@@ -1135,9 +1213,9 @@ describe('API routes', () => {
11351213                        getResponseBody ( res ,  err  =>  { 
11361214                            assert . ifError ( err ) ; 
11371215
1138-                             assert . strictEqual ( cache1 . length ,  1 ) ; 
1139-                             assert . deepStrictEqual ( cache1 [ 0 ] . channel ,  channel1 ) ; 
1140-                             const  message  =  JSON . parse ( cache1 [ 0 ] . message ) ; 
1216+                             assert . strictEqual ( cache2 . length ,  1 ) ; 
1217+                             assert . deepStrictEqual ( cache2 [ 0 ] . channel ,  channel2 ) ; 
1218+                             const  message  =  JSON . parse ( cache2 [ 0 ] . message ) ; 
11411219                            assert . equal ( 'resumeService' ,  message . action ) ; 
11421220                            const  date  =  new  Date ( ) ; 
11431221                            const  scheduleDate  =  new  Date ( message . date ) ; 
@@ -1147,25 +1225,25 @@ describe('API routes', () => {
11471225                            assert ( scheduleDate  -  date  <=  millisecondPerHour ) ; 
11481226                            done ( ) ; 
11491227                        } ) ; 
1150-                     } ,  1000 ) ; 
1228+                     } ,  2000 ) ; 
11511229                } ) ; 
11521230            } ) ; 
11531231
11541232            it ( 'should receive a scheduled resume request without specified '  + 
1155-             `hours from route /_/${ svc . name } ${ firstSite }  , 
1233+             `hours from route /_/${ svc . name } ${ secondSite }  , 
11561234            done  =>  { 
11571235                const  options  =  Object . assign ( { } ,  defaultOptions ,  { 
11581236                    method : 'POST' , 
1159-                     path : `/_/${ svc . name } ${ firstSite }  , 
1237+                     path : `/_/${ svc . name } ${ secondSite }  , 
11601238                } ) ; 
11611239                makeRequest ( options ,  emptyBody ,  err  =>  { 
11621240                    assert . ifError ( err ) ; 
11631241
11641242                    setTimeout ( ( )  =>  { 
1165-                         assert . strictEqual ( cache1 . length ,  1 ) ; 
1166-                         assert . deepStrictEqual ( cache1 [ 0 ] . channel ,  channel1 ) ; 
1243+                         assert . strictEqual ( cache2 . length ,  1 ) ; 
1244+                         assert . deepStrictEqual ( cache2 [ 0 ] . channel ,  channel2 ) ; 
11671245
1168-                         const  message  =  JSON . parse ( cache1 [ 0 ] . message ) ; 
1246+                         const  message  =  JSON . parse ( cache2 [ 0 ] . message ) ; 
11691247                        assert . equal ( 'resumeService' ,  message . action ) ; 
11701248
11711249                        const  date  =  new  Date ( ) ; 
@@ -1177,7 +1255,7 @@ describe('API routes', () => {
11771255                        assert ( ( scheduleDate  -  date  <=  millisecondPerHour  *  6 ) 
11781256                            &&  ( scheduleDate  -  date )  >=  millisecondPerHour  *  5 ) ; 
11791257                        done ( ) ; 
1180-                     } ,  1000 ) ; 
1258+                     } ,  2000 ) ; 
11811259                } ) ; 
11821260            } ) ; 
11831261
0 commit comments