Skip to content

Commit f9a74b2

Browse files
combine two function in one
1 parent 448d37d commit f9a74b2

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

tests/ociswrapper/ocis/ocis.go

+44-2
Original file line numberDiff line numberDiff line change
@@ -322,13 +322,13 @@ func StopService(service string) (bool, string) {
322322
}
323323

324324
// service takes 30sec time to completely shutdown
325-
success := WaitForServiceDown(service)
325+
// success := WaitForServiceDown(service)
326+
success := WaitForServiceStatus(service, false)
326327
if !success {
327328
StopService(service)
328329
}
329330

330331
delete(runningServices, service)
331-
log.Println(fmt.Sprintf("oCIS service %s has been stopped successfully", service))
332332

333333
return true, fmt.Sprintf("Service %s stopped successfully", service)
334334
}
@@ -401,3 +401,45 @@ func WaitForServiceDown(service string) bool {
401401
}
402402
}
403403

404+
func WaitForServiceStatus(service string, waitForUp bool) bool {
405+
overallTimeout := time.After(30 * time.Second)
406+
ticker := time.NewTicker(2 * time.Second)
407+
defer ticker.Stop()
408+
409+
port := config.GetServiceDebugPort(service)
410+
411+
for {
412+
select {
413+
case <-overallTimeout:
414+
status := "available"
415+
if !waitForUp {
416+
status = "shut down"
417+
}
418+
log.Println(fmt.Errorf("timeout: %s service did not %s within 30 seconds", service, status).Error())
419+
return false
420+
case <-ticker.C:
421+
if _, exists := runningServices[service]; !exists {
422+
log.Println(fmt.Sprintf("Service %s not found in running services. Retrying...", service))
423+
continue
424+
}
425+
426+
address := fmt.Sprintf(":%d", port)
427+
conn, err := net.DialTimeout("tcp", address, 1*time.Second)
428+
if waitForUp {
429+
if err == nil {
430+
_ = conn.Close()
431+
log.Println(fmt.Sprintf("%s service is ready to listen on port %d", service, port))
432+
return true
433+
}
434+
log.Println(fmt.Sprintf("%s service is not ready on port %d. %v", service, port, err))
435+
} else {
436+
if err != nil {
437+
log.Println(fmt.Sprintf("%s service port %d is no longer reachable", service, port))
438+
return true
439+
}
440+
_ = conn.Close()
441+
log.Println(fmt.Sprintf("%s service port %d is still active. Retrying...", service, port))
442+
}
443+
}
444+
}
445+
}

tests/ociswrapper/wrapper/handlers/handler.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ func OcisServiceHandler(res http.ResponseWriter, req *http.Request) {
240240
common.Wg.Add(1)
241241
go ocis.StartService(serviceName, serviceEnvMap)
242242

243-
success := ocis.WaitForService(serviceName)
243+
success := ocis.WaitForServiceStatus(serviceName, true)
244244
if success {
245245
log.Println(fmt.Sprintf("Found Port for %s...", serviceName))
246246
sendResponse(res, http.StatusOK, fmt.Sprintf("oCIS service %s started successfully", serviceName))

0 commit comments

Comments
 (0)