Support testing.TB as logger for WaitForPort.
This commit is contained in:
parent
b67bb030c3
commit
b93d1be1dc
2 changed files with 8 additions and 5 deletions
|
@ -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
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue