diff --git a/tools/testtool/freeport.go b/tools/testtool/freeport.go index 197b3bf..437e0fd 100644 --- a/tools/testtool/freeport.go +++ b/tools/testtool/freeport.go @@ -3,7 +3,6 @@ package testtool import ( "fmt" "net" - "os" "strconv" "time" ) @@ -26,10 +25,14 @@ func GetFreePort() (string, error) { 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(timeout time.Duration, port string) error { +func WaitForPort(l logger, timeout time.Duration, port string) error { stopTimer := time.NewTimer(timeout) defer stopTimer.Stop() @@ -42,7 +45,7 @@ func WaitForPort(timeout time.Duration, port string) error { return fmt.Errorf("no server started listening on port %s after timeout %d", port, timeout) case <-t.C: if err := portIsReady(port); err != nil { - _, _ = fmt.Fprintf(os.Stderr, "waiting for port: %s\n", err) + l.Logf("waiting for port: %s\n", err) break } return nil diff --git a/tools/testtool/freeport_test.go b/tools/testtool/freeport_test.go index f9cbbbb..486fb44 100644 --- a/tools/testtool/freeport_test.go +++ b/tools/testtool/freeport_test.go @@ -26,12 +26,12 @@ func TestWaitForPort(t *testing.T) { _, port, err := net.SplitHostPort(u.Host) require.Nil(t, err) - require.NoError(t, WaitForPort(time.Second, port)) + require.NoError(t, WaitForPort(t, time.Second, port)) } func TestWaitForPort_timeout(t *testing.T) { p, err := GetFreePort() require.NoError(t, err) - require.Error(t, WaitForPort(time.Second, p)) + require.Error(t, WaitForPort(t, time.Second, p)) }