2020-02-15 19:39:40 +00:00
|
|
|
package testtool
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetFreePort returns free local tcp port.
|
|
|
|
func GetFreePort() (string, error) {
|
|
|
|
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
l, err := net.ListenTCP("tcp", addr)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer func() { _ = l.Close() }()
|
|
|
|
|
|
|
|
p := l.Addr().(*net.TCPAddr).Port
|
|
|
|
|
|
|
|
return strconv.Itoa(p), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WaitForPort tries to connect to given local port with constant backoff.
|
|
|
|
//
|
2020-02-18 21:08:37 +00:00
|
|
|
// Returns error if port is not ready after timeout.
|
|
|
|
func WaitForPort(timeout time.Duration, port string) error {
|
|
|
|
stopTimer := time.NewTimer(timeout)
|
|
|
|
defer stopTimer.Stop()
|
|
|
|
|
2020-02-15 19:39:40 +00:00
|
|
|
t := time.NewTicker(time.Millisecond * 100)
|
|
|
|
defer t.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
2020-02-18 21:08:37 +00:00
|
|
|
case <-stopTimer.C:
|
2020-02-18 21:11:10 +00:00
|
|
|
return fmt.Errorf("no server started listening on port %s after timeout %d", port, timeout)
|
2020-02-15 19:39:40 +00:00
|
|
|
case <-t.C:
|
|
|
|
if err := portIsReady(port); err != nil {
|
|
|
|
_, _ = fmt.Fprintf(os.Stderr, "waiting for port: %s\n", err)
|
|
|
|
break
|
|
|
|
}
|
2020-02-18 21:08:37 +00:00
|
|
|
return nil
|
2020-02-15 19:39:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func portIsReady(port string) error {
|
|
|
|
conn, err := net.Dial("tcp", net.JoinHostPort("localhost", port))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return conn.Close()
|
|
|
|
}
|