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