-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathports.go
34 lines (30 loc) · 1002 Bytes
/
ports.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package setup
import (
"fmt"
"os/exec"
"strconv"
"time"
log "github.com/sirupsen/logrus"
)
// WaitForPort waits 10 seconds for a process to listen to the port, and returns an error if
// the port remains open
func WaitForPort(port int) error {
return WaitForPortTimeout(port, 10*time.Second)
}
// WaitForPortTimeout waits timeout for a process to listen to the port, and returns an error if
// the port remains open
func WaitForPortTimeout(port int, timeout time.Duration) error {
log.Infof("Waiting for port %v for %v", port, timeout)
end := time.Now().Add(timeout)
for !time.Now().After(end) {
// Use exec.Command because we don't worry about these getting orphaned,
// and don't want to fill up our Cmds's list of running cmds
cmd := exec.Command("nc", "-z", "localhost", strconv.Itoa(port))
if err := cmd.Run(); err == nil {
log.Infof("Port %v active", port)
return nil
}
time.Sleep(500 * time.Millisecond)
}
return fmt.Errorf("port %v is not up after 5s", port)
}