Merge branch '9-add-helpers-to-get-free-tcp-port-and-wait-for-listener' into 'master'

Resolve "add helpers to get free tcp port and wait for listener"

Closes #9

See merge request slon/shad-go-private!7
This commit is contained in:
verytable 2020-02-18 21:29:52 +00:00
commit 6ff6eaa3fa
2 changed files with 101 additions and 0 deletions

View file

@ -0,0 +1,62 @@
package testtool
import (
"fmt"
"net"
"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
}
type logger interface {
Logf(format string, args ...interface{})
}
// WaitForPort tries to connect to given local port with constant backoff.
//
// Returns error if port is not ready after timeout.
func WaitForPort(l logger, timeout time.Duration, port string) error {
stopTimer := time.NewTimer(timeout)
defer stopTimer.Stop()
t := time.NewTicker(time.Millisecond * 100)
defer t.Stop()
for {
select {
case <-stopTimer.C:
return fmt.Errorf("no server started listening on port %s after timeout %s", port, timeout)
case <-t.C:
if err := portIsReady(port); err != nil {
l.Logf("waiting for port: %s\n", err)
break
}
return nil
}
}
}
func portIsReady(port string) error {
conn, err := net.Dial("tcp", net.JoinHostPort("localhost", port))
if err != nil {
return err
}
return conn.Close()
}

View file

@ -0,0 +1,39 @@
package testtool
import (
"net"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestGetFreePort(t *testing.T) {
p, err := GetFreePort()
require.NoError(t, err)
require.NotEmpty(t, p)
}
func TestWaitForPort(t *testing.T) {
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
defer s.Close()
u, err := url.Parse(s.URL)
require.Nil(t, err)
_, port, err := net.SplitHostPort(u.Host)
require.Nil(t, err)
require.NoError(t, WaitForPort(t, time.Second, port))
}
func TestWaitForPort_timeout(t *testing.T) {
p, err := GetFreePort()
require.NoError(t, err)
err = WaitForPort(t, time.Second, p)
require.Error(t, err)
t.Log(err.Error())
}