1
0
mirror of https://github.com/golang/go synced 2024-11-26 02:47:58 -07:00

net: remove hard-coded timeout in dialClosedPort test helper

The helper function claims that dialing a closed port should be
"nearly instantaneous", but that is empirically not the case on
OpenBSD or Windows. The tests do not appear to be particularly
sensitive to the exact upper bound otherwise, so let's just
remove the arbitrary latency assumption.

Fixes #46884

Change-Id: If00c9fdc3063da6aaf60d365d4a2ee2c94dc6df1
Reviewed-on: https://go-review.googlesource.com/c/go/+/330250
Trust: Bryan C. Mills <bcmills@google.com>
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Bryan C. Mills 2021-06-22 21:48:11 -04:00
parent 86d72fa2cb
commit a9bb38222a

View File

@ -155,40 +155,27 @@ func slowDialTCP(ctx context.Context, network string, laddr, raddr *TCPAddr) (*T
return c, err
}
func dialClosedPort(t *testing.T) (actual, expected time.Duration) {
// Estimate the expected time for this platform.
// On Windows, dialing a closed port takes roughly 1 second,
// but other platforms should be instantaneous.
if runtime.GOOS == "windows" {
expected = 1500 * time.Millisecond
} else if runtime.GOOS == "darwin" || runtime.GOOS == "ios" {
expected = 150 * time.Millisecond
} else {
expected = 95 * time.Millisecond
}
func dialClosedPort(t *testing.T) (dialLatency time.Duration) {
// On most platforms, dialing a closed port should be nearly instantaneous —
// less than a few hundred milliseconds. However, on some platforms it may be
// much slower: on Windows and OpenBSD, it has been observed to take up to a
// few seconds.
l, err := Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Logf("dialClosedPort: Listen failed: %v", err)
return 999 * time.Hour, expected
t.Fatalf("dialClosedPort: Listen failed: %v", err)
}
addr := l.Addr().String()
l.Close()
// On OpenBSD, interference from TestTCPSelfConnect is mysteriously
// causing the first attempt to hang for a few seconds, so we throw
// away the first result and keep the second.
for i := 1; ; i++ {
startTime := time.Now()
c, err := Dial("tcp", addr)
if err == nil {
c.Close()
}
elapsed := time.Now().Sub(startTime)
if i == 2 {
t.Logf("dialClosedPort: measured delay %v", elapsed)
return elapsed, expected
}
startTime := time.Now()
c, err := Dial("tcp", addr)
if err == nil {
c.Close()
}
elapsed := time.Now().Sub(startTime)
t.Logf("dialClosedPort: measured delay %v", elapsed)
return elapsed
}
func TestDialParallel(t *testing.T) {
@ -198,10 +185,7 @@ func TestDialParallel(t *testing.T) {
t.Skip("both IPv4 and IPv6 are required")
}
closedPortDelay, expectClosedPortDelay := dialClosedPort(t)
if closedPortDelay > expectClosedPortDelay {
t.Errorf("got %v; want <= %v", closedPortDelay, expectClosedPortDelay)
}
closedPortDelay := dialClosedPort(t)
const instant time.Duration = 0
const fallbackDelay = 200 * time.Millisecond
@ -675,10 +659,7 @@ func TestDialerDualStack(t *testing.T) {
t.Skip("both IPv4 and IPv6 are required")
}
closedPortDelay, expectClosedPortDelay := dialClosedPort(t)
if closedPortDelay > expectClosedPortDelay {
t.Errorf("got %v; want <= %v", closedPortDelay, expectClosedPortDelay)
}
closedPortDelay := dialClosedPort(t)
origTestHookLookupIP := testHookLookupIP
defer func() { testHookLookupIP = origTestHookLookupIP }()