Skip to content

Commit f9c698f

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

File tree

2 files changed

+23
-51
lines changed

2 files changed

+23
-51
lines changed

tests/ociswrapper/ocis/ocis.go

+21-49
Original file line numberDiff line numberDiff line change
@@ -321,20 +321,17 @@ func StopService(service string) (bool, string) {
321321
return false, fmt.Sprintf("Failed to stop service with process id %d", pid)
322322
}
323323

324-
// service takes 30sec time to completely shutdown
325-
success := WaitForServiceDown(service)
324+
success := WaitForServiceStatus(service, false)
326325
if !success {
327326
StopService(service)
328327
}
329328

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

333331
return true, fmt.Sprintf("Service %s stopped successfully", service)
334332
}
335333

336-
// wait until the port for a given service is listening
337-
func WaitForService(service string) bool {
334+
func WaitForServiceStatus(service string, waitForUp bool) bool {
338335
overallTimeout := time.After(30 * time.Second)
339336
ticker := time.NewTicker(2 * time.Second)
340337
defer ticker.Stop()
@@ -344,60 +341,35 @@ func WaitForService(service string) bool {
344341
for {
345342
select {
346343
case <-overallTimeout:
347-
// Timeout occurred while waiting for the service to be available
348-
log.Println(fmt.Errorf("timeout: %s service did not become available within 30 seconds", service).Error())
349-
return false
350-
case <-ticker.C:
351-
// Retry if the service is not in `runningServices`
352-
if _, exists := runningServices[service]; !exists {
353-
log.Println(fmt.Sprintf("Service %s not found in running services. Retrying...\n", service))
354-
continue
344+
status := "available"
345+
if !waitForUp {
346+
status = "shut down"
355347
}
356-
357-
address := fmt.Sprintf(":%d", port)
358-
// Try to connect to the port
359-
conn, err := net.DialTimeout("tcp", address, 1*time.Second)
360-
if err == nil {
361-
_ = conn.Close()
362-
log.Println(fmt.Sprintf("%s service is ready to listen port %d", service, port))
363-
return true
364-
}
365-
log.Println(fmt.Sprintf("%s service is not ready on port %v. %v\n", service, port, err))
366-
}
367-
}
368-
}
369-
370-
// wwait until service completely down
371-
func WaitForServiceDown(service string) bool {
372-
overallTimeout := time.After(30 * time.Second)
373-
ticker := time.NewTicker(2 * time.Second)
374-
defer ticker.Stop()
375-
376-
port := config.GetServiceDebugPort(service)
377-
378-
for {
379-
select {
380-
case <-overallTimeout:
381-
// Timeout occurred while waiting for the service to be available
382-
log.Println(fmt.Errorf("timeout: %s service did not shut down within 30 seconds", service).Error())
348+
log.Println(fmt.Errorf("Timeout: %s service did not %s within 30 seconds", service, status).Error())
383349
return false
384350
case <-ticker.C:
385-
// Retry if the service is not in `runningServices`
386351
if _, exists := runningServices[service]; !exists {
387-
log.Println(fmt.Sprintf("Service %s not found in running services. Retrying...\n", service))
352+
log.Println(fmt.Sprintf("Service %s not found in running services. Retrying...", service))
388353
continue
389354
}
390355

391356
address := fmt.Sprintf(":%d", port)
392-
// Try to connect to the port
393357
conn, err := net.DialTimeout("tcp", address, 1*time.Second)
394-
if err != nil {
395-
log.Println(fmt.Sprintf("%s service port %d is no longer reachable", service, port))
396-
return true
358+
if waitForUp {
359+
if err == nil {
360+
_ = conn.Close()
361+
log.Println(fmt.Sprintf("%s service is ready to listen on port %d", service, port))
362+
return true
363+
}
364+
log.Println(fmt.Sprintf("%s service is not ready on port %d. %v", service, port, err))
365+
} else {
366+
if err != nil {
367+
log.Println(fmt.Sprintf("%s service port %d is no longer reachable", service, port))
368+
return true
369+
}
370+
_ = conn.Close()
371+
log.Println(fmt.Sprintf("%s service port %d is still active. Retrying...", service, port))
397372
}
398-
_ = conn.Close()
399-
log.Println(fmt.Sprintf("%s service port %d is still active. Retrying...", service, port))
400373
}
401374
}
402375
}
403-

tests/ociswrapper/wrapper/handlers/handler.go

+2-2
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))
@@ -260,5 +260,5 @@ func OcisServiceHandler(res http.ResponseWriter, req *http.Request) {
260260
}
261261
return
262262
}
263-
sendResponse(res, http.StatusMethodNotAllowed, "Invalid method requested")
263+
sendResponse(res, http.StatusMethodNotAllowed, "Method Not Allowed")
264264
}

0 commit comments

Comments
 (0)